0

Is there a way to obtain the file attributes (specifically, the file key) of an already-open file channel? This operation would be similar to the POSIX fstat function.

If I know a path of the file, I can call Files.readAttributes(…), but this still has a race condition on systems where open files can be deleted or renamed. I do not see a way to avoid this race condition, so something else would be needed.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
  • What's wrong with using `Files::readAttributes` on a path with an open channel? Or do you not know its path? – M. Prokhorov Sep 01 '19 at 10:38
  • The race condition. Some systems allow renaming or replacing open files. As a result, there can also be open file channels which no longer have a path to it. – Florian Weimer Sep 01 '19 at 10:40
  • And how obtaining that key from an open channel (assuming that's possible) will help with that? – M. Prokhorov Sep 01 '19 at 10:41
  • I need the file key (or any other indicator of file identity) for managing advisory file locks, and due to the race, I cannot be sure I got the right file key. – Florian Weimer Sep 01 '19 at 10:45
  • What about first reading a key, store it somewhere near the channel, then opening the channel, then, at a point where you need to check whether it was replaced, read the key again and compare the two? Normally file channel doesn't read attributes, these are deliberately two separate operations. – M. Prokhorov Sep 01 '19 at 11:20

0 Answers0