Skip to content

Commit c8b3094

Browse files
committed
util-lib: split out file attribute calls to chattr-util.[ch]
1 parent 89a5a90 commit c8b3094

File tree

12 files changed

+144
-85
lines changed

12 files changed

+144
-85
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ libbasic_la_SOURCES = \
797797
src/basic/dirent-util.h \
798798
src/basic/xattr-util.c \
799799
src/basic/xattr-util.h \
800+
src/basic/chattr-util.c \
801+
src/basic/chattr-util.h \
800802
src/basic/mount-util.c \
801803
src/basic/mount-util.h \
802804
src/basic/hexdecoct.c \

src/basic/chattr-util.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
/***
4+
This file is part of systemd.
5+
6+
Copyright 2010 Lennart Poettering
7+
8+
systemd is free software; you can redistribute it and/or modify it
9+
under the terms of the GNU Lesser General Public License as published by
10+
the Free Software Foundation; either version 2.1 of the License, or
11+
(at your option) any later version.
12+
13+
systemd is distributed in the hope that it will be useful, but
14+
WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public License
19+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
20+
***/
21+
22+
#include <sys/ioctl.h>
23+
#include <sys/stat.h>
24+
#include <linux/fs.h>
25+
26+
#include "chattr-util.h"
27+
#include "fd-util.h"
28+
#include "util.h"
29+
30+
int chattr_fd(int fd, unsigned value, unsigned mask) {
31+
unsigned old_attr, new_attr;
32+
struct stat st;
33+
34+
assert(fd >= 0);
35+
36+
if (fstat(fd, &st) < 0)
37+
return -errno;
38+
39+
/* Explicitly check whether this is a regular file or
40+
* directory. If it is anything else (such as a device node or
41+
* fifo), then the ioctl will not hit the file systems but
42+
* possibly drivers, where the ioctl might have different
43+
* effects. Notably, DRM is using the same ioctl() number. */
44+
45+
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
46+
return -ENOTTY;
47+
48+
if (mask == 0)
49+
return 0;
50+
51+
if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
52+
return -errno;
53+
54+
new_attr = (old_attr & ~mask) | (value & mask);
55+
if (new_attr == old_attr)
56+
return 0;
57+
58+
if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0)
59+
return -errno;
60+
61+
return 1;
62+
}
63+
64+
int chattr_path(const char *p, unsigned value, unsigned mask) {
65+
_cleanup_close_ int fd = -1;
66+
67+
assert(p);
68+
69+
if (mask == 0)
70+
return 0;
71+
72+
fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
73+
if (fd < 0)
74+
return -errno;
75+
76+
return chattr_fd(fd, value, mask);
77+
}
78+
79+
int read_attr_fd(int fd, unsigned *ret) {
80+
struct stat st;
81+
82+
assert(fd >= 0);
83+
84+
if (fstat(fd, &st) < 0)
85+
return -errno;
86+
87+
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
88+
return -ENOTTY;
89+
90+
if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0)
91+
return -errno;
92+
93+
return 0;
94+
}
95+
96+
int read_attr_path(const char *p, unsigned *ret) {
97+
_cleanup_close_ int fd = -1;
98+
99+
assert(p);
100+
assert(ret);
101+
102+
fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
103+
if (fd < 0)
104+
return -errno;
105+
106+
return read_attr_fd(fd, ret);
107+
}

src/basic/chattr-util.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
#pragma once
4+
5+
/***
6+
This file is part of systemd.
7+
8+
Copyright 2010 Lennart Poettering
9+
10+
systemd is free software; you can redistribute it and/or modify it
11+
under the terms of the GNU Lesser General Public License as published by
12+
the Free Software Foundation; either version 2.1 of the License, or
13+
(at your option) any later version.
14+
15+
systemd is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public License
21+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
22+
***/
23+
24+
int chattr_fd(int fd, unsigned value, unsigned mask);
25+
int chattr_path(const char *p, unsigned value, unsigned mask);
26+
27+
int read_attr_fd(int fd, unsigned *ret);
28+
int read_attr_path(const char *p, unsigned *ret);

src/basic/copy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <sys/xattr.h>
2424

