文件系统

文件系统

文件存储

首先了解如下文件存储相关概念:inode、 dentry(d : dir, entry)、 数据存储、文件系统。

inode

其本质为结构体,存储文件的属性信息。如:权限、类型、大小、时间、用户、盘块位置……也叫作文件属性管理结构,ls 列出的信息也保存在 inode 中

大多数的inode都存储在磁盘上。

少量常用、近期使用的inode会被缓存到内存中。

dentry

目录项,其本质依然是结构体,重要成员变量有两个 {文件名,inode,...},而文件内容(data)保存在磁盘盘块中。

文件系统

文件系统是,一组规则,规定对文件的存储及读取的一般方法。文件系统在磁盘格式化过程中指定

常见的文件系统有:fat32  ntfs  exfat  ext2 、ext3 、ext4

 

 

 

 

ext2文件系统

                                                     

我们知道,一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如某种mkfs命令)格式化成某种格式的文件系统,然后才能存储文件格式化的过程会在磁盘上写一些管理存储布局的信息。上图是一个磁盘分区格式化成ext2文件系统后的存储布局。

文件系统中存储的最小单位是块(Block),一个块究竟多大是在格式化时确定的,例如mke2fs的-b选项可以设定块大小为1024、2048或4096字节。而上图中启动块(BootBlock)的大小是确定的,就是1KB,启动块是由PC标准规定的用来存储磁盘分区信息和启动信息,任何文件系统都不能使用启动块。启动块之后才是ext2文件系统的开始,ext2文件系统将整个分区划成若干个同样大小的块组(Block Group),每个块组都由以下部分组成。

超级块(Super Block)

描述整个分区的文件系统信息,例如块大小、文件系统版本号、上次mount的时间等等。超级块在每个块组的开头都有一份拷贝

块组描述符表GDT,Group Descriptor Table) 由很多块组描述符组成整个分区分成多少个块组就对应有多少个块组描述符。每个块组描述符(Group Descriptor)存储一个块组的描述信息,例如在这个块组中从哪里开始是inode表,从哪里开始是数据块,空闲的inode和数据块还有多少个等等。和超级块类似,块组描述符表在每个块组的开头也都有一份拷贝,这些信息是非常重要的,一旦超级块意外损坏就会丢失整个分区的数据,一旦块组描述符意外损坏就会丢失整个块组的数据,因此它们都有多份拷贝通常内核只用到第0个块组中的拷贝,当执行e2fsck检查文件系统一致性时,第0个块组中的超级块和块组描述符表就会拷贝到其它块组,这样当第0个块组的开头意外损坏时就可以用其它拷贝来恢复,从而减少损失。

块位图(Block Bitmap)

一个块组中的块是这样利用的:数据块存储所有文件的数据,比如某个分区的块大小是1024字节,某个文件是2049字节,那么就需要三个数据块来存,即使第三个块只存了一个字节也需要占用一个整块;超级块、块组描述符表、块位图、inode位图、inode表这几部分存储该块组的描述信息那么如何知道哪些块已经用来存储文件数据或其它描述信息,哪些块仍然空闲可用呢?块位图就是用来描述整个块组中哪些块已用哪些块空闲的,它本身占一个块,其中的每个bit代表本块组中的一个块,这个bit为1表示该块已用,这个bit为0表示该块空闲可用。

为什么用df命令统计整个磁盘的已用空间非常快呢?因为只需要查看每个块组的块位图即可,而不需要搜遍整个分区。相反,用du命令查看一个较大目录的已用空间就非常慢,因为不可避免地要搜遍整个目录的所有文件。

与此相联系的另一个问题是:在格式化一个分区时究竟会划出多少个块组呢?主要的限制在于块位图本身必须只占一个块。用mke2fs格式化时默认块大小是1024字节,可以用-b参数指定块大小,现在设块大小指定为b字节,那么一个块可以有8b个bit,这样大小的一个块位图就可以表示8b个块的占用情况,因此一个块组最多可以有8b个块,如果整个分区有s个块,那么就可以有s/(8b)个块组。格式化时可以用-g参数指定一个块组有多少个块,但是通常不需要手动指定,mke2fs工具会计算出最优的数值。

inode位图(inode Bitmap)

和块位图类似,本身占一个块,其中每个bit表示一个inode是否空闲可用

inode(inode Table)

