static int unionfs_getattr(const char *path, struct stat *stbuf) { DBG("%s\n", path); if (uopt.stats_enabled && strcmp(path, STATS_FILENAME) == 0) { memset(stbuf, 0, sizeof(stbuf)); stbuf->st_mode = S_IFREG | 0444; stbuf->st_nlink = 1; stbuf->st_size = STATS_SIZE; RETURN(0); } int i = find_rorw_branch(path); if (i == -1) RETURN(-errno); char p[PATHLEN_MAX]; if (BUILD_PATH(p, uopt.branches[i].path, path)) RETURN(-ENAMETOOLONG); int res = lstat(p, stbuf); if (res == -1) RETURN(-errno); /* This is a workaround for broken gnu find implementations. Actually, * n_links is not defined at all for directories by posix. However, it * seems to be common for filesystems to set it to one if the actual value * is unknown. Since nlink_t is unsigned and since these broken implementations * always substract 2 (for . and ..) this will cause an underflow, setting * it to max(nlink_t). */ if (S_ISDIR(stbuf->st_mode)) stbuf->st_nlink = 1; RETURN(0); }
static int unionfs_open(const char *path, struct fuse_file_info *fi) { DBG("%s\n", path); int i; if (fi->flags & (O_WRONLY | O_RDWR)) { i = find_rw_branch_cutlast(path); } else { i = find_rorw_branch(path); } if (i == -1) RETURN(-errno); char p[PATHLEN_MAX]; if (BUILD_PATH(p, uopt.branches[i].path, path)) RETURN(-ENAMETOOLONG); int fd = open(p, fi->flags); if (fd == -1) RETURN(-errno); if (fi->flags & (O_WRONLY | O_RDWR)) { // There might have been a hide file, but since we successfully // wrote to the real file, a hide file must not exist anymore remove_hidden(path, i); } // This makes exec() fail //fi->direct_io = 1; fi->fh = (unsigned long)fd; DBG("fd = %"PRIx64"\n", fi->fh); RETURN(0); }
/** * This is called *after* unlink() or rmdir(), create a whiteout file * if the same file/dir does exist in a lower branch */ int maybe_whiteout(const char *path, int branch_rw, enum whiteout mode) { DBG("%s\n", path); // we are not interested in the branch itself, only if it exists at all if (find_rorw_branch(path) != -1) { int res = do_create_whiteout(path, branch_rw, mode); RETURN(res); } RETURN(0); }
static int unionfs_listxattr(const char *path, char *list, size_t size) { DBG("%s\n", path); int i = find_rorw_branch(path); if (i == -1) RETURN(-errno); char p[PATHLEN_MAX]; if (BUILD_PATH(p, uopt.branches[i].path, path)) RETURN(-ENAMETOOLONG); int res = llistxattr(p, list, size); if (res == -1) RETURN(-errno); RETURN(res); }
static int unionfs_readlink(const char *path, char *buf, size_t size) { DBG("%s\n", path); int i = find_rorw_branch(path); if (i == -1) RETURN(-errno); char p[PATHLEN_MAX]; if (BUILD_PATH(p, uopt.branches[i].path, path)) RETURN(-ENAMETOOLONG); int res = readlink(p, buf, size - 1); if (res == -1) RETURN(-errno); buf[res] = '\0'; RETURN(0); }
static int unionfs_getxattr(const char *path, const char *name, char *value, size_t size, uint32_t position) { #else static int unionfs_getxattr(const char *path, const char *name, char *value, size_t size) { #endif DBG("%s\n", path); int i = find_rorw_branch(path); if (i == -1) RETURN(-errno); char p[PATHLEN_MAX]; if (BUILD_PATH(p, uopt.branches[i].path, path)) RETURN(-ENAMETOOLONG); #if __APPLE__ int res = getxattr(p, name, value, size, position, XATTR_NOFOLLOW); #else int res = lgetxattr(p, name, value, size); #endif if (res == -1) RETURN(-errno); RETURN(res); }
/** * unionfs rename function * TODO: If we rename a directory on a read-only branch, we need to copy over * all files to the renamed directory on the read-write branch. */ static int unionfs_rename(const char *from, const char *to) { DBG("from %s to %s\n", from, to); bool is_dir = false; // is 'from' a file or directory int j = find_rw_branch_cutlast(to); if (j == -1) RETURN(-errno); int i = find_rorw_branch(from); if (i == -1) RETURN(-errno); if (!uopt.branches[i].rw) { i = find_rw_branch_cow_common(from, true); if (i == -1) RETURN(-errno); } if (i != j) { USYSLOG(LOG_ERR, "%s: from and to are on different writable branches %d vs %d, which" "is not supported yet.\n", __func__, i, j); RETURN(-EXDEV); } char f[PATHLEN_MAX], t[PATHLEN_MAX]; if (BUILD_PATH(f, uopt.branches[i].path, from)) RETURN(-ENAMETOOLONG); if (BUILD_PATH(t, uopt.branches[i].path, to)) RETURN(-ENAMETOOLONG); filetype_t ftype = path_is_dir(f); if (ftype == NOT_EXISTING) RETURN(-ENOENT); else if (ftype == IS_DIR) is_dir = true; int res; if (!uopt.branches[i].rw) { // since original file is on a read-only branch, we copied the from file to a writable branch, // but since we will rename from, we also need to hide the from file on the read-only branch if (is_dir) res = hide_dir(from, i); else res = hide_file(from, i); if (res) RETURN(-errno); } res = rename(f, t); if (res == -1) { int err = errno; // unlink() might overwrite errno // if from was on a read-only branch we copied it, but now rename failed so we need to delete it if (!uopt.branches[i].rw) { if (unlink(f)) USYSLOG(LOG_ERR, "%s: cow of %s succeeded, but rename() failed and now " "also unlink() failed\n", __func__, from); if (remove_hidden(from, i)) USYSLOG(LOG_ERR, "%s: cow of %s succeeded, but rename() failed and now " "also removing the whiteout failed\n", __func__, from); } RETURN(-err); } if (uopt.branches[i].rw) { // A lower branch still *might* have a file called 'from', we need to delete this. // We only need to do this if we have been on a rw-branch, since we created // a whiteout for read-only branches anyway. if (is_dir) maybe_whiteout(from, i, WHITEOUT_DIR); else maybe_whiteout(from, i, WHITEOUT_FILE); } remove_hidden(to, i); // remove hide file (if any) RETURN(0); }