Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
f1adc05e | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
3 | * Licensed under the GPL |
4 | */ | |
5 | ||
1da177e4 | 6 | #include <stdio.h> |
84b3db04 JD |
7 | #include <stddef.h> |
8 | #include <unistd.h> | |
1da177e4 LT |
9 | #include <dirent.h> |
10 | #include <errno.h> | |
84b3db04 | 11 | #include <fcntl.h> |
1da177e4 LT |
12 | #include <string.h> |
13 | #include <sys/stat.h> | |
14 | #include <sys/time.h> | |
84b3db04 | 15 | #include <sys/types.h> |
1da177e4 | 16 | #include <sys/vfs.h> |
9a423bb6 | 17 | #include <sys/syscall.h> |
1da177e4 | 18 | #include "hostfs.h" |
84b3db04 | 19 | #include <utime.h> |
1da177e4 | 20 | |
39b743c6 AV |
21 | static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p) |
22 | { | |
23 | p->ino = buf->st_ino; | |
24 | p->mode = buf->st_mode; | |
25 | p->nlink = buf->st_nlink; | |
26 | p->uid = buf->st_uid; | |
27 | p->gid = buf->st_gid; | |
28 | p->size = buf->st_size; | |
29 | p->atime.tv_sec = buf->st_atime; | |
30 | p->atime.tv_nsec = 0; | |
31 | p->ctime.tv_sec = buf->st_ctime; | |
32 | p->ctime.tv_nsec = 0; | |
33 | p->mtime.tv_sec = buf->st_mtime; | |
34 | p->mtime.tv_nsec = 0; | |
35 | p->blksize = buf->st_blksize; | |
36 | p->blocks = buf->st_blocks; | |
37 | p->maj = os_major(buf->st_rdev); | |
38 | p->min = os_minor(buf->st_rdev); | |
39 | } | |
40 | ||
41 | int stat_file(const char *path, struct hostfs_stat *p, int fd) | |
1da177e4 LT |
42 | { |
43 | struct stat64 buf; | |
44 | ||
84b3db04 | 45 | if (fd >= 0) { |
5822b7fa | 46 | if (fstat64(fd, &buf) < 0) |
f1adc05e | 47 | return -errno; |
84b3db04 | 48 | } else if (lstat64(path, &buf) < 0) { |
f1adc05e | 49 | return -errno; |
5822b7fa | 50 | } |
39b743c6 | 51 | stat64_to_hostfs(&buf, p); |
f1adc05e | 52 | return 0; |
1da177e4 LT |
53 | } |
54 | ||
1da177e4 LT |
55 | int access_file(char *path, int r, int w, int x) |
56 | { | |
57 | int mode = 0; | |
58 | ||
84b3db04 JD |
59 | if (r) |
60 | mode = R_OK; | |
61 | if (w) | |
62 | mode |= W_OK; | |
63 | if (x) | |
64 | mode |= X_OK; | |
65 | if (access(path, mode) != 0) | |
f1adc05e JD |
66 | return -errno; |
67 | else return 0; | |
1da177e4 LT |
68 | } |
69 | ||
70 | int open_file(char *path, int r, int w, int append) | |
71 | { | |
72 | int mode = 0, fd; | |
73 | ||
84b3db04 | 74 | if (r && !w) |
1da177e4 | 75 | mode = O_RDONLY; |
84b3db04 | 76 | else if (!r && w) |
1da177e4 | 77 | mode = O_WRONLY; |
84b3db04 | 78 | else if (r && w) |
1da177e4 LT |
79 | mode = O_RDWR; |
80 | else panic("Impossible mode in open_file"); | |
81 | ||
84b3db04 | 82 | if (append) |
1da177e4 LT |
83 | mode |= O_APPEND; |
84 | fd = open64(path, mode); | |
84b3db04 | 85 | if (fd < 0) |
f1adc05e JD |
86 | return -errno; |
87 | else return fd; | |
1da177e4 LT |
88 | } |
89 | ||
90 | void *open_dir(char *path, int *err_out) | |
91 | { | |
92 | DIR *dir; | |
93 | ||
94 | dir = opendir(path); | |
95 | *err_out = errno; | |
c5c6dd4e | 96 | |
f1adc05e | 97 | return dir; |
1da177e4 LT |
98 | } |
99 | ||
100 | char *read_dir(void *stream, unsigned long long *pos, | |
3ee6bd8e GU |
101 | unsigned long long *ino_out, int *len_out, |
102 | unsigned int *type_out) | |
1da177e4 LT |
103 | { |
104 | DIR *dir = stream; | |
105 | struct dirent *ent; | |
106 | ||
107 | seekdir(dir, *pos); | |
108 | ent = readdir(dir); | |
84b3db04 | 109 | if (ent == NULL) |
f1adc05e | 110 | return NULL; |
1da177e4 LT |
111 | *len_out = strlen(ent->d_name); |
112 | *ino_out = ent->d_ino; | |
3ee6bd8e | 113 | *type_out = ent->d_type; |
1da177e4 | 114 | *pos = telldir(dir); |
f1adc05e | 115 | return ent->d_name; |
1da177e4 LT |
116 | } |
117 | ||
118 | int read_file(int fd, unsigned long long *offset, char *buf, int len) | |
119 | { | |
120 | int n; | |
121 | ||
122 | n = pread64(fd, buf, len, *offset); | |
84b3db04 | 123 | if (n < 0) |
f1adc05e | 124 | return -errno; |
1da177e4 | 125 | *offset += n; |
f1adc05e | 126 | return n; |
1da177e4 LT |
127 | } |
128 | ||
129 | int write_file(int fd, unsigned long long *offset, const char *buf, int len) | |
130 | { | |
131 | int n; | |
132 | ||
133 | n = pwrite64(fd, buf, len, *offset); | |
84b3db04 | 134 | if (n < 0) |
f1adc05e | 135 | return -errno; |
1da177e4 | 136 | *offset += n; |
f1adc05e | 137 | return n; |
1da177e4 LT |
138 | } |
139 | ||
140 | int lseek_file(int fd, long long offset, int whence) | |
141 | { | |
142 | int ret; | |
143 | ||
144 | ret = lseek64(fd, offset, whence); | |
84b3db04 | 145 | if (ret < 0) |
f1adc05e JD |
146 | return -errno; |
147 | return 0; | |
1da177e4 LT |
148 | } |
149 | ||
a2d76bd8 PBG |
150 | int fsync_file(int fd, int datasync) |
151 | { | |
152 | int ret; | |
153 | if (datasync) | |
154 | ret = fdatasync(fd); | |
155 | else | |
156 | ret = fsync(fd); | |
157 | ||
158 | if (ret < 0) | |
159 | return -errno; | |
160 | return 0; | |
161 | } | |
162 | ||
f8ad850f AV |
163 | int replace_file(int oldfd, int fd) |
164 | { | |
165 | return dup2(oldfd, fd); | |
166 | } | |
167 | ||
1da177e4 LT |
168 | void close_file(void *stream) |
169 | { | |
170 | close(*((int *) stream)); | |
171 | } | |
172 | ||
173 | void close_dir(void *stream) | |
174 | { | |
175 | closedir(stream); | |
176 | } | |
177 | ||
b98b9102 | 178 | int file_create(char *name, int mode) |
1da177e4 | 179 | { |
b98b9102 RW |
180 | int fd; |
181 | ||
1da177e4 | 182 | fd = open64(name, O_CREAT | O_RDWR, mode); |
84b3db04 | 183 | if (fd < 0) |
f1adc05e JD |
184 | return -errno; |
185 | return fd; | |
1da177e4 LT |
186 | } |
187 | ||
5822b7fa | 188 | int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) |
1da177e4 | 189 | { |
39b743c6 | 190 | struct hostfs_stat st; |
5822b7fa | 191 | struct timeval times[2]; |
1da177e4 LT |
192 | int err, ma; |
193 | ||
5822b7fa AB |
194 | if (attrs->ia_valid & HOSTFS_ATTR_MODE) { |
195 | if (fd >= 0) { | |
196 | if (fchmod(fd, attrs->ia_mode) != 0) | |
c5c6dd4e | 197 | return -errno; |
5822b7fa | 198 | } else if (chmod(file, attrs->ia_mode) != 0) { |
f1adc05e | 199 | return -errno; |
5822b7fa | 200 | } |
1da177e4 | 201 | } |
5822b7fa AB |
202 | if (attrs->ia_valid & HOSTFS_ATTR_UID) { |
203 | if (fd >= 0) { | |
204 | if (fchown(fd, attrs->ia_uid, -1)) | |
f1adc05e | 205 | return -errno; |
84b3db04 | 206 | } else if (chown(file, attrs->ia_uid, -1)) { |
f1adc05e | 207 | return -errno; |
5822b7fa | 208 | } |
1da177e4 | 209 | } |
5822b7fa AB |
210 | if (attrs->ia_valid & HOSTFS_ATTR_GID) { |
211 | if (fd >= 0) { | |
212 | if (fchown(fd, -1, attrs->ia_gid)) | |
f1adc05e | 213 | return -errno; |
5822b7fa | 214 | } else if (chown(file, -1, attrs->ia_gid)) { |
f1adc05e | 215 | return -errno; |
5822b7fa | 216 | } |
1da177e4 | 217 | } |
5822b7fa AB |
218 | if (attrs->ia_valid & HOSTFS_ATTR_SIZE) { |
219 | if (fd >= 0) { | |
220 | if (ftruncate(fd, attrs->ia_size)) | |
f1adc05e | 221 | return -errno; |
5822b7fa | 222 | } else if (truncate(file, attrs->ia_size)) { |
f1adc05e | 223 | return -errno; |
5822b7fa | 224 | } |
1da177e4 | 225 | } |
5822b7fa | 226 | |
84b3db04 JD |
227 | /* |
228 | * Update accessed and/or modified time, in two parts: first set | |
5822b7fa | 229 | * times according to the changes to perform, and then call futimes() |
84b3db04 JD |
230 | * or utimes() to apply them. |
231 | */ | |
5822b7fa AB |
232 | ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET); |
233 | if (attrs->ia_valid & ma) { | |
39b743c6 | 234 | err = stat_file(file, &st, fd); |
5822b7fa AB |
235 | if (err != 0) |
236 | return err; | |
237 | ||
39b743c6 AV |
238 | times[0].tv_sec = st.atime.tv_sec; |
239 | times[0].tv_usec = st.atime.tv_nsec / 1000; | |
240 | times[1].tv_sec = st.mtime.tv_sec; | |
241 | times[1].tv_usec = st.mtime.tv_nsec / 1000; | |
5822b7fa AB |
242 | |
243 | if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) { | |
244 | times[0].tv_sec = attrs->ia_atime.tv_sec; | |
d7b88513 | 245 | times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000; |
1da177e4 | 246 | } |
5822b7fa AB |
247 | if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) { |
248 | times[1].tv_sec = attrs->ia_mtime.tv_sec; | |
d7b88513 | 249 | times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000; |
5822b7fa AB |
250 | } |
251 | ||
252 | if (fd >= 0) { | |
253 | if (futimes(fd, times) != 0) | |
f1adc05e | 254 | return -errno; |
5822b7fa | 255 | } else if (utimes(file, times) != 0) { |
f1adc05e | 256 | return -errno; |
1da177e4 LT |
257 | } |
258 | } | |
5822b7fa | 259 | |
baabd156 | 260 | /* Note: ctime is not handled */ |
84b3db04 | 261 | if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) { |
39b743c6 AV |
262 | err = stat_file(file, &st, fd); |
263 | attrs->ia_atime = st.atime; | |
264 | attrs->ia_mtime = st.mtime; | |
84b3db04 | 265 | if (err != 0) |
f1adc05e | 266 | return err; |
1da177e4 | 267 | } |
f1adc05e | 268 | return 0; |
1da177e4 LT |
269 | } |
270 | ||
271 | int make_symlink(const char *from, const char *to) | |
272 | { | |
273 | int err; | |
274 | ||
275 | err = symlink(to, from); | |
84b3db04 | 276 | if (err) |
f1adc05e JD |
277 | return -errno; |
278 | return 0; | |
1da177e4 LT |
279 | } |
280 | ||
281 | int unlink_file(const char *file) | |
282 | { | |
283 | int err; | |
284 | ||
285 | err = unlink(file); | |
84b3db04 | 286 | if (err) |
f1adc05e JD |
287 | return -errno; |
288 | return 0; | |
1da177e4 LT |
289 | } |
290 | ||
291 | int do_mkdir(const char *file, int mode) | |
292 | { | |
293 | int err; | |
294 | ||
295 | err = mkdir(file, mode); | |
84b3db04 | 296 | if (err) |
f1adc05e JD |
297 | return -errno; |
298 | return 0; | |
1da177e4 LT |
299 | } |
300 | ||
301 | int do_rmdir(const char *file) | |
302 | { | |
303 | int err; | |
304 | ||
305 | err = rmdir(file); | |
84b3db04 | 306 | if (err) |
f1adc05e JD |
307 | return -errno; |
308 | return 0; | |
1da177e4 LT |
309 | } |
310 | ||
88f6cd0c | 311 | int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor) |
1da177e4 LT |
312 | { |
313 | int err; | |
314 | ||
005a59ec | 315 | err = mknod(file, mode, os_makedev(major, minor)); |
84b3db04 | 316 | if (err) |
f1adc05e JD |
317 | return -errno; |
318 | return 0; | |
1da177e4 LT |
319 | } |
320 | ||
321 | int link_file(const char *to, const char *from) | |
322 | { | |
323 | int err; | |
324 | ||
325 | err = link(to, from); | |
84b3db04 | 326 | if (err) |
f1adc05e JD |
327 | return -errno; |
328 | return 0; | |
1da177e4 LT |
329 | } |
330 | ||
ea7e743e | 331 | int hostfs_do_readlink(char *file, char *buf, int size) |
1da177e4 LT |
332 | { |
333 | int n; | |
334 | ||
335 | n = readlink(file, buf, size); | |
84b3db04 | 336 | if (n < 0) |
f1adc05e | 337 | return -errno; |
84b3db04 | 338 | if (n < size) |
1da177e4 | 339 | buf[n] = '\0'; |
f1adc05e | 340 | return n; |
1da177e4 LT |
341 | } |
342 | ||
343 | int rename_file(char *from, char *to) | |
344 | { | |
345 | int err; | |
346 | ||
347 | err = rename(from, to); | |
84b3db04 | 348 | if (err < 0) |
f1adc05e JD |
349 | return -errno; |
350 | return 0; | |
1da177e4 LT |
351 | } |
352 | ||
9a423bb6 MS |
353 | int rename2_file(char *from, char *to, unsigned int flags) |
354 | { | |
355 | int err; | |
356 | ||
357 | #ifndef SYS_renameat2 | |
358 | # ifdef __x86_64__ | |
359 | # define SYS_renameat2 316 | |
360 | # endif | |
361 | # ifdef __i386__ | |
362 | # define SYS_renameat2 353 | |
363 | # endif | |
364 | #endif | |
365 | ||
366 | #ifdef SYS_renameat2 | |
367 | err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags); | |
368 | if (err < 0) { | |
369 | if (errno != ENOSYS) | |
370 | return -errno; | |
371 | else | |
372 | return -EINVAL; | |
373 | } | |
374 | return 0; | |
375 | #else | |
376 | return -EINVAL; | |
377 | #endif | |
378 | } | |
379 | ||
1da177e4 LT |
380 | int do_statfs(char *root, long *bsize_out, long long *blocks_out, |
381 | long long *bfree_out, long long *bavail_out, | |
382 | long long *files_out, long long *ffree_out, | |
1b627d57 | 383 | void *fsid_out, int fsid_size, long *namelen_out) |
1da177e4 LT |
384 | { |
385 | struct statfs64 buf; | |
386 | int err; | |
387 | ||
388 | err = statfs64(root, &buf); | |
84b3db04 | 389 | if (err < 0) |
f1adc05e JD |
390 | return -errno; |
391 | ||
1da177e4 LT |
392 | *bsize_out = buf.f_bsize; |
393 | *blocks_out = buf.f_blocks; | |
394 | *bfree_out = buf.f_bfree; | |
395 | *bavail_out = buf.f_bavail; | |
396 | *files_out = buf.f_files; | |
397 | *ffree_out = buf.f_ffree; | |
398 | memcpy(fsid_out, &buf.f_fsid, | |
399 | sizeof(buf.f_fsid) > fsid_size ? fsid_size : | |
400 | sizeof(buf.f_fsid)); | |
401 | *namelen_out = buf.f_namelen; | |
1b627d57 | 402 | |
f1adc05e | 403 | return 0; |
1da177e4 | 404 | } |