Commit 3a955a1f authored by Paul Eggert's avatar Paul Eggert
Browse files

* 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.
parent 20de6ab6
2013-01-19 Paul Eggert <eggert@cs.ucla.edu> 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). Fix size bug on DOS_NT introduced by CIFS workaround (Bug#13149).
* fileio.c (Fwrite_region): Use O_BINARY in checking code, too. * fileio.c (Fwrite_region): Use O_BINARY in checking code, too.
   
......
...@@ -4741,7 +4741,9 @@ This calls `write-region-annotate-functions' at the start, and ...@@ -4741,7 +4741,9 @@ This calls `write-region-annotate-functions' at the start, and
(Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew) (Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew)
{ {
int desc; int desc;
off_t offset; int open_flags;
int mode;
off_t offset IF_LINT (= 0);
bool ok; bool ok;
int save_errno = 0; int save_errno = 0;
const char *fn; const char *fn;
...@@ -4861,28 +4863,20 @@ This calls `write-region-annotate-functions' at the start, and ...@@ -4861,28 +4863,20 @@ This calls `write-region-annotate-functions' at the start, and
#endif /* CLASH_DETECTION */ #endif /* CLASH_DETECTION */
encoded_filename = ENCODE_FILE (filename); encoded_filename = ENCODE_FILE (filename);
fn = SSDATA (encoded_filename); fn = SSDATA (encoded_filename);
offset = 0; open_flags = O_WRONLY | O_BINARY | O_CREAT;
desc = -1; open_flags |= EQ (mustbenew, Qexcl) ? O_EXCL : !NILP (append) ? 0 : O_TRUNC;
if (!NILP (append)) if (NUMBERP (append))
{ offset = file_offset (append);
if (NUMBERP (append)) else if (!NILP (append))
offset = file_offset (append); open_flags |= O_APPEND;
desc = emacs_open (fn, O_WRONLY | O_BINARY, 0);
}
if (desc < 0 && (NILP (append) || errno == ENOENT))
#ifdef DOS_NT #ifdef DOS_NT
desc = emacs_open (fn, mode = S_IREAD | S_IWRITE;
O_WRONLY | O_CREAT | O_BINARY #else
| (EQ (mustbenew, Qexcl) ? O_EXCL : O_TRUNC), mode = auto_saving ? auto_save_mode_bits : 0666;
S_IREAD | S_IWRITE); #endif
#else /* not DOS_NT */
desc = emacs_open (fn, O_WRONLY | O_TRUNC | O_CREAT desc = emacs_open (fn, open_flags, mode);
| (EQ (mustbenew, Qexcl) ? O_EXCL : 0),
auto_saving ? auto_save_mode_bits : 0666);
#endif /* not DOS_NT */
if (desc < 0) if (desc < 0)
{ {
...@@ -4897,9 +4891,9 @@ This calls `write-region-annotate-functions' at the start, and ...@@ -4897,9 +4891,9 @@ This calls `write-region-annotate-functions' at the start, and
record_unwind_protect (close_file_unwind, make_number (desc)); 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) if (ret < 0)
{ {
#ifdef CLASH_DETECTION #ifdef CLASH_DETECTION
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment