0

I want to handle files in a single folder according to their modification time. But sometimes I get undefined behavior when the underlying file system doesn't support nanoseconds - so the files created in interval of 1 millisecond have the same modification time almost always. The rare exception occurs, if this 1 millisecond passes the 1 second boundary.

Is there a way to detect the support for nanoseconds in C?

abyss.7
  • 13,882
  • 11
  • 56
  • 100
  • You can try to set the mtime of a temporary file to something that will have a different value if there is no nanosecond precision. Then read back the mtime and compare it to your actual value. – ThiefMaster Mar 12 '14 at 07:29
  • @ThiefMaster as I described - it's a probabilistic method, since the file modification interval may pass the 1 second boundary - and give false-positive. I want an accurate method. – abyss.7 Mar 12 '14 at 07:31
  • 1
    How is this probabilistic? It is a temporary file created by you that is only accessible by you. So you can set whatever mtime you want. – ThiefMaster Mar 12 '14 at 07:32
  • @abyss You can set the metime like this http://stackoverflow.com/questions/2185338/how-to-set-the-modification-time-of-a-file-programmatically – Klas Lindbäck Mar 12 '14 at 07:32
  • @ThiefMaster sorry, misunderstood your point - I tried your method. And it seems the problem is somewhere else - my mtime is correct on both machines, while the mtime after open(O_CREAT|O_TRUNC) is not precise enough. I'm confused. – abyss.7 Mar 12 '14 at 07:48
  • 2
    I would assume that by default file systems don't support nanosecond precision. – Basile Starynkevitch Mar 12 '14 at 08:00
  • If order is important then you cannot rely on timestamp alone if you want portability. – Klas Lindbäck Mar 12 '14 at 08:16
  • @KlasLindbäck even timestamp in seconds is good enough for me, but the problem is in testing my code - since I rely on a natural mtime after creating the files in my tests. It's my final goal, but the question stays on its own - I hope there is some cool system call. – abyss.7 Mar 12 '14 at 08:20
  • If you don't find a better way, you can always set mtime programmatically as a workaround. – Klas Lindbäck Mar 12 '14 at 08:25

0 Answers0