我们知道,一个文件除了数据需要存储之外,一些描述信息也需要存储,例如文件类型(常规、目录、符号链接等),权限,文件大小,创建/修改/访问时间等,

也就是ls -l命令看到的那些信息,这些信息存在inode中而不是数据块中。每个文件都有一个inode,一个块组中的所有inode组成了inode表。

inode表占多少个块在格式化时就要决定并写入块组描述符中,mke2fs格式化工具的默认策略是一个块组有多少个8KB就分配多少个inode

由于数据块占了整个块组的绝大部分,也可以近似认为数据块有多少个8KB就分配多少个inode,换句话说,如果平均每个文件的大小是8KB,

当分区存满的时候inode表会得到比较充分的利用,数据块也不浪费。

如果这个分区存的都是很大的文件(比如电影),则数据块用完的时候inode会有一些浪费,如果这个分区存的都是很小的文件(比如源代码),

则有可能数据块还没用完inode就已经用完了,数据块可能有很大的浪费。如果用户在格式化时能够对这个分区以后要存储的文件大小做一个预测,

也可以用mke2fs的-i参数手动指定每多少个字节分配一个inode。

inode表中每个inode的大小:ext2、ext3中128字节,ext4中256字节

数据块(Data Block)

根据不同的文件类型有以下几种情况:

对于常规文件,文件的数据存储在数据块中

对于目录,该目录下的所有文件名和目录名存储在数据块中,注意文件名保存在它所在目录的数据块中,除文件名之外,ls -l命令看到的其它信息都保存在该文件的inode中。注意这个概念:目录也是一种文件,是一种特殊类型的文件。

对于符号链接,如果目标路径名较短则直接保存在inode中以便更快地查找,如果目标路径名较长则分配一个数据块来保存。

设备文件、FIFO和socket等特殊文件没有数据块,设备文件的主设备号和次设备号保存在inode中。

 

 

数据块寻址:

    https://baijiahao.baidu.com/s?id=1590984722027150374&wfr=spider&for=pc

         如果一个文件有多个数据块,这些数据块很可能不是连续存放的。这些数据块通过inode中的索引项Block来找到。

         这样的索引项一共有15个,Block[0]--Block[14],每个索引项占4字节。前12个索引项都表示块的编号,如Block[0]保存27,表示第27个块是该文件的数据块。

         如果块的大小是1KB,这种方法可以表示从0-12KB的文件。如果剩下的三个索引项也这么用,那就只能表示最大15KB的文件了。这是远远不够的。

         剩下的3个索引项Block[12]、Block[13]、Block[14]都是间接索引。假设块的大小为b,那么一个间接寻址块中可以存放b/4个索引项,指向b/4个数据块。

         如:b=1KB,那么Block[0]--Block[12]都用上,最大可以表示 1024/4 + 12 = 268K大小的文件。虽然大很多,但仍然不够用。

         于是有了二级间接寻址块Block[13],三级间接寻址块Block[14]。这样1K的块最大可以表示16.06GB大小的文件。

 

stat命令中: “块”--> 物理块(512B)的个数;“IO块”--> 逻辑块的大小

文件操作

stat函数

获取文件属性,(从inode结构体中获取)            

         int stat(const char *path, struct stat *buf);   成返回0;失败返回-1 设置errno为恰当值。

                   参数1:文件名

                   参数2:inode结构体指针 (传出参数)

 命令行:stat filename ,查看文件属性

文件属性将通过传出参数返回给调用者。

练习:使用stat函数查看文件属性 【stat.c】

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    struct stat buf;

    int ret = stat("temp.txt", &buf);
    if(ret == -1){
        perror("stat error");
        exit(1);
    }

    printf("st_ino = %ld\n", buf.st_ino);
    printf("st_size = %ld\n", buf.st_size);
    printf("st_nlink = %d\n", buf.st_nlink);
    printf("st_uid = %d\n", buf.st_uid);
    printf("st_gid = %d\n", buf.st_gid);
    printf("st_mode = %x\n", buf.st_mode);

    return 0;
}

 

判断文件类型:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

