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


相关推荐

  • 阿里用什么替代了dubbo_阿里面试必问题:Spring+MyBaits+微服务+Dubbo+Kakfa带解析

    前言很多同学在群里和我抱怨,面试的时候准备的不充分,导致面试结果不理想,也有很多同学苦于没有一份合适的面试指导。针对这些的同学,在这分享总结的Java面试的高频面试题(包括了Java集合,JVM,并发与多线程,Spring,MyBaits,微服务,Dubbo,Kakfa,中间件,Redis,数据库,设计模式等),进行了整理,免费分享给大家,希望大家能带着这些问题和答案解析,能让你进行有针对性行的学…

    2022年4月5日
    341
  • Android TextView两端对齐、文本两端对齐

    Android TextView两端对齐、文本两端对齐如题 在开发中 经常会用到比如说类似于表格的排版 效果图 这样的效果 谷歌也没给出这样效果的方法 网上查也没找到实现方法 然后想 中间用空格填充呢 试试 然后写了几个 TextView 对比一下 如下 放大效果 可以看出 不能完美对齐 这肯定不是想要的效果 然后就发现了两个占字符来实现 nbsp 他们与汉字换算关系就是 1 个汉字 4 个

    2025年11月5日
    3
  • Tomcat中的ResourceBundle国际化解析「建议收藏」

    Tomcat中的ResourceBundle国际化解析「建议收藏」一、ResourceBundle简介:资源束(ResourceBundle)是一个本地化对象。它封装了适用于本地环境的资源;这个类主要用来解决国际化和本地化问题。国际化和本地化可不是两个概念,两者都是一起出现的。可以说,国际化的目的就是为了实现本地化。比如对于“取消”,中文中我们使用“取消”来表示,而英文中我们使用“cancel”。若我们的程序是面向国际的…

    2022年7月12日
    18
  • Hmily 临时笔记「建议收藏」

    Hmily 临时笔记「建议收藏」概述Hmily是一款高性能分布式事务tcc开源框架。基于java语言来开发(JDK1.8),支持Dubbo、SpringCloud、Motan等RPC框架进行分布式事务。功能高可靠性:支持分布式场景下,事务异常回滚,超时异常恢复,防止事务悬挂易用性:提供零侵入性式的Spring-Boot,Spring-Name…

    2022年5月21日
    50
  • 笔记:解决redis连接错误:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to…

    笔记:解决redis连接错误:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to…今天重启游戏服务器在连接redis数据库时突然报错:MISCONFRedisisconfiguredtosaveRDBsnapshots,butitiscurrentlynotabletopersistondisk.Commandsthatmaymodifythedatasetaredisabled,becausethisinstance

    2025年7月31日
    5
  • java jsonstring变成jsonobject(jpa实体类)

    本文的JsoonObject是fastJSON提供的对象JSONObject所需的maven依赖:<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.69</version></dependency>先定义一个实体类: @Data@ToS

    2022年4月13日
    52

发表回复

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

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