Linux学习入门--文件编程学习二
2018-03-05 00:00
471 查看
摘要: 文件高级操作的学习,涉及到文件的模式、文件的权限、文件截断、文件重命名、测试文件是否存在及权限、文件信息查询
1. 文件模式 (S_IFMT & mode) 测试文件类型的:比如 普通文件 目录文件 设备文件等,见后文stat fstat lstat测试例子。
2. 文件权限
先看一般情况下open函数创建的文件,测试代码如下:
执行后的结果可以看出,group组和其他组没有写权限,open函数中明明写的mode为0777,这是因为umask的作用,终端执行umask命令,可以看到结果为0022,屏蔽了group和other组的写权限,具体可以查阅umask的相关资料
如果我们需要使我们创建的文件指定我们的设定的权限,可以这么做,在线程中设定umask后在创建文件,此处设定umask并不改变系统的umask值
从测试结果可以看出,文件权限为所设定的权限,并且系统的umask值未修改,一般情况下shell中才会用umask,修改文件权限一般用chmod 或fchmod函数。
chmod或fchmod,二者区别 传入文件名和文件描述符
测试结果与umask一致。
chown fchown此处不再演示,与chmod一致。
获取文件的信息,以及测试文件类型
stat以及fstat用来获取文件的信息,结构体如下,可通过man 2 stat查看:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
下面例子获取了文件的状态,并判断文件是否是普通文件
用两种方法来测试文件的类型:S_ISREG(m)和bits flags定义:
S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set-user-ID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
运行结果:
此外文件的时间操作(ctime atime mtime),重命名rename以及截断 truncate ,ftruncate ,access测试权限此处不再演示了。
1. 文件模式 (S_IFMT & mode) 测试文件类型的:比如 普通文件 目录文件 设备文件等,见后文stat fstat lstat测试例子。
2. 文件权限
先看一般情况下open函数创建的文件,测试代码如下:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { int fd = 0; fd = open("./file.txt",O_RDWR|O_CREAT,0777); if (fd == -1) { perror("open"); } close(fd); }
执行后的结果可以看出,group组和其他组没有写权限,open函数中明明写的mode为0777,这是因为umask的作用,终端执行umask命令,可以看到结果为0022,屏蔽了group和other组的写权限,具体可以查阅umask的相关资料
如果我们需要使我们创建的文件指定我们的设定的权限,可以这么做,在线程中设定umask后在创建文件,此处设定umask并不改变系统的umask值
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { int fd = 0; if (umask(0000) == -1) { perror("umask"); } fd = open("./file.txt",O_RDWR|O_CREAT,0777); if (fd == -1) { perror("open"); } close(fd); }
从测试结果可以看出,文件权限为所设定的权限,并且系统的umask值未修改,一般情况下shell中才会用umask,修改文件权限一般用chmod 或fchmod函数。
chmod或fchmod,二者区别 传入文件名和文件描述符
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { int fd = 0; #if 0 if (umask(0000) == -1) { perror("umask"); } #endif fd = open("./file.txt",O_RDWR|O_CREAT,0777); fchmod(fd,0777); if (fd == -1) { perror("open"); } close(fd); }
测试结果与umask一致。
chown fchown此处不再演示,与chmod一致。
获取文件的信息,以及测试文件类型
stat以及fstat用来获取文件的信息,结构体如下,可通过man 2 stat查看:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
下面例子获取了文件的状态,并判断文件是否是普通文件
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { int fd = 0; struct stat buf; int status; if (umask(0000) == -1) { perror("umask"); } fd = open("./file.txt",O_RDWR|O_CREAT,0777); if (fd == -1) { perror("open"); } fstat(fd,&buf); if (status == -1) { perror("stat"); } if (S_ISREG(fd) != -1) { printf("this is a regular file1!\n"); } if (S_IFREG == (S_IFMT & (buf.st_mode))) { printf("this is a regular file2!\n"); } close(fd); }
用两种方法来测试文件的类型:S_ISREG(m)和bits flags定义:
S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set-user-ID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
运行结果:
此外文件的时间操作(ctime atime mtime),重命名rename以及截断 truncate ,ftruncate ,access测试权限此处不再演示了。
相关文章推荐
- Linux学习入门--文件编程学习一
- Linux学习入门--文件编程学习三
- UNIX/LINUX编程学习之文件I/O
- 【Linux入门学习之】Linux文件权限与目录配置
- LINUX C系统编程学习笔记-----------文件编程
- UNIX/LINUX编程学习之文件共享
- Linux操作系统下C语言编程入门学习笔记(...20090601(第一遍学完待整理)...)
- 文件I/O编程学习笔记三(linux 时间编程)
- 文件编程---那些年我们一起学习linux程序设计
- Linux编程学习笔记--proc文件系统
- linux学习入门10——linux文件系統挂载管理(linuxcast.net)(mount、umount、配置fstab进行自动挂载)
- linux学习入门3——linux文件基本操作(linuxcast.net)(复制、移动、重命名、创建、删除文件及文件夹)
- linux操作系统下c语言编程入门 -- (3)文件操作
- linux学习入门2——linux文件系統基本结构(linuxcast.net)(倒转树状结构、命名机制、查看文件类型)
- Linux命令(shell)从入门到精通 学习笔记之1:文件安全与权限
- Linux学习笔记一 ---- Linux基础知识认知以及初识Linux下C编程入门
- 【嵌入式Linux学习七步曲之第五篇 Linux内核及驱动编程】揭开Linux Proc文件系统的神秘面纱
- 【Linux入门学习之】Linux文件权限与目录配置
- 推荐一本学习Linux高级编程入门的书籍
- 怎样学习LINUX编程 如何入门【多篇文章整理集合】