Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <dirent.h> |
| 10 | #include <errno.h> |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 11 | #include <fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/time.h> |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 15 | #include <sys/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <sys/vfs.h> |
| 17 | #include "hostfs.h" |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 18 | #include "os.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "user.h" |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 20 | #include <utime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | int stat_file(const char *path, unsigned long long *inode_out, int *mode_out, |
| 23 | int *nlink_out, int *uid_out, int *gid_out, |
| 24 | unsigned long long *size_out, struct timespec *atime_out, |
| 25 | struct timespec *mtime_out, struct timespec *ctime_out, |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 26 | int *blksize_out, unsigned long long *blocks_out, int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | struct stat64 buf; |
| 29 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 30 | if (fd >= 0) { |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 31 | if (fstat64(fd, &buf) < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 32 | return -errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 33 | } else if (lstat64(path, &buf) < 0) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 34 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 35 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 37 | if (inode_out != NULL) |
| 38 | *inode_out = buf.st_ino; |
| 39 | if (mode_out != NULL) |
| 40 | *mode_out = buf.st_mode; |
| 41 | if (nlink_out != NULL) |
| 42 | *nlink_out = buf.st_nlink; |
| 43 | if (uid_out != NULL) |
| 44 | *uid_out = buf.st_uid; |
| 45 | if (gid_out != NULL) |
| 46 | *gid_out = buf.st_gid; |
| 47 | if (size_out != NULL) |
| 48 | *size_out = buf.st_size; |
| 49 | if (atime_out != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | atime_out->tv_sec = buf.st_atime; |
| 51 | atime_out->tv_nsec = 0; |
| 52 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 53 | if (mtime_out != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | mtime_out->tv_sec = buf.st_mtime; |
| 55 | mtime_out->tv_nsec = 0; |
| 56 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 57 | if (ctime_out != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | ctime_out->tv_sec = buf.st_ctime; |
| 59 | ctime_out->tv_nsec = 0; |
| 60 | } |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 61 | if (blksize_out != NULL) |
| 62 | *blksize_out = buf.st_blksize; |
| 63 | if (blocks_out != NULL) |
| 64 | *blocks_out = buf.st_blocks; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 65 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int file_type(const char *path, int *maj, int *min) |
| 69 | { |
| 70 | struct stat64 buf; |
| 71 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 72 | if (lstat64(path, &buf) < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 73 | return -errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 74 | /* |
| 75 | * We cannot pass rdev as is because glibc and the kernel disagree |
| 76 | * about its definition. |
| 77 | */ |
| 78 | if (maj != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | *maj = major(buf.st_rdev); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 80 | if (min != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | *min = minor(buf.st_rdev); |
| 82 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 83 | if (S_ISDIR(buf.st_mode)) |
| 84 | return OS_TYPE_DIR; |
| 85 | else if (S_ISLNK(buf.st_mode)) |
| 86 | return OS_TYPE_SYMLINK; |
| 87 | else if (S_ISCHR(buf.st_mode)) |
| 88 | return OS_TYPE_CHARDEV; |
| 89 | else if (S_ISBLK(buf.st_mode)) |
| 90 | return OS_TYPE_BLOCKDEV; |
| 91 | else if (S_ISFIFO(buf.st_mode)) |
| 92 | return OS_TYPE_FIFO; |
| 93 | else if (S_ISSOCK(buf.st_mode)) |
| 94 | return OS_TYPE_SOCK; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 95 | else return OS_TYPE_FILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | int access_file(char *path, int r, int w, int x) |
| 99 | { |
| 100 | int mode = 0; |
| 101 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 102 | if (r) |
| 103 | mode = R_OK; |
| 104 | if (w) |
| 105 | mode |= W_OK; |
| 106 | if (x) |
| 107 | mode |= X_OK; |
| 108 | if (access(path, mode) != 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 109 | return -errno; |
| 110 | else return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int open_file(char *path, int r, int w, int append) |
| 114 | { |
| 115 | int mode = 0, fd; |
| 116 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 117 | if (r && !w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | mode = O_RDONLY; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 119 | else if (!r && w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | mode = O_WRONLY; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 121 | else if (r && w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | mode = O_RDWR; |
| 123 | else panic("Impossible mode in open_file"); |
| 124 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 125 | if (append) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | mode |= O_APPEND; |
| 127 | fd = open64(path, mode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 128 | if (fd < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 129 | return -errno; |
| 130 | else return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void *open_dir(char *path, int *err_out) |
| 134 | { |
| 135 | DIR *dir; |
| 136 | |
| 137 | dir = opendir(path); |
| 138 | *err_out = errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 139 | if (dir == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 140 | return NULL; |
| 141 | return dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | char *read_dir(void *stream, unsigned long long *pos, |
| 145 | unsigned long long *ino_out, int *len_out) |
| 146 | { |
| 147 | DIR *dir = stream; |
| 148 | struct dirent *ent; |
| 149 | |
| 150 | seekdir(dir, *pos); |
| 151 | ent = readdir(dir); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 152 | if (ent == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 153 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | *len_out = strlen(ent->d_name); |
| 155 | *ino_out = ent->d_ino; |
| 156 | *pos = telldir(dir); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 157 | return ent->d_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int read_file(int fd, unsigned long long *offset, char *buf, int len) |
| 161 | { |
| 162 | int n; |
| 163 | |
| 164 | n = pread64(fd, buf, len, *offset); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 165 | if (n < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 166 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | *offset += n; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 168 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | int write_file(int fd, unsigned long long *offset, const char *buf, int len) |
| 172 | { |
| 173 | int n; |
| 174 | |
| 175 | n = pwrite64(fd, buf, len, *offset); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 176 | if (n < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 177 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | *offset += n; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 179 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int lseek_file(int fd, long long offset, int whence) |
| 183 | { |
| 184 | int ret; |
| 185 | |
| 186 | ret = lseek64(fd, offset, whence); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 187 | if (ret < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 188 | return -errno; |
| 189 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Paolo 'Blaisorblade' Giarrusso | a2d76bd | 2005-07-28 21:16:15 -0700 | [diff] [blame] | 192 | int fsync_file(int fd, int datasync) |
| 193 | { |
| 194 | int ret; |
| 195 | if (datasync) |
| 196 | ret = fdatasync(fd); |
| 197 | else |
| 198 | ret = fsync(fd); |
| 199 | |
| 200 | if (ret < 0) |
| 201 | return -errno; |
| 202 | return 0; |
| 203 | } |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | void close_file(void *stream) |
| 206 | { |
| 207 | close(*((int *) stream)); |
| 208 | } |
| 209 | |
| 210 | void close_dir(void *stream) |
| 211 | { |
| 212 | closedir(stream); |
| 213 | } |
| 214 | |
| 215 | int file_create(char *name, int ur, int uw, int ux, int gr, |
| 216 | int gw, int gx, int or, int ow, int ox) |
| 217 | { |
| 218 | int mode, fd; |
| 219 | |
| 220 | mode = 0; |
| 221 | mode |= ur ? S_IRUSR : 0; |
| 222 | mode |= uw ? S_IWUSR : 0; |
| 223 | mode |= ux ? S_IXUSR : 0; |
| 224 | mode |= gr ? S_IRGRP : 0; |
| 225 | mode |= gw ? S_IWGRP : 0; |
| 226 | mode |= gx ? S_IXGRP : 0; |
| 227 | mode |= or ? S_IROTH : 0; |
| 228 | mode |= ow ? S_IWOTH : 0; |
| 229 | mode |= ox ? S_IXOTH : 0; |
| 230 | fd = open64(name, O_CREAT | O_RDWR, mode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 231 | if (fd < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 232 | return -errno; |
| 233 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 236 | int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 238 | struct timeval times[2]; |
| 239 | struct timespec atime_ts, mtime_ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | int err, ma; |
| 241 | |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 242 | if (attrs->ia_valid & HOSTFS_ATTR_MODE) { |
| 243 | if (fd >= 0) { |
| 244 | if (fchmod(fd, attrs->ia_mode) != 0) |
| 245 | return (-errno); |
| 246 | } else if (chmod(file, attrs->ia_mode) != 0) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 247 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 250 | if (attrs->ia_valid & HOSTFS_ATTR_UID) { |
| 251 | if (fd >= 0) { |
| 252 | if (fchown(fd, attrs->ia_uid, -1)) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 253 | return -errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 254 | } else if (chown(file, attrs->ia_uid, -1)) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 255 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 258 | if (attrs->ia_valid & HOSTFS_ATTR_GID) { |
| 259 | if (fd >= 0) { |
| 260 | if (fchown(fd, -1, attrs->ia_gid)) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 261 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 262 | } else if (chown(file, -1, attrs->ia_gid)) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 263 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 266 | if (attrs->ia_valid & HOSTFS_ATTR_SIZE) { |
| 267 | if (fd >= 0) { |
| 268 | if (ftruncate(fd, attrs->ia_size)) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 269 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 270 | } else if (truncate(file, attrs->ia_size)) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 271 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 275 | /* |
| 276 | * Update accessed and/or modified time, in two parts: first set |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 277 | * times according to the changes to perform, and then call futimes() |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 278 | * or utimes() to apply them. |
| 279 | */ |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 280 | ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET); |
| 281 | if (attrs->ia_valid & ma) { |
| 282 | err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, |
| 283 | &atime_ts, &mtime_ts, NULL, NULL, NULL, fd); |
| 284 | if (err != 0) |
| 285 | return err; |
| 286 | |
| 287 | times[0].tv_sec = atime_ts.tv_sec; |
| 288 | times[0].tv_usec = atime_ts.tv_nsec * 1000; |
| 289 | times[1].tv_sec = mtime_ts.tv_sec; |
| 290 | times[1].tv_usec = mtime_ts.tv_nsec * 1000; |
| 291 | |
| 292 | if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) { |
| 293 | times[0].tv_sec = attrs->ia_atime.tv_sec; |
| 294 | times[0].tv_usec = attrs->ia_atime.tv_nsec * 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 296 | if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) { |
| 297 | times[1].tv_sec = attrs->ia_mtime.tv_sec; |
| 298 | times[1].tv_usec = attrs->ia_mtime.tv_nsec * 1000; |
| 299 | } |
| 300 | |
| 301 | if (fd >= 0) { |
| 302 | if (futimes(fd, times) != 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 303 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 304 | } else if (utimes(file, times) != 0) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 305 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 308 | |
Jeff Dike | baabd15 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 309 | /* Note: ctime is not handled */ |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 310 | if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, |
| 312 | &attrs->ia_atime, &attrs->ia_mtime, NULL, |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 313 | NULL, NULL, fd); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 314 | if (err != 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 315 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 317 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | int make_symlink(const char *from, const char *to) |
| 321 | { |
| 322 | int err; |
| 323 | |
| 324 | err = symlink(to, from); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 325 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 326 | return -errno; |
| 327 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | int unlink_file(const char *file) |
| 331 | { |
| 332 | int err; |
| 333 | |
| 334 | err = unlink(file); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 335 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 336 | return -errno; |
| 337 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | int do_mkdir(const char *file, int mode) |
| 341 | { |
| 342 | int err; |
| 343 | |
| 344 | err = mkdir(file, mode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 345 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 346 | return -errno; |
| 347 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int do_rmdir(const char *file) |
| 351 | { |
| 352 | int err; |
| 353 | |
| 354 | err = rmdir(file); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 355 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 356 | return -errno; |
| 357 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Johannes Stezenbach | 88f6cd0 | 2007-01-29 13:19:44 -0800 | [diff] [blame] | 360 | int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
| 362 | int err; |
| 363 | |
Johannes Stezenbach | 88f6cd0 | 2007-01-29 13:19:44 -0800 | [diff] [blame] | 364 | err = mknod(file, mode, makedev(major, minor)); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 365 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 366 | return -errno; |
| 367 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | int link_file(const char *to, const char *from) |
| 371 | { |
| 372 | int err; |
| 373 | |
| 374 | err = link(to, from); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 375 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 376 | return -errno; |
| 377 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | int do_readlink(char *file, char *buf, int size) |
| 381 | { |
| 382 | int n; |
| 383 | |
| 384 | n = readlink(file, buf, size); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 385 | if (n < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 386 | return -errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 387 | if (n < size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | buf[n] = '\0'; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 389 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | int rename_file(char *from, char *to) |
| 393 | { |
| 394 | int err; |
| 395 | |
| 396 | err = rename(from, to); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 397 | if (err < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 398 | return -errno; |
| 399 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | int do_statfs(char *root, long *bsize_out, long long *blocks_out, |
| 403 | long long *bfree_out, long long *bavail_out, |
| 404 | long long *files_out, long long *ffree_out, |
| 405 | void *fsid_out, int fsid_size, long *namelen_out, |
| 406 | long *spare_out) |
| 407 | { |
| 408 | struct statfs64 buf; |
| 409 | int err; |
| 410 | |
| 411 | err = statfs64(root, &buf); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 412 | if (err < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 413 | return -errno; |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | *bsize_out = buf.f_bsize; |
| 416 | *blocks_out = buf.f_blocks; |
| 417 | *bfree_out = buf.f_bfree; |
| 418 | *bavail_out = buf.f_bavail; |
| 419 | *files_out = buf.f_files; |
| 420 | *ffree_out = buf.f_ffree; |
| 421 | memcpy(fsid_out, &buf.f_fsid, |
| 422 | sizeof(buf.f_fsid) > fsid_size ? fsid_size : |
| 423 | sizeof(buf.f_fsid)); |
| 424 | *namelen_out = buf.f_namelen; |
| 425 | spare_out[0] = buf.f_spare[0]; |
| 426 | spare_out[1] = buf.f_spare[1]; |
| 427 | spare_out[2] = buf.f_spare[2]; |
| 428 | spare_out[3] = buf.f_spare[3]; |
| 429 | spare_out[4] = buf.f_spare[4]; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 430 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |