现在的位置: 首页 > 综合 > 正文

apue(3)

2013年07月11日 ⁄ 综合 ⁄ 共 8310字 ⁄ 字号 评论关闭

 

4.2. stat, fstat, and lstat Functions

#include <sys/stat.h>

int stat(const char *restrict pathname, struct stat *restrict buf); int fstat(int filedes, struct stat *buf); int lstat(const char *restrict pathname, struct stat *restrict buf);
The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link.
struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number) */ dev_t st_dev; /* device number (file system) */ // dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ // blksize_t st_blksize; /* best I/O block size */ // blkcnt_t st_blocks; /* number of disk blocks allocated */ };

4.3. File Types

Regular file.
Directories.
All devices on a system are either block special files or character special files.
FIFO. A type of file used for communication between processes. It's sometimes called a named pipe.
Socket.
Symbolic link
The type of a file is encoded in the st_mode member of the stat structure.

Macro

Type of file

S_ISREG(mode_t)

regular file

S_ISDIR(mode_t)

directory file

S_ISCHR(mode_t)

character special file

S_ISBLK(mode_t)

block special file

S_ISFIFO(mode_t)

pipe or FIFO

S_ISLNK(mode_t)

symbolic link

S_ISSOCK(mode_t)

socket

IPC type

 

Macro

Type of object

S_TYPEISMQ(mode_t*)

message queue

S_TYPEISSEM(mode_t*)

semaphore

S_TYPEISSHM(mode_t*)

shared memory object

 

4.4. Set-User-ID and Set-Group-ID


Every process has six or more IDs associated with it.

real user ID
real group ID

who we really are

effective user ID
effective group ID
supplementary group IDs

used for file access permission checks

saved set-user-ID
saved set-group-ID

saved by exec functions

Normally, the effective user ID equals the real user ID, and the effective group ID equals the real group ID.

The set-user-ID bit and the set-group-ID bit are contained in the file's st_mode value. These two bits can be tested against the constants
S_ISUID and S_ISGID.

4.5. File Access Permissions

st_mode mask

Meaning

S_IRUSR

user-read

S_IWUSR

user-write

S_IXUSR

user-execute

S_IRGRP

group-read

S_IWGRP

group-write

S_IXGRP

group-execute

S_IROTH

other-read

S_IWOTH

other-write

S_IXOTH

other-execute

 

Note that read permission for a directory and execute permission for a directory mean different things. Read permission lets us read the directory, obtaining a list of all the filenames in the directory. Execute permission lets us pass through the directory
when it is a component of a pathname that we are trying to access.

 

We cannot create a new file in a directory unless we have write permission and execute permission in the directory.

 

To delete an existing file, we need write permission and execute permission in the directory containing the file. We do not need read permission or write permission for the file itself.

 

Execute permission for a file must be on if we want to execute the file using any of the six
exec functions (Section 8.10). The file also has to be a regular file.

root->user->group->other进程的权限依次与文件要求匹配,如果中间有一个正确但无权限(例:是owner但没有write权限),后边不予考虑(就算other有write权限 也不可以以write方式打开文件)。

 

4.6. Ownership of New Files and Directories

The user ID of a new file is set to the effective user ID of the process.

The group ID of a new file can be the effective group ID of the process.

The group ID of a new file can be the group ID of the directory in which the file is being created.

linux can be either with proper "mount"

 

4.7. access Function

#include <unistd.h> int access(const char *pathname, int mode);
The access function bases its tests on the real user and group IDs, instead of effective uid and gid.

mode

Description

R_OK

test
for read permission

W_OK

test for write permission

X_OK

test
for execute permission

F_OK

test for existence of file

测试real uid , gid是否有某个权限。

 

4.8. umask Function

#include <sys/stat.h> mode_t umask(mode_t cmask);

Returns: previous file mode creation mask。一个没有错误返回的函数。

linux中的 umask 函数主要用于:在创建新文件或目录时 屏蔽掉新文件或目录不应有的访问允许权限。

在创建文件或者目录时制定的权限基础上拿掉权限,不冲突。

 

4.9. chmod and fchmod Functions

#include <sys/stat.h> int chmod(const char *pathname, mode_t mode); int fchmod(int filedes, mode_t mode);

mode

Description

S_ISUID

set-user-ID
on execution*

S_ISGID

set-group-ID on execution*

S_ISVTX

saved-text
(sticky bit)***

S_IRWXU

read, write, and execute by user (owner)

 

 

S_IRUSR

read
by user (owner)

 

 

S_IWUSR

write
by user (owner)

 

 

S_IXUSR

execute
by user (owner)

S_IRWXG

read, write, and execute by group

 

 

S_IRGRP

read
by group

 

 

S_IWGRP

write by
group

 

 

S_IXGRP

