]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/chattr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
12 #include "chattr-util.h"
16 int chattr_fd(int fd
, unsigned value
, unsigned mask
) {
17 unsigned old_attr
, new_attr
;
22 if (fstat(fd
, &st
) < 0)
25 /* Explicitly check whether this is a regular file or
26 * directory. If it is anything else (such as a device node or
27 * fifo), then the ioctl will not hit the file systems but
28 * possibly drivers, where the ioctl might have different
29 * effects. Notably, DRM is using the same ioctl() number. */
31 if (!S_ISDIR(st
.st_mode
) && !S_ISREG(st
.st_mode
))
37 if (ioctl(fd
, FS_IOC_GETFLAGS
, &old_attr
) < 0)
40 new_attr
= (old_attr
& ~mask
) | (value
& mask
);
41 if (new_attr
== old_attr
)
44 if (ioctl(fd
, FS_IOC_SETFLAGS
, &new_attr
) < 0)
50 int chattr_path(const char *p
, unsigned value
, unsigned mask
) {
51 _cleanup_close_
int fd
= -1;
58 fd
= open(p
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
62 return chattr_fd(fd
, value
, mask
);
65 int read_attr_fd(int fd
, unsigned *ret
) {
70 if (fstat(fd
, &st
) < 0)
73 if (!S_ISDIR(st
.st_mode
) && !S_ISREG(st
.st_mode
))
76 if (ioctl(fd
, FS_IOC_GETFLAGS
, ret
) < 0)
82 int read_attr_path(const char *p
, unsigned *ret
) {
83 _cleanup_close_
int fd
= -1;
88 fd
= open(p
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
92 return read_attr_fd(fd
, ret
);