int main(int argc, char * argv[])
{
    struct stat sbuf;

    int ret = stat(argv[1], &sbuf);         //穿透符号链接
    if (ret < 0) {
        perror("stat");
        exit(1);
    }

    printf("inode = %d\n", (int)sbuf.st_ino);
    printf("size = %d\n", (int)sbuf.st_size);

    if (S_ISREG(sbuf.st_mode)) {
        printf("It's a regular file\n");
    } else if (S_ISDIR(sbuf.st_mode)) {
        printf("It's a directory\n");
    } else if (S_ISFIFO(sbuf.st_mode)) {
        printf("It's a fifo\n");
    } else if (S_ISLNK(sbuf.st_mode)) {
        printf("It's a symbolic link\n");
    }

    return 0;
}

 

std_mode

 

lstat函数               

int lstat(const char *path, struct stat *buf); 成返回0;失败返回-1 设置errno为恰当值。

 

练习:给定文件名,判断文件类型。【get_file_type.c】

 

文件类型判断方法:st_mode 取高4位。 但应使用宏函数:          

       S_ISREG(m)      is it a regular file?

       S_ISDIR(m)       directory?

       S_ISCHR(m)     character device?

       S_ISBLK(m)      block device?

       S_ISFIFO(m)      FIFO (named pipe)?

       S_ISLNK(m)      symbolic link?  (Not in POSIX.1-1996.)

       S_ISSOCK(m)     socket?  (Not in POSIX.1-1996.)

穿透符号链接:stat:会;lstat:不会

 

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    char *p;
    struct stat statbuf;

    int ret = lstat(argv[1], &statbuf);
    
    if(S_ISREG(statbuf.st_mode))
        p = "regular file";
    else if(S_ISDIR(statbuf.st_mode))
        p = "directory";
    else if(S_ISLNK(statbuf.st_mode))    //???
        p = "symbolic link";
    else if(S_ISCHR(statbuf.st_mode))
        p = "character device";
    else if(S_ISBLK(statbuf.st_mode))
        p = "block device";
    else if(S_ISFIFO(statbuf.st_mode))
        p = "named pipe";
    else if(S_ISSOCK(statbuf.st_mode))
        p = "socket";
    else
        p = "unknown";

    printf("\"%s\" is %s\n", argv[1], p);
    
    return 0;
}

 

特殊权限位:

         包含三个二进制位。依次是:设置组ID位setGID;设置用户ID位setUID;黏住位sticky

黏住位

早起计算机内存紧,只有精要的常用的程序可以常驻物理内存,剩下的要暂存磁盘中。当内存不够用的时候会将该部分程序存回磁盘,腾出内存空间。若文件设置了黏住位,那么即使在内存比较吃紧的情况下,也不会将该文件回存到磁盘上。由于现阶段操作系统的虚拟内存管理分页算法完善。该功能已经被废弃。

但我们仍然可以对目录设置黏住位。被设置了该位的目录,其内部文件只有:

①超级管理员

②该目录所有者

③该文件的所有者

以上三种用户有权限做删除操作。其他用户可以写、读但不能随意删除。

设置黏住位 chmod 01777 filename

setUID

进程有两个ID:EID(有效用户ID),表示进程履行哪个用户的权限。(执行 sudo 的时候其实改变的就是有效用户ID

                         UID(实际用户ID),表示进程实际属于哪个用户。

         多数情况下,EID和UID相同。但是,当文件的setID被设置后两个ID则有可能不一样。

         例如:当进程执行一个root用户的文件,若该文件的setID位被设置为1, 那么执行该文件时,进程的UID不变。EID变为root,表示进程开始履行root用户权限。

setGID位于setID相类似。

access函数

测试指定文件是否存在/拥有某种权限。

         int access(const char *pathname,  int mode);

     返回值:成功/具备该权限:0;失败/不具备 -1 设置errno为相应值。

                  参数2:R_OK(测试是否有读权限)、W_OK(写权限)、X_OK(执行权限)

      chmod a-x file

         通常使用access函数来测试某个文件是否存在。F_OK   

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    int ret;
    if ((ret = access("abc", W_OK)) < 0) {
        printf("error ret = %d\n", ret);
        perror("abc");
        exit(1);
    }
    printf("ret = %d\n", ret);
    printf("abc is exist\n");

    return 0;
}

 

chmod函数

修改文件的访问权限

int chmod(const char *path, mode_t mode);              成功:0;失败:-1设置errno为相应值

int fchmod(int fd, mode_t mode);

truncate函数

截断文件长度成指定长度。常用来拓展文件大小,代替lseek不要打开文件,也不需要IO操作

       int truncate(const char *path, off_t length);     成功:0;失败:-1设置errno为相应值

       int ftruncate(int fd, off_t length);

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

int main(void)
{
    int fd;

    fd = open("./file.t", O_RDWR | O_CREAT | O_TRUNC, 0644);
    if (fd < 0) {
        perror("open error");
        exit(1);
    }

#if 0
    lseek(fd, 99, SEEK_SET);
    write(fd, "s", 1);

    close(fd);
#endif

    int ret = truncate("file.t", 200);
    if (ret < 0) {
        perror("open error");
        exit(1);
    }

    return 0;
}

 

link函数

思考,为什么目录项要游离于inode之外,画蛇添足般的将文件名单独存储呢??这样的存储方式有什么样的好处呢?

其目的是为了实现文件共享Linux允许多个目录项共享一个inode,即共享盘块(data)。不同文件名,在人类眼中将它理解成两个文件,但是在内核眼里是同一个文件

link函数,可以为已经存在的文件创建目录项(硬链接 ln命令)。

         int link(const char *oldpath,  const char *newpath);   成功:0;失败:-1设置errno为相应值

         注意:由于两个参数可以使用“相对/绝对路径+文件名”的方式来指定,所以易出错。

                   如:link("../abc/a.c", "../ioc/b.c")若a.c,b.c都对, 但abc,ioc目录不存在也会失败。

         mv命令既是修改了目录项,而并不修改文件本身

 

unlink函数

 删除一个文件的目录项;

         int unlink(const char *pathname); 成功:0;失败:-1设置errno为相应值

练习:编程实现mv命令的改名操作【imp_mv.c】

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    if(link(argv[1], argv[2]) == -1){
        perror("link error");
        exit(1);
    }

    if(unlink(argv[1]) == -1){
        perror("unlink error");
        exit(1);
    }

    return 0;
}

 

         注意Linux下删除文件的机制不断将st_nlink -1,直至减到0为止。无目录项对应的文件,将会被操作系统择机释放。(具体时间由系统内部调度算法决定)

         因此,我们删除文件,从某种意义上说,只是让文件具备了被释放的条件。

  删除文件,只是将硬连接数减到了0,磁盘上的数据并不会擦除,对应的inode map位会置位,重新写入数据时,只会是新的数据覆盖旧的数据,不会有数据擦除的动作

  所以,即使我们将一个硬盘格式化了,还没有写入新的数据之前,是有办法将数据恢复的

unlink函数的特征:清除文件时,如果文件的硬链接数到0了,没有dentry对应,但该文件仍不会马上被释放。要等到所有打开该文件的进程关闭该文件,系统才会挑时间将该文件释放掉。【unlink_exe.c】

/*
 *unlink函数是删除一个dentry
 */
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


int main(void)
{
    int fd;
    char *p = "test of unlink\n";
    char *p2 = "after write something.\n";

    fd = open("temp.txt", O_RDWR|O_CREAT|O_TRUNC, 0644); // 一个临时文件,程序执行完之后就将其释放
    if(fd < 0){
        perror("open temp error");
        exit(1);
    }
    int ret = unlink("temp.txt");         //具备了被释放的条件
    if(ret < 0){
        perror("unlink error");
        exit(1);
    }

    ret = write(fd, p, strlen(p));// 虽然上面unlink掉了,但是这个write不会出错
    if (ret == -1) {
        perror("-----write error");
    }

    // p[0] = 'H'; // 打开着句话,将出现段错误

    printf("hi! I'm printf\n");
    ret = write(fd, p2, strlen(p2));
    if (ret == -1) {
        perror("-----write error");
    }


    printf("Enter anykey continue\n");
    getchar();

    close(fd);


    return 0;
}

 

隐式回收:

         当进程结束运行时,所有该进程打开的文件会被关闭,申请的内存空间会被释放。系统的这一特性称之为隐式回收系统资源。

symlink函数

创建一个符号链接

int symlink(const char *oldpath, const char *newpath);        成功:0;失败:-1设置errno为相应值

readlink函数

读取符号链接文件本身内容,得到链接所指向的文件名。穿透的

ssize_t readlink(const char *path, char *buf, size_t bufsiz);   成功:返回实际读到的字节数;失败:-1设置errno为相应值。

rename函数

重命名一个文件。

 int rename(const char *oldpath, const char *newpath); 成功:0;失败:-1设置errno为相应值

用link, unlink 来实现 rename

目录操作

工作目录:“./”代表当前目录,指的是进程当前的工作目录默认是进程所执行的程序所在的目录位置

getcwd函数

获取进程当前工作目录         (卷3,标库函数)

                  char *getcwd(char *buf, size_t size);       成功:buf中保存当前进程工作目录位置。失败返回NULL。

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main(void)
{
    char buf[64];
    printf("%s\n", getcwd(buf, 64));

    if (chdir("..") < 0) {
        perror("chdir error");
        exit(1);
    }

    printf("%s\n", getcwd(buf, 64));

    if (chdir("/home/itcast") < 0) {
        perror("chdir error");
        exit(1);
    }
    printf("%s\n", getcwd(buf, 64));
    int fd = open("haha.c", O_RDWR | O_CREAT | O_TRUNC, 0644);

    close(fd);

    return 0;
}

 

chdir函数

改变当前进程的工作目录

                  int chdir(const char *path);   成功:0;失败:-1设置errno为相应值

 

         练习:获取及修改当前进程的工作目录,并打印至屏幕。【imp_cd.c】

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    char buf[64];
    char *p;
    system("ls -l");

    int ret = chdir(argv[1]);    //改变当前进程工作目录
    if(ret < 0){
        perror("chdir error");
        exit(1);
    }
    system("ls -l");

    chdir("..");            //改变当前进程当前工作目录

    system("ls -l");

    return 0;
}

 

文件、目录权限

注意:目录文件也是“文件”。其文件内容是该目录下所有子文件的目录项dentry。 可以尝试用vim打开一个目录。

 

r

w

x

文件

文件的内容可以被查看

内容可以被修改

可以运行产生一个进程

 

cat、more、less…

vi、> …

./文件名

       

目录

目录可以被浏览

创建、删除、修改文件

可以被打开、进入

 

ls、tree…

mv、touch、mkdir…

cd

目录设置黏住位:若有w权限,创建不变,删除、修改只能由root、目录所有者、文件所有者操作。

opendir函数

根据传入的目录名打开一个目录 (库函数)                            DIR * 类似于 FILE *

         DIR *opendir(const char *name);     成功返回指向该目录结构体指针,失败返回NULL       

    参数支持相对路径、绝对路径两种方式:例如:打开当前目录:① getcwd() , opendir() ② opendir(".");

closedir函数

关闭打开的目录

int closedir(DIR *dirp);              成功:0;失败:-1设置errno为相应值

readdir函数

读取目录         (库函数)

         struct dirent *readdir(DIR *dirp);  成功返回目录项结构体指针;失败返回NULL设置errno为相应值

                  需注意返回值,读取数据结束时也返回NULL值,所以应借助errno进一步加以区分。

         struct 结构体:

           struct dirent {

               ino_t          d_ino;      inode编号

               off_t          d_off;      

               unsigned short  d_reclen;    文件名有效长度

               unsigned char   d_type;     类型(vim打开看到的类似@*/等)

               char          d_name[256];文件名

           };

         其成员变量重点记忆两个:d_ino、d_name。实际应用中只使用到d_name。

 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <dirent.h>

int main(int argc, char **argv)
{
    DIR *dp;
    struct dirent *sdp;
    dp = opendir(argv[1]);
    if (dp == NULL) {
        perror("open dir error");
        exit(1);
    }
    printf("--open succcess\n");

    while((sdp = readdir(dp)) != NULL) {
        printf("%s\t",sdp->d_name);
    }
    printf("\n");

    closedir(dp);
    return 0;
}

 

         练习:实现ls不打印隐藏文件。每5个文件换一个行显示。 【imp_ls2.c】

 

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    DIR *dp;
    struct dirent *item;
    int count = 0;

    if (argc == 1) {
        dp = opendir(".");        //ls是查看当前目录的内容
        if(dp == NULL){
            perror("opendir error");
            exit(1);
        }
    } else if (argc == 2) {
        dp = opendir(argv[1]);        //ls是查看当前目录的内容
        if(dp == NULL){
            perror("opendir error");
            exit(1);
        }
    }

    //读目录项,把目录项的d_name打印
    while((item = readdir(dp))){        //item == NULL读完
        if(item->d_name[0] != '.'){
            printf("%-10s%c", item->d_name, ++count % 5 ? '\t' : '\n');    
        }
    }
    putchar('\n');

    closedir(dp);

    return 0;
}
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    DIR *dp;
    struct dirent *item;
    int count = 0;

    if (argc == 1) {
        dp = opendir(".");        //ls是查看当前目录的内容
        if(dp == NULL){
            perror("opendir error");
            exit(1);
        }
    } else if (argc == 2) {
        dp = opendir(argv[1]);        //ls是查看当前目录的内容
        if(dp == NULL){
            perror("opendir error");
            exit(1);
        }
    }

    //读目录项,把目录项的d_name打印
    while((item = readdir(dp))){        //item == NULL读完
        if(item->d_name[0] != '.'){
            printf("%-10s%c", item->d_name, ++count % 5 ? '\t' : '\n');    
        }
    }
    putchar('\n');

    closedir(dp);

    return 0;
}
View Code

 

