Linux System Programming note 8 ——File and Directory Management

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 namespaces

system

security

trusted

user

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 Events

21.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_CLOEXEC

IN_NONBLOCK

errno:

EMFILE

ENFILE

ENOMEN

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_ACCESS

IN_MODIFY

IN_ATTRIB

IN_CLOSE_WRITE

IN_CLOSE_NOWRITE

IN_OPEN

IN_MOVED_FROM

IN_MOVED_TO

IN_CREATE

IN_DELETE

IN_DELETE_SELF

IN_MOVE_SELF

The following events are also definedm grouping two or more events into a single value:

IN_ALL_EVENTS

IN_CLOSE

IN_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 events

21.5 Advanced inotify events

IN_IGNORED

IN_ISDIR

IN_Q_OVERFLOW

IN_UNMOUNT

21.6 Removing an inotify Watch

#include <inotify.h>

int inotify_rm_watch(int fd, uint32_t wd);

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-10-03 22:32:26

Linux System Programming note 8 ——File and Directory Management的相关文章

Linux System Programming 学习笔记(八) 文件和目录管理

1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是LInux内核通过数据结构表示的实体 inode存储相关联文件的元数据 ls -i 命令获取文件的inode number /* obtaining the metadata of a file */ #include <sys/types.h> #include <sys/stat.h>

Linux System Programming 学习笔记(六) 进程调度

1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进程调度器需要使 处理器使用率最大化,并且提供 使多个进程并发执行的虚拟 Deciding which processes run, when, and for how long is the process scheduler's fundamental responsibility. 时间片:th

Linux System Programming 学习笔记(四) 高级I/O

1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and multiple buffers This type of I/O is so named because the data is scattered into or gathered from the given vector of buffers Scatter/Gather I/O 相比于 C标准

Linux System Programming 学习笔记(二) 文件I/O

1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情况下,子进程会获得其父进程文件表的完整拷贝 2.打开文件 open系统调用必须包含 O_RDONLY,O_WRONLY,O_RDWR 三种存取模式之一 注意 O_NONBLOCK模式 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644

Linux System Programming 学习笔记(十) 信号

1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0) 内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIGKILL和SIGSTOP不能被忽略. (2) 捕捉并且处理该信号.The kernel will suspend execution of the process's current code path and jump to a previously registered function. SIGK

Linux System Programming 学习笔记(五) 进程管理

1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity inside of a process the virtualization of memory is associated with the process, the threads all share the same memory address space 2. pid The idle pro

Linux System Programming 学习笔记(九) 内存管理

1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系结构决定了内存页大小,32位系统通常是 4KB, 64位系统通常是 8KB 内存页分为 valid or invalid: A valid page is associated with an actual page of data,例如RAM或者磁盘上的文件 An invalid page is

Linux System Programming 学习笔记(七) 线程

1. Threading is the creation and management of multiple units of execution within a single process 二进制文件是驻留在存储介质上,已被编译成操作系统可以使用,准备执行但没有正运行的休眠程序 进程是操作系统对 正在执行中的二进制文件的抽象:已加载的二进制.虚拟内存.内核资源 线程是进程内的执行单元 processes are running binaries, threads are the smal

Linux System Programming 学习笔记(十一) 时间

1. 内核提供三种不同的方式来记录时间: Wall time (or real time):actual time and date in the real world Process time:the time that a process spends executing on a processor 包括用户时间user time 和 系统时间system time Monotonic time:use the system's uptime (time since boot) for t