为什么linux下空目录大小也是4kb?

    前几天同学问我:我用ls -l看目录大小。怎么都是4kb?

如图

    test目录下面有个空目录tt,tt的实际大小为4096。如果我在tt下面创建几个新的文件,tt的大小结果还是4096。也就是说,目录的大小的概念他不是很清楚。

    本人之前也未仔细想过这个问题。翻阅《深入理解linux内核》也未得到答案。于是寻思半天,给出一个答案,不知是否正确,还请有了解的赐教了。

    文件的大小大家都知道,比如一个文件1kb,由于簇的关系,实际占用4kb(假设)。如果我要读取这个文件的全部内容,我是alloc 1kb还是 4kb的内存呢?想必大家都是清楚的,应该是1kb。

    目录在linux下也是相当于一个文件,为了读取目录里面的目录项,我就必须读取它的全部内容。而这个大小就是盘块大小(或者几个盘块大小,视目录项个数决定)。由于目录项并不是连续排列的,比如tt目录里原先有500个文件,删了499个文件,剩下的1个目录项在哪里?是的,可能在一开始,也可能在最后。为了得到里面的那个目录项,我只能把盘块全部读取出来。在没有读取盘块之前,你是没有办法知道里面的目录项的!

    由于目录的特殊性,目录的实际大小就是占用空间的大小(上图就是4096)。这样子,可以使得读取目录和读取文件一样,可以统一起来了。

    话说的有点啰嗦了,不知道有过同样疑问的各位是否能够看懂,如果你有自己的想法,也欢迎讨论~~~


补充:

    这是资料截图。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上试试。欢迎讨论.

转载于:https://my.oschina.net/u/225495/blog/122578

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值