拓展2:统计目录及其子目录中的普通文件的个数

rewinddir函数

回卷目录读写位置至起始。

void rewinddir(DIR *dirp);       返回值:无。

telldir/seekdir函数

         获取目录读写位置

                   long telldir(DIR *dirp); 成功:与dirp相关的目录当前读写位置。失败-1,设置errno

         修改目录读写位置

                   void seekdir(DIR *dirp, long loc); 返回值:无

                   参数loc一般由telldir函数的返回值来决定。

递归遍历目录

         查询指定目录,递归列出目录中文件,同时显示文件大小。 【ls_R.c】

#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define PATH_LEN 256
//dir=/home/itcast/1110_linux    fcn=isfile

void fetchdir(const char *dir, void (*fcn)(char *))//该函数被调用,既已被判定为目录
{
    char name[PATH_LEN];
    struct dirent *sdp;
    DIR *dp;

    if ((dp = opendir(dir)) == NULL) {     //打开目录失败,如:没有x权限
        //perror("fetchdir can't open");
        fprintf(stderr, "fetchdir: can't open %s\n", dir);
        return;
    }

    //
    while ((sdp = readdir(dp)) != NULL) {
        if (strcmp(sdp->d_name, ".") == 0 || strcmp(sdp->d_name, "..") == 0) {    //防止出现无限递归
            continue; 
        }

        if (strlen(dir)+strlen(sdp->d_name)+2 > sizeof(name)) {
            fprintf(stderr, "fetchdir: name %s %s too long\n", dir, sdp->d_name);
        } else {
            sprintf(name, "%s/%s", dir, sdp->d_name);
            (*fcn)(name);                     //这是一个什么??  
        }
    }

    closedir(dp);
}

void isfile(char *name)          //处理目录/文件
{
    struct stat sbuf;

   if (stat(name, &sbuf) == -1) {   //文件名无效
        fprintf(stderr, "isfile: can't access %s\n", name);
        exit(1);
    }
    if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {  //判定是否为目录
        fetchdir(name, isfile);                //回调函数,谁是回调函数呢?
    }

    printf("%8ld %s\n", sbuf.st_size, name);   //不是目录,则是普通文件,直接打印文件名
}
//./ls_R ~/1110_linux
int main(int argc, char *argv[])
{
    if (argc == 1) 
        isfile(".");
    else
        while (--argc > 0)          //可一次查询多个目录 
            isfile(*++argv);        //循环调用该函数处理各个命令行传入的目录

    return 0;
}

 

重定向

         dup 和 dup2函数

         int dup(int oldfd); 成功:返回一个新文件描述符;失败:-1设置errno为相应值

         int dup2(int oldfd, int newfd);

 

            重定向示

记忆方法两种:

1. 文件描述符的本质角度理解记忆(指针 old 结构体 拷贝给 new)。

2. 从函数原型及使用角度,反向记忆(new 指向 old)。

练习:借助dup函数编写mycat程序,实现cat file1 > file2 命令相似功能。【mycat.c】

#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void sys_err(int fd, char *err_name)
{
    if(fd < 0){
        perror(err_name);
        exit(1);
    }
}

