FileException when calling getTimes on a DirEntry

Renato renato at athaydes.com
Sun Dec 24 14:19:18 UTC 2023


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.

```d

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 at 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?


More information about the Digitalmars-d-learn mailing list