]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Make read_attr_path() more generic
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 24 Jul 2024 12:54:06 +0000 (14:54 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 24 Jul 2024 16:58:41 +0000 (18:58 +0200)
Let's make this an openat() like function so it can be used in more
scenarios.

src/basic/chattr-util.c
src/basic/chattr-util.h
src/shared/discover-image.c

index d76be5c99b686b3af6b5bec6d4fc66e4b4151463..49831a26cee32a9e8250686602a59bb9c32a58d6 100644 (file)
@@ -148,15 +148,15 @@ int read_attr_fd(int fd, unsigned *ret) {
         return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
 }
 
-int read_attr_path(const char *p, unsigned *ret) {
+int read_attr_at(int dir_fd, const char *path, unsigned *ret) {
         _cleanup_close_ int fd = -EBADF;
 
-        assert(p);
+        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
         assert(ret);
 
-        fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
+        fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
         if (fd < 0)
-                return -errno;
+                return fd;
 
         return read_attr_fd(fd, ret);
 }
index c1ee63b4fae94927a7344ee12343ee7d30d9ef5a..1fe38e32b15dd4e56126a22a3d8a0308b871c529 100644 (file)
@@ -52,7 +52,7 @@ static inline int chattr_path(const char *path, unsigned value, unsigned mask, u
 }
 
 int read_attr_fd(int fd, unsigned *ret);
-int read_attr_path(const char *p, unsigned *ret);
+int read_attr_at(int dir_fd, const char *path, unsigned *ret);
 
 /* Combination of chattr flags, that should be appropriate for secrets stored on disk: Secure Remove +
  * Exclusion from Dumping + Synchronous Writing (i.e. not caching in memory) + In-Place Updating (i.e. not
index d7a9df70b1c7dc5243f13679ac27ef0c9146dcdc..dd78a013aaf26c49d3b94ff6c1c21b27477c0dac 100644 (file)
@@ -1070,7 +1070,7 @@ int image_rename(Image *i, const char *new_name) {
 
         case IMAGE_DIRECTORY:
                 /* Turn of the immutable bit while we rename the image, so that we can rename it */
-                (void) read_attr_path(i->path, &file_attr);
+                (void) read_attr_at(AT_FDCWD, i->path, &file_attr);
 
                 if (file_attr & FS_IMMUTABLE_FL)
                         (void) chattr_path(i->path, 0, FS_IMMUTABLE_FL, NULL);