int main(void)
{
    int fd_f1, fd_f2, fd_out;
    int ret;
    char buf[1024];
    
    fd_f1 = open("file1", O_RDONLY);
sys_err(fd_f1, "open file1 error");

    fd_f2 = open("file2", O_RDONLY);
sys_err(fd_f2, "open file2 error");

    fd_out = open("out", O_WRONLY|O_TRUNC|O_CREAT, 0644);
sys_err(fd_out, "open out error");

    dup2(fd_out, STDOUT_FILENO);

    while ((ret = read(fd_f1, buf, sizeof(buf)))) {
        write(fd_out, buf, ret);
    }

    while ((ret = read(STDIN_FILENO, buf, sizeof(buf)))) {
        write(fd_out, buf, ret);
    }

    while ((ret = read(fd_f2, buf, sizeof(buf)))) {
        write(fd_out, buf, ret);
    }

    close(fd_f1);
    close(fd_f2);
    close(fd_out);

    return 0;
}

 

VFS虚拟文件系统

Linux支持各种各样的文件系统格式,如ext2、ext3、reiserfs、FAT、NTFS、iso9660等等,不同的磁盘分区、光盘或其它存储设备都有不同的文件系统格式,

然而这些文件系统都可以mount到某个目录下,使我们看到一个统一的目录树,各种文件系统上的目录和文件我们用ls命令看起来是一样的,读写操作用起来也都是一样的,这是怎么做到的呢?

Linux内核在各种不同的文件系统格式之上做了一个抽象层,使得文件、目录、读写访问等概念成为抽象层的概念,因此各种文件系统看起来用起来都一样,这个抽象层称为虚拟文件系统(VFS,Virtual Filesystem)。

 

 

 

          

 

.bss: 未初始化的全局变量   初始化为0的全局变量  static修饰(静态变量)的未初始化的或初始化为0的变量

data:初始化为非0的全局变量    static修饰(静态变量)的初始化为非0的变量

rodata:常量、只读全局变量

stack:局部变量

// 声明提升为定义 如:int var; 若前面加了extern 则不能提升为定义,只能为一个变量声明
a.c  extern int var; 

b.c  extern int var;  ->  a.out   

段错误:

    1. 对只读区域进程写操作。    char *p = "hello"; p[0] = 'H';

    2. 对非访问区域进行读写操。    地址1000

    3. stack空间耗尽。        函数内部 int arr[N]= {1};  #define N 100000000

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int i, m, n;
    int count = 1;          //用来设置显示格式

    if (argc != 2) {
        printf("Enter: ./a.out number\n");
        exit(1);
    }

    printf("1~%d之间的素数有:\n", n = atoi(argv[1]));

    for (i = 2; i <= n; i++) {
        for(m = 2; m < i; m++) {
            if(i % m == 0) {
                break;
            }
        }
        if (m >= i)
            printf("%2d %c", i, (count++) % 10 ? ' ' : '\n');
    } 
    printf("\n");

    return 0;
}
将小于n 的所有质数找出来

编程统计指定目录下普通文件个数。 包括其子目录下的普通文件, 将文件总数
打印至屏幕。

#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int count(char *root)
{
    DIR *dp;
    struct dirent *item;
    int n = 0;  char path[1024];

    dp = opendir(root);             //打开目录
    if (dp == NULL) {
        perror("----opendir error");
        exit(1);        
    }
    while ((item = readdir(dp))) {    //遍历每一个目录项,NULL表示读完
        struct stat statbuf;

        if (strcmp(item->d_name, ".") == 0 || strcmp(item->d_name, "..") == 0)
            continue;

        sprintf(path, "%s/%s", root, item->d_name);//将子目录和当前工作目录拼接成一个完整文件访问路径

        /*取文件的属性, lstat防止穿透*/
        if (lstat(path, &statbuf) == -1) {
            perror("lstat error");
            exit(1);
        }

        if (S_ISREG(statbuf.st_mode)) {
            n++;
        } else if (S_ISDIR(statbuf.st_mode)) {
            n += count(path);       //递归调用该函数
        }
    }

    closedir(dp);

    return n;
}

int main(int argc, char *argv[])
{
    int total = 0;

    if (argc == 1) {
        total = count(".");
        printf("There are %d files in ./\n", total);
    } else if (argv[1]) {
        total = count(argv[1]);
        printf("There are %d files in %s\n", total, argv[1]);
    }

    return 0;
}
View Code

 

posted @ 2019-02-24 22:58  狂奔~  阅读(631)  评论(0编辑  收藏  举报