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