From 6c00c6cee5a32b6af0e4c589d74c6974be50aa20 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 8 Apr 2015 20:47:35 +0200 Subject: [PATCH] util: merge change_attr_fd() and chattr_fd() --- src/shared/copy.c | 2 +- src/shared/util.c | 36 +++++------------------------------- src/shared/util.h | 5 ++--- 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/src/shared/copy.c b/src/shared/copy.c index 775a33985..1282cb88b 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -372,7 +372,7 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned } if (chattr_flags != 0) - (void) chattr_fd(fdt, true, chattr_flags); + (void) chattr_fd(fdt, chattr_flags, (unsigned) -1); r = copy_file_fd(from, fdt, true); if (r < 0) { diff --git a/src/shared/util.c b/src/shared/util.c index d7e4318de..ffde3ff8f 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -7634,7 +7634,7 @@ int fd_setcrtime(int fd, usec_t usec) { return 0; } -int chattr_fd(int fd, bool b, unsigned mask) { +int chattr_fd(int fd, unsigned value, unsigned mask) { unsigned old_attr, new_attr; assert(fd >= 0); @@ -7645,21 +7645,17 @@ int chattr_fd(int fd, bool b, unsigned mask) { if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0) return -errno; - if (b) - new_attr = old_attr | mask; - else - new_attr = old_attr & ~mask; - + new_attr = (old_attr & ~mask) | (value & mask); if (new_attr == old_attr) return 0; if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0) return -errno; - return 0; + return 1; } -int chattr_path(const char *p, bool b, unsigned mask) { +int chattr_path(const char *p, unsigned value, unsigned mask) { _cleanup_close_ int fd = -1; assert(p); @@ -7671,29 +7667,7 @@ int chattr_path(const char *p, bool b, unsigned mask) { if (fd < 0) return -errno; - return chattr_fd(fd, b, mask); -} - -int change_attr_fd(int fd, unsigned value, unsigned mask) { - unsigned old_attr, new_attr; - - assert(fd >= 0); - - if (mask == 0) - 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) - return 0; - - if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0) - return -errno; - - return 0; + return chattr_fd(fd, value, mask); } int read_attr_fd(int fd, unsigned *ret) { diff --git a/src/shared/util.h b/src/shared/util.h index 2154623af..427b52ef1 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -1057,9 +1057,8 @@ int fd_getcrtime(int fd, usec_t *usec); int path_getcrtime(const char *p, usec_t *usec); int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags); -int chattr_fd(int fd, bool b, unsigned mask); -int chattr_path(const char *p, bool b, unsigned mask); -int change_attr_fd(int fd, unsigned value, unsigned mask); +int chattr_fd(int fd, unsigned value, unsigned mask); +int chattr_path(const char *p, unsigned value, unsigned mask); int read_attr_fd(int fd, unsigned *ret); int read_attr_path(const char *p, unsigned *ret); -- 2.30.2