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

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

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

  1. CMake error:System Error:No such file or directory CMake error:Could not open file for write in copy operation xxxx.ros_Config.cmake.tmp.

    微型电脑或嵌入式与电脑还是有点不同的,在微型电脑上ros indigo 版本下利用catkin编译如果你遇到如下错误: CMake error:System Error:No such file or ...

  2. fatal error: linux/videodev.h: No such file or directory

    Run Build Command:"/usr/bin/make" "cmTC_162a3/fast"/usr/bin/make -f CMakeFiles/c ...

  3. linux/videodev.h: No such file or directory错误解决方法

    sudo apt-get install libv4l-dev* file yum install libv4l-dev* yum install libv4l-dev* 上面错误的问题是两个2.4以 ...

  4. Linux System Programming --Chapter Seven

    文件和目录管理 一.文件与其元数据 我们首先看一下一个简单的文本文件是怎么保存的: 打开vim,编辑一段文本: [root@localhost ~]# vim hello.txt 编辑内容如下: op ...

  5. Linux,Docker,Jenkins No such file or directory

    你们先休息下,我先哭哭! 今天在做交接项目的bug修改的时候,在创建文件的时候报错 No such file or directory 然后跟着路径去linux中查看了该路径,但确实存在,并且权限都是 ...

  6. linux/module.h: No such file or directory 内核模块编译过程

    1.缺少Linux kernel头文件 To install just the headers in Ubuntu: sudo apt-get install linux-headers-$(unam ...

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

    1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...

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

    1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...

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

    1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and mu ...

随机推荐

  1. Github上四种Lisp方言的流行度 | 肉山博客 (Wenshan's Blog)

    Github上四种Lisp方言的流行度 | 肉山博客 (Wenshan's Blog) Github上四种Lisp方言的流行度

  2. COCOS2D中对精灵的操作、对图片的各种操作

    内容简要: 1.初始化 2.创建无图的精灵 3.设置精灵贴图大小  4.添加入层中 5.对精灵进行缩放  6.对精灵宽或高进行缩放  7.旋转精灵 8.设置精灵透明度  9.精灵的镜像反转  10.设 ...

  3. 通过加载Kernel32来动态判断 当前操作系统32bit还是64bit

    工作原理:通过加载Kernel32来获取IsWow64Process 函数然后通过函数的地址操作,执行函数的操作. 在程序中只要我们获取了一个函数的地址,就可以找到正确的方法执行这个函数. 但是这种方 ...

  4. Zxing中文乱码解决方法

    Zxing中文乱码解决方法总结 尝试过非常多方法  最后发现此方法解决的乱码最多....... 在百度搜索二维码图片 经过前2页的測试  除开一张图之外  其余都能扫描出结果 假设大家有更好的解决方法 ...

  5. 一个Java对象到底占多大内存?(转)

    最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...

  6. Java Swing界面编程(28)---复选框:JCheckBox

    程序能够通过JRadioButton实现单选button的功能,那么要实现复选框的功能,则必须使用JCheckBox完毕. package com.beyole.util; import java.a ...

  7. JAVA - 优雅的记录日志(log4j实战篇) (转)

    写在前面 项目开发中,记录错误日志有以下好处: 方便调试 便于发现系统运行过程中的错误 存储业务数据,便于后期分析 在java中,记录日志有很多种方式: 自己实现 自己写类,将日志数据,以io操作方式 ...

  8. iOS 搜索框控件 最简单的dome

    刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...

  9. cocos2dx-3.0(1)------win7 32位android环境搭建

    參照链接http://blog.csdn.net/wonengxing/article/details/23601359 ----我的生活,我的点点滴滴!! 一. Android工具安装 1. 安装J ...

  10. GitHub基本操作(转)

    一.上传代码到仓库 步骤一:创建本地仓库,如下: 创建结果: 步骤二:发布自己创建的仓库,如下: 发布完显示如下: 步骤三:向自己发布仓库上传代码,如下: 首先将代码复制到本地仓库,如下: 复制完,显 ...