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 | |
Al Viro | 39b743c | 2010-06-06 20:08:56 -0400 | [diff] [blame] | 22 | static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p) |
| 23 | { |
| 24 | p->ino = buf->st_ino; |
| 25 | p->mode = buf->st_mode; |
| 26 | p->nlink = buf->st_nlink; |
| 27 | p->uid = buf->st_uid; |
| 28 | p->gid = buf->st_gid; |
| 29 | p->size = buf->st_size; |
| 30 | p->atime.tv_sec = buf->st_atime; |
| 31 | p->atime.tv_nsec = 0; |
| 32 | p->ctime.tv_sec = buf->st_ctime; |
| 33 | p->ctime.tv_nsec = 0; |
| 34 | p->mtime.tv_sec = buf->st_mtime; |
| 35 | p->mtime.tv_nsec = 0; |
| 36 | p->blksize = buf->st_blksize; |
| 37 | p->blocks = buf->st_blocks; |
| 38 | p->maj = os_major(buf->st_rdev); |
| 39 | p->min = os_minor(buf->st_rdev); |
| 40 | } |
| 41 | |
| 42 | int stat_file(const char *path, struct hostfs_stat *p, int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | struct stat64 buf; |
| 45 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 46 | if (fd >= 0) { |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 47 | if (fstat64(fd, &buf) < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 48 | return -errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 49 | } else if (lstat64(path, &buf) < 0) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 50 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 51 | } |
Al Viro | 39b743c | 2010-06-06 20:08:56 -0400 | [diff] [blame] | 52 | stat64_to_hostfs(&buf, p); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 53 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | int access_file(char *path, int r, int w, int x) |
| 57 | { |
| 58 | int mode = 0; |
| 59 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 60 | if (r) |
| 61 | mode = R_OK; |
| 62 | if (w) |
| 63 | mode |= W_OK; |
| 64 | if (x) |
| 65 | mode |= X_OK; |
| 66 | if (access(path, mode) != 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 67 | return -errno; |
| 68 | else return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int open_file(char *path, int r, int w, int append) |
| 72 | { |
| 73 | int mode = 0, fd; |
| 74 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 75 | if (r && !w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | mode = O_RDONLY; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 77 | else if (!r && w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | mode = O_WRONLY; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 79 | else if (r && w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | mode = O_RDWR; |
| 81 | else panic("Impossible mode in open_file"); |
| 82 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 83 | if (append) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | mode |= O_APPEND; |
| 85 | fd = open64(path, mode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 86 | if (fd < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 87 | return -errno; |
| 88 | else return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void *open_dir(char *path, int *err_out) |
| 92 | { |
| 93 | DIR *dir; |
| 94 | |
| 95 | dir = opendir(path); |
| 96 | *err_out = errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 97 | if (dir == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 98 | return NULL; |
| 99 | return dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | char *read_dir(void *stream, unsigned long long *pos, |
| 103 | unsigned long long *ino_out, int *len_out) |
| 104 | { |
| 105 | DIR *dir = stream; |
| 106 | struct dirent *ent; |
| 107 | |
| 108 | seekdir(dir, *pos); |
| 109 | ent = readdir(dir); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 110 | if (ent == NULL) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 111 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | *len_out = strlen(ent->d_name); |
| 113 | *ino_out = ent->d_ino; |
| 114 | *pos = telldir(dir); |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 115 | return ent->d_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 123 | if (n < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 124 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | *offset += n; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 126 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 134 | if (n < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 135 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | *offset += n; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 137 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | int lseek_file(int fd, long long offset, int whence) |
| 141 | { |
| 142 | int ret; |
| 143 | |
| 144 | ret = lseek64(fd, offset, whence); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 145 | if (ret < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 146 | return -errno; |
| 147 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Paolo 'Blaisorblade' Giarrusso | a2d76bd | 2005-07-28 21:16:15 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | void close_file(void *stream) |
| 164 | { |
| 165 | close(*((int *) stream)); |
| 166 | } |
| 167 | |
| 168 | void close_dir(void *stream) |
| 169 | { |
| 170 | closedir(stream); |
| 171 | } |
| 172 | |
| 173 | int file_create(char *name, int ur, int uw, int ux, int gr, |
| 174 | int gw, int gx, int or, int ow, int ox) |
| 175 | { |
| 176 | int mode, fd; |
| 177 | |
| 178 | mode = 0; |
| 179 | mode |= ur ? S_IRUSR : 0; |
| 180 | mode |= uw ? S_IWUSR : 0; |
| 181 | mode |= ux ? S_IXUSR : 0; |
| 182 | mode |= gr ? S_IRGRP : 0; |
| 183 | mode |= gw ? S_IWGRP : 0; |
| 184 | mode |= gx ? S_IXGRP : 0; |
| 185 | mode |= or ? S_IROTH : 0; |
| 186 | mode |= ow ? S_IWOTH : 0; |
| 187 | mode |= ox ? S_IXOTH : 0; |
| 188 | fd = open64(name, O_CREAT | O_RDWR, mode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 189 | if (fd < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 190 | return -errno; |
| 191 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 194 | int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Al Viro | 39b743c | 2010-06-06 20:08:56 -0400 | [diff] [blame] | 196 | struct hostfs_stat st; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 197 | struct timeval times[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | int err, ma; |
| 199 | |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 200 | if (attrs->ia_valid & HOSTFS_ATTR_MODE) { |
| 201 | if (fd >= 0) { |
| 202 | if (fchmod(fd, attrs->ia_mode) != 0) |
| 203 | return (-errno); |
| 204 | } else if (chmod(file, attrs->ia_mode) != 0) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 205 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 208 | if (attrs->ia_valid & HOSTFS_ATTR_UID) { |
| 209 | if (fd >= 0) { |
| 210 | if (fchown(fd, attrs->ia_uid, -1)) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 211 | return -errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 212 | } else if (chown(file, attrs->ia_uid, -1)) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 213 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 216 | if (attrs->ia_valid & HOSTFS_ATTR_GID) { |
| 217 | if (fd >= 0) { |
| 218 | if (fchown(fd, -1, attrs->ia_gid)) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 219 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 220 | } else if (chown(file, -1, attrs->ia_gid)) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 221 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 224 | if (attrs->ia_valid & HOSTFS_ATTR_SIZE) { |
| 225 | if (fd >= 0) { |
| 226 | if (ftruncate(fd, attrs->ia_size)) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 227 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 228 | } else if (truncate(file, attrs->ia_size)) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 229 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 233 | /* |
| 234 | * Update accessed and/or modified time, in two parts: first set |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 235 | * times according to the changes to perform, and then call futimes() |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 236 | * or utimes() to apply them. |
| 237 | */ |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 238 | ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET); |
| 239 | if (attrs->ia_valid & ma) { |
Al Viro | 39b743c | 2010-06-06 20:08:56 -0400 | [diff] [blame] | 240 | err = stat_file(file, &st, fd); |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 241 | if (err != 0) |
| 242 | return err; |
| 243 | |
Al Viro | 39b743c | 2010-06-06 20:08:56 -0400 | [diff] [blame] | 244 | times[0].tv_sec = st.atime.tv_sec; |
| 245 | times[0].tv_usec = st.atime.tv_nsec / 1000; |
| 246 | times[1].tv_sec = st.mtime.tv_sec; |
| 247 | times[1].tv_usec = st.mtime.tv_nsec / 1000; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 248 | |
| 249 | if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) { |
| 250 | times[0].tv_sec = attrs->ia_atime.tv_sec; |
Dominique Quatravaux | d7b8851 | 2008-02-04 22:31:15 -0800 | [diff] [blame] | 251 | times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 253 | if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) { |
| 254 | times[1].tv_sec = attrs->ia_mtime.tv_sec; |
Dominique Quatravaux | d7b8851 | 2008-02-04 22:31:15 -0800 | [diff] [blame] | 255 | times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | if (fd >= 0) { |
| 259 | if (futimes(fd, times) != 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 260 | return -errno; |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 261 | } else if (utimes(file, times) != 0) { |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 262 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
Alberto Bertogli | 5822b7f | 2007-05-08 00:23:16 -0700 | [diff] [blame] | 265 | |
Jeff Dike | baabd15 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 266 | /* Note: ctime is not handled */ |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 267 | if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) { |
Al Viro | 39b743c | 2010-06-06 20:08:56 -0400 | [diff] [blame] | 268 | err = stat_file(file, &st, fd); |
| 269 | attrs->ia_atime = st.atime; |
| 270 | attrs->ia_mtime = st.mtime; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 271 | if (err != 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 272 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 274 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | int make_symlink(const char *from, const char *to) |
| 278 | { |
| 279 | int err; |
| 280 | |
| 281 | err = symlink(to, from); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 282 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 283 | return -errno; |
| 284 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | int unlink_file(const char *file) |
| 288 | { |
| 289 | int err; |
| 290 | |
| 291 | err = unlink(file); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 292 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 293 | return -errno; |
| 294 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | int do_mkdir(const char *file, int mode) |
| 298 | { |
| 299 | int err; |
| 300 | |
| 301 | err = mkdir(file, mode); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 302 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 303 | return -errno; |
| 304 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | int do_rmdir(const char *file) |
| 308 | { |
| 309 | int err; |
| 310 | |
| 311 | err = rmdir(file); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 312 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 313 | return -errno; |
| 314 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Johannes Stezenbach | 88f6cd0 | 2007-01-29 13:19:44 -0800 | [diff] [blame] | 317 | 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] | 318 | { |
| 319 | int err; |
| 320 | |
Al Viro | 005a59e | 2009-04-21 01:27:08 -0400 | [diff] [blame] | 321 | err = mknod(file, mode, os_makedev(major, minor)); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 322 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 323 | return -errno; |
| 324 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int link_file(const char *to, const char *from) |
| 328 | { |
| 329 | int err; |
| 330 | |
| 331 | err = link(to, from); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 332 | if (err) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 333 | return -errno; |
| 334 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
WANG Cong | ea7e743 | 2008-11-19 15:36:46 -0800 | [diff] [blame] | 337 | int hostfs_do_readlink(char *file, char *buf, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
| 339 | int n; |
| 340 | |
| 341 | n = readlink(file, buf, size); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 342 | if (n < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 343 | return -errno; |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 344 | if (n < size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | buf[n] = '\0'; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 346 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | int rename_file(char *from, char *to) |
| 350 | { |
| 351 | int err; |
| 352 | |
| 353 | err = rename(from, to); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 354 | if (err < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 355 | return -errno; |
| 356 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | int do_statfs(char *root, long *bsize_out, long long *blocks_out, |
| 360 | long long *bfree_out, long long *bavail_out, |
| 361 | long long *files_out, long long *ffree_out, |
| 362 | void *fsid_out, int fsid_size, long *namelen_out, |
| 363 | long *spare_out) |
| 364 | { |
| 365 | struct statfs64 buf; |
| 366 | int err; |
| 367 | |
| 368 | err = statfs64(root, &buf); |
Jeff Dike | 84b3db0 | 2007-10-16 01:27:13 -0700 | [diff] [blame] | 369 | if (err < 0) |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 370 | return -errno; |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | *bsize_out = buf.f_bsize; |
| 373 | *blocks_out = buf.f_blocks; |
| 374 | *bfree_out = buf.f_bfree; |
| 375 | *bavail_out = buf.f_bavail; |
| 376 | *files_out = buf.f_files; |
| 377 | *ffree_out = buf.f_ffree; |
| 378 | memcpy(fsid_out, &buf.f_fsid, |
| 379 | sizeof(buf.f_fsid) > fsid_size ? fsid_size : |
| 380 | sizeof(buf.f_fsid)); |
| 381 | *namelen_out = buf.f_namelen; |
| 382 | spare_out[0] = buf.f_spare[0]; |
| 383 | spare_out[1] = buf.f_spare[1]; |
| 384 | spare_out[2] = buf.f_spare[2]; |
| 385 | spare_out[3] = buf.f_spare[3]; |
| 386 | spare_out[4] = buf.f_spare[4]; |
Jeff Dike | f1adc05 | 2007-05-08 00:23:18 -0700 | [diff] [blame] | 387 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |