07 文件和目录

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_29350001/article/details/68484338

文件I/O部分断断续续写了三天,最后总结发现还有好多内容是略过没讲的,我的内心是崩溃的。UNIX环境高级编程这本书,虽然我只看了四章我就发现了书里面的内容讲的太跳,如果是刚接触UNIX或者没有一点C语言基础的会很难上手。这就造成了,前面讲的会漏掉很多内容。

一、函数 stat、fstat、fstatat 和 lstat

详细内容可 man fstat 查看


   
   
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. int stat(const char *path, struct stat *buf);
  5. int fstat(int fd, struct stat *buf);
  6. int lstat(const char *path, struct stat *buf);

1、参数解析

第一个参数:文件路径/文件描述符
第二个参数:结构体指针,结构体变量的地址。结构体基本形式如下:

    
    
  1. struct stat {
  2. dev_t st_dev; /* ID of device containing file */
  3. ino_t st_ino; /* inode number */
  4. mode_t st_mode; /* protection */
  5. nlink_t st_nlink; /* number of hard links */
  6. uid_t st_uid; /* user ID of owner */
  7. gid_t st_gid; /* group ID of owner */
  8. dev_t st_rdev; /* device ID (if special file) */
  9. off_t st_size; /* total size, in bytes */
  10. blksize_t st_blksize; /* blocksize for file system I/O */
  11. blkcnt_t st_blocks; /* number of 512B blocks allocated */
  12. time_t st_atime; /* time of last access */
  13. time_t st_mtime; /* time of last modification */
  14. time_t st_ctime; /* time of last status change */
  15. };
其中时间函数部分,参看:C语言再学习 -- 时间函数

2、返回值

成功返回 0, 失败返回 -1

3、函数功能

获取指定文件的状态信息

4、示例说明


    
    
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. int main (void)
  9. {
  10. struct stat st = {};
  11. int res = stat ( "a.txt", &st);
  12. if ( -1 == res)
  13. perror ( "Fail to stat"), exit ( 1);
  14. printf ( "st_mode = %o, st_size = %ld, st_mtime = %ld\n",
  15. st.st_mode, st.st_size, st.st_mtime);
  16. printf ( "文件的权限是:%o\n", st.st_mode& 0777);
  17. printf ( "文件的大小是:%ld\n", st.st_size);
  18. if (S_ISREG (st.st_mode))
  19. printf ( "是普通文件\n");
  20. if (S_ISDIR (st.st_mode))
  21. printf ( "是目录文件\n");
  22. printf ( "最后一次修改时间是:%s\n", ctime (&st.st_mtime));
  23. struct tm* pt = localtime (&st.st_mtime);
  24. printf( "最后一次修改时间是:%d-%d-%d %02d:%02d:%02d\n",pt->tm_year+ 1900,pt->tm_mon+ 1,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec);
  25. return 0;
  26. }
  27. 输出结果:
  28. st_mode = 100644, st_size = 0, st_mtime = 1490857501
  29. 文件的权限是: 644
  30. 文件的大小是: 0
  31. 是普通文件
  32. 最后一次修改时间是:Thu Mar 30 15: 05: 01 2017
  33. 最后一次修改时间是: 2017 -3 -30 15: 05: 01

5、总结

