www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - FileException when calling getTimes on a DirEntry

reply Renato <renato athaydes.com> writes:
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

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
std.file.FileException /Users/renato/dlang/ldc-1.35.0/bin/../impo
t/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

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 

Dec 24 2023
parent kdevel <kdevel vogtner.de> writes:
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.
 ```d
 [...]
     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.
Dec 24 2023