Linux System Programming note 8 ——File and Directory Management

來源:互聯網
上載者:User

標籤:

1. The Stat Family#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>
int stat(const char *path, struct stat *buf);int fstat(int fd, struct stat *buf);int lstat(const char *path, struct stat *buf);
struct stat{     dev_t st_dev;     ino_t  st_ino;     mode_t st_mode;     nlink_t st_nlink;     uid_t st_uid;     gid_t st_gid;     dev_t st_rdev;     off_t st_size;     blksize_t st_blksize;     blkcnt_t st_blocks;     time_t st_atime;     time_t st_mtime;     time_t st_ctime;};
2. Permissions
#include <sys/types.h>#include <sys/stat.h>
int chmod(const char *path, mode_t mode);int fchmod(int fd, mode_t mode);
3. Ownership
#include <sys/types.h>#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group);int lchown(const char *path, uid_t owner, gid_t group);int fchown(int fd, uid_t owner, gid_t group);
4. Extended attribute namespacessystemsecuritytrusteduser
5. Retrieving an extended attribute#include <sys/types.h>#inlcude <attr/xattr.h>
ssize_t getxattr(const char *path, const char *key, void *value, size_t size);ssize_t lgetxattr(const char *path, const char *key, void *value, size_t size);ssize_t fgetxattr(int fd, const char *key, void *value, size_t size);
6. Setting an extended attribute#include <sys/types.h>#include <attr/xattr.h>
int setxattr(const char *path, const char *key, const void *value, size_t size, int flags);int lsetxattr(const char *path, const char *key, const void *value, size_t size, int flags);int fsetxattr(int fd, const char *key, const void *value, size_t size, int flags);
7. Listing the extended attributes on a file#include <sys/types.h>#include <attr/xattr.h>
ssize_t listxattr(const char *path, char *list, size_t size);ssize_t llistxattr(const char *path, char *list, size_t size);ssize_t flistxattr(int fd, char *list, size_t size);
8. Removing an extended attribute#include <sys/types.h>#include <attr/xattr.h>
int removexattr(const char *path, const char *key);int lremovexattr(const char *path, chost char *key);int fremovexattr(int fd, const char *key);
9. Obtaining the current working directory#include <unistd.h>
char *getcwd(char *buf, size_t size);
#define _GNU_SOURCE#include <unistd.h>
char *get_current_dir_name(void);
#define _XOPEN_SOURCE_EXTENDED     /* or _BSD_SOURCE */#include <unistd.h>
char *getwd(char *buf);
10. Changing the current working directory#include <unistd.h>
int chdir(const char *path);int fchdir(int fd);
11. Creating Directories#include <sys/stat.h>#include <sys/types.h>
int mkdir(const char *path, mode_t mode);
12. Removing Directories#include <unistd.h>
int rmdir (const char *path);
13. Reading a Directory‘s Contents#include <sys/types.h>#include <dirent.h>
DIR *opendir(cosnt char *name);
To obtain the file descriptor behind a given directory stream:#define _BSD_SOURCE /* or _SVID_SOURCE */#include <sys/types.h>#include <dirent.h>
int dirfd(DIR *dir);
14. Reading from a directory stream#include <sys/types.h>#include <dirent.h>
struct dirent *readdir(DIR *dir);
struct dirent{     ino_t d_ino;     off_t d_off;     unsigned short d_reclen;     unsigned char d_type;     char d_name[256];};
15. Closing the directory stream#include <sys/types.h>#include <dirent.h>
int closedir(DIR *dir);
16. System call for reading directory contents#include <unistd.h>#include <sys/types.h>#include <linux/dirent.h>#include <linux/unistd.h>#incllude <errno.h>
/* * Not defined for user space: need to  * use the _syscall3() macro to access */
int readdir(unsigned int fd, struct dirent *dirp, unsigned int count);
int getdents(unsigned int fd, struct dirent *dirp, unsigned int count);
You do not want to use these system calls! They are obtuse and not portable.
17. Hard Links#include <unistd.h>
int link(const char *oldpath, const char *new path);
18. Symbolic Links#include <unistd.h>
int symlink(const char *oldpath, const char *newpath);
19. Unlinking#include <unistd.h>
int unlink(const char *pathname);
20. Moving #include <stdio.h>
int rename(const char *oldpath, const char *newpath);
21. Monitoring File Events21.1 Initializing inotify#include <sys/inotify.h>
int inotify_init1(int flags);The flags parameter is usually 0, but may be a bitwise OR of the following flags:IN_CLOEXECIN_NONBLOCK
errno:EMFILEENFILEENOMEN
example:
int fd;
fd = inotify_init1(0);if (fd == -1){     perror("inotify_init1");     exit(EXIT_FAILURE);}
21.2 Watches
#include <sys/inotify>
int inotify_add_watch(int fd, const char *path, uint32_t mask);
mask:IN_ACCESSIN_MODIFYIN_ATTRIBIN_CLOSE_WRITEIN_CLOSE_NOWRITEIN_OPENIN_MOVED_FROMIN_MOVED_TOIN_CREATEIN_DELETEIN_DELETE_SELFIN_MOVE_SELF
The following events are also definedm grouping two or more events into a single value:IN_ALL_EVENTSIN_CLOSEIN_MOVE
21.3 inotify Events
#include <sys/inotify.h>
struct inotify_event{}     int wd;     uint32_t mask;     uint32_t cookie;     uint32_t len;     char name[];};
21.4 Reading inotify events21.5 Advanced inotify events
IN_IGNOREDIN_ISDIRIN_Q_OVERFLOWIN_UNMOUNT
21.6 Removing an inotify Watch#include <inotify.h>
int inotify_rm_watch(int fd, uint32_t wd);

著作權聲明:本文部落格原創文章。部落格,未經同意,不得轉載。

Linux System Programming note 8 ——File and Directory Management

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.