diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ChangeLog | 11 | ||||
| -rw-r--r-- | src/fileio.c | 40 |
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 @@ | |||
| 1 | 2013-01-19 Paul Eggert <eggert@cs.ucla.edu> | 1 | 2013-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 |