6

The problem I face is simple. I want to keep track of the file/folder even after it has been renamed / deleted etc? Does NodeJS provide a way to access this information a file? I've tried the default file system module fs.stats() : https://nodejs.org/api/fs.html#fs_class_fs_stats . Unfortunately, it does not seem to provide such unique ID referencing for a particular file.

Does there exist such a solution in NodeJS?

Note: I DO NOT want to generate a Unique ID for a file. It's pretty easy to assign a random string to a file and associate the string with this this. But it's the other way around. I want to associate a file with a system wide string of some sort.

Any help is appreciated.

  • _"The problem I face is simple"_. Well, only in describing it. First, it's both OS and filesystem-dependent if such an identifier exists. Secondly, it sounds like you also want to be able to access the file/folder using that unique identifier, which I think is not going to a possibility for most OS'es. Perhaps using a file-system watcher like [`chokidar`](https://github.com/paulmillr/chokidar) is what you're _really_ looking for? – robertklep Apr 02 '17 at 16:41
  • I strictly don't mean access. I can write a simple script to map each file id to its path. I can thereafter use this as my guiding principle. The underlying problem however is getting this file id. –  Apr 02 '17 at 16:49
  • Okay, in that case, the answer presented should be sufficient for you :) – robertklep Apr 02 '17 at 16:50
  • What is the actual problem you're trying to solve? You've described a solution you are attempting, but not told us what the actual problem is that you're triyng to solve thus depriving yourself of other possible solution ideas and informing us about what the real problem is here so we can think more broadly about a solution (this is all-to-common here on stackoverflow). People describe a problem with their solution rather than describe the actual original problem. – jfriend00 Apr 02 '17 at 17:07
  • @jfriend00 The problem with that being that mods link to arbitrary questions which share common keywords/ concerns. Its quite frustrating to see mods readily mark a question as duplicate, link up a few arbitrary questions and move on. The problem is simple when broadly put. How do I get a unique "ID" for a file on NodeJS? I'm sure the OS keeps track of its files with some ID such sort. I'm using the `fs` module as a quick solution. If there is a better alternative, then please let us know :) –  Apr 02 '17 at 17:24
  • So, did you accept an answer that only works sometimes on some platforms? And the answer doesn't even show how you find the file by inode if it's been renamed to another place in the directory hierarchy. Is that really a general solution to your problem? – jfriend00 Apr 02 '17 at 17:28
  • And, the fact that questions occasionally get marked as dups is no reason to avoid describing the actual problem you are trying to solve. That just deprives you of the opportunity to get answers that you haven't even thought of to your actual problem, particularly when the path you're going down is fraught with complications and exceptions. – jfriend00 Apr 02 '17 at 17:30
  • @jfriend00 Agreed. I'm yet to accept an answer for this. The below solution is a UNIX dependent. For reasons mentioned below, it doesnt work on Windows. Looking forward to more solutions. –  Apr 02 '17 at 17:32
  • Are you just trying to see if one particular file you are already looking at happens to be the same file that you previously were looking at even if the filename or location has been changed? Or are you trying to save some invariant reference to the file and then at some point in the future, go find where that file is again (even if it's been renamed to a new location)? These are somewhat different problems. Do the contents of the file change? – jfriend00 Apr 02 '17 at 17:35
  • "*Are you just trying to see if one particular file you are already looking at happens to be the same file ...?*". Yes. "*Or are you trying to save some invariant reference to the file and then at some point in the future, go find whe...*" Yes. "*Do the contents of the file change?*" Maybe. I believe all three are mutually exclusively and a good way to deal with this problem is to implement all three. I'm using your point 2 to solve point 1. But, If point 3 is implies point 1, then does point 2 work? –  Apr 02 '17 at 17:40
  • Just knowing whether a file you have now happens to be the same file as you previously saved a reference to is a less involved problem than trying to find that previous reference from scratch when it may have been moved. I'm trying to figure out what your actual requirement is. And, if all file contents can be changed, then there is no opportunity to compare a hash of certain portions of the file. – jfriend00 Apr 02 '17 at 18:31

1 Answers1

8

Looking at the link https://nodejs.org/api/fs.html#fs_class_fs_stats

Stats {
  dev: 2114,
  ino: 48064969,
  mode: 33188,
  nlink: 1,
  uid: 85,
  gid: 100,
  rdev: 0,
  size: 527,
  blksize: 4096,
  blocks: 8,
  atime: Mon, 10 Oct 2011 23:24:11 GMT,
  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  birthtime: Mon, 10 Oct 2011 23:24:11 GMT }

I can see unix inode number.

Can two files have the same inode number?

Two files can have the same inode, but only if they are part of different partitions. Inodes are only unique on a partition level, not on the whole system.

Thus, in addition to the inode number one also compares the device number.

var uniqueFileId[fileName] = (Stats.dev + Stats.ino)
Ratan Kumar
  • 1,640
  • 3
  • 25
  • 52
  • Interesting. I lack a technical understanding of this. I will take it for a fact that what you said is true. Let me run a few quick tests and see if this is the case. –  Apr 02 '17 at 16:51
  • 1
    Is this a cross platform solution? What I mean is, does fs.stats work on all platforms? –  Apr 02 '17 at 16:54
  • @SaiKrishnaDeep I'm pretty sure this won't work (properly) on Windows (`fs.stat()` will, but the `ino` number probably not). – robertklep Apr 02 '17 at 16:55
  • 1
    @robertklep How so? 2 files on the same drive represent files on the same partition right. Im guessing inode nuber is something specific to UNIX? But, for some reason `fs.stats` gives an Inode number to the file/folder on Windows as well. Is this an expected behavior? –  Apr 02 '17 at 16:58
  • 2
    @SaiKrishnaDeep the problem is that "inodes" on Windows are 64-bit numbers, which cannot be represented properly in JavaScript numbers. This can cause different files to appear to have the same `ino` value. See [this remark about Node v7](https://github.com/nodejs/node-v0.x-archive/issues/2670#issuecomment-265795379). Also [this issue](https://github.com/tivac/modular-css/issues/242). – robertklep Apr 02 '17 at 17:00
  • There are two things here. The term INode, and a file-system implementation that uses either INode terminology or something like INode in its place. All Windows file-systems(FAT*,NTFS) I know of, use Inode-like structures in actual implementation. To further simplify the answer (Think of INode as a block of metadata about a file.) INode as term : No windows file system dont have it. INode as concept : Windows will have some other structures, similar in property and usage but used with different name – Ratan Kumar Apr 02 '17 at 17:01
  • 1
    @RatanKumar the main difference is the for most Unix-like OS'es, inode numbers are 32-bit, whereas on Windows (and also the upcoming new macOS file system, APFS), they are 64-bit. Those cannot be represented in JS as numbers. It'll appear to work, until you hit files/folders whose identifier gets truncated to `Number.MAX_SAFE_INTEGER`. – robertklep Apr 02 '17 at 17:04
  • @RatanKumar The suggestion you made seems to be legit: https://github.com/nodejs/node-v0.x-archive/issues/2670#issuecomment-102507354 . However, @robertklep finding is unforunately true as shown here: https://github.com/nodejs/node-v0.x-archive/issues/2670#issuecomment-265795379 Where 2 files show the same `ino` & `dev`. I think this is the best way by which NodeJS can identify a file as of now or is there anything better? –  Apr 02 '17 at 17:09
  • 1
    I think including another string like `ctime`,`birthime`,`atime` can be a small workaround hack. –  Apr 02 '17 at 17:14
  • Unfortunately, with Windows, the `ino` value that node gives, changes when a file is saved. – CascadiaJS Jun 17 '21 at 22:39