aboutsummaryrefslogtreecommitdiff
path: root/text.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-07-10 15:38:24 +0200
committerMarc André Tanner <mat@brain-dump.org>2015-07-10 15:38:24 +0200
commit8275230764deae7d708818ad565a9560f89ad6af (patch)
treeffc213e8a50bf999e0e73ae5306e24f4778cc9b9 /text.c
parent142f58a35adc20b131a5b7280121d080d9c83b86 (diff)
downloadvis-8275230764deae7d708818ad565a9560f89ad6af.tar.gz
vis-8275230764deae7d708818ad565a9560f89ad6af.tar.xz
text: upon successfull file save store new modification time
Diffstat (limited to 'text.c')
-rw-r--r--text.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/text.c b/text.c
index a77bc07..520211e 100644
--- a/text.c
+++ b/text.c
@@ -799,7 +799,7 @@ static bool preserve_selinux_context(int src, int dest) {
* - SELinux security context can not be preserved (if enabled)
*/
static bool text_range_save_atomic(Text *txt, Filerange *range, const char *filename) {
- struct stat meta = { 0 };
+ struct stat meta = { 0 }, oldmeta = { 0 };
int fd = -1, oldfd = -1, saved_errno;
char *tmpname = NULL;
size_t size = text_range_size(range);
@@ -807,12 +807,12 @@ static bool text_range_save_atomic(Text *txt, Filerange *range, const char *file
if ((oldfd = open(filename, O_RDONLY)) == -1 && errno != ENOENT)
goto err;
- if (oldfd != -1 && lstat(filename, &meta) == -1)
+ if (oldfd != -1 && lstat(filename, &oldmeta) == -1)
goto err;
if (oldfd != -1) {
- if (S_ISLNK(meta.st_mode)) /* symbolic link */
+ if (S_ISLNK(oldmeta.st_mode)) /* symbolic link */
goto err;
- if (meta.st_nlink > 1) /* hard link */
+ if (oldmeta.st_nlink > 1) /* hard link */
goto err;
}
if (!(tmpname = calloc(1, namelen)))
@@ -820,7 +820,7 @@ static bool text_range_save_atomic(Text *txt, Filerange *range, const char *file
snprintf(tmpname, namelen, "%s~", filename);
/* O_RDWR is needed because otherwise we can't map with MAP_SHARED */
- if ((fd = open(tmpname, O_CREAT|O_RDWR|O_TRUNC, oldfd == -1 ? S_IRUSR|S_IWUSR : meta.st_mode)) == -1)
+ if ((fd = open(tmpname, O_CREAT|O_RDWR|O_TRUNC, oldfd == -1 ? S_IRUSR|S_IWUSR : oldmeta.st_mode)) == -1)
goto err;
if (ftruncate(fd, size) == -1)
goto err;
@@ -828,11 +828,11 @@ static bool text_range_save_atomic(Text *txt, Filerange *range, const char *file
if (!preserve_acl(oldfd, fd) || !preserve_selinux_context(oldfd, fd))
goto err;
/* change owner if necessary */
- if (meta.st_uid != getuid() && fchown(fd, meta.st_uid, (uid_t)-1) == -1)
+ if (oldmeta.st_uid != getuid() && fchown(fd, oldmeta.st_uid, (uid_t)-1) == -1)
goto err;
/* change group if necessary, in case of failure some editors reset
* the group permissions to the same as for others */
- if (meta.st_gid != getgid() && fchown(fd, (uid_t)-1, meta.st_gid) == -1)
+ if (oldmeta.st_gid != getgid() && fchown(fd, (uid_t)-1, oldmeta.st_gid) == -1)
goto err;
}
@@ -866,6 +866,10 @@ static bool text_range_save_atomic(Text *txt, Filerange *range, const char *file
if (fsync(fd) == -1)
goto err;
+
+ if (fstat(fd, &meta) == -1)
+ goto err;
+
if (close(fd) == -1) {
fd = -1;
goto err;