这部分只讲函数 stat,其实在 Linux 下 stat 命令 也可以实现这些功能了。  参看:stat 命令

    
    
  1. # stat c_test/
  2. 文件: "c_test/"
  3. 大小: 4096 块: 8 IO 块: 4096 目录
  4. 设备: 801h/ 2049d Inode: 2102110 硬链接: 2
  5. 权限:( 0775/drwxrwxr-x) Uid:( 1000/ tarena) Gid:( 1000/ tarena)
  6. 最近访问: 2017 -03 -30 15: 05: 03.502154419 + 0800
  7. 最近更改: 2017 -03 -30 15: 05: 01.858151879 + 0800
  8. 最近改动: 2017 -03 -30 15: 05: 01.858151879 + 0800
  9. 创建时间:-
  10. # stat a.txt
  11. 文件: "a.txt"
  12. 大小: 0 块: 0 IO 块: 4096 普通空文件
  13. 设备: 801h/ 2049d Inode: 2121684 硬链接: 1
  14. 权限:( 0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
  15. 最近访问: 2017 -03 -30 15: 05: 01.858151879 + 0800
  16. 最近更改: 2017 -03 -30 15: 05: 01.858151879 + 0800
  17. 最近改动: 2017 -03 -30 15: 05: 01.858151879 + 0800
  18. 创建时间:-

二、文件类型

UNIX 系统的大多数文件是普通文件或目录,但是也有另外一些文件类型。文件类型包括如下几种.

1、普通文件

在 UNIX/Linux 系统中通常所见到的文件,如用 C/C++ 语言编写的源代码文件,编译器、汇编器和连接器产生的汇编文件、目标文件和可执行文件,各种系统配置文件,shell 脚本文件,包括音频、视频等数字内容的多媒体文件,乃至包括数据库在内的各种应用程序所维护、管理和使用的数据文件等,都是普通文件。
一个文件包括两部分数据,一部分是元数据,如文件的类型、权限、大小、用户、组、各种时间戳等,存储在 i 节点中,另一部分是内容数据,存储在数据块中

2、目录文件

系统通过 i 节点唯一地标识一个文件的存在,但人们更愿意使用有意义的文件名来访问文件,目录就是用来建立文件名和 i 节点之间的映射的。
目录的本质就是一个普通文件,与其它普通文件唯一的区别就是它仅仅存储文件名和 i 节点号的映射,每一个这样的映射,用目录中的一个条目表示,谓之硬链接。

介绍相对路径和绝对路径:
Lniux的文件系统中有一个大分组,它包含了文件系统中所有文件,这个大的分组用一个专门的目录表示,这个目录叫做根目录,根目录可以使用“/”表示
路径可以用来表示文件或者文件夹所在的位置,路径是从一个文件夹开始走到另一个文件夹或者文件位置中间的这条路。把这条路经过的所有文件夹名称按顺序书写出来的结果就可以表示这条路。
路径分为绝对路径和相对路径
绝对路径:
起点必须是根目录,如 /abc/def  所有绝对路径一定是以“/”作为开头的
相对路径:可以把任何一个目录作为起点,如../../abc/def  相对路径编写时不应该包含起点位置
相对目录中“..”表示上层目录
相对路径中用“.”表示当前
终端窗口里的当前目录是所有相对路径的起点,当前目录的位置是可以修改的。
pwd 命令:可以用来查看当前目录的位置
cd  命令:可以用来修改当前目录位置   
ls  命令:可以用来查看一个目录的内容

3、块特殊文件

这种类型的文件提供对设备(如磁盘)带缓冲的访问,每次访问以固定长度为单位进行。

4、字符特殊文件

这种类型的文件提供对设备不带缓冲的访问,每次访问长度可变。系统中的所有设备要么是字符特殊文件,要么是块特殊文件。

5、FIFO

这种类型的文件用于进程间通信,有时也称为命名管道。

6、套接字

这种类型的文件用于进程间的网络通信。套接字也可用于一台宿主机上进程之间的非网络通信。

7、符号链接

这种类型的文件指向另一个文件。

再讲到 stat 函数时,其结构中的 st_mode 有各类文件类型的信息

    
    
  1. S_ISREG(m) is it a regular file (普通文件)
  2. S_ISDIR(m) directory (目录文件)
  3. S_ISCHR(m) character device (字符特殊文件)
  4. S_ISBLK(m) block device (块特殊文件)
  5. S_ISFIFO(m) FIFO (named pipe) (管道或 FIFO)
  6. S_ISLNK(m) symbolic link (符号链接)(Not in POSIX .1 -1996.)
  7. S_ISSOCK(m) socket (套接字)(Not in POSIX .1 -1996.)
我们讲权限的时候,也讲过使用 ls -l 命令 也可以查看文件的类型的:

    
    
  1. # ls -l
  2. 总用量 40
  3. drwxr-xr-x 2 root root 4096 Mar 30 16: 42 test
  4. -rw-r--r-- 1 root root 872 Mar 30 15: 04 test.c
其中第一个字符含义:
-  普通文件  (例如:/etc/passwd)
d  目录  (例如:/etc)
s  本地套接字  (例如:/dev/log)
c  字符设备  (例如:/dev/tty)
b  块设备  (例如:/dev/sr0)
l  符号链接  (例如:/dev/cdrom)
p  有名管道/FIFO  (例如:/var/lib/oprofile/opd_pipe)

相对于用函数,我更喜欢用指令来获取文件类型

    
    
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. int main(int argc, char *argv[])
  7. {
  8. struct stat sb;
  9. if (argc != 2) {
  10. fprintf( stderr, "Usage: %s <pathname>\n", argv[ 0]);
  11. exit(EXIT_FAILURE);
  12. }
  13. if (stat(argv[ 1], &sb) == -1) {
  14. perror( "stat");
  15. exit(EXIT_FAILURE);
  16. }
  17. printf( "File type: ");
  18. switch (sb.st_mode & S_IFMT) {
  19. case S_IFBLK: printf( "block device\n"); break;
  20. case S_IFCHR: printf( "character device\n"); break;
  21. case S_IFDIR: printf( "directory\n"); break;
  22. case S_IFIFO: printf( "FIFO/pipe\n"); break;
  23. case S_IFLNK: printf( "symlink\n"); break;
  24. case S_IFREG: printf( "regular file\n"); break;
  25. case S_IFSOCK: printf( "socket\n"); break;
  26. default: printf( "unknown?\n"); break;
  27. }
  28. printf( "I-node number: %ld\n", ( long) sb.st_ino);
  29. printf( "Mode: %lo (octal)\n",
  30. ( unsigned long) sb.st_mode);
  31. printf( "Link count: %ld\n", ( long) sb.st_nlink);
  32. printf( "Ownership: UID=%ld GID=%ld\n",
  33. ( long) sb.st_uid, ( long) sb.st_gid);
  34. printf( "Preferred I/O block size: %ld bytes\n",
  35. ( long) sb.st_blksize);
  36. printf( "File size: %lld bytes\n",
  37. ( long long) sb.st_size);
  38. printf( "Blocks allocated: %lld\n",
  39. ( long long) sb.st_blocks);
  40. printf( "Last status change: %s", ctime(&sb.st_ctime));
  41. printf( "Last file access: %s", ctime(&sb.st_atime));
  42. printf( "Last file modification: %s", ctime(&sb.st_mtime));
  43. exit(EXIT_SUCCESS);
  44. }
  45. 输出结果:
  46. # ./a.out test.c
  47. File type: regular file
  48. I-node number: 2121694
  49. Mode: 100644 (octal)
  50. Link count: 1
  51. Ownership: UID= 0 GID= 0
  52. Preferred I/O block size: 4096 bytes
  53. File size: 1642 bytes
  54. Blocks allocated: 8
  55. Last status change: Fri Mar 31 09: 15: 48 2017
  56. Last file access: Fri Mar 31 09: 15: 53 2017
  57. Last file modification: Fri Mar 31 09: 15: 48 2017

    
    
  1. # ls -l /dev/tty
  2. crw-rw-rw- 1 root tty 5, 0 Mar 29 16: 00 /dev/tty

    
    
  1. # stat /dev/tty
  2. 文件: "/dev/tty"
  3. 大小: 0 块: 0 IO 块: 4096 字符特殊文件
  4. 设备: 5h/ 5d Inode: 4961 硬链接: 1 设备类型: 5, 0
  5. 权限:( 0666/crw-rw-rw-) Uid:( 0/ root) Gid:( 5/ tty)
  6. 最近访问: 2017 -03 -30 16: 35: 58.445938548 + 0800
  7. 最近更改: 2017 -03 -29 16: 00: 37.867616181 + 0800
  8. 最近改动: 2017 -03 -29 16: 00: 37.867616181 + 0800
  9. 创建时间:-

三、文件访问权限

当提及 文件 时,指的是前面所提到的任何类型的文件。所有文件类型(目录、字符特别文件等)都是有访问权限的
st_mode 值也包含了对文件的访问权限位:

     
     
  1. The following flags are defined for the st_mode field:
  2. S_IFMT 0170000 bit mask for the file type bit fields
  3. S_IFSOCK 0140000 socket
  4. S_IFLNK 0120000 symbolic link
  5. S_IFREG 0100000 regular file
  6. S_IFBLK 0060000 block device
  7. S_IFDIR 0040000 directory
  8. S_IFCHR 0020000 character device
  9. S_IFIFO 0010000 FIFO
  10. S_ISUID 0004000 set UID bit
  11. S_ISGID 0002000 set-group- ID bit (see below)
  12. S_ISVTX 0001000 sticky bit (see below)
  13. S_IRWXU 00700 mask for file owner permissions
  14. S_IRUSR 00400 owner has read permission
  15. S_IWUSR 00200 owner has write permission
  16. S_IXUSR 00100 owner has execute permission
  17. S_IRWXG 00070 mask for group permissions
  18. S_IRGRP 00040 group has read permission
  19. S_IWGRP 00020 group has write permission
  20. S_IXGRP 00010 group has execute permission
  21. S_IRWXO 00007 mask for permissions for others (not in group)
  22. S_IROTH 00004 others have read permission
  23. S_IWOTH 00002 others have write permission
  24. S_IXOTH 00001 others have execute permission
chmod 命令用于修改这 9 个权限位。该命令允许我们用 u 表示用户(所有者),用 g 表示组,用 o 表示其他。
对于一个文件的读权限决定了我们是否能够打开现有文件进行读操作。这与 open 函数的 O_RDONLY 和 O_RDWR 标志有关。
对于一个文件的写权限决定了我们是否能够打开现有文件进行写操作。这与 open 函数的 O_WRONLY 和 O)RDWR 标志有关。
为了在 open 函数中对一个文件指定 O_TRUNC 标志,必须对该文件具有写权限(经测试不限于写权限)
为了在一个目录中创建一个新文件,必须对该目录具有写权限和执行权限
为了删除一个现有文件,必须对包含该文件的目录具有写权限和执行权限。对该文件本身则不需要读、写权限。
如果用 7 个 exec 函数中的任何一个执行某个文件,都必须对该文件具有执行权限。该文件还必须是一个普通文件