execute
by group

S_IRWXO

read, write, and execute by other (world)

 

 

S_IROTH

read
by other (world)

 

 

S_IWOTH

write
by other (world)

 

 

S_IXOTH

execute
by other (world)

 

Note that nine of the entries in Figure 4.11 are the nine file access permission bits from Figure 4.6. We've added the two set-ID constants (S_ISUID and S_ISGID), the saved-text constant (S_ISVTX), and the three combined constants (S_IRWXU, S_IRWXG, and S_IRWXO).

chmod函数会更新inode中的修改时间,但ls -l只会显示更新内容的最近时间,chmod后不一定显示。

Specifically, if the group ID of the new file does not equal either the effective group ID of the process or one of the process's supplementary group IDs and if the process does not have superuser privileges, then the set-group-ID bit is automatically turned off.

 

4.11. chown, fchown, and lchown Functions

The chown functions allow us to change the user ID of a file and the group ID of a file.

#include <unistd.h> int chown(const char *pathname, uid_t owner, gid_t group); int fchown(int filedes, uid_t owner, gid_t group); int lchown(const char *pathname, uid_t owner, gid_t group);

These three functions operate similarly unless the referenced file is a symbolic link. In that case, lchown changes the owners of the symbolic link itself, not the file pointed to by the symbolic link.

If the owner or group is specified as -1, then that ID is not changed.

When the owner or group of an executable file are changed by an unprivileged user the S_ISUID and S_ISGID mode bits are cleared.

Only a privileged process (Linux: one with the CAP_CHOWN capability) may change the owner of a file. The owner of a file may change the group of the file to any group of which that owner is a member.

4.12. File Size

The st_size member of the stat structure is meaningful only for regular files, directories, and symbolic links.

对于文件,长度可以为0.对于目录,长度一般为2的阶。对于link,长度为绝对文件名,例lib->usr/lib 长度为7

Most contemporary UNIX systems provide the fields st_blksize and
st_blocks
. The first is the preferred block size for I/O for the file, and the latter is the actual number of 512-byte blocks(unsure) that are allocated.

 

4.13. File Truncation

 

 

include <unistd.h> int truncate(const char *pathname, off_t length); int ftruncate(int filedes, off_t length);

 

 

4.14. File Systems

The i-nodes are fixed-length entries that contain most of the information about a file.

 

 

4.15. link, unlink,
remove, and rename Functions

 

#include <unistd.h> int link(const char *existingpath, const char *newpath);

 

 

The creation of the new directory entry and the increment of the link count must be an atomic operation.

If an implementation supports the creation of hard links to directories, it is restricted to only the superuser. The reason is that doing this can cause loops in the file system. (We show an example of a loop introduced by a symbolic link in

Section 4.16
.)

 

#include <unistd.h> int unlink(const char *pathname);
This property of unlink is often used by a program to ensure that a temporary file it creates won't be left around in case the program crashes.
This function removes the directory entry and decrements the link count of the file referenced by pathname. If there are other links to the file, the data in the file is still accessible through the other links.
// $ ./a.out & run the program in Figure 4.16 in the background 
It's better to use it with file.
The process creates a file using either open or creat and then immediately calls unlink. The file is not deleted, however, because it is still open. Only when the process either closes the file or terminates, which causes the kernel to close all its open files, is the file deleted.
#include <stdio.h> int remove(const char *pathname);
We can also unlink a file or a directory with the remove function. For a file, remove is identical to unlink. For a directory, remove is identical to rmdir.
#include <stdio.h> int rename(const char *oldname, const char *newname);
如果源文件非目录,且新名字存在且非目录,那么删除存在的重命名成功。如果源文件是目录,且新名字为存在的目录,如果存在的目录为空,那么重命名成功。
不许新路径包括原路径。

4.16. Symbolic Links

Function

Does
not follow symbolic link

Follows symbolic link

access

 

chdir

 

chmod

 

chown

creat

 

exec

 

lchown

 

link

 

lstat

 

open

 

opendir

 

pathconf

 

readlink

 

remove

 

rename

 

stat

 

truncate

 

unlink

 

Follows symbolic link means dealing with real file, or not means dealing with the symbolic link itself. The functions mkdir, mkfifo, mknod, and rmdir are not in this figure, as they return an error when the pathname is a symbolic link. Also, the functions that take a file descriptor argument, such as fstat and fchmod, are not listed, as the handling of a symbolic link is done by the function that returns the file descriptor (usually open). Whether or not chown follows a symbolic link depends on the implementation. One exception to Figure 4.17 is when the open function is called with both O_CREAT and O_EXCL set. In this case, if the pathname refers to a symbolic link, open will fail with errno set to EEXIST.  Note that on Linux, the ftw function uses lstat.

抱歉!评论已关闭.