apue 01 文件和目录的操作 文件系统
2014-01-05 19:20
239 查看
来自:http://www.cppcourse.com/
linux文件系统的结构
1. clear, get and set file flags
2.fcntl设置文件锁
3.目录操作
4.stat获取文件的状态
linux文件系统的结构
1. clear, get and set file flags
/* get and set file flags */ void Set_flags(int fd, int flags) { int val ; val = fcntl(0, F_GETFL, 0); if(-1 == val) ERR_EXIT("fcntl get flags error"); val |= flags; if( (fcntl(0, F_SETFL, val)) < 0 ) ERR_EXIT("fcntl set flags error"); } /* clear file flags*/ void Clear_flags(int fd, int flags) { int val ; val = fcntl(0, F_GETFL, 0); if(-1 == val) ERR_EXIT("fcntl get flags error"); val &= ~flags; if( (fcntl(0, F_SETFL, val)) < 0 ) ERR_EXIT("fcntl set flags error"); } Set_flags(0, O_NONBLOCK); Clear_flags(0,O_NONBLOCK);
2.fcntl设置文件锁
struct flock flck; memset(&flck, 0, sizeof(flck)); flck.l_type = F_WRLCK;/*lock type*/ flck.l_whence = SEEK_SET; flck.l_start = 0; flck.l_len = 0; if( 0 == fcntl(fd, F_SETLKW, &flck) ) ERR_EXIT("lock ok"); else ERR_EXIT("lock fail");
3.目录操作
DIR *dir = opendir("."); struct dirent *dirent_; while( (dirent_ = readdir(dir)) != NULL) { if( 0 == strncmp(dirent_->d_name, ".", 1) ) /*remove .file */ continue; printf("%s\n", dirent_->d_name); } closedir(dir);
4.stat获取文件的状态
int file_type(struct stat *buf) { int flag = 0; printf("Filetype:"); mode_t mode; mode = buf->st_mode; switch (mode & S_IFMT) { case S_IFSOCK: printf("socket\n"); break; case S_IFLNK: printf("symbolic link\n"); break; case S_IFREG: printf("regular file\n"); break; case S_IFBLK: printf("block device\n"); flag = 1; break; case S_IFDIR: printf("directory\n"); break; case S_IFCHR: printf("character device\n"); flag = 1; break; case S_IFIFO: printf("FIFO\n"); break; default: printf("unknown file type\n"); break; } return flag; } void file_perm(struct stat* sbuf, char *perm) { strcpy(perm, "----------"); perm[0] = '?'; mode_t mode; mode = sbuf->st_mode; switch(mode & S_IFMT) { case S_IFSOCK: perm[0] = 's'; break; case S_IFLNK: perm[0] = 'l'; break; case S_IFREG: perm[0] = '-'; break; case S_IFBLK: perm[0] = 'b'; break; case S_IFDIR: perm[0] = 'd'; break; case S_IFCHR: perm[0] = 'c'; break; case S_IFIFO: perm[0] = 'p'; break; } if (mode & S_IRUSR) perm[1] = 'r'; if (mode & S_IWUSR) perm[2] = 'w'; if (mode & S_IXUSR) perm[3] = 'x'; if (mode & S_IRGRP) perm[4] = 'r'; if (mode & S_IWGRP) perm[5] = 'w'; if (mode & S_IXGRP) perm[6] = 'x'; if (mode & S_IROTH) perm[7] = 'r'; if (mode & S_IWOTH) perm[8] = 'w'; if (mode & S_IXOTH) perm[9] = 'x'; perm[10] = '\0'; } #define MAJOR(a) (int)((unsigned short)a >> 8) #define MINOR(a) (int)((unsigned short)a & 0xFF) struct stat sbuf; if (lstat(argv[1], &sbuf) == -1) ERR_EXIT("stat error"); /* 1.get file major and minor */ printf("File number:major %d,minor %d inode %d\n", MAJOR(sbuf.st_dev), MINOR(sbuf.st_dev), (int)sbuf.st_ino); if (file_type(&sbuf)) { printf("Device number:major %d,minor %d\n", MAJOR(sbuf.st_rdev), MINOR(sbuf.st_rdev)); } /* 2. get file perm */ char perm[11] = {0}; file_perm(&sbuf, perm); printf("File permission bits=%o %s\n", sbuf.st_mode & 07777, perm);
相关文章推荐
- node.js 使用fs模块对系统文件及目录进行读写操作
- iphone编程--详解ios文件系统文件目录读写操作
- Linux文件基本操作管理和系统目录结构
- node.js基于fs模块对系统文件及目录进行读写操作的方法详解
- MAC 系统下的快捷键 和 终端下的文件目录操作
- iphone编程--详解ios文件系统文件目录读写操作
- python 目录,文件,系统 操作
- 详解ios文件系统文件目录读写操作-备用
- Linux系统学习第二章:文件与目录操作(四):文件查找
- PHP文件系统-目录操作
- node.js中fs文件系统目录操作与文件信息操作
- 详解ios文件系统文件目录读写操作
- 二.Linux系统目录结构及文件基本操作
- Linux系统文件和目录操作
- nodejs-操作文件系统创建读取目录
- LInux文件基础知识和文件目录操作(系统调用函数方式)
- Shell 命令习惯用法[1] --文件、目录操作、系统管理
- Linux 系统常用命令汇总(一) 文件和目录操作
- Linux系统之文件目录创建、删除操作
- 常用的文件系统操作命令--目录命令