四、函数 access 


    
    
  1. #include <unistd.h>
  2. int access(const char *pathname, int mode);

1、参数解析

第一个参数:文件的路径和文件名
第二个参数:操作模式
F_OK:判断文件是否存在
R_OK:判断文件是否可读
W_OK:判断文件是否可写
X_OK:判断文件是否可执行

查看 /usr/include/unistd.h 可发现这几个模式可用数字表示

    
    
  1. /* Values for the second argument to access.
  2. These may be OR'd together. */
  3. #define R_OK 4 /* Test for read permission. */
  4. #define W_OK 2 /* Test for write permission. */
  5. #define X_OK 1 /* Test for execute permission. */
  6. #define F_OK 0 /* Test for existence. */

2、函数功能:

确定文件或文件夹的访问权限。即,检查某个文件的存取方式,比如说是只读方式、只写方式等。

3、返回值

如果指定的存取方式有效,则函数返回 0,否则函数返回 -1。

4、示例说明


    
    
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. int main (void)
  4. {
  5. if ( 0 == access ( "a.txt", F_OK))
  6. printf ( "文件存在\n");
  7. if ( 0 == access ( "a.txt", 4))
  8. printf ( "文件可读\n");
  9. if ( 0 == access ( "a.txt", W_OK))
  10. printf ( "文件可写\n");
  11. if ( 0 == access ( "a.txt", 1))
  12. printf ( "文件可执行\n");
  13. return 0;
  14. }
  15. 输出结果:
  16. 文件存在
  17. 文件可读
  18. 文件可写

五、函数 umask


    
    
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. mode_t umask( mode_t mask);

1、函数功能

主要用于设置创建文件时需要屏蔽的权限,返回之前屏蔽的权限
 umask() 会将系统umask值设成参数 mask&0777 后的值,然后将先前的umask值返回。在使用 open() 建立新文件时,该参数 mode 并非真正建立文件的权限,而是 (mode& ~umask) 的权限值.
系统默认的屏蔽权限为 (0022):

    
    
  1. //创建文件 hh 查看权限:
  2. # ls -l hh
  3. -rw-r--r-- 1 root root 0 Mar 31 10: 34 hh

    
    
  1. 查看:
  2. # umask
  3. 0022

2、示例说明


     
     
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. int main (void)
  8. {
  9. //使用umask函数设置屏蔽的权限
  10. mode_t old = umask ( 0055);
  11. //设置新的屏蔽字,返回旧的系统默认
  12. // 当前系统默认屏蔽 0055
  13. printf ( "old = %o\n", old);
  14. int fd = open ( "a.txt", O_RDWR | O_CREAT, 0777);
  15. if ( -1 == fd)
  16. perror ( "fail to open"), exit ( 1);
  17. //恢复系统默认的屏蔽字
  18. //对已经创建过的文件权限没有影响
  19. umask (old);
  20. close (fd);
  21. return 0;
  22. }
  23. 输出结果:
  24. old = 22
  25. 查看 a.txt 权限,说明实际权限为 07220777&~ 0055)屏蔽了 0055
  26. # ls -la a.txt
  27. -rwx-w--w- 1 root root 0 Mar 31 10: 25 a.txt
