unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Eli Zaretskii <eliz@gnu.org>
Cc: sds@gnu.org, spinuvit@gmail.com, emacs-devel@gnu.org
Subject: Re: feature request: view part of file
Date: Sat, 19 Jan 2013 04:47:58 -0800	[thread overview]
Message-ID: <50FA95FE.2090105@cs.ucla.edu> (raw)
In-Reply-To: <838v7pqwcs.fsf@gnu.org>

On 01/19/2013 02:51 AM, Eli Zaretskii wrote:
> That patch calls lseek inside write-region on non-regular files as
> well, where previously we didn't.  Why is that a good idea?  (It's an
> unrelated change anyway.) 

Yes, it's unrelated; sorry, it was related in an earlier version
of the patch, but I forgot to undo that part.

It's a good idea for a couple of reasons.  First, the old code
mishandled things if the file was renamed by some other process
between the time that Emacs opened it and the time that
file-regular-p invoked stat on it.  Second, the typical GNU style
is that the behavior of a program should not depend on the type of
output device.

I looked at the old change you mentioned, and it appears that
there was a problem on DOS_NT with invoking lseek (fd, 0, SEEK_END) when
FD is not a regular file.  Here's a patch that should avoid this problem:

=== modified file 'src/ChangeLog'
--- src/ChangeLog	2013-01-19 12:29:10 +0000
+++ src/ChangeLog	2013-01-19 12:37:41 +0000
@@ -1,5 +1,16 @@
 2013-01-19  Paul Eggert  <eggert@cs.ucla.edu>
 
+	* fileio.c: Use O_APPEND to append.
+	This corresponds better to the natural interpretation of "append",
+	and avoids the need to open the output file twice, or to invoke
+	lseek when APPEND is neither nil nor a number.
+	This relies on POSIX 1003.1-1988 or later, which is OK nowadays.
+	(Fwrite_region): Simplify.  Use O_APPEND instead of opening the
+	file possibly twice, and lseeking to its end; this avoids the
+	need to lseek on non-regular files.  Do not use O_EXCL and O_TRUNC
+	at the same time: the combination is never needed and apparently
+	it doesn't work with DOS_NT.
+
 	Fix size bug on DOS_NT introduced by CIFS workaround (Bug#13149).
 	* fileio.c (Fwrite_region): Use O_BINARY in checking code, too.
 

=== modified file 'src/fileio.c'
--- src/fileio.c	2013-01-19 12:29:10 +0000
+++ src/fileio.c	2013-01-19 12:37:41 +0000
@@ -4741,7 +4741,9 @@
   (Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew)
 {
   int desc;
-  off_t offset;
+  int open_flags;
+  int mode;
+  off_t offset IF_LINT (= 0);
   bool ok;
   int save_errno = 0;
   const char *fn;
@@ -4863,26 +4865,19 @@
   encoded_filename = ENCODE_FILE (filename);
 
   fn = SSDATA (encoded_filename);
-  offset = 0;
-  desc = -1;
-  if (!NILP (append))
-    {
-      if (NUMBERP (append))
-	offset = file_offset (append);
-      desc = emacs_open (fn, O_WRONLY | O_BINARY, 0);
-    }
-
-  if (desc < 0 && (NILP (append) || errno == ENOENT))
+  open_flags = O_WRONLY | O_BINARY | O_CREAT;
+  open_flags |= EQ (mustbenew, Qexcl) ? O_EXCL : O_TRUNC;
+  if (NUMBERP (append))
+    offset = file_offset (append);
+  else if (!NILP (append))
+    open_flags |= O_APPEND;
 #ifdef DOS_NT
-  desc = emacs_open (fn,
-		     O_WRONLY | O_CREAT | O_BINARY
-		     | (EQ (mustbenew, Qexcl) ? O_EXCL : O_TRUNC),
-		     S_IREAD | S_IWRITE);
-#else  /* not DOS_NT */
-  desc = emacs_open (fn, O_WRONLY | O_TRUNC | O_CREAT
-		     | (EQ (mustbenew, Qexcl) ? O_EXCL : 0),
-		     auto_saving ? auto_save_mode_bits : 0666);
-#endif /* not DOS_NT */
+  mode = S_IREAD | S_IWRITE;
+#else
+  mode = auto_saving ? auto_save_mode_bits : 0666;
+#endif
+
+  desc = emacs_open (fn, open_flags, mode);
 
   if (desc < 0)
     {
@@ -4897,9 +4892,9 @@
 
   record_unwind_protect (close_file_unwind, make_number (desc));
 
-  if (!NILP (append))
+  if (NUMBERP (append))
     {
-      off_t ret = lseek (desc, offset, NUMBERP (append) ? SEEK_SET : SEEK_END);
+      off_t ret = lseek (desc, offset, SEEK_SET);
       if (ret < 0)
 	{
 #ifdef CLASH_DETECTION





  reply	other threads:[~2013-01-19 12:47 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-13 20:52 feature request: view part of file Sam Steingold
2012-06-13 23:16 ` Stefan Monnier
2012-06-14 16:20   ` Sam Steingold
2012-06-14 16:53     ` Mathias Dahl
2012-06-14 17:32     ` Stephen J. Turnbull
2012-06-14 18:21       ` Paul Eggert
     [not found]     ` <jwvd3517qww.fsf-monnier+emacs@gnu.org>
     [not found]       ` <CABrcCQ5zDfB2tw9DRrwpCZmDqHPc+BB6W5w9ULNU95e_v4yyJw@mail.gmail.com>
     [not found]         ` <87395xu768.fsf@gnu.org>
     [not found]           ` <CABrcCQ6rpG9qhsCO+ZEpTiNqbQRtg-PBeb=q_B5F8YgrGxoWKA@mail.gmail.com>
     [not found]             ` <jwvvcit67sc.fsf-monnier+emacs@gnu.org>
2012-06-14 19:34               ` Sam Steingold
2012-06-14 21:29 ` Sam Steingold
2012-06-18 20:34   ` Štěpán Němec
2012-07-19 17:58     ` Samuel Bronson
2012-07-19 19:38       ` Stephen J. Turnbull
2012-08-04 11:58   ` Andrey Kotlarski
2013-01-18 23:30   ` Vitalie Spinu
2013-01-18 23:52     ` Vitalie Spinu
2013-01-19  6:54       ` Eli Zaretskii
2013-01-19 10:18         ` Paul Eggert
2013-01-19 10:51           ` Eli Zaretskii
2013-01-19 12:47             ` Paul Eggert [this message]
2013-01-19 13:47               ` Eli Zaretskii
2013-01-19 19:00                 ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50FA95FE.2090105@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=sds@gnu.org \
    --cc=spinuvit@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).