util-lib: split out file attribute calls to chattr-util.[ch]
This commit is contained in:
parent
89a5a90cb0
commit
c8b3094de5
|
@ -797,6 +797,8 @@ libbasic_la_SOURCES = \
|
||||||
src/basic/dirent-util.h \
|
src/basic/dirent-util.h \
|
||||||
src/basic/xattr-util.c \
|
src/basic/xattr-util.c \
|
||||||
src/basic/xattr-util.h \
|
src/basic/xattr-util.h \
|
||||||
|
src/basic/chattr-util.c \
|
||||||
|
src/basic/chattr-util.h \
|
||||||
src/basic/mount-util.c \
|
src/basic/mount-util.c \
|
||||||
src/basic/mount-util.h \
|
src/basic/mount-util.h \
|
||||||
src/basic/hexdecoct.c \
|
src/basic/hexdecoct.c \
|
||||||
|
|
107
src/basic/chattr-util.c
Normal file
107
src/basic/chattr-util.c
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
***/
|
||||||
|
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <linux/fs.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
28
src/basic/chattr-util.h
Normal file
28
src/basic/chattr-util.h
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
***/
|
||||||
|
|
||||||
|
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);
|
|
@ -23,6 +23,7 @@
|
||||||
#include <sys/xattr.h>
|
#include <sys/xattr.h>
|
||||||
|
|
||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
|
#include "chattr-util.h"
|
||||||
#include "copy.h"
|
#include "copy.h"
|
||||||
#include "dirent-util.h"
|
#include "dirent-util.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
|
|
|
@ -1962,85 +1962,6 @@ int is_device_node(const char *path) {
|
||||||
return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
|
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 syslog_parse_priority(const char **p, int *priority, bool with_facility) {
|
||||||
int a = 0, b = 0, c = 0;
|
int a = 0, b = 0, c = 0;
|
||||||
int k;
|
int k;
|
||||||
|
|
|
@ -500,12 +500,6 @@ union inotify_event_buffer {
|
||||||
|
|
||||||
#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
|
#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 syslog_parse_priority(const char **p, int *priority, bool with_facility);
|
||||||
|
|
||||||
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
|
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include "sd-event.h"
|
#include "sd-event.h"
|
||||||
|
|
||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
|
#include "chattr-util.h"
|
||||||
#include "copy.h"
|
#include "copy.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "sd-daemon.h"
|
#include "sd-daemon.h"
|
||||||
|
|
||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
|
#include "chattr-util.h"
|
||||||
#include "copy.h"
|
#include "copy.h"
|
||||||
#include "curl-util.h"
|
#include "curl-util.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
|
#include "chattr-util.h"
|
||||||
#include "compress.h"
|
#include "compress.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "journal-authenticate.h"
|
#include "journal-authenticate.h"
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
#include "bus-error.h"
|
#include "bus-error.h"
|
||||||
#include "bus-util.h"
|
#include "bus-util.h"
|
||||||
#include "catalog.h"
|
#include "catalog.h"
|
||||||
|
#include "chattr-util.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
#include "fsprg.h"
|
#include "fsprg.h"
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include <sys/statfs.h>
|
#include <sys/statfs.h>
|
||||||
|
|
||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
|
#include "chattr-util.h"
|
||||||
#include "copy.h"
|
#include "copy.h"
|
||||||
#include "dirent-util.h"
|
#include "dirent-util.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
|
|
|
@ -41,6 +41,7 @@
|
||||||
#include "acl-util.h"
|
#include "acl-util.h"
|
||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
#include "capability.h"
|
#include "capability.h"
|
||||||
|
#include "chattr-util.h"
|
||||||
#include "conf-files.h"
|
#include "conf-files.h"
|
||||||
#include "copy.h"
|
#include "copy.h"
|
||||||
#include "escape.h"
|
#include "escape.h"
|
||||||
|
|
Loading…
Reference in a new issue