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