可以将inode和crtime用作唯一的文件标识符吗? [英] Can inode and crtime be used as a unique file identifier?

查看:167
本文介绍了可以将inode和crtime用作唯一的文件标识符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Linux 上有一个文件索引数据库.目前,我使用文件路径作为标识符. 但是,如果文件被移动/重命名,则其路径会更改,并且我无法将数据库记录与新文件匹配,因此必须删除/重新创建记录.更糟糕的是,如果目录被移动/重命名,那么我必须删除/重新创建所有文件和嵌套目录的记录.

I have a file indexing database on Linux. Currently I use file path as an identifier. But if a file is moved/renamed, its path is changed and I cannot match my DB record to the new file and have to delete/recreate the record. Even worse, if a directory is moved/renamed, then I have to delete/recreate records for all files and nested directories.

我想使用 inode 号作为唯一的文件标识符,但是如果删除文件并创建另一个文件,则可以重新使用inode号.

I would like to use inode number as a unique file identifier, but inode number can be reused if file is deleted and another file created.

因此,我想知道是否可以使用一对{inode,crtime}作为唯一文件标识符. 我希望在ext4上使用i_crtime,在NTFS上使用creation_time. 在我有限的测试中(使用ext4),在同一文件系统中重命名或移动文件或目录时,inode和crtime确实保持不变.

So, I wonder whether I can use a pair of {inode,crtime} as a unique file identifier. I hope to use i_crtime on ext4 and creation_time on NTFS. In my limited testing (with ext4) inode and crtime do, indeed, remain unchanged when renaming or moving files or directories within the same file system.

因此,问题是文件的inode或crtime是否在某些情况下可能会更改. 例如,fsck或碎片整理或分区大小调整是否可以更改inode或crtime或文件?

So, the question is whether there are cases when inode or crtime of a file may change. For example, can fsck or defragmentation or partition resizing change inode or crtime or a file?

有趣的是 http://msdn.microsoft.com/en-us /library/aa363788%28VS.85%29.aspx 说:

  • "在NTFS文件系统中,文件会保留相同的文件ID,直到删除为止."
    而且:
  • "在某些情况下,文件的文件ID可能会随时间变化."
  • "In the NTFS file system, a file keeps the same file ID until it is deleted."
    but also:
  • "In some cases, the file ID for a file can change over time."

那么,他们提到的是什么情况?

So, what are those cases they mentioned?

请注意,我研究了类似的问题:

Note that I studied similar questions:

  • How to determine the uniqueness of a file in linux?
  • Executing 'mv A B': Will the 'inode' be changed?
  • Best approach to detecting a move or rename to a file in Linux?

但他们没有回答我的问题.

but they do not answer my question.

推荐答案

Unix中i节点的分配和管理取决于文件系统.因此,对于每个文件系统,答案可能会有所不同.

The allocation and management of i-nodes in Unix is dependent upon the filesystem. So, for each filesystem, the answer may vary.

对于Ext3文件系统(最流行),索引节点被重用,因此不能用作唯一的文件标识符,也不会按照任何可预测的模式进行重用.

For the Ext3 filesystem (the most popular), i-nodes are reused, and thus cannot be used as a unique file identifier, nor is does reuse occur according to any predictable pattern.

在Ext3中,以位向量跟踪i节点,每个位代表一个i节点编号.当i节点释放时,它的位设置为零.当需要一个新的i节点时,会在位向量中搜索第一个零位,然后重用i节点号(可能先前已分配给另一个文件).

In Ext3, i-nodes are tracked in a bit vector, each bit representing a single i-node number. When an i-node is freed, it's bit is set to zero. When a new i-node is needed, the bit vector is searched for the first zero-bit and the i-node number (which may have been previously allocated to another file) is reused.

这可能会得出幼稚的结论,即编号最少的可用i节点将被重用.但是,Ext3文件系统非常复杂且经过高度优化,因此即使可以明确使用i-node编号,也不应做任何假设.

This may lead to the naive conclusion that the lowest numbered available i-node will be the one reused. However, the Ext3 file system is complex and highly optimised, so no assumptions should be made about when and how i-node numbers can be reused, even though they clearly will.

从ialloc.c的源代码中,其中分配了i节点:

From the source code for ialloc.c, where i-nodes are allocated:

有两种分配索引节点的策略.如果新的inode是 目录,然后向前搜索具有两个 可用空间和目录到索引的比率低;如果失败了,那么 他的平均可用空间高于平均水平,而该组最少 已选择目录.对于其他inode,请从以下位置向前搜索 父目录的阻止组以查找空闲的索引节点.

There are two policies for allocating an inode. If the new inode is a directory, then a forward search is made for a block group with both free space and a low directory-to-inode ratio; if that fails, then of he groups with above-average free space, that group with the fewest directories already is chosen. For other inodes, search forward from the parent directory's block group to find a free inode.

为Ext3管理此操作的源代码称为ialloc,最终版本在此处: https://github.com/torvalds/linux/blob/master/fs/ext3/ialloc.c

The source code that manages this for Ext3 is called ialloc and the definitive version is here: https://github.com/torvalds/linux/blob/master/fs/ext3/ialloc.c

这篇关于可以将inode和crtime用作唯一的文件标识符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