示例说明:
在建立文件时指定文件权限为0777,通常umask值默认为 0022,将 umask值新设为 0055,返回老的默认值 22;则该文件的真正权限则为0777&~055=0722,也就是rw-w--w--

3、Linux 下的umask 指令

参看:umask 命令

(1)选项


     
     
  1. -p:输出的权限掩码可直接作为指令来执行;
  2. -S:以符号方式输出权限掩码。

(2)示例


     
     
  1. 设置新屏蔽权限:
  2. # umask -p 0055
  3. # umask -S 0055
  4. u=rwx,g=w,o=w
  5. 查看:
  6. # umask
  7. 0055

六、函数 chmod 和 fchmod


     
     
  1. #include <sys/stat.h>
  2. int chmod(const char *path, mode_t mode);
  3. int fchmod(int fd, mode_t mode);

1、参数解析

第一个参数:文件路径/文件描述符
第二个参数:权限模式
查看 man fchmod

      
      
  1. The new file permissions are specified in mode, which is a bit mask created by ORing together zero or more of the following:
  2. S_ISUID ( 04000) set-user-ID ( set process effective user ID on execve( 2))
  3. S_ISGID ( 02000) set-group-ID ( set process effective group ID on execve( 2); mandatory locking, as described in fcntl( 2); take a new
  4. file's group from parent directory, as described in chown( 2) and mkdir( 2))
  5. S_ISVTX ( 01000) sticky bit (restricted deletion flag, as described in unlink( 2))
  6. S_IRUSR ( 00400) read by owner
  7. S_IWUSR ( 00200) write by owner
  8. S_IXUSR ( 00100) execute/search by owner ( "search" applies for directories, and means that entries within the directory can be
  9. accessed)
  10. S_IRGRP ( 00040) read by group
  11. S_IWGRP ( 00020) write by group
  12. S_IXGRP ( 00010) execute/search by group
  13. S_IROTH ( 00004) read by others
  14. S_IWOTH ( 00002) write by others
  15. S_IXOTH ( 00001) execute/search by others

2、返回值

成功返回 0,失败返回 -1.

3、函数功能

修改现有文件的访问权限。
chmod 函数在指定的文件上进行操作,而 fchmod 函数则对已打开的文件进行操作
为了改变一个文件的权限位,进程的有效用户 ID 必须等于文件的所有者 ID,或者该进程必须具有超级用户权限

4、示例说明


      
      
  1. //示例一 chmod
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. int main (void)
  9. {
  10. struct stat st;
  11. int res = stat ( "a.txt", &st);
  12. if ( -1 == res)
  13. perror ( "fail to stat"), exit ( 1);
  14. printf ( "权限是:%o\n", st.st_mode& 0777);
  15. res = chmod ( "a.txt", 0600);
  16. if ( -1 == res)
  17. perror ( "fail to chmod"), exit ( 1);
  18. res = stat ( "a.txt", &st);
  19. if ( -1 == res)
  20. perror ( "fail to stat"), exit ( 1);
  21. printf ( "权限是:%o\n", st.st_mode& 0777);
  22. return 0;
  23. }
  24. 输出结果:
  25. 权限是: 644
  26. 权限是: 600

      
      
  1. //示例二 fchmod
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. int main (void)
  9. {
  10. int fd = open ( "a.txt", O_RDONLY | O_CREAT, 0644);
  11. struct stat st;
  12. int res = stat ( "a.txt", &st);
  13. if ( -1 == res)
  14. perror ( "fail to stat"), exit ( 1);
  15. printf ( "权限是:%o\n", st.st_mode& 0777);
  16. res = fchmod (fd, 0600);
  17. if ( -1 == res)
  18. perror ( "fail to chmod"), exit ( 1);
  19. res = stat ( "a.txt", &st);
  20. if ( -1 == res)
  21. perror ( "fail to stat"), exit ( 1);
  22. printf ( "权限是:%o\n", st.st_mode& 0777);
  23. return 0;
  24. }
  25. 输出结果:
  26. 权限是: 644
  27. 权限是: 600

5、示例总结

示例一:chmod 函数在指定的文件上进行操作。
示例二:fchmod 函数则对已打开的文件进行操作

6、Linux 下 chmod 命令

(1)修改文件 a.txt 权限为 0644


      
      
  1. chmod 644 a.txt
  2. 查看:
  3. # ls -l a.txt
  4. -rw-r--r-- 1 tarena root 0 Mar 31 16:16 a.txt

七、函数 chown、fchown、lchown


     
     
  1. #include <unistd.h>
  2. int chown(const char *path, uid_t owner, gid_t group);
  3. int fchown(int fd, uid_t owner, gid_t group);
  4. int lchown(const char *path, uid_t owner, gid_t group);

1、参数解析

第一个参数:文件路径/文件描述符
第二个参数:用户 ID
第三个参数:组 ID

2、函数功能

可用于更改文件的用户 ID 和组 ID。
除了所引用的文件是符号链接以外,这三个函数的操作相似。在符号链接的情况下,lchown更改符号链接本身的所有者,而不是该符号链接所指向的文件
如若两个参数 owne r或 group 中的任意一个是 -1,则对应的 ID 不变。
基于 BSD 的系统一直规定只有超级用户才能更改一个文件的所有者。这样做的原因是防止用户改变其文件的所有者从而摆脱磁盘空间限额对他们的限制。系统 V 则允许任一用户更改他们所拥有的文件的所有者。

3、返回值

成功返回 0, 失败返回 -1.

4、示例说明


    
    
  1. //示例一 chown
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. int main (void)
  5. {
  6. //chown ("a.txt", 0, -1);
  7. chown ("a.txt", 0, 0);
  8. return 0;
  9. }
  10. 修改前:
  11. # ls -l a.txt
  12. 总用量 20
  13. -rw------- 1 tarena tarena 0 Mar 31 14:24 a.txt
  14. chown ("a.txt", 0, 0); 修改后:
  15. # ls -l a.txt
  16. 总用量 20
  17. -rw------- 1 root root 0 Mar 31 14:24 a.txt
  18. chown ("a.txt", 0, -1); 修改后:
  19. # ls -l a.txt
  20. 总用量 20
  21. -rw------- 1 root tarena 0 Mar 31 14:24 a.txt

    
    
  1. //示例二 fchown
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8. int main (void)
  9. {
  10. int fd = open ("a.txt", O_RDONLY | O_CREAT, 0644);
  11. if (-1 == fd)
  12. perror ("fail to open"), exit (1);
  13. fchown (fd , 1000, 1000);
  14. return 0;
  15. }
  16. 修改前:
  17. # ls -la
  18. 总用量 12
  19. -rw-r--r-- 1 root root 0 Mar 31 15:34 a.txt
  20. 修改后:
  21. # ls -la
  22. 总用量 20
  23. -rw-r--r-- 1 tarena tarena 0 Mar 31 15:34 a.txt

    
    
  1. //示例三 lchown
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. int main (void)
  6. {
  7. int res = lchown ("/dev/cdrom1", 1000, 1000);
  8. if (-1 == res)
  9. perror ("fail to lchown"), exit (1);
  10. return 0;
  11. }
  12. 修改前:
  13. # ls -l cdrom1 sr0
  14. lrwxrwxrwx 1 root root 3 Mar 29 16:00 cdrom1 -> sr0
  15. brw-rw---- 1 root cdrom 11, 0 Mar 29 16:00 sr0
  16. 修改后:
  17. # ls -l cdrom1 sr0
  18. lrwxrwxrwx 1 tarena tarena 3 Mar 29 16:00 cdrom1 -> sr0
  19. brw-rw---- 1 root cdrom 11, 0 Mar 29 16:00 sr0

5、示例总结

示例一:我们查看 cat /etc/passwd 可以看到所有用户对应的 用户 ID 和 组 ID。
其中超级用户 root 的用户 ID 为 0,组 ID 也为 0。而我所用的普通用户 tarena 用户 ID 为 1000,组 ID 为 1000
如若两个参数 owne r或 group 中的任意一个是 -1,则对应的 ID 不变

    
    
  1. # cat /etc/passwd
  2. root:x: 0: 0:root:/root:/bin/bash
  3. tarena:x: 1000: 1000:tarena,,,:/home/tarena:/bin/bash
示例二:fchown,打开时文件描述符。
示例三: 这里有个名词 符号链接,上面我们讲文件类型是有讲到。比如  /dev/cdrom1就是符号链接它所指向的文件为sr0。 通过示例三可以验证,lchown 是改变符号链接本身的所有者,而不是该符号链接所指向的文件。

    
    
  1. # stat /dev/cdrom1
  2. 文件: "/dev/cdrom1" -> "sr0"
  3. 大小: 3 块: 0 IO 块: 4096 符号链接
  4. 设备: 5h/ 5d Inode: 7459 硬链接: 1
  5. 权限:( 0777/lrwxrwxrwx) Uid:( 0/ root) Gid:( 0/ root)
  6. 最近访问: 2017 -03 -31 15: 59: 53.712010169 + 0800
  7. 最近更改: 2017 -03 -29 16: 00: 36.411616237 + 0800
  8. 最近改动: 2017 -03 -31 15: 59: 47.936011311 + 0800
  9. 创建时间:-

6、linux 下 chown 和 chgrp 命令

参看:C语言再学习 -- 修改linux文件权限

(1)将test目录所在的组由root修改为zslf组


    
    
  1. sudo chgrp -R zslf test
  2. 查看:
  3. root@zslf-:/mnt # ls -la test/
  4. 总用量 8
  5. drwxr-xr-x 2 root zslf 4096 1124 17: 20 .
  6. drwxr-xr-x 5 root root 4096 1124 17: 20 ..

(2)将test目录下的所有文件和子目录的属主由root改为zslf


    
    
  1. sudo chown -R zslf.zslf test
  2. 查看:
  3. root@zslf- virtual-machine:/mnt # ls -la test/
  4. 总用量 8
  5. drwxr-xr-x 2 zslf root 4096 1124 17: 20 .
  6. drwxr-xr-x 5 root root 4096 1124 17: 20 ..

(3)将test目录下的所有文件和子目录的属主由root改为zslf,属组有root改为zslf。


    
    
  1. sudo chown -R zslf.zslf test
  2. 查看:
  3. root@zslf- virtual-machine:/mnt # ls -la test/
  4. 总用量 8
  5. drwxr-xr-x 2 zslf zslf 4096 1124 17: 33 .
  6. drwxr-xr-x 5 root root 4096 1124 17: 20 ..
  7. -rw-r--r-- 1 zslf zslf 0 1124 17: 33 hh

八、文件长度(大小)

stat 结构成员 st_size 表示以字节为单位的文件的长度。此字段只对普通文件、目录文件和符号链接有意义。
对于普通文件,其文件长度可以是 0,在开始读这种文件时,将得到文件结束(EOF)指示
参看:C语言再学习 -- EOF、feof函数、ferror函数   对于目录,文件长度通常是一个数(如 16 或 512)的整倍数。例如,下面的例子中,目录文件长度为 4096,普通文件 a.txt 文件长度可以是 0.

    
    
  1. # ls -la
  2. 总用量 20
  3. drwxrwxr-x 2 tarena tarena 4096 Mar 31 16: 16 .
  4. drwxrwxr-x 4 tarena tarena 4096 Mar 28 15: 38 ..
  5. -rwxr-xr-x 1 root root 7233 Mar 31 16: 16 a.out
  6. -rw-r--r-- 1 tarena root 0 Mar 31 16: 16 a.txt
  7. -rw-r--r-- 1 root root 184 Mar 31 16: 15 test.c
对于符号链接,文件长度是在文件名中的实际字节数。注意,因为符号链接文件长度总是由 st_size 指示,所以它并不包含通常 C 语言用作名字结尾的 null 字节)。例如,在下面的例子中,符号链接 cdrom1 的文件长度为 3

    
    
  1. # ls -l /dev/cdrom1
  2. lrwxrwxrwx 1 root root 3 Apr 5 09: 19 /dev/cdrom1 -> sr0

九、文件空洞

文件空洞,是由所设置的偏移量超过文件尾端,并写入了某些数据后造成的。位于文件中但没有被写过的字节都被设为 0,文件空洞不占用磁盘空间,但被计算在文件大小之内。

作为一个例子,考虑下列情况:

    
    
  1. # ls -l a.out
  2. -rwxr-xr-x 1 root root 7233 Mar 31 16: 16 a.out
  3. # du -h a.out
  4. 8.0K a.out
文件 a.out 长度为 7.2K,可 du 命令报告该文件所使用的磁盘空间总量为 8.0K。很明显,此文件中有很多空洞.
du 命令,参看:du 命令

十、文件截断


    
    
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. int truncate(const char *path, off_t length);
  4. int ftruncate(int fd, off_t length);

1、参数解析

第一个参数:文件路径/文件描述符
第二个参数:文件长度

2、函数功能:修改文件长度

这两个函数将一个现有文件长度截断为 length。如果该文件以前的长度大于 length,则超过 length 以外的数据就不再能访问。如果以前的长度小于 length,文件长度增加,在以前的文件尾端和新的文件尾端之间的数据将读作 0 (也就是可能再文件中创建了一个空洞)。

3、返回值

成功返回 0,失败返回 -1.

4、示例说明


    
    
  1. #include <stdio.h>
  2. //示例一 truncate 函数
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. int main (void)
  6. {
  7. int res = truncate ( "a.txt", 50);
  8. if ( -1 == res)
  9. perror ( "fail to truncate"), exit ( 1);
  10. return 0;
  11. }
  12. a.txt 为空时:
  13. 执行前:
  14. -rw-r--r-- 1 tarena root 0 Mar 31 16: 16 a.txt
  15. 执行后:
  16. -rw-r--r-- 1 tarena root 50 Apr 5 13: 14 a.txt
  17. 长度截断为 50,后面的数据填充为 0
  18. a.txt 不为空时:
  19. 执行前:
  20. -rw-r--r-- 1 tarena root 296 Apr 5 13: 17 a.txt
  21. 执行后:
  22. -rw-r--r-- 1 tarena root 50 Apr 5 13: 14 a.txt
  23. 长度截断为 50, 后面的数据不再能访问。

    
    
  1. //示例二 ftruncate 函数
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. int main (void)
  9. {
  10. int fd = open ( "a.txt", O_RDWR);
  11. if ( -1 == fd)
  12. perror ( "fail to open"), exit ( 1);
  13. int res = ftruncate (fd, 50);
  14. if ( -1 == res)
  15. perror ( "fail to truncate"), exit ( 1);
  16. return 0;
  17. }
  18. a.txt 为空时:
  19. 执行前:
  20. -rw-r--r-- 1 tarena root 0 Mar 31 16: 16 a.txt
  21. 执行后:
  22. -rw-r--r-- 1 tarena root 50 Apr 5 13: 14 a.txt
  23. 长度截断为 50,后面的数据填充为 0
  24. a.txt 不为空时:
  25. 执行前:
  26. -rw-r--r-- 1 tarena root 296 Apr 5 13: 17 a.txt
  27. 执行后:
  28. -rw-r--r-- 1 tarena root 50 Apr 5 13: 14 a.txt
  29. 长度截断为 50, 后面的数据不再能访问。

5、与 O_TRUNC 

打开文件时,可以使用 O_TRUNC 来将文件的长度截断为 0.

十一、函数 rename 和 renameat


     
     
  1. #include <stdio.h>
  2. int rename(const char *oldname, const char *newname);
  3. int renameat(int oldfd,const char *oldname, int newfd, const char *newname);

1、函数功能

用于文件或目录的重命名
根据 oldname 是指文件、目录还是符号链接,有几种情况需要加以说明。我们必须说明如果 newname 已经存在时将会发生什么
(1)如果 oldname 指的是一个文件而不是目录,那么为该文件或符号链接重命名。在这种情况下,如果 newname 已存在,则它不能引用一个目录。如果 newname 已存在,而且不是一个目录,则先将目录项删除然后将oldname 重命名为 newname。对包含 oldname 的目录以及包含 newname 的目录,调用进程必须具有写权限,因为将更改这两个目录。
(2)如若oldname指的是一个目录,那么为该目录重命名。如果 newname 已存在,则它必须引用一个目录,而且该目录应当是空目录(空目录指只有.和..项)。如果 newname 存在(而且是一个新目录),则先将其删除,然后将oldname 重命名为 newname。另外,当为一个目录重命名时,newname 不能包含 oldname 作为其路径名字的路径前缀。例如,不能将 /usr/foo 重命名为 /usr/foo/testdir,因为旧名字 (/usr/foo) 是新名字的路径前缀,因而不能将其删除。
(3)如若 oldname 或 newname 引用符号链接,则处理的是符号链接本身,而不是它所引用的文件。
(4)不能对 . 和 .. 重命名。
更确切的说,.和..都不能出现在 oldname 和 newname 的最后部分。
(5)作为一个特例,如果 oldname 和 newname 引用同一文件,则函数不做任何更改而成功返回。
如若 newname 已经存在,则调用进程对它需要有写权限。
另外,调用进程将删除 oldname 目录项,并可能要创建 newname 目录项,所以它需要对包含的 oldname 及包含 newname 的目录具有写和执行权限。
除了当 oldname 或 newname 指向相对路径名时,其他情况下 renameat 函数与 rename 函数功能相同。如果 oldname 参数指定了相对路径,就相对于 oldfd 参数引用的目录来计算 oldname。类似的,如果 oldname 参数指定了相对路径,就相对于newfd引用的目录来计算 newname。oldfd 或 newfd 参数都能设置成 AT_FDCWD,此时相对于当前目录来计算相应的路径名。

2、返回值

成功返回0,失败返回 -1.

3、示例说明


    
    
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main (void)
  4. {
  5. rename ( "a.txt", "b.txt");
  6. return 0;
  7. }

4、Linux 下的 mv 命令

参看:mv 命令
mv命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中。

(1)选项


    
    
  1. --backup=<备份模式>:若需覆盖文件,则覆盖前先行备份;
  2. -b:当文件存在时,覆盖前,为其创建一个备份;
  3. -f:若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目录;
  4. -i:交互式操作,覆盖前先行询问用户,如果源文件与目标文件或目标目录中的文件同名,则询问用户是否覆盖目标文件。用户输入”y”,表示将覆盖目标文件;输入”n”,表示取消对源文件的移动。这样可以避免误将文件覆盖。
  5. --strip-trailing-slashes:删除源文件中的斜杠“/”;
  6. -S<后缀>:为备份文件指定后缀,而不使用默认的后缀;
  7. --target-directory=<目录>:指定源文件要移动到目标目录;
  8. -u:当源文件比目标文件新或者目标文件不存在时,才执行移动操作。

(2)示例


    
    
  1. 将文件ex3改名为new1
  2. mv ex3 new1

十二、函数 mkdir、mkdirat和 rmdir

用 mkdir 和mkdirat 函数创建目录,用 rmdir 函数删除目录。

     
     
  1. #include <sys/stat.h>
  2. int mkdir (const char *pathname, mode_t mode);
  3. int mkdirat(int dirfd, const char *pathname, mode_t mode);

1、函数功能

这两个函数创建一个新的空目录。其中 . 和 .. 目录项是自动创建的。所指定的文件访问权限 mode 由进程的文件模式创建屏蔽字修改。

2、返回值

成功返回0,失败返回 -1。

3、mode 方式


     
     
  1. S_IRWXU 00700权限,代表该文件所有者拥有读,写和执行操作的权限
  2. S_IRUSR(S_IREAD) 00400权限,代表该文件所有者拥有可读的权限
  3. S_IWUSR(S_IWRITE) 00200权限,代表该文件所有者拥有可写的权限
  4. S_IXUSR(S_IEXEC) 00100权限,代表该文件所有者拥有执行的权限
  5. S_IRWXG 00070权限,代表该文件用户组拥有读,写和执行操作的权限
  6. S_IRGRP 00040权限,代表该文件用户组拥有可读的权限
  7. S_IWGRP 00020权限,代表该文件用户组拥有可写的权限
  8. S_IXGRP 00010权限,代表该文件用户组拥有执行的权限
  9. S_IRWXO 00007权限,代表其他用户拥有读,写和执行操作的权限
  10. S_IROTH 00004权限,代表其他用户拥有可读的权限
  11. S_IWOTH 00002权限,代表其他用户拥有可写的权限
  12. S_IXOTH 00001权限,代表其他用户拥有执行的权限

4、示例说明

一般,新建目录的权限为 0755

     
     
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main (void)
  4. {
  5. mkdir ( "test", 0755);
  6. return 0;
  7. }

5、Linux 下的 mkdir 命令

参看:mkdir 命令

(1)选项 


     
     
  1. -Z:设置安全上下文,当使用SELinux时有效;
  2. -m <目标属性>或--mode<目标属性>建立目录的同时设置目录的权限;
  3. -p或--parents 若所要建立目录的上层目录目前尚未建立,则会一并建立上层目录;
  4. --version 显示版本信息。

(2)示例


     
     
  1. 在目录/usr/meng下建立子目录test,并且只有文件主有读、写和执行权限,其他人无权访问
  2. mkdir -m 700 /usr/meng/test
  3. 在当前目录中建立bin和bin下的os_1目录,权限设置为文件主可读、写、执行,同组用户可读和执行,其他用户无权访问
  4. mkdir -p-m 750 bin/os_1

6、函数 rmdir

用 rmdir 函数可以删除一个空目录。空目录是只可包含 . 和 .. 这两项的目录。

     
     
  1. #include <unistd.h>
  2. int rmdir( const char *pathname );
  3. 返回值:若成功则返回 0,若出错则返回 -1
如果调用此函数使目录的链接计数成为0,并且也没有其他进程打开此目录,则释放由此目录占用的空间。如果在链接计数达到0时,有一个或几个进程打开了此目录,则在此函数返回前删除最后一个链接及.和..项。另外,在此目录中不能再创建新文件。但是在最后一个进程关闭它之前并不释放此目录。(即使另一个进程打开该目录,它们在此目录下也不能执行其他操作。这样处理的原因是,为了使rmdir函数成功执行,该目录必须是空的。)

(1)示例


     
     
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main (void)
  4. {
  5. rmdir ( "test");
  6. return 0;
  7. }

7、Linux 下的 rmdir 命令

参看:rmdir 命令  
删除目录必须是 空目录

