]>
Commit | Line | Data |
---|---|---|
89a5a90c LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
11c3a366 TA |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
22 | #include <stdint.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/time.h> | |
89a5a90c LP |
26 | #include <sys/xattr.h> |
27 | ||
b5efdb8a | 28 | #include "alloc-util.h" |
89a5a90c | 29 | #include "fd-util.h" |
11c3a366 | 30 | #include "macro.h" |
93cc7779 | 31 | #include "sparse-endian.h" |
15a5e950 | 32 | #include "stdio-util.h" |
93cc7779 | 33 | #include "time-util.h" |
89a5a90c LP |
34 | #include "xattr-util.h" |
35 | ||
36 | int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink) { | |
37 | char *v; | |
38 | size_t l; | |
39 | ssize_t n; | |
40 | ||
41 | assert(path); | |
42 | assert(name); | |
43 | assert(value); | |
44 | ||
45 | for (l = 100; ; l = (size_t) n + 1) { | |
46 | v = new0(char, l); | |
47 | if (!v) | |
48 | return -ENOMEM; | |
49 | ||
50 | if (allow_symlink) | |
51 | n = lgetxattr(path, name, v, l); | |
52 | else | |
53 | n = getxattr(path, name, v, l); | |
54 | ||
55 | if (n >= 0 && (size_t) n < l) { | |
56 | *value = v; | |
57 | return n; | |
58 | } | |
59 | ||
60 | free(v); | |
61 | ||
62 | if (n < 0 && errno != ERANGE) | |
63 | return -errno; | |
64 | ||
65 | if (allow_symlink) | |
66 | n = lgetxattr(path, name, NULL, 0); | |
67 | else | |
68 | n = getxattr(path, name, NULL, 0); | |
69 | if (n < 0) | |
70 | return -errno; | |
71 | } | |
72 | } | |
73 | ||
74 | int fgetxattr_malloc(int fd, const char *name, char **value) { | |
75 | char *v; | |
76 | size_t l; | |
77 | ssize_t n; | |
78 | ||
79 | assert(fd >= 0); | |
80 | assert(name); | |
81 | assert(value); | |
82 | ||
83 | for (l = 100; ; l = (size_t) n + 1) { | |
84 | v = new0(char, l); | |
85 | if (!v) | |
86 | return -ENOMEM; | |
87 | ||
88 | n = fgetxattr(fd, name, v, l); | |
89 | ||
90 | if (n >= 0 && (size_t) n < l) { | |
91 | *value = v; | |
92 | return n; | |
93 | } | |
94 | ||
95 | free(v); | |
96 | ||
97 | if (n < 0 && errno != ERANGE) | |
98 | return -errno; | |
99 | ||
100 | n = fgetxattr(fd, name, NULL, 0); | |
101 | if (n < 0) | |
102 | return -errno; | |
103 | } | |
104 | } | |
105 | ||
106 | ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags) { | |
107 | char fn[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1]; | |
108 | _cleanup_close_ int fd = -1; | |
109 | ssize_t l; | |
110 | ||
111 | /* The kernel doesn't have a fgetxattrat() command, hence let's emulate one */ | |
112 | ||
c4b69156 | 113 | fd = openat(dirfd, filename, O_CLOEXEC|O_PATH|(flags & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW : 0)); |
89a5a90c LP |
114 | if (fd < 0) |
115 | return -errno; | |
116 | ||
117 | xsprintf(fn, "/proc/self/fd/%i", fd); | |
118 | ||
119 | l = getxattr(fn, attribute, value, size); | |
120 | if (l < 0) | |
121 | return -errno; | |
122 | ||
123 | return l; | |
124 | } | |
125 | ||
126 | static int parse_crtime(le64_t le, usec_t *usec) { | |
127 | uint64_t u; | |
128 | ||
129 | assert(usec); | |
130 | ||
131 | u = le64toh(le); | |
4c701096 | 132 | if (IN_SET(u, 0, (uint64_t) -1)) |
89a5a90c LP |
133 | return -EIO; |
134 | ||
135 | *usec = (usec_t) u; | |
136 | return 0; | |
137 | } | |
138 | ||
139 | int fd_getcrtime(int fd, usec_t *usec) { | |
140 | le64_t le; | |
141 | ssize_t n; | |
142 | ||
143 | assert(fd >= 0); | |
144 | assert(usec); | |
145 | ||
146 | /* Until Linux gets a real concept of birthtime/creation time, | |
147 | * let's fake one with xattrs */ | |
148 | ||
149 | n = fgetxattr(fd, "user.crtime_usec", &le, sizeof(le)); | |
150 | if (n < 0) | |
151 | return -errno; | |
152 | if (n != sizeof(le)) | |
153 | return -EIO; | |
154 | ||
155 | return parse_crtime(le, usec); | |
156 | } | |
157 | ||
158 | int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags) { | |
159 | le64_t le; | |
160 | ssize_t n; | |
161 | ||
162 | n = fgetxattrat_fake(dirfd, name, "user.crtime_usec", &le, sizeof(le), flags); | |
163 | if (n < 0) | |
164 | return -errno; | |
165 | if (n != sizeof(le)) | |
166 | return -EIO; | |
167 | ||
168 | return parse_crtime(le, usec); | |
169 | } | |
170 | ||
171 | int path_getcrtime(const char *p, usec_t *usec) { | |
172 | le64_t le; | |
173 | ssize_t n; | |
174 | ||
175 | assert(p); | |
176 | assert(usec); | |
177 | ||
178 | n = getxattr(p, "user.crtime_usec", &le, sizeof(le)); | |
179 | if (n < 0) | |
180 | return -errno; | |
181 | if (n != sizeof(le)) | |
182 | return -EIO; | |
183 | ||
184 | return parse_crtime(le, usec); | |
185 | } | |
186 | ||
187 | int fd_setcrtime(int fd, usec_t usec) { | |
188 | le64_t le; | |
189 | ||
190 | assert(fd >= 0); | |
191 | ||
192 | if (usec <= 0) | |
193 | usec = now(CLOCK_REALTIME); | |
194 | ||
195 | le = htole64((uint64_t) usec); | |
196 | if (fsetxattr(fd, "user.crtime_usec", &le, sizeof(le), 0) < 0) | |
197 | return -errno; | |
198 | ||
199 | return 0; | |
200 | } |