Linux System Programming note 8 ——File and Directory Management

Linux System Programming note 8 ——File and Directory Management

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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);

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117716.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月1日 下午3:00
下一篇 2022年1月1日 下午3:00


相关推荐

  • 航天金税开票导入导出txt格式

    航天金税开票导入导出txt格式防伪开票文本接口软件接口文件格式说明一、接口文件格式1.文件种类接口文件为纯文本文件,各行尾以回车换行码(ASCII码13和10)或换行码(ASCII10)分隔均可,可用各种文本编辑器编写或通过应用程序生成。2.注释行文件中以两个斜杠(//)置于行首的行为注释行,系统读入文件时忽略注释行和空行。3.分隔符每行中各个项目之间以两个波浪号(“~~”)分隔,行尾各项目均省略时可省略相应的分隔符。4.日期格式日期以四位年份+两位月份+两位日期表示:YYYYMMDD,…

    2022年5月29日
    117
  • 即梦AI这样调优渲染速度!手把手教你超详细性能设置

    即梦AI这样调优渲染速度!手把手教你超详细性能设置

    2026年3月13日
    1
  • 最详细的DeepSeek-R1:7B+RAGFlow本地知识库搭建教程,建议收藏起来慢慢学!!

    最详细的DeepSeek-R1:7B+RAGFlow本地知识库搭建教程,建议收藏起来慢慢学!!

    2026年3月15日
    2
  • Maven历史版本下载「建议收藏」

    Maven历史版本下载「建议收藏」一.Maven官网下载历史版本1.maven下载地址(1)、打开Mvaen官网下载地址(2)、进入历史版本下载地址(3)、历史版本下载页面,选择一个版本进入。(4)、我们选择一个历史版本进来后显示二进制和源码两个下载方式。二进制版本是编译好的,可以直接使用。源码版本未经编译,需要自行编译(5)、选择二进制版本,点击进入下载。(6)、下载下来后直接解压就可以使用了。…

    2022年8月21日
    10
  • 如何控制input框!

    如何控制input框!

    2021年9月22日
    56
  • 浅谈Android指纹识别技术[通俗易懂]

    浅谈Android指纹识别技术[通俗易懂]浅谈Android指纹识别技术当今时代,随着移动智能手机的普及,指纹解锁早已是手机不可或缺的一个功能。除了现在比较新款的iPhone或者部分手机采用了FaceID之外,人们几乎天天都会用到指纹解锁技术。但你知道指纹解锁技术背后的原理吗?原理指纹识别的前提是对指纹的采集,所以我们首先就应该了解第一步:指纹采集。第一步:指纹采集指纹采集主要分为两种方式:滑动式采集和按压式采集滑动式采集是将手指在传感器上滑过,从而使手机获得手指指纹图像。滑动式采集具有成本相对偏低,而且可以采集大面积图像的优势。但这

    2022年8月10日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号