(1)选项


    
    
  1. -p或--parents:删除指定目录后,若该目录的上层目录已变成空目录,则将其一并删除;
  2. --ignore-fail-on-non-empty:此选项使rmdir命令忽略由于删除非空目录时导致的错误信息;
  3. -v或-verboes:显示命令的详细执行过程;
  4. --help:显示命令的帮助信息;
  5. --version:显示命令的版本信息。

(2)示例


    
    
  1. 删除子目录os_1和其父目录bin
  2. rmdir -p bin/os_1

8、Linux 下的 rm 命令

正如上面的删除目录命令 rmdir 必须是空目录。非空目录则可以使用 rm 命令

(1)选项


    
    
  1. -d:直接把欲删除的目录的硬连接数据删除成 0,删除该目录;
  2. -f:强制删除文件或目录;
  3. -i:删除已有文件或目录之前先询问用户;
  4. -r或-R:递归处理,将指定目录下的所有文件与子目录一并处理;
  5. --preserve-root:不对根目录进行递归操作;
  6. -v:显示指令的详细执行过程。

(2)示例


    
    
  1. 删除 test 目录下除隐含文件外的所有文件和子目录
  2. # rm -r test

(3)说明

不过使用 rm 是很危险的,删除的东西是无法恢复。有时养成好的习惯使用 -i 选项删除前先询问用户一下也不错。

十三、读目录

1、opendir 函数


    
    
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. DIR *opendir(const char *name);

(1)函数功能

主要用于打开参数指定的目录,并且返回该目录所在的地址。

(2)返回值

成功返回指针,失败返回NULL

2、readdir 函数


    
    
  1. #include <dirent.h>
  2. struct dirent *readdir(DIR *dirp);

(1)函数功能

主要用于读取参数指定的目录中内容,返回参数指针

(2)结构体参数


    
    
  1. struct dirent {
  2. ino_t d_ino; /* inode number */ //i节点的编号
  3. off_t d_off; /* offset to the next dirent */ //距离下一个子项的偏移量
  4. unsigned short d_reclen; /* length of this record */ //记录的长度
  5. unsigned char d_type; /* type of file; not supported //文件的类型
  6. by all file system types */
  7. char d_name[ 256]; /* filename */ //文件名
  8. };

3、closedir 函数


    
    
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. int closedir(DIR *dirp);

(1)函数功能

主要用于关闭参数指定的目录

(2)示例说明


    
    
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <dirent.h>
  5. int main (void)
  6. {
  7. DIR *dir = opendir ( "code");
  8. if ( NULL == dir)
  9. perror ( "fail to opendir"), exit ( 1);
  10. struct dirent *ent;
  11. while (ent = readdir (dir))
  12. printf ( "文件名为:%s\n", ent->d_name);
  13. closedir (dir);
  14. return 0;
  15. }
  16. 输出结果:
  17. 文件名为:test
  18. 文件名为:.
  19. 文件名为:..

十四、函数 chdir、fchdir 和 getcwd

 1、chdir、fchdir

这两个函数中,分别用 pathname 或打开文件描述符来指定新的当前工作目录

     
     
  1. #include <unistd.h>
  2. int chdir(const char *path);
  3. int fchdir(int fd);

(1)函数功能

更改当前工作目录。
工作目录只是进程的一个属性,所以它只影响进程本身。

(2)返回值

成功返回 0,失败返回 -1.

(3)示例说明


     
     
  1. //示例一 chdir
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. int main (void)
  6. {
  7. if ( -1 == chdir ( "/tmp"))
  8. perror ( "fail to chdir"), exit ( 1);
  9. return 0;
  10. }

     
     
  1. //示例二 fchdir
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. int main (void)
  9. {
  10. int fd = open ( "/tmp", O_RDONLY);
  11. if ( -1 == fchdir (fd))
  12. perror ( "fail to fchdir"), exit ( 1);
  13. char buf[ 100];
  14. if (getcwd(buf, 100) == NULL)
  15. perror ( "fail to getcwd"), exit ( 1);
  16. printf( "cwd = %s\n", buf);
  17. return 0;
  18. }
  19. 输出结果:
  20. cwd = /tmp

(4)示例总结

执行示例,使用 pwd 命令查看当前目录可以发现,并没有发生改变。这是因为每个程序运行在独立的进程中,shell 的当前工作目录并不会随着程序调用 chdir 而改变。由此可见,为了改变 shell 进程自己的工作目录,shell 应当直接调用 chdir 函数,为此, cd 命令内建在 shell 中。

2、函数 getcwd


     
     
  1. #include <unistd.h>
  2. char *getcwd(char *buf, size_t size);

(1)函数功能

获取当前工作目录的绝对路径

(2)返回值

成功返回 buf,失败返回 NULL

(3)示例说明


     
     
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. int main (void)
  5. {
  6. if (chdir( "/usr/bin/") < 0)
  7. perror ( "chdir failed"), exit ( 1);
  8. char buf[ 100];
  9. if (getcwd(buf, 100) == NULL)
  10. perror ( "fail to getcwd"), exit ( 1);
  11. printf( "cwd = %s\n", buf);
  12. return0;
  13. }
  14. 输出结果:
  15. cwd = /usr/bin

(4)Linux 下的 pwd 命令

pwd 命令以绝对路径的方式显示用户当前工作目录。
命令将当前目录的全路径名称(从根目录)写入标准输出。全部目录使用 / 分隔。第一个 / 表示根目录,最后一个目录是当前目录。执行 pwd 命令可立刻得知您目前所在的工作目录的绝对路径名称。

十五、未讲部分:

设置用户 ID和设置组 ID
新文件和目录的所有权
粘着位
文件系统
函数 link
符号链接
创建和读取符号链接
文件的时间
函数 futimens
设备特殊文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值