December 24

I was trying to use a library (fswatch) for watching the file system but I immediately ran into an error using it so I decided to write my own.

Funny thing: I ran into the same error.

It seems that there's some problem with files with a name like #tests.txt# (which emacs likes to create).

My code is really basic, when the directory modified timestamp changes, I list the directory entries with dirEntries and then call dirEntry.getTimes() on it.


import std.file;
import std.stdio;
import std.datetime.systime;
import std.stdio: writefln;


void main(string[] args) {
  assert(args.length == 2);
  foreach (DirEntry entry; args[1].dirEntries(SpanMode.shallow)) {
    SysTime st, ac;
    entry.getTimes(ac, st);
    writefln("entry %s changed at %s", entry.name, st);
  }
}

Create a directory like this:

hello
├── #hi.txt#
└── hi.txt

Running this, I get:

▶ ldc2 -L-ld_classic -run main.d hello
entry hello/hi.txt changed at 2023-Dec-24 14:36:01.1020473
entry hello/#hi.txt# changed at 2023-Dec-24 14:40:26.7945088
std.file.FileException@/Users/renato/dlang/ldc-1.35.0/bin/../import/std/file.d(1282): hello/.#hi.txt: No such file or directory
----------------
??:? object.Throwable.TraceInfo core.runtime.defaultTraceHandler(void*) [0x107210e52]
Error: /var/folders/m_/mxbg6vcj25qdx5nkbsllb4kw0000gn/T/main-7fe78b failed with status: 1
(ldc-1.35.0)

As you can see, the name of the file in the exception seems wrong. It should be #hi.txt#, but it tries to access .#hi.txt.

I actually reproduced this in both Mac and Linux... on Linux, though, it shows a "funny" directory listing which perhaps explains something:

l
rwxrwxrwx 1 renato renato   31 Dec 24 15:13 .#hi.txt -> renato@renato.902129:1701726658

The .#hi.txt -> (seems like a link?) is shown in red on the terminal... I don't understand what exactly this is (links are not shown red).

Can anyone help me understand this? Should I ignore file names starting with .# perhas?

December 24

On Sunday, 24 December 2023 at 14:19:18 UTC, Renato wrote:

>

I was trying to use a library (fswatch) for watching the file system

Watching for what?

>

[...]
My code is really basic, when the directory modified timestamp changes, I list the directory entries with dirEntries and then call dirEntry.getTimes() on it.

The right way to implement a function which watches for changes IN the directory is to use an appropriate notification API, e.g. inotify [1]. No one wants software running on their computer which runs in a loop and polls the file system endlessly though there is no actual work to do.

>
[...]
    entry.getTimes(ac, st);

You know what symlinks [2] are? You want the timestamps of the symlink and not that of the pointed-to file. In C you would use lstat instead of stat for that purpose.

Sometimes symlinks are used to link to objects which are not located in the filesystem [3]. Trying to use getTimes on such object throws an exception.

[1] https://man7.org/linux/man-pages/man7/inotify.7.html
[2] https://man7.org/linux/man-pages/man7/symlink.7.html
[3] e.g. ls -laF /proc/self/ns in Linux.