8

ext2 文件属性分析

 3 years ago
source link: https://blog.popkx.com/ext2-file-attribute-analysis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ext2 文件属性分析

发表于 2018-05-03 16:05:00   |   已被 访问: 313 次   |   分类于:   文件系统   |   暂无评论
    本节以 linux 系统的一些实际文件为例,对 ext2 文件系统下的文件属性进行分析。

进入linux 系统

  • 在 home 目录下 ls -l
lcc@lcc-vm:~$ ls -l
total 32
drwxr-xr-x 114 akaedu akaedu 12288 2008-10-25 11:33 akaedu
drwxr-xr-x 114 ftp    ftp     4096 2008-10-25 10:30 ftp
drwx------   2 root   root   16384 2008-07-04 05:58 lost+found
  • 为什么各目录的大小都是 4096 的整数倍?因为这个分区的块大小是 4096 ,目录的大小总是数据块的整数倍。为什么有的目录大有的目录小?因为目录的数据块保存着它下边所有文件和目录的名字,如果一个目录中的文件很多,一个块装不下这么多文件名,就可能分配更多的数据块给这个目录。再比如:
lcc@lcc-vm:~$ ls -l /dev
......
prw-r-----  1 syslog adm            0 2008-10-25 11:39 xconsole
crw-rw-rw-  1 root   root      1,   5 2008-10-24 16:44 zero
  • xconsole 文件的类型是 p (表示pipe),是一个 FIFO 文件,后面会讲到它其实是一块内核缓冲区的标识,不在磁盘上保存数据,因此没有数据块,文件大小是 0。 zero 文件的类型是 c,表示字符设备文件,它代表内核中的一个设备驱动程序,也没有数据块,原本应该写文件大小的地方写了 1, 5 这两个数字,表示主设备号和次设备号,访问该文件时,内核根据设备号找到相应的驱动程序。再比如:
lcc@lcc-vm:~$ touch hello
lcc@lcc-vm:~$ ln -s ./hello halo
lcc@lcc-vm:~$ ls -l
total 0
lrwxrwxrwx 1 akaedu akaedu 7 2008-10-25 15:04 halo -> ./hello
-rw-r--r-- 1 akaedu akaedu 0 2008-10-25 15:04 hello
  • 文件 hello 是刚创建的,字节数为 0,符号链接文件 halo 指向 hello,字节数却是7,为什么呢?其实 7 就是 “./hello” 这7个字符,符号链接文件就保存着这样一个路径名。再试试硬链接:
lcc@lcc-vm:~$ ln ./hello hello2
lcc@lcc-vm:~$ ls -l
total 0
lrwxrwxrwx 1 akaedu akaedu 7 2008-10-25 15:08 halo -> ./hello
-rw-r--r-- 2 akaedu akaedu 0 2008-10-25 15:04 hello
-rw-r--r-- 2 akaedu akaedu 0 2008-10-25 15:04 hello2
  • hello2 和 hello 除了文件名不一样之外,别的属性都一模一样,并且 hello 的属性发生了变化,第二栏的数字原本是 1,现在变成 2 了。从根本上说, hello 和 hello2 是同一个文件在文件系统中的两个名字, ls -l 第二栏的数字是硬链接数,表示一个文件在文件系统中有几个名字(这些名字可以保存在不同目录的数据块中,或者说可以位于不同的路径下),硬链接数也保存在 inode 中。既然是同一个文件, inode 当然只有一个,所以用 ls -l 看它们的属性是一模一样的,因为都是从这个 inode 里读出来的。再研究一下目录的硬链接数:
lcc@lcc-vm:~$ mkdir a
lcc@lcc-vm:~$ mkdir a/b
lcc@lcc-vm:~$ ls -ld a
drwxr-xr-x 3 akaedu akaedu 4096 2008-10-25 16:15 a
lcc@lcc-vm:~$ ls -la a
total 20
drwxr-xr-x   3 akaedu akaedu  4096 2008-10-25 16:15 .
drwxr-xr-x 115 akaedu akaedu 12288 2008-10-25 16:14 ..
drwxr-xr-x   2 akaedu akaedu  4096 2008-10-25 16:15 b
lcc@lcc-vm:~$ ls -la a/b
total 8
drwxr-xr-x 2 akaedu akaedu 4096 2008-10-25 16:15 .
drwxr-xr-x 3 akaedu akaedu 4096 2008-10-25 16:15 ..
  • 首先创建目录 a,然后在它下面创建子目录 a/b。目录 a 的硬链接数是 3,这 3 个名字分别是当前目录下的 a, a 目录下的 . 和 b 目录下的 ..。目录 b 的硬链接数是 2,这两个名字分别是 a 目录下的 b 和 b 目录下的 .。
注意,目录的硬链接只能这种方式创建,用 ln 命令可以创建目录的符号链接,但不能创建目录的硬链接。

阅读更多:   文件系统


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK