0

I'm trying to distinguish files on the filesystem with the following requirements(assuming all files are within the same filesystem):

  1. If file A was renamed to file B it should be appeared to be the same
  2. If file A was removed and then created it should appear to be different one

Since I assumed that all files are within the same file system I tended to use inode_number for that. But it does not satisfy the point 2.. Actually:

root@spc:~/test# touch test
root@spc:~/test# ls -li
total 0
2098203 -rw-r--r-- 1 root root 0 Jan 15 14:55 test
root@spc:~/test# rm test
root@spc:~/test# touch test
root@spc:~/test# ls -li
total 0
2098203 -rw-r--r-- 1 root root 0 Jan 15 14:55 test

So the two different files appeared to have the same inode number. Is there a way to do this for linux/ext4? Maybe we can use some file system specific api to store some meta uid?

2
  • 1
    The (st_dev, st_ino, st_gen) tuple would suffice, but unfortunately st_gen (inode generation number) is not exported by the kernel. It was considered for the statx() call, but it seems it didn't make into it. Jan 15, 2019 at 13:53
  • 1
    That said, (st_dev, st_ino, st_btime) as provided by statx() might work. You'll need to use syscall() from <sys/syscall.h>, and copy the struct statx from kernel include/linux/uapi/stat.h:struct statx. Jan 15, 2019 at 13:57

2 Answers 2

1

You can set extended filesystem attributes (where supported, i.e. with ext4 when mounted with option user_xattr):

#include <sys/types.h>
#include <attr/xattr.h>

int main (void) {
    setxattr("test", "user.fileguid", "someuniqueguid", 15, 0);
}

and read it back with

char uuid[100];
int len = getxattr("test", "user.fileguid", uuid, sizeof(uuid), 0);

If this attribute should only be modified by a privileged process, you can use the security namespace instead of user (i.e. attribute-name security.fileguid). In this case, the file owner itself cannot modify the attributes himself. If you do not use the user namespace, the mount option user_xattr isn't even necessary.

However, these attributes are not set immediately after creation of the file. But it should be sufficient to set a random guid at the first time when accessing the file with your tool (i.e. when no guid is set yet).

These file attributes are saved with the inode, so renaming the file or (hard-)linking it will retain the attributes. However, deleting and creating it newly will not.

1

Alas it is not possible because:

  1. for all data/metadata (including extended attributes, etc) in control of the user, it is possible to set their values/content to the exact same value as the "original" file.

  2. for inode number, it is dependent on the filesystem support in the kernel and many of them (FS) recycle inode numbers.

2
  • Can you give some examples of metadata users can set?
    – Some Name
    Jan 15, 2019 at 12:10
  • @SomeName timestamps, file length (indirectly), ids, link count, etc Jan 15, 2019 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.