]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/xattr-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
22 #include <sys/xattr.h>
24 #include "alloc-util.h"
26 #include "sparse-endian.h"
27 #include "stdio-util.h"
29 #include "xattr-util.h"
31 int getxattr_malloc(const char *path
, const char *name
, char **value
, bool allow_symlink
) {
40 for (l
= 100; ; l
= (size_t) n
+ 1) {
46 n
= lgetxattr(path
, name
, v
, l
);
48 n
= getxattr(path
, name
, v
, l
);
50 if (n
>= 0 && (size_t) n
< l
) {
57 if (n
< 0 && errno
!= ERANGE
)
61 n
= lgetxattr(path
, name
, NULL
, 0);
63 n
= getxattr(path
, name
, NULL
, 0);
69 int fgetxattr_malloc(int fd
, const char *name
, char **value
) {
78 for (l
= 100; ; l
= (size_t) n
+ 1) {
83 n
= fgetxattr(fd
, name
, v
, l
);
85 if (n
>= 0 && (size_t) n
< l
) {
92 if (n
< 0 && errno
!= ERANGE
)
95 n
= fgetxattr(fd
, name
, NULL
, 0);
101 ssize_t
fgetxattrat_fake(int dirfd
, const char *filename
, const char *attribute
, void *value
, size_t size
, int flags
) {
102 char fn
[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
103 _cleanup_close_
int fd
= -1;
106 /* The kernel doesn't have a fgetxattrat() command, hence let's emulate one */
108 fd
= openat(dirfd
, filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_PATH
|(flags
& AT_SYMLINK_NOFOLLOW
? O_NOFOLLOW
: 0));
112 xsprintf(fn
, "/proc/self/fd/%i", fd
);
114 l
= getxattr(fn
, attribute
, value
, size
);
121 static int parse_crtime(le64_t le
, usec_t
*usec
) {
127 if (u
== 0 || u
== (uint64_t) -1)
134 int fd_getcrtime(int fd
, usec_t
*usec
) {
141 /* Until Linux gets a real concept of birthtime/creation time,
142 * let's fake one with xattrs */
144 n
= fgetxattr(fd
, "user.crtime_usec", &le
, sizeof(le
));
150 return parse_crtime(le
, usec
);
153 int fd_getcrtime_at(int dirfd
, const char *name
, usec_t
*usec
, int flags
) {
157 n
= fgetxattrat_fake(dirfd
, name
, "user.crtime_usec", &le
, sizeof(le
), flags
);
163 return parse_crtime(le
, usec
);
166 int path_getcrtime(const char *p
, usec_t
*usec
) {
173 n
= getxattr(p
, "user.crtime_usec", &le
, sizeof(le
));
179 return parse_crtime(le
, usec
);
182 int fd_setcrtime(int fd
, usec_t usec
) {
188 usec
= now(CLOCK_REALTIME
);
190 le
= htole64((uint64_t) usec
);
191 if (fsetxattr(fd
, "user.crtime_usec", &le
, sizeof(le
), 0) < 0)