help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best way to check file modification time?


From: Joost Kremers
Subject: Best way to check file modification time?
Date: 17 Nov 2015 11:09:52 GMT
User-agent: slrn/1.0.1 (Linux)

Hi all,

I'm dealing with files that I do not visit directly, instead I read
their contents into a temp buffer and parse them. At a later point, I
write the new contents into a temp buffer and save them to the original
file, overwriting the previous version.

I would like to check whether the original file may have been
overwritten by some other process (possibly by Emacs itself if the user
visits the same file in a buffer, but also by external programs).

Right now, I'm essentially doing the following:

```
(setq my-file-modtime (nth 5 (file-attributes my-file)))
(my-parse-function file)
```

where `my-parse-function' reads the contents of FILE into a temp buffer
and does the parsing. IOW, there is a function call between saving the
mod time and reading the file.

Saving is basically the same: the mod time of the file on disk is read
again and checked against the saved mod time, then if that checks out, a
function is called that creates the temp buffer, writes out the
information and saves the file.

Now, it seems to me that there may be too much time between checking the
mod time and reading/saving the file, such that it is in principle
possible that the relevant file gets overwritten at just the wrong moment.

I can easily move the mod time checking to a position in the code that's
closer to the actual reading or saving of the file, but I suspect that
only reduces the problem, it doesn't eliminate it altogether. So I'm
wondering if there's a Right Way™ to do this. If not, any advice on how
to reduce the risks as much as possible would be great!

TIA


-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]