0

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?

qiuxiafei
  • 5,827
  • 5
  • 30
  • 43
  • 1
    If having the same contents doesn't make them identical files, what does? I can imagine answers to that question, but I think you need to clarify. – Jamey Sharp Nov 01 '12 at 07:14
  • if A.txt is hard link to B.txt you interpret them as different files ? – zb' Nov 01 '12 at 07:26
  • if file A.txt was updated (write-append) it is new file in your definition ? – zb' Nov 01 '12 at 07:27
  • I assume if file have same inode same content same name and same mtime it is uniq file in your definitions – zb' Nov 01 '12 at 07:28
  • Thanks for the clarification. I guess I should have asked, why do you need to use this particular definition of uniqueness? The question is tagged inotify; are you concerned about race conditions involving stale inotify events? – Jamey Sharp Nov 01 '12 at 07:29
  • if you use initify, just use close_write and create events to track all new/modifed files; – zb' Nov 01 '12 at 07:36
  • @eicto any modification to hard link will affect original file, so let's do NOT treat hard link as new file. file update will not create new file, so it's not another file. – qiuxiafei Nov 01 '12 at 07:36
  • @qiuxiafei so content matter ? because if not, same content on same inode === same file – zb' Nov 01 '12 at 07:37
  • @eicto what about same inode, same content, different creation time? – qiuxiafei Nov 01 '12 at 07:41
  • ok and same time, let's assume same stat() and same md5, also if you talk about creation time, why different name not mean different file ? – zb' Nov 01 '12 at 07:43
  • @JameySharp 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? – qiuxiafei Nov 01 '12 at 07:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18905/discussion-between-qiuxiafei-and-eicto) – qiuxiafei Nov 01 '12 at 07:50
  • https://en.wikipedia.org/wiki/Ship_of_Theseus :) – Lassi May 29 '19 at 07:12

1 Answers1

4

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.

janneb
  • 36,249
  • 2
  • 81
  • 97