summaryrefslogtreecommitdiffstats
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-06-25 20:04:07 +0200
committerLennart Poettering <lennart@poettering.net>2018-10-08 21:40:44 +0200
commitdb9a42545adaa54d789760f842702816625cc384 (patch)
tree1166dec405939b1fbcc2fc17e707651fbc0ab5f8 /src/basic
parentefi: allow ESP validity checks to be turned off (diff)
downloadsystemd-db9a42545adaa54d789760f842702816625cc384.tar.xz
systemd-db9a42545adaa54d789760f842702816625cc384.zip
chattr: optionally, return the old flags when updating them
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/btrfs-util.c2
-rw-r--r--src/basic/chattr-util.c16
-rw-r--r--src/basic/chattr-util.h4
-rw-r--r--src/basic/copy.c4
4 files changed, 16 insertions, 10 deletions
diff --git a/src/basic/btrfs-util.c b/src/basic/btrfs-util.c
index 0308048b7f..1c9c5fd162 100644
--- a/src/basic/btrfs-util.c
+++ b/src/basic/btrfs-util.c
@@ -1715,7 +1715,7 @@ int btrfs_subvol_snapshot_fd(int old_fd, const char *new_path, BtrfsSnapshotFlag
* it: the IMMUTABLE bit. Let's use this here, if this is requested. */
if (flags & BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE)
- (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL);
+ (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL, NULL);
} else {
r = btrfs_subvol_set_read_only(new_path, true);
if (r < 0)
diff --git a/src/basic/chattr-util.c b/src/basic/chattr-util.c
index 4ec14515eb..235cfb9bd7 100644
--- a/src/basic/chattr-util.c
+++ b/src/basic/chattr-util.c
@@ -10,7 +10,7 @@
#include "fd-util.h"
#include "macro.h"
-int chattr_fd(int fd, unsigned value, unsigned mask) {
+int chattr_fd(int fd, unsigned value, unsigned mask, unsigned *previous) {
unsigned old_attr, new_attr;
struct stat st;
@@ -28,23 +28,29 @@ int chattr_fd(int fd, unsigned value, unsigned mask) {
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
return -ENOTTY;
- if (mask == 0)
+ if (mask == 0 && !previous)
return 0;
if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
return -errno;
new_attr = (old_attr & ~mask) | (value & mask);
- if (new_attr == old_attr)
+ if (new_attr == old_attr) {
+ if (previous)
+ *previous = old_attr;
return 0;
+ }
if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0)
return -errno;
+ if (previous)
+ *previous = old_attr;
+
return 1;
}
-int chattr_path(const char *p, unsigned value, unsigned mask) {
+int chattr_path(const char *p, unsigned value, unsigned mask, unsigned *previous) {
_cleanup_close_ int fd = -1;
assert(p);
@@ -56,7 +62,7 @@ int chattr_path(const char *p, unsigned value, unsigned mask) {
if (fd < 0)
return -errno;
- return chattr_fd(fd, value, mask);
+ return chattr_fd(fd, value, mask, previous);
}
int read_attr_fd(int fd, unsigned *ret) {
diff --git a/src/basic/chattr-util.h b/src/basic/chattr-util.h
index 0c0816344a..7570bba2fa 100644
--- a/src/basic/chattr-util.h
+++ b/src/basic/chattr-util.h
@@ -1,8 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
-int chattr_fd(int fd, unsigned value, unsigned mask);
-int chattr_path(const char *p, unsigned value, unsigned mask);
+int chattr_fd(int fd, unsigned value, unsigned mask, unsigned *previous);
+int chattr_path(const char *p, unsigned value, unsigned mask, unsigned *previous);
int read_attr_fd(int fd, unsigned *ret);
int read_attr_path(const char *p, unsigned *ret);
diff --git a/src/basic/copy.c b/src/basic/copy.c
index e06a503a29..714cbb8fc3 100644
--- a/src/basic/copy.c
+++ b/src/basic/copy.c
@@ -695,7 +695,7 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned
}
if (chattr_flags != 0)
- (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
+ (void) chattr_fd(fdt, chattr_flags, (unsigned) -1, NULL);
r = copy_file_fd(from, fdt, copy_flags);
if (r < 0) {
@@ -743,7 +743,7 @@ int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned cha
}
if (chattr_flags != 0)
- (void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
+ (void) chattr_fd(fdt, chattr_flags, (unsigned) -1, NULL);
r = copy_file_fd(from, fdt, copy_flags);
if (r < 0)