目录与文件属性——《Unix/Linux编程实践教程》读书笔记
1、ls产生一个文件名的列表,它大致是这样工作的:
open directory +-> read entry - end of dir? -+ |__ display file info | close directory <--------------+
3、man 3 readdir
#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *dir_name); struct dirent *readdir(DIR *dir_name); int readdir_r(DIR *dir_ptr, struct dirent *entry, struct dirent **result); long telldir(DIR *dir_ptr); void seekdir(DIR *dir_ptr, long location); void rewinddir(DIR *dir_ptr); int closedir(DIR *dir_ptr);
main() opendir while (readdir) print d_name closedir
man 2 stat
#include <sys/stat.h> int res = stat(const char *path, struct stat *buf); /* return 0 for success, -1 for error */
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 filesystem 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 */ };
7、SUID 位告诉内核,运行这个程序的时候认为是由文件所有者在运行这个程序;SGID位告诉内核,运行这个程序的时候认为文件所属的组在运行这个程序;sticky位对于文件,在早期用于交换(swap)技术,对于目录而言被设计用来存放临时文件,如/tmp。
ls -l sample -rwsr-sr-t 1 root root 2345 Jul 24 00:45 sample
#include <sys/types.h> #include <sys/stat.h> int res = chmod(const char *path, mod_t mode); int res = chmod(int fd, mode_t mode); /* On success, 0 is returned. On error, -1 is returned and errno is set appropriately */
#include <unistd.h> int chown(const char *path, uid_t owner, gid_t group); int fchown(int fd, uid_t owner, gid_t group); int lchown(const char *path, uid_t owner, gid_t group); /* On success, 0 is returned. On error, -1 is returned and errno is set appropriately */
man utime
man 2 rename
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。