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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • f1 score与f2 score的区别「建议收藏」

    f1 score与f2 score的区别「建议收藏」beta值代表1或者2beta=1,f1scorebeta=2,f2score

    2022年10月9日
    1
  • dell服务器显示器fre,戴尔全新 Freesync 显示器,专门针对游戏玩家[通俗易懂]

    dell服务器显示器fre,戴尔全新 Freesync 显示器,专门针对游戏玩家[通俗易懂]戴尔拥有一对全新的Ultrasharp显示器,专门针对游戏玩家,对于那些重视整体速度和响应能力的人来说,它们可能是不久的将来理想的升级途径。运动刷新率高达155Hz,分辨率高达1440P,以及24英寸和27英寸面板的选项,有很多值得关注的新的,配备Freesync的显示器。但这会是如今最好的游戏显示器吗?戴尔2719DGF是一款27英寸TN面板显示器,其机箱外观干净,专…

    2022年6月4日
    32
  • java中decode和encode_java encoding

    java中decode和encode_java encoding一概述前端传递过来的url被转码了;应该chuan

    2022年10月7日
    4
  • pp图与qq图_画图python

    pp图与qq图_画图python统计学中有时会会用到PP图或QQ图

    2022年8月10日
    33
  • 大数据开源舆情分析系统-数据采集技术架构浅析

    大数据开源舆情分析系统-数据采集技术架构浅析舆情系统中数据采集是一个关键部分,此部分核心技术虽然由爬虫技术框架构建,但抓取海量的互联网数据绝不是靠一两个爬虫程序能搞定,特别是抓取大量网站的情况下,每天有大量网站的状态和样式发生变化以后,爬虫程序能快速的反应和维护。一旦分布式的爬虫规模大了以后会出现很多问题,都是种种技术挑战,会有很多门槛,例如:1.检测出你是爬虫,拉黑你IP(人家究竟是通过你的ua、行为特则还是别的检测出你是爬虫的?你怎么规避?)2人家给你返回脏数据,你怎么辨认?3对方被你爬死,你怎么设计调度规则?4要求你一天爬.

    2022年9月19日
    2
  • 开源版本Visifire的应用「建议收藏」

    开源版本Visifire的应用「建议收藏」Visifire曾经开源,保持使用开源版本是不会有版权问题滴。引用的命名控件usingVisifire.Charts;usingVisifire.Commons;一、应用示例主要代码//

    2022年7月4日
    26

发表回复

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

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