diff options
author | Po Lu <luangruo@yahoo.com> | 2023-08-08 16:02:49 +0800 |
---|---|---|
committer | Po Lu <luangruo@yahoo.com> | 2023-08-08 16:02:49 +0800 |
commit | 27113c22f77b7a409c33b956a1a8d8be2d5bc673 (patch) | |
tree | 663e42ad962f3383873213e6923e842da47e3d7f /src/fileio.c | |
parent | 27a57f4cca55fd64a61eb8952b6422712f27b0af (diff) | |
download | emacs-27113c22f77b7a409c33b956a1a8d8be2d5bc673.tar.gz |
Minor improvements to write-region heuristic
* src/androidvfs.c (android_saf_stat): Set STATB->st_dev.
(android_fstat): Likewise.
(NATIVE_NAME): Seek to start of file after truncation.
* src/fileio.c (write_region): Use stat instead of open+fstat
to obtain updated mtime.
Diffstat (limited to 'src/fileio.c')
-rw-r--r-- | src/fileio.c | 68 |
1 files changed, 35 insertions, 33 deletions
diff --git a/src/fileio.c b/src/fileio.c index 26b7e193f0a..5d01e10f0ef 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -5582,42 +5582,44 @@ write_region (Lisp_Object start, Lisp_Object end, Lisp_Object filename, if (timespec_valid_p (modtime) && ! (valid_timestamp_file_system && st.st_dev == timestamp_file_system)) { - int desc1 = emacs_open (fn, O_WRONLY, 0); - if (desc1 >= 0) + struct stat st1; + + /* The code below previously tried to open FN O_WRONLY, + subsequently calling fstat on the opened file descriptor. + This proved inefficient and resulted in FN being truncated + under several Android filesystems, and as such has been + changed to a call to `stat'. */ + + if (emacs_fstatat (AT_FDCWD, fn, &st1, 0) == 0 + && st.st_dev == st1.st_dev && st.st_ino == st1.st_ino) { - struct stat st1; - if (sys_fstat (desc1, &st1) == 0 - && st.st_dev == st1.st_dev && st.st_ino == st1.st_ino) + /* Use the heuristic if it appears to be valid. With neither + O_EXCL nor O_TRUNC, if Emacs happened to write nothing to the + file, the time stamp won't change. Also, some non-POSIX + systems don't update an empty file's time stamp when + truncating it. Finally, file systems with 100 ns or worse + resolution sometimes seem to have bugs: on a system with ns + resolution, checking ns % 100 incorrectly avoids the heuristic + 1% of the time, but the problem should be temporary as we will + try again on the next time stamp. */ + bool use_heuristic + = ((open_flags & (O_EXCL | O_TRUNC)) != 0 + && st.st_size != 0 + && modtime.tv_nsec % 100 != 0); + + struct timespec modtime1 = get_stat_mtime (&st1); + if (use_heuristic + && timespec_cmp (modtime, modtime1) == 0 + && st.st_size == st1.st_size) { - /* Use the heuristic if it appears to be valid. With neither - O_EXCL nor O_TRUNC, if Emacs happened to write nothing to the - file, the time stamp won't change. Also, some non-POSIX - systems don't update an empty file's time stamp when - truncating it. Finally, file systems with 100 ns or worse - resolution sometimes seem to have bugs: on a system with ns - resolution, checking ns % 100 incorrectly avoids the heuristic - 1% of the time, but the problem should be temporary as we will - try again on the next time stamp. */ - bool use_heuristic - = ((open_flags & (O_EXCL | O_TRUNC)) != 0 - && st.st_size != 0 - && modtime.tv_nsec % 100 != 0); - - struct timespec modtime1 = get_stat_mtime (&st1); - if (use_heuristic - && timespec_cmp (modtime, modtime1) == 0 - && st.st_size == st1.st_size) - { - timestamp_file_system = st.st_dev; - valid_timestamp_file_system = 1; - } - else - { - st.st_size = st1.st_size; - modtime = modtime1; - } + timestamp_file_system = st.st_dev; + valid_timestamp_file_system = 1; + } + else + { + st.st_size = st1.st_size; + modtime = modtime1; } - emacs_close (desc1); } } |