From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help Subject: Re: Best way to check file modification time? Date: Tue, 17 Nov 2015 11:04:35 -0500 Organization: A noiseless patient Spider Message-ID: References: NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1447776347 20116 80.91.229.3 (17 Nov 2015 16:05:47 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 17 Nov 2015 16:05:47 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Nov 17 17:05:47 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zyikv-0008TS-Q0 for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Nov 2015 17:05:17 +0100 Original-Received: from localhost ([::1]:59254 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zyikv-0006jf-6c for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Nov 2015 11:05:17 -0500 Original-Path: usenet.stanford.edu!news.kjsl.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!barmar.motzarella.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 55 Injection-Info: barmar.motzarella.org; posting-host="2be9e9f5dd9af768b8861af71b85fc28"; logging-data="785"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199r82KfshqJ3TY3PZ3PbwJ" User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Cancel-Lock: sha1:pUJSCO2HApPbUbUEEryl3xgaPTU= Original-Xref: usenet.stanford.edu gnu.emacs.help:215828 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:108114 Archived-At: In article , Joost Kremers wrote: > 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! The same window exists when visiting files normally. There's no way to get the mod time and read a file atomically. Instead of saving the file's modification time, remember the time that you started reading the file. Then when you're about to save it, see if the mod time is newer than that. This can potentially run into a problem if the file is on a server and there's clock skew between the client and server, but that's a common problem. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me ***