1

file mtime can be used to set the modifcation time of a file. But if it's a symlink, it will set the mtime of the target. How do I set the mtime of the symlink itself?

Lars Brinkhoff
  • 13,542
  • 2
  • 28
  • 48

1 Answers1

2

By far the easiest approach will be to run an external command:

proc SetMtime {filename timestamp} {
    # A little bit of type enforcement; it's not necessary, but avoids potential trouble
    exec touch -h -t [expr {int($timestamp)}] [file normalize $filename]
}

This is because Tcl does not provide any native access to the utimensat(2) system call (or its wrapper, lutimes(3)). You could make your own access functions in a Tcl extension (either directly, or using Critcl or SWIG) but for just setting a single link occasionally, calling out to touch with the -h option is easiest.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • My reason for looking into this is in part because `-h` is not available in all implementations of `touch`. – Lars Brinkhoff Sep 22 '20 at 10:53
  • Yeah, but on operating systems where it is absent, it's probably because the underlying OS doesn't support the feature either. At that point, there's not really any sort of workaround. – Donal Fellows Sep 22 '20 at 13:22