util: merge change_attr_fd() and chattr_fd()

This commit is contained in:
Lennart Poettering 2015-04-08 20:47:35 +02:00
parent 171181bcd6
commit 1ed8f8c16d
8 changed files with 17 additions and 45 deletions

View file

@ -188,7 +188,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) {
if (converted_fd < 0) if (converted_fd < 0)
return log_error_errno(errno, "Failed to create %s: %m", t); return log_error_errno(errno, "Failed to create %s: %m", t);
r = chattr_fd(converted_fd, true, FS_NOCOW_FL); r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL);
if (r < 0) if (r < 0)
log_warning_errno(errno, "Failed to set file attributes on %s: %m", t); log_warning_errno(errno, "Failed to set file attributes on %s: %m", t);
@ -277,7 +277,7 @@ static int raw_import_open_disk(RawImport *i) {
if (i->output_fd < 0) if (i->output_fd < 0)
return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path); return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path);
r = chattr_fd(i->output_fd, true, FS_NOCOW_FL); r = chattr_fd(i->output_fd, FS_NOCOW_FL, FS_NOCOW_FL);
if (r < 0) if (r < 0)
log_warning_errno(errno, "Failed to set file attributes on %s: %m", i->temp_path); log_warning_errno(errno, "Failed to set file attributes on %s: %m", i->temp_path);

View file

@ -216,7 +216,7 @@ static int raw_pull_maybe_convert_qcow2(RawPull *i) {
if (converted_fd < 0) if (converted_fd < 0)
return log_error_errno(errno, "Failed to create %s: %m", t); return log_error_errno(errno, "Failed to create %s: %m", t);
r = chattr_fd(converted_fd, true, FS_NOCOW_FL); r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL);
if (r < 0) if (r < 0)
log_warning_errno(errno, "Failed to set file attributes on %s: %m", t); log_warning_errno(errno, "Failed to set file attributes on %s: %m", t);
@ -292,7 +292,7 @@ static int raw_pull_make_local_copy(RawPull *i) {
* performance on COW file systems like btrfs, since it * performance on COW file systems like btrfs, since it
* reduces fragmentation caused by not allowing in-place * reduces fragmentation caused by not allowing in-place
* writes. */ * writes. */
r = chattr_fd(dfd, true, FS_NOCOW_FL); r = chattr_fd(dfd, FS_NOCOW_FL, FS_NOCOW_FL);
if (r < 0) if (r < 0)
log_warning_errno(errno, "Failed to set file attributes on %s: %m", tp); log_warning_errno(errno, "Failed to set file attributes on %s: %m", tp);
@ -434,7 +434,7 @@ static int raw_pull_job_on_open_disk(PullJob *j) {
if (j->disk_fd < 0) if (j->disk_fd < 0)
return log_error_errno(errno, "Failed to create %s: %m", i->temp_path); return log_error_errno(errno, "Failed to create %s: %m", i->temp_path);
r = chattr_fd(j->disk_fd, true, FS_NOCOW_FL); r = chattr_fd(j->disk_fd, FS_NOCOW_FL, FS_NOCOW_FL);
if (r < 0) if (r < 0)
log_warning_errno(errno, "Failed to set file attributes on %s: %m", i->temp_path); log_warning_errno(errno, "Failed to set file attributes on %s: %m", i->temp_path);

View file

@ -149,7 +149,7 @@ void journal_file_close(JournalFile *f) {
* reenable all the good bits COW usually provides * reenable all the good bits COW usually provides
* (such as data checksumming). */ * (such as data checksumming). */
(void) chattr_fd(f->fd, false, FS_NOCOW_FL); (void) chattr_fd(f->fd, 0, FS_NOCOW_FL);
(void) btrfs_defrag_fd(f->fd); (void) btrfs_defrag_fd(f->fd);
} }
@ -2608,7 +2608,7 @@ int journal_file_open(
* the expense of data integrity features (which * the expense of data integrity features (which
* shouldn't be too bad, given that we do our own * shouldn't be too bad, given that we do our own
* checksumming). */ * checksumming). */
r = chattr_fd(f->fd, true, FS_NOCOW_FL); r = chattr_fd(f->fd, FS_NOCOW_FL, FS_NOCOW_FL);
if (r < 0 && r != -ENOTTY) if (r < 0 && r != -ENOTTY)
log_warning_errno(r, "Failed to set file attributes: %m"); log_warning_errno(r, "Failed to set file attributes: %m");

View file

@ -1377,7 +1377,7 @@ static int setup_keys(void) {
/* Enable secure remove, exclusion from dump, synchronous /* Enable secure remove, exclusion from dump, synchronous
* writing and in-place updating */ * writing and in-place updating */
r = chattr_fd(fd, true, FS_SECRM_FL|FS_NODUMP_FL|FS_SYNC_FL|FS_NOCOW_FL); r = chattr_fd(fd, FS_SECRM_FL|FS_NODUMP_FL|FS_SYNC_FL|FS_NOCOW_FL, FS_SECRM_FL|FS_NODUMP_FL|FS_SYNC_FL|FS_NOCOW_FL);
if (r < 0) if (r < 0)
log_warning_errno(errno, "Failed to set file attributes: %m"); log_warning_errno(errno, "Failed to set file attributes: %m");

View file

@ -372,7 +372,7 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned
} }
if (chattr_flags != 0) 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); r = copy_file_fd(from, fdt, true);
if (r < 0) { if (r < 0) {

View file

@ -7678,7 +7678,7 @@ int same_fd(int a, int b) {
return fa == fb; return fa == fb;
} }
int chattr_fd(int fd, bool b, unsigned mask) { int chattr_fd(int fd, unsigned value, unsigned mask) {
unsigned old_attr, new_attr; unsigned old_attr, new_attr;
assert(fd >= 0); assert(fd >= 0);
@ -7689,21 +7689,17 @@ int chattr_fd(int fd, bool b, unsigned mask) {
if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0) if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
return -errno; return -errno;
if (b) new_attr = (old_attr & ~mask) | (value & mask);
new_attr = old_attr | mask;
else
new_attr = old_attr & ~mask;
if (new_attr == old_attr) if (new_attr == old_attr)
return 0; return 0;
if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0) if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0)
return -errno; 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; _cleanup_close_ int fd = -1;
assert(p); assert(p);
@ -7715,29 +7711,7 @@ int chattr_path(const char *p, bool b, unsigned mask) {
if (fd < 0) if (fd < 0)
return -errno; return -errno;
return chattr_fd(fd, b, mask); return chattr_fd(fd, value, 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;
} }
int read_attr_fd(int fd, unsigned *ret) { int read_attr_fd(int fd, unsigned *ret) {

View file

@ -1058,9 +1058,8 @@ int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
int same_fd(int a, int b); int same_fd(int a, int b);
int chattr_fd(int fd, bool b, unsigned mask); int chattr_fd(int fd, unsigned value, unsigned mask);
int chattr_path(const char *p, bool b, unsigned mask); int chattr_path(const char *p, unsigned value, unsigned mask);
int change_attr_fd(int fd, unsigned value, unsigned mask);
int read_attr_fd(int fd, unsigned *ret); int read_attr_fd(int fd, unsigned *ret);
int read_attr_path(const char *p, unsigned *ret); int read_attr_path(const char *p, unsigned *ret);

View file

@ -869,14 +869,13 @@ static int path_set_attrib(Item *item, const char *path) {
} }
fd = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC); fd = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
if (fd < 0) if (fd < 0)
return log_error_errno(errno, "Cannot open \"%s\": %m", path); return log_error_errno(errno, "Cannot open \"%s\": %m", path);
f = item->attrib_value & item->attrib_mask; f = item->attrib_value & item->attrib_mask;
if (!S_ISDIR(st.st_mode)) if (!S_ISDIR(st.st_mode))
f &= ~FS_DIRSYNC_FL; f &= ~FS_DIRSYNC_FL;
r = change_attr_fd(fd, f, item->attrib_mask); r = chattr_fd(fd, f, item->attrib_mask);
if (r < 0) if (r < 0)
return log_error_errno(errno, return log_error_errno(errno,
"Cannot set attrib for \"%s\", value=0x%08lx, mask=0x%08lx: %m", "Cannot set attrib for \"%s\", value=0x%08lx, mask=0x%08lx: %m",