diff options
Diffstat (limited to 'shared/systemd/src/basic/fs-util.c')
| -rw-r--r-- | shared/systemd/src/basic/fs-util.c | 87 |
1 files changed, 82 insertions, 5 deletions
diff --git a/shared/systemd/src/basic/fs-util.c b/shared/systemd/src/basic/fs-util.c index e50252cb..2ed9ee0e 100644 --- a/shared/systemd/src/basic/fs-util.c +++ b/shared/systemd/src/basic/fs-util.c @@ -1,4 +1,4 @@ -/* SPDX-License-Identifier: LGPL-2.1+ */ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "nm-sd-adapt-shared.h" @@ -821,7 +821,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, * * 3. With CHASE_STEP: in this case only a single step of the normalization is executed, i.e. only the first * symlink or ".." component of the path is resolved, and the resulting path is returned. This is useful if - * a caller wants to trace the a path through the file system verbosely. Returns < 0 on error, > 0 if the + * a caller wants to trace the path through the file system verbosely. Returns < 0 on error, > 0 if the * path is fully normalized, and == 0 for each normalization step. This may be combined with * CHASE_NONEXISTENT, in which case 1 is returned when a component is not found. * @@ -945,7 +945,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, /* Preserve the trailing slash */ if (flags & CHASE_TRAIL_SLASH) - if (!strextend(&done, "/", NULL)) + if (!strextend(&done, "/")) return -ENOMEM; break; @@ -1016,7 +1016,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, if (streq_ptr(done, "/")) *done = '\0'; - if (!strextend(&done, first, todo, NULL)) + if (!strextend(&done, first, todo)) return -ENOMEM; exists = false; @@ -1109,7 +1109,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, if (streq(done, "/")) *done = '\0'; - if (!strextend(&done, first, NULL)) + if (!strextend(&done, first)) return -ENOMEM; } @@ -1626,4 +1626,81 @@ int path_is_encrypted(const char *path) { return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */); } + +int conservative_rename( + int olddirfd, const char *oldpath, + int newdirfd, const char *newpath) { + + _cleanup_close_ int old_fd = -1, new_fd = -1; + struct stat old_stat, new_stat; + + /* Renames the old path to thew new path, much like renameat() — except if both are regular files and + * have the exact same contents and basic file attributes already. In that case remove the new file + * instead. This call is useful for reducing inotify wakeups on files that are updated but don't + * actually change. This function is written in a style that we rather rename too often than suppress + * too much. i.e. whenever we are in doubt we rather rename than fail. After all reducing inotify + * events is an optimization only, not more. */ + + old_fd = openat(olddirfd, oldpath, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_NOFOLLOW); + if (old_fd < 0) + goto do_rename; + + new_fd = openat(newdirfd, newpath, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_NOFOLLOW); + if (new_fd < 0) + goto do_rename; + + if (fstat(old_fd, &old_stat) < 0) + goto do_rename; + + if (!S_ISREG(old_stat.st_mode)) + goto do_rename; + + if (fstat(new_fd, &new_stat) < 0) + goto do_rename; + + if (new_stat.st_ino == old_stat.st_ino && + new_stat.st_dev == old_stat.st_dev) + goto is_same; + + if (old_stat.st_mode != new_stat.st_mode || + old_stat.st_size != new_stat.st_size || + old_stat.st_uid != new_stat.st_uid || + old_stat.st_gid != new_stat.st_gid) + goto do_rename; + + for (;;) { + char buf1[16*1024]; + char buf2[sizeof(buf1) + 1]; + ssize_t l1, l2; + + l1 = read(old_fd, buf1, sizeof(buf1)); + if (l1 < 0) + goto do_rename; + + l2 = read(new_fd, buf2, l1 + 1); + if (l1 != l2) + goto do_rename; + + if (l1 == 0) /* EOF on both! And everything's the same so far, yay! */ + break; + + if (memcmp(buf1, buf2, l1) != 0) + goto do_rename; + } + +is_same: + /* Everything matches? Then don't rename, instead remove the source file, and leave the existing + * destination in place */ + + if (unlinkat(olddirfd, oldpath, 0) < 0) + goto do_rename; + + return 0; + +do_rename: + if (renameat(olddirfd, oldpath, newdirfd, newpath) < 0) + return -errno; + + return 1; +} #endif /* NM_IGNORED */ |