2525
#include "btrfs-util.h"
26+
#include "chattr-util.h"
2627
#include "copy.h"
2728
#include "dirent-util.h"
2829
#include "fd-util.h"

src/basic/util.c

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,85 +1962,6 @@ int is_device_node(const char *path) {
19621962
return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
19631963
}
19641964

1965-
int chattr_fd(int fd, unsigned value, unsigned mask) {
1966-
unsigned old_attr, new_attr;
1967-
struct stat st;
1968-
1969-
assert(fd >= 0);
1970-
1971-
if (fstat(fd, &st) < 0)
1972-
return -errno;
1973-
1974-
/* Explicitly check whether this is a regular file or
1975-
* directory. If it is anything else (such as a device node or
1976-
* fifo), then the ioctl will not hit the file systems but
1977-
* possibly drivers, where the ioctl might have different
1978-
* effects. Notably, DRM is using the same ioctl() number. */
1979-
1980-
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
1981-
return -ENOTTY;
1982-
1983-
if (mask == 0)
1984-
return 0;
1985-
1986-
if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
1987-
return -errno;
1988-
1989-
new_attr = (old_attr & ~mask) | (value & mask);
1990-
if (new_attr == old_attr)
1991-
return 0;
1992-
1993-
if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0)
1994-
return -errno;
1995-
1996-
return 1;
1997-
}
1998-
1999-
int chattr_path(const char *p, unsigned value, unsigned mask) {
2000-
_cleanup_close_ int fd = -1;
2001-
2002-
assert(p);
2003-
2004-
if (mask == 0)
2005-
return 0;
2006-
2007-
fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
2008-
if (fd < 0)
2009-
return -errno;
2010-
2011-
return chattr_fd(fd, value, mask);
2012-
}
2013-
2014-
int read_attr_fd(int fd, unsigned *ret) {
2015-
struct stat st;
2016-
2017-
assert(fd >= 0);
2018-
2019-
if (fstat(fd, &st) < 0)
2020-
return -errno;
2021-
2022-
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
2023-
return -ENOTTY;
2024-
2025-
if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0)
2026-
return -errno;
2027-
2028-
return 0;
2029-
}
2030-
2031-
int read_attr_path(const char *p, unsigned *ret) {
2032-
_cleanup_close_ int fd = -1;
2033-
2034-
assert(p);
2035-
assert(ret);
2036-
2037-
fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
2038-
if (fd < 0)
2039-
return -errno;
2040-
2041-
return read_attr_fd(fd, ret);
2042-
}
2043-
20441965
int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
20451966
int a = 0, b = 0, c = 0;
20461967
int k;

src/basic/util.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,6 @@ union inotify_event_buffer {
500500

501501
#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
502502

503-
int chattr_fd(int fd, unsigned value, unsigned mask);
504-
int chattr_path(const char *p, unsigned value, unsigned mask);
505-
506-
int read_attr_fd(int fd, unsigned *ret);
507-
int read_attr_path(const char *p, unsigned *ret);
508-
509503
int syslog_parse_priority(const char **p, int *priority, bool with_facility);
510504

511505
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);

src/import/import-raw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "sd-event.h"
2626

2727
#include "btrfs-util.h"
28+
#include "chattr-util.h"
2829
#include "copy.h"
2930
#include "fd-util.h"
3031
#include "fileio.h"

src/import/pull-raw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "sd-daemon.h"
2727

2828
#include "btrfs-util.h"
29+
#include "chattr-util.h"
2930
#include "copy.h"
3031
#include "curl-util.h"
3132
#include "fd-util.h"

src/journal/journal-file.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <unistd.h>
3030

3131
#include "btrfs-util.h"
32+
#include "chattr-util.h"
3233
#include "compress.h"
3334
#include "fd-util.h"
3435
#include "journal-authenticate.h"

src/journal/journalctl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "bus-error.h"
4343
#include "bus-util.h"
4444
#include "catalog.h"
45+
#include "chattr-util.h"
4546
#include "fd-util.h"
4647
#include "fileio.h"
4748
#include "fsprg.h"

0 commit comments

Comments
 (0)