How to determine the uniqueness of a file in linux

2019-08-29 18:02发布

问题:

What i mean to "uniqueness" here also concerns about the time.

  • Every time a file created on file system, there's a unique file.
  • Files in same directory with same name but appears in different time stage are different.
  • Definition to "unique" has nothing to do with file content.

Firstly, i use inode to identify a file, files with different inode are different, a file alway have a fixed inode within it's lifecycle even it's been moved and touched.

BUT, the inode may be reused by the OS. If file A.txt has inode 22345, if i delete A.txt and create B.txt, B.txt MAY have in ode 22345.

What if there's creation-time for files? So that I can use inode+creation-time to identify a file in the file system's history. But linux didn't offer that.

I also try inode+file_md5, but what if A.txt and B.txt have same con ten?

So, do you have any ideas?

=========== edit ===========

My scenario is a kind of log file collecting. In a logging directory, log files maybe created, moved and deleted. We use mapping from file offset to timestamp to do some "check point" like work. So how to defile the "file" mentioned just now?

回答1:

Usually in addition to the inode number one also compares the device number, since two files on two different filesystems may have the same inode number.

Anyways, comparing inode/dev numbers is a way to answer the question "Do these two file descriptors refer to the same file?". Note the usage of "file descriptor" rather than "path" in the question, which avoids the race if the path is subsequently removed after stat()'in it and before comparing the inode/dev numbers. As you point out yourself, inode numbers are guaranteed to be unique only as long as they have active references (path's and/or they are opened by some process).

In your case, I guess one solution would be to keep track of the inode/dev numbers of the files you're interested in, and delete from the list if a file is deleted. Though I'm not sure what you're really trying to accomplish.