From c8b3094de58e3b1e37d06f3d56c9346cffbe320a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Oct 2015 20:39:23 +0100 Subject: [PATCH] util-lib: split out file attribute calls to chattr-util.[ch] --- Makefile.am | 2 + src/basic/chattr-util.c | 107 +++++++++++++++++++++++++++++++++++++ src/basic/chattr-util.h | 28 ++++++++++ src/basic/copy.c | 1 + src/basic/util.c | 79 --------------------------- src/basic/util.h | 6 --- src/import/import-raw.c | 1 + src/import/pull-raw.c | 1 + src/journal/journal-file.c | 1 + src/journal/journalctl.c | 1 + src/shared/machine-image.c | 1 + src/tmpfiles/tmpfiles.c | 1 + 12 files changed, 144 insertions(+), 85 deletions(-) create mode 100644 src/basic/chattr-util.c create mode 100644 src/basic/chattr-util.h diff --git a/Makefile.am b/Makefile.am index 33d66d4dd0..15aace196a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -797,6 +797,8 @@ libbasic_la_SOURCES = \ src/basic/dirent-util.h \ src/basic/xattr-util.c \ src/basic/xattr-util.h \ + src/basic/chattr-util.c \ + src/basic/chattr-util.h \ src/basic/mount-util.c \ src/basic/mount-util.h \ src/basic/hexdecoct.c \ diff --git a/src/basic/chattr-util.c b/src/basic/chattr-util.c new file mode 100644 index 0000000000..d49ca0537a --- /dev/null +++ b/src/basic/chattr-util.c @@ -0,0 +1,107 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include + +#include "chattr-util.h" +#include "fd-util.h" +#include "util.h" + +int chattr_fd(int fd, unsigned value, unsigned mask) { + unsigned old_attr, new_attr; + struct stat st; + + assert(fd >= 0); + + if (fstat(fd, &st) < 0) + return -errno; + + /* Explicitly check whether this is a regular file or + * directory. If it is anything else (such as a device node or + * fifo), then the ioctl will not hit the file systems but + * possibly drivers, where the ioctl might have different + * effects. Notably, DRM is using the same ioctl() number. */ + + if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) + return -ENOTTY; + + 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 1; +} + +int chattr_path(const char *p, unsigned value, unsigned mask) { + _cleanup_close_ int fd = -1; + + assert(p); + + if (mask == 0) + return 0; + + fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); + if (fd < 0) + return -errno; + + return chattr_fd(fd, value, mask); +} + +int read_attr_fd(int fd, unsigned *ret) { + struct stat st; + + assert(fd >= 0); + + if (fstat(fd, &st) < 0) + return -errno; + + if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) + return -ENOTTY; + + if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0) + return -errno; + + return 0; +} + +int read_attr_path(const char *p, unsigned *ret) { + _cleanup_close_ int fd = -1; + + assert(p); + assert(ret); + + fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); + if (fd < 0) + return -errno; + + return read_attr_fd(fd, ret); +} diff --git a/src/basic/chattr-util.h b/src/basic/chattr-util.h new file mode 100644 index 0000000000..ba6b8eb5c1 --- /dev/null +++ b/src/basic/chattr-util.h @@ -0,0 +1,28 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +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); diff --git a/src/basic/copy.c b/src/basic/copy.c index 60a4bee0fe..8910052116 100644 --- a/src/basic/copy.c +++ b/src/basic/copy.c @@ -23,6 +23,7 @@ #include #include "btrfs-util.h" +#include "chattr-util.h" #include "copy.h" #include "dirent-util.h" #include "fd-util.h" diff --git a/src/basic/util.c b/src/basic/util.c index dfebff7ec1..889ca288fc 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -1962,85 +1962,6 @@ int is_device_node(const char *path) { return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode)); } -int chattr_fd(int fd, unsigned value, unsigned mask) { - unsigned old_attr, new_attr; - struct stat st; - - assert(fd >= 0); - - if (fstat(fd, &st) < 0) - return -errno; - - /* Explicitly check whether this is a regular file or - * directory. If it is anything else (such as a device node or - * fifo), then the ioctl will not hit the file systems but - * possibly drivers, where the ioctl might have different - * effects. Notably, DRM is using the same ioctl() number. */ - - if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) - return -ENOTTY; - - 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 1; -} - -int chattr_path(const char *p, unsigned value, unsigned mask) { - _cleanup_close_ int fd = -1; - - assert(p); - - if (mask == 0) - return 0; - - fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); - if (fd < 0) - return -errno; - - return chattr_fd(fd, value, mask); -} - -int read_attr_fd(int fd, unsigned *ret) { - struct stat st; - - assert(fd >= 0); - - if (fstat(fd, &st) < 0) - return -errno; - - if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) - return -ENOTTY; - - if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0) - return -errno; - - return 0; -} - -int read_attr_path(const char *p, unsigned *ret) { - _cleanup_close_ int fd = -1; - - assert(p); - assert(ret); - - fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); - if (fd < 0) - return -errno; - - return read_attr_fd(fd, ret); -} - int syslog_parse_priority(const char **p, int *priority, bool with_facility) { int a = 0, b = 0, c = 0; int k; diff --git a/src/basic/util.h b/src/basic/util.h index 8f9f3f8fe6..30c88d38ad 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -500,12 +500,6 @@ union inotify_event_buffer { #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW) -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); - int syslog_parse_priority(const char **p, int *priority, bool with_facility); int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath); diff --git a/src/import/import-raw.c b/src/import/import-raw.c index d94d8ab4ae..0c81001dbe 100644 --- a/src/import/import-raw.c +++ b/src/import/import-raw.c @@ -25,6 +25,7 @@ #include "sd-event.h" #include "btrfs-util.h" +#include "chattr-util.h" #include "copy.h" #include "fd-util.h" #include "fileio.h" diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c index 4d0ac770aa..39759b50b0 100644 --- a/src/import/pull-raw.c +++ b/src/import/pull-raw.c @@ -26,6 +26,7 @@ #include "sd-daemon.h" #include "btrfs-util.h" +#include "chattr-util.h" #include "copy.h" #include "curl-util.h" #include "fd-util.h" diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index a3b09dddff..6f9b8a2915 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -29,6 +29,7 @@ #include #include "btrfs-util.h" +#include "chattr-util.h" #include "compress.h" #include "fd-util.h" #include "journal-authenticate.h" diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 5b3b6cd143..ef5518136a 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -42,6 +42,7 @@ #include "bus-error.h" #include "bus-util.h" #include "catalog.h" +#include "chattr-util.h" #include "fd-util.h" #include "fileio.h" #include "fsprg.h" diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index ca85e26095..c5444ee010 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -24,6 +24,7 @@ #include #include "btrfs-util.h" +#include "chattr-util.h" #include "copy.h" #include "dirent-util.h" #include "fd-util.h" diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 85a2d6c2f5..8cf327a15f 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -41,6 +41,7 @@ #include "acl-util.h" #include "btrfs-util.h" #include "capability.h" +#include "chattr-util.h" #include "conf-files.h" #include "copy.h" #include "escape.h"