1. Data struct and function
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
#include
#include
#include
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
Note:
stat and fstat is different because of the param type.
lstat will not follow the link.
2. File type
file type defined in field st_mode in strauct stat:
S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
We can write macro to identify if a file is regular file like this:
#define IS_FREG ((st.st_mode & S_IFMT) == S_IFREG)
But yoy'd better use macro defined by system or library:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
3. File access permission
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
4. user id and group id
Usually , a PROCESS has
real user(group)-id : who actually run the process.
effective user(group)-id: used for file access permisson check.
saved set-user(group)-id: a file has two property called set-user-id and
set-group-id. They are two bits in st_mode. If
they are setted as 1, when user run the program
file, the effective user-id of the process is no
longer the actual user(group)-id, they are setted
as saved set-user(group)-id. And usually they are user id
and group id of the executable file owner's id
and group id. Like passwd tool.
-rwsr-xr-x 1 root root 41284 2011-06-24 17:36 /usr/bin/passwd
the s means set-user-id property is on, and S means set-group-id
bit is on.
We can use
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
to test if set user(group)-id is on. At last we must
understand that these bits are file properties, and will
effect the process which run the file. So if an unexcuted
file has these bits on, that means nothing.
5. Sticky bit
When an executable file has sticky bit on,
The first time you run it, system load it as normal,
when you close it, the text section(contains instructions)
of the executable file will be stored in swap disk. And
when you run it later, system will load it
from swap diak directly.
S_ISVTX 0001000 sticky bit (see below)
6. File and Directory access permission effect
6.1 Directory:
6.1.1 Read Bit: This decides if you can read what contained
in the directory.
Example:
ville@ville-SX11S:~/test$ ls -l
total 4
d-wxrwxr-x 3 ville ville 4096 2011-12-17 21:24 test
ville@ville-SX11S:~/test$ cd test/
ville@ville-SX11S:~/test/test$ ls
ls: cannot open directory .: Permission denied
6.1.2 Write Bit: This decides if you can create and delete
files and directories in this directory.
Example:
ville@ville-SX11S:~/test$ ls -l
total 4
dr-xrwxr-x 3 ville ville 4096 2011-12-17 21:24 test
ville@ville-SX11S:~/test/test$ rm abc
rm: cannot remove `abc': Permission denied
ville@ville-SX11S:~/test/test$ touch def
touch: cannot touch `def': Permission denied
6.1.3 Execute Bit: This decides if you can enter or go
through the directory. If this bit
is not on, you even can not access
the files and diretory in it.
Example:
ville@ville-SX11S:~/test$ ls -l
total 4
drw-rwxr-x 3 ville ville 4096 2011-12-17 21:24 test
ville@ville-SX11S:~/test$ cd test/
bash: cd: test/: Permission denied
ville@ville-SX11S:~/test$ cat ./test/abc
cat: ./test/abc: Permission denied
So if you want to create or remove files and directories
in a directory, you must have both the Write and Execute
permission.
7. permisson of a new created file
The owner-user-id will be the effective user-id of the creating process.
But group-id of the file can be
(1) effective group-id of the process.
(2) group-id of the parent directory.
That depends on how you mount the file system.
8. Time of a file
We know a file(inode) has three time:
--last access time: last time file content is read.
--last modification time: last time file content is changed.
--last state changed time:last time file properity is changed,
that means inode is modified.
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
we could use utime() to set st_atime and st_mtime, but
sc_ctime can not be changed, beacause it's maintained
by kernel file system only.
9. device number
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
These are the device number of the file system that files
are in.
dev_t st_rdev; /* device ID (if special file) */
For a Character or Block device file, these are the device
number of themselves.
#include
dev_t makedev(int maj, int min);
int major(dev_t dev);
int minor(dev_t dev);
Use these function to get major and minor device
number.
阅读(889) | 评论(0) | 转发(0) |