前几天同窗问我:我用ls -l看目录大小。怎么都是4kb? node
如图 linux
test目录下面有个空目录tt,tt的实际大小为4096。若是我在tt下面建立几个新的文件,tt的大小结果仍是4096。也就是说,目录的大小的概念他不是很清楚。 socket
本人以前也未仔细想过这个问题。翻阅《深刻理解linux内核》也未获得答案。因而寻思半天,给出一个答案,不知是否正确,还请有了解的赐教了。 测试
文件的大小你们都知道,好比一个文件1kb,因为簇的关系,实际占用4kb(假设)。若是我要读取这个文件的所有内容,我是alloc 1kb仍是 4kb的内存呢?想必你们都是清楚的,应该是1kb。 this
目录在linux下也是至关于一个文件,为了读取目录里面的目录项,我就必须读取它的所有内容。而这个大小就是盘块大小(或者几个盘块大小,视目录项个数决定)。因为目录项并非连续排列的,好比tt目录里原先有500个文件,删了499个文件,剩下的1个目录项在哪里?是的,可能在一开始,也可能在最后。为了获得里面的那个目录项,我只能把盘块所有读取出来。在没有读取盘块以前,你是没有办法知道里面的目录项的! spa
因为目录的特殊性,目录的实际大小就是占用空间的大小(上图就是4096)。这样子,可使得读取目录和读取文件同样,能够统一块儿来了。 code
话说的有点啰嗦了,不知道有过一样疑问的各位是否可以看懂,若是你有本身的想法,也欢迎讨论~~~ orm
补充: ip
这是资料截图。inode 指i_node节点编号,rec_len指离下个目录项的偏移,后面的看看也知道的吧。因为name不丁长,因此须要name_lens。因此一个目录项至少12字节。一个目录块4096字节,能够放多少目录项呢。答案是2+339 =341 。2指 . 和 .. 两个目录项 , 339 为本身建立的文件的目录项。经过测试也证实了个人猜想。你若是建立340个文件,那目录块就会立马变大到12kb。(为何不是8kb,有谁知道?)。 内存
下面的原文如是说:
The directory entries must be aligned on 4 bytes boundaries and there cannot be any directory entry spanning multiple data blocks. If an entry cannot completely fit in one block, it must be pushed to the next data block and the rec_len of the previous entry properly adjusted.(一个放不下,就放到下一个去,前面的目录项的rec_len字段作相应调整)
事实上,你删除目录里的文件把目录项删除后,空余的盘块是不会回收的。也就是说,目录的大小只增不减。
原文以下:
A directory is a filesystem object and has an inode just like a file. It is a specially formatted file containing records which associate each name with an inode number. Later revisions of the filesystem also encode the type of the object (file, directory, symlink, device, fifo, socket) to avoid the need to check the inode itself for this information.
The inode allocation code should try to assign inodes which are in the same block group as the directory in which they are first created.
The original Ext2 revision used singly-linked list to store the filenames in the directory; newer revisions are able to use hashes and binary trees.
Also note that as directory grows additional blocks are assigned to store the additional file records. When filenames are removed, some implementations do not free these additional blocks.(记住:若是目录项不少,就会有多个额外的盘块来保存,但当里面的文件删除后,某些实现并不删除这些多余的盘块) 各位能够在本身的pc上试试。欢迎讨论.