aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2013-01-19 10:58:40 -0800
committerPaul Eggert2013-01-19 10:58:40 -0800
commit3a955a1f8617ae65709772863a4cb8bb9ec73de7 (patch)
tree7543c597dd872c9f41ac759d8f61dd8d17f182ea /src
parent20de6ab6a5567351df9f28d74052bad04877ef08 (diff)
downloademacs-3a955a1f8617ae65709772863a4cb8bb9ec73de7.tar.gz
emacs-3a955a1f8617ae65709772863a4cb8bb9ec73de7.zip
* 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.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog11
-rw-r--r--src/fileio.c40
2 files changed, 28 insertions, 23 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 57b36782697..c66963738bd 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,16 @@
12013-01-19 Paul Eggert <eggert@cs.ucla.edu> 12013-01-19 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 * fileio.c: Use O_APPEND to append.
4 This corresponds better to the natural interpretation of "append",
5 and avoids the need to open the output file twice, or to invoke
6 lseek when APPEND is neither nil nor a number.
7 This relies on POSIX 1003.1-1988 or later, which is OK nowadays.
8 (Fwrite_region): Simplify. Use O_APPEND instead of opening the
9 file possibly twice, and lseeking to its end; this avoids the
10 need to lseek on non-regular files. Do not use O_EXCL and O_TRUNC
11 at the same time: the combination is never needed and apparently
12 it doesn't work with DOS_NT.
13
3 Fix size bug on DOS_NT introduced by CIFS workaround (Bug#13149). 14 Fix size bug on DOS_NT introduced by CIFS workaround (Bug#13149).
4 * fileio.c (Fwrite_region): Use O_BINARY in checking code, too. 15 * fileio.c (Fwrite_region): Use O_BINARY in checking code, too.
5 16
diff --git a/src/fileio.c b/src/fileio.c
index a2413c8a52f..51f966787b9 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -4741,7 +4741,9 @@ This calls `write-region-annotate-functions' at the start, and
4741 (Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew) 4741 (Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew)
4742{ 4742{
4743 int desc; 4743 int desc;
4744 off_t offset; 4744 int open_flags;
4745 int mode;
4746 off_t offset IF_LINT (= 0);
4745 bool ok; 4747 bool ok;
4746 int save_errno = 0; 4748 int save_errno = 0;
4747 const char *fn; 4749 const char *fn;
@@ -4861,28 +4863,20 @@ This calls `write-region-annotate-functions' at the start, and
4861#endif /* CLASH_DETECTION */ 4863#endif /* CLASH_DETECTION */
4862 4864
4863 encoded_filename = ENCODE_FILE (filename); 4865 encoded_filename = ENCODE_FILE (filename);
4864
4865 fn = SSDATA (encoded_filename); 4866 fn = SSDATA (encoded_filename);
4866 offset = 0; 4867 open_flags = O_WRONLY | O_BINARY | O_CREAT;
4867 desc = -1; 4868 open_flags |= EQ (mustbenew, Qexcl) ? O_EXCL : !NILP (append) ? 0 : O_TRUNC;
4868 if (!NILP (append)) 4869 if (NUMBERP (append))
4869 { 4870 offset = file_offset (append);
4870 if (NUMBERP (append)) 4871 else if (!NILP (append))
4871 offset = file_offset (append); 4872 open_flags |= O_APPEND;
4872 desc = emacs_open (fn, O_WRONLY | O_BINARY, 0);
4873 }
4874
4875 if (desc < 0 && (NILP (append) || errno == ENOENT))
4876#ifdef DOS_NT 4873#ifdef DOS_NT
4877 desc = emacs_open (fn, 4874 mode = S_IREAD | S_IWRITE;
4878 O_WRONLY | O_CREAT | O_BINARY 4875#else
4879 | (EQ (mustbenew, Qexcl) ? O_EXCL : O_TRUNC), 4876 mode = auto_saving ? auto_save_mode_bits : 0666;
4880 S_IREAD | S_IWRITE); 4877#endif
4881#else /* not DOS_NT */ 4878
4882 desc = emacs_open (fn, O_WRONLY | O_TRUNC | O_CREAT 4879 desc = emacs_open (fn, open_flags, mode);
4883 | (EQ (mustbenew, Qexcl) ? O_EXCL : 0),
4884 auto_saving ? auto_save_mode_bits : 0666);
4885#endif /* not DOS_NT */
4886 4880
4887 if (desc < 0) 4881 if (desc < 0)
4888 { 4882 {
@@ -4897,9 +4891,9 @@ This calls `write-region-annotate-functions' at the start, and
4897 4891
4898 record_unwind_protect (close_file_unwind, make_number (desc)); 4892 record_unwind_protect (close_file_unwind, make_number (desc));
4899 4893
4900 if (!NILP (append)) 4894 if (NUMBERP (append))
4901 { 4895 {
4902 off_t ret = lseek (desc, offset, NUMBERP (append) ? SEEK_SET : SEEK_END); 4896 off_t ret = lseek (desc, offset, SEEK_SET);
4903 if (ret < 0) 4897 if (ret < 0)
4904 { 4898 {
4905#ifdef CLASH_DETECTION 4899#ifdef CLASH_DETECTION