From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.bugs Subject: bug#13149: 24.3.50; Emacs thinks file was changed outside Emacs, but it was not Date: Thu, 17 Jan 2013 13:14:37 -0800 Message-ID: <50F869BD.1040302@cs.ucla.edu> References: <6CDE13E3BCAA4AFAAB8BCE105C6ABF12@us.oracle.com> <874njs19zb.fsf@yandex.ru> <50F3935A.2090003@yandex.ru> <50F41CE7.60306@gmail.com> <50F44E6B.8090007@cs.ucla.edu> <50F484CB.6010905@gmail.com> <50F4FB0B.5070003@cs.ucla.edu> <50F5192B.602@yandex.ru> <50F5928A.9010009@cs.ucla.edu> <50F5CC3D.5090802@yandex.ru> <50F5CE65.9030002@cs.ucla.edu> <50F5D3F5.6050604@yandex.ru> <50F5DA58.3020404@cs.ucla.edu> <50F5E1C1.2040301@yandex.ru> <50F5E9DB.1030309@gmail.com> <50F64149.6010704@cs.ucla.edu> <50F7D358.9030100@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1358457327 18828 80.91.229.3 (17 Jan 2013 21:15:27 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 17 Jan 2013 21:15:27 +0000 (UTC) Cc: 13149@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Jan 17 22:15:45 2013 Return-path: Envelope-to: geb-bug-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 1TvwoH-0006Cv-CR for geb-bug-gnu-emacs@m.gmane.org; Thu, 17 Jan 2013 22:15:41 +0100 Original-Received: from localhost ([::1]:49697 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tvwo0-0003gV-E5 for geb-bug-gnu-emacs@m.gmane.org; Thu, 17 Jan 2013 16:15:24 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:46168) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tvwnx-0003g7-56 for bug-gnu-emacs@gnu.org; Thu, 17 Jan 2013 16:15:22 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tvwnv-0003uk-Vd for bug-gnu-emacs@gnu.org; Thu, 17 Jan 2013 16:15:21 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:60185) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tvwnv-0003uc-Rz for bug-gnu-emacs@gnu.org; Thu, 17 Jan 2013 16:15:19 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1Tvwoc-0003Xr-D6 for bug-gnu-emacs@gnu.org; Thu, 17 Jan 2013 16:16:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Paul Eggert Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 17 Jan 2013 21:16:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 13149 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: unreproducible moreinfo Original-Received: via spool by 13149-submit@debbugs.gnu.org id=B13149.135845732813584 (code B ref 13149); Thu, 17 Jan 2013 21:16:02 +0000 Original-Received: (at 13149) by debbugs.gnu.org; 17 Jan 2013 21:15:28 +0000 Original-Received: from localhost ([127.0.0.1]:37416 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1Tvwo3-0003X3-KF for submit@debbugs.gnu.org; Thu, 17 Jan 2013 16:15:28 -0500 Original-Received: from smtp.cs.ucla.edu ([131.179.128.62]:42459) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1Tvwnz-0003Ws-HL for 13149@debbugs.gnu.org; Thu, 17 Jan 2013 16:15:25 -0500 Original-Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp.cs.ucla.edu (Postfix) with ESMTP id C880AA60003; Thu, 17 Jan 2013 13:14:39 -0800 (PST) X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu Original-Received: from smtp.cs.ucla.edu ([127.0.0.1]) by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id u-o8U+EUE36h; Thu, 17 Jan 2013 13:14:38 -0800 (PST) Original-Received: from penguin.cs.ucla.edu (Penguin.CS.UCLA.EDU [131.179.64.200]) by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 064C239E810E; Thu, 17 Jan 2013 13:14:38 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: <50F7D358.9030100@gmail.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:69944 Archived-At: On 01/17/13 02:32, Dmitry Gutov wrote: > I think we need this to work without requiring the user to customize a variable. I agree. But I would rather avoid having Emacs assume that file system time stamps can be off by several seconds, since that'll cause Emacs to miss changes that it should report. Does the following patch help? It should be applied anyway, since it closes a minor race even on non-buggy file systems. I'm hoping that it helps to work around your bug by using fstat both times, instead of fstat in one place and stat in another, since the file system bug seems to be that fstat and stat disagree. === modified file 'src/ChangeLog' --- src/ChangeLog 2013-01-17 06:29:40 +0000 +++ src/ChangeLog 2013-01-17 21:09:26 +0000 @@ -1,3 +1,11 @@ +2013-01-17 Paul Eggert + + Close a race when statting and reading files (Bug#13149). + * fileio.c (Finsert_file_contents): Use open+fstat, not stat+open. + This avoids a race if the file is renamed between stat and open. + Also, perhaps it works around a file system bug that happen in + virtualized environments based on MS-Windows hosts. + 2013-01-17 Dmitry Antipov * lisp.h (toplevel): Add comment about using Lisp_Save_Value === modified file 'src/fileio.c' --- src/fileio.c 2013-01-17 06:29:40 +0000 +++ src/fileio.c 2013-01-17 21:09:26 +0000 @@ -3492,7 +3492,6 @@ (Lisp_Object filename, Lisp_Object visit, Lisp_Object beg, Lisp_Object end, Lisp_Object replace) { struct stat st; - int file_status; EMACS_TIME mtime; int fd; ptrdiff_t inserted = 0; @@ -3554,26 +3553,9 @@ orig_filename = filename; filename = ENCODE_FILE (filename); - fd = -1; - -#ifdef WINDOWSNT - { - Lisp_Object tem = Vw32_get_true_file_attributes; - - /* Tell stat to use expensive method to get accurate info. */ - Vw32_get_true_file_attributes = Qt; - file_status = stat (SSDATA (filename), &st); - Vw32_get_true_file_attributes = tem; - } -#else - file_status = stat (SSDATA (filename), &st); -#endif /* WINDOWSNT */ - - if (file_status == 0) - mtime = get_stat_mtime (&st); - else + fd = emacs_open (SSDATA (filename), O_RDONLY, 0); + if (fd < 0) { - badopen: save_errno = errno; if (NILP (visit)) report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); @@ -3585,6 +3567,17 @@ goto notfound; } + /* Replacement should preserve point as it preserves markers. */ + if (!NILP (replace)) + record_unwind_protect (restore_point_unwind, Fpoint_marker ()); + + record_unwind_protect (close_file_unwind, make_number (fd)); + + if (fstat (fd, &st) != 0) + report_file_error ("Getting input file status", + Fcons (orig_filename, Qnil)); + mtime = get_stat_mtime (&st); + /* This code will need to be changed in order to work on named pipes, and it's probably just not worth it. So we should at least signal an error. */ @@ -3600,17 +3593,6 @@ build_string ("not a regular file"), orig_filename); } - if (fd < 0) - if ((fd = emacs_open (SSDATA (filename), O_RDONLY, 0)) < 0) - goto badopen; - - /* Replacement should preserve point as it preserves markers. */ - if (!NILP (replace)) - record_unwind_protect (restore_point_unwind, Fpoint_marker ()); - - record_unwind_protect (close_file_unwind, make_number (fd)); - - if (!NILP (visit)) { if (!NILP (beg) || !NILP (end))