📄 fuse.c
字号:
fprintf(stderr, "OPEN[%llu] flags: 0x%x %s\n", (unsigned long long) fi->fh, fi->flags, path); } } else reply_err(req, err); if (path) free(path); pthread_rwlock_unlock(&f->tree_lock);}static void fuse_lib_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi){ struct fuse *f = req_fuse_prepare(req); char *path; char *buf; int res; buf = (char *) malloc(size); if (buf == NULL) { reply_err(req, -ENOMEM); return; } res = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; if (f->conf.debug) fprintf(stderr, "READ[%llu] %lu bytes from %llu\n", (unsigned long long) fi->fh, (unsigned long) size, (unsigned long long) off); fuse_prepare_interrupt(f, req, &d); res = fuse_fs_read(f->fs, path, buf, size, off, fi); fuse_finish_interrupt(f, req, &d); free(path); } pthread_rwlock_unlock(&f->tree_lock); if (res >= 0) { if (f->conf.debug) fprintf(stderr, " READ[%llu] %u bytes\n", (unsigned long long)fi->fh, res); if ((size_t) res > size) fprintf(stderr, "fuse: read too many bytes"); fuse_reply_buf(req, buf, res); } else reply_err(req, res); free(buf);}static void fuse_lib_write(fuse_req_t req, fuse_ino_t ino, const char *buf, size_t size, off_t off, struct fuse_file_info *fi){ struct fuse *f = req_fuse_prepare(req); char *path; int res; res = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; if (f->conf.debug) fprintf(stderr, "WRITE%s[%llu] %lu bytes to %llu\n", fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, (unsigned long) size, (unsigned long long) off); fuse_prepare_interrupt(f, req, &d); res = fuse_fs_write(f->fs, path, buf, size, off, fi); fuse_finish_interrupt(f, req, &d); free(path); } pthread_rwlock_unlock(&f->tree_lock); if (res >= 0) { if (f->conf.debug) fprintf(stderr, " WRITE%s[%llu] %u bytes\n", fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, res); if ((size_t) res > size) fprintf(stderr, "fuse: wrote too many bytes"); fuse_reply_write(req, res); } else reply_err(req, res);}static void fuse_lib_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, struct fuse_file_info *fi){ struct fuse *f = req_fuse_prepare(req); char *path; int err; err = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; if (f->conf.debug) fprintf(stderr, "FSYNC[%llu]\n", (unsigned long long) fi->fh); fuse_prepare_interrupt(f, req, &d); err = fuse_fs_fsync(f->fs, path, datasync, fi); fuse_finish_interrupt(f, req, &d); free(path); } pthread_rwlock_unlock(&f->tree_lock); reply_err(req, err);}static struct fuse_dh *get_dirhandle(const struct fuse_file_info *llfi, struct fuse_file_info *fi){ struct fuse_dh *dh = (struct fuse_dh *) (uintptr_t) llfi->fh; memset(fi, 0, sizeof(struct fuse_file_info)); fi->fh = dh->fh; fi->fh_old = dh->fh; return dh;}static void fuse_lib_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *llfi){ struct fuse *f = req_fuse_prepare(req); struct fuse_intr_data d; struct fuse_dh *dh; struct fuse_file_info fi; char *path; int err; dh = (struct fuse_dh *) malloc(sizeof(struct fuse_dh)); if (dh == NULL) { reply_err(req, -ENOMEM); return; } memset(dh, 0, sizeof(struct fuse_dh)); dh->fuse = f; dh->contents = NULL; dh->len = 0; dh->filled = 0; dh->nodeid = ino; fuse_mutex_init(&dh->lock); llfi->fh = (uintptr_t) dh; memset(&fi, 0, sizeof(fi)); fi.flags = llfi->flags; err = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { fuse_prepare_interrupt(f, req, &d); err = fuse_fs_opendir(f->fs, path, &fi); fuse_finish_interrupt(f, req, &d); dh->fh = fi.fh; } if (!err) { if (fuse_reply_open(req, llfi) == -ENOENT) { /* The opendir syscall was interrupted, so it must be cancelled */ fuse_prepare_interrupt(f, req, &d); fuse_fs_releasedir(f->fs, path, &fi); fuse_finish_interrupt(f, req, &d); pthread_mutex_destroy(&dh->lock); free(dh); } } else { reply_err(req, err); free(dh); } free(path); pthread_rwlock_unlock(&f->tree_lock);}static int extend_contents(struct fuse_dh *dh, unsigned minsize){ if (minsize > dh->size) { char *newptr; unsigned newsize = dh->size; if (!newsize) newsize = 1024; while (newsize < minsize) newsize *= 2; newptr = (char *) realloc(dh->contents, newsize); if (!newptr) { dh->error = -ENOMEM; return -1; } dh->contents = newptr; dh->size = newsize; } return 0;}static int fill_dir(void *dh_, const char *name, const struct stat *statp, off_t off){ struct fuse_dh *dh = (struct fuse_dh *) dh_; struct stat stbuf; size_t newlen; if (statp) stbuf = *statp; else { memset(&stbuf, 0, sizeof(stbuf)); stbuf.st_ino = FUSE_UNKNOWN_INO; } if (!dh->fuse->conf.use_ino) { stbuf.st_ino = FUSE_UNKNOWN_INO; if (dh->fuse->conf.readdir_ino) { struct node *node; pthread_mutex_lock(&dh->fuse->lock); node = lookup_node(dh->fuse, dh->nodeid, name); if (node) stbuf.st_ino = (ino_t) node->nodeid; pthread_mutex_unlock(&dh->fuse->lock); } } if (off) { if (extend_contents(dh, dh->needlen) == -1) return 1; dh->filled = 0; newlen = dh->len + fuse_add_direntry(dh->req, dh->contents + dh->len, dh->needlen - dh->len, name, &stbuf, off); if (newlen > dh->needlen) return 1; } else { newlen = dh->len + fuse_add_direntry(dh->req, NULL, 0, name, NULL, 0); if (extend_contents(dh, newlen) == -1) return 1; fuse_add_direntry(dh->req, dh->contents + dh->len, dh->size - dh->len, name, &stbuf, newlen); } dh->len = newlen; return 0;}static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_dh *dh, struct fuse_file_info *fi){ int err = -ENOENT; char *path; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; dh->len = 0; dh->error = 0; dh->needlen = size; dh->filled = 1; dh->req = req; fuse_prepare_interrupt(f, req, &d); err = fuse_fs_readdir(f->fs, path, dh, fill_dir, off, fi); fuse_finish_interrupt(f, req, &d); dh->req = NULL; if (!err) err = dh->error; if (err) dh->filled = 0; free(path); } pthread_rwlock_unlock(&f->tree_lock); return err;}static void fuse_lib_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *llfi){ struct fuse *f = req_fuse_prepare(req); struct fuse_file_info fi; struct fuse_dh *dh = get_dirhandle(llfi, &fi); pthread_mutex_lock(&dh->lock); /* According to SUS, directory contents need to be refreshed on rewinddir() */ if (!off) dh->filled = 0; if (!dh->filled) { int err = readdir_fill(f, req, ino, size, off, dh, &fi); if (err) { reply_err(req, err); goto out; } } if (dh->filled) { if (off < dh->len) { if (off + size > dh->len) size = dh->len - off; } else size = 0; } else { size = dh->len; off = 0; } fuse_reply_buf(req, dh->contents + off, size); out: pthread_mutex_unlock(&dh->lock);}static void fuse_lib_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *llfi){ struct fuse *f = req_fuse_prepare(req); struct fuse_intr_data d; struct fuse_file_info fi; struct fuse_dh *dh = get_dirhandle(llfi, &fi); char *path; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); fuse_prepare_interrupt(f, req, &d); fuse_fs_releasedir(f->fs, path ? path : "-", &fi); fuse_finish_interrupt(f, req, &d); if (path) free(path); pthread_rwlock_unlock(&f->tree_lock); pthread_mutex_lock(&dh->lock); pthread_mutex_unlock(&dh->lock); pthread_mutex_destroy(&dh->lock); free(dh->contents); free(dh); reply_err(req, 0);}static void fuse_lib_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, struct fuse_file_info *llfi){ struct fuse *f = req_fuse_prepare(req); struct fuse_file_info fi; char *path; int err; get_dirhandle(llfi, &fi); err = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = fuse_fs_fsyncdir(f->fs, path, datasync, &fi); fuse_finish_interrupt(f, req, &d); free(path); } pthread_rwlock_unlock(&f->tree_lock); reply_err(req, err);}static void fuse_lib_statfs(fuse_req_t req, fuse_ino_t ino){ struct fuse *f = req_fuse_prepare(req); struct statvfs buf; char *path; int err; memset(&buf, 0, sizeof(buf)); pthread_rwlock_rdlock(&f->tree_lock); if (!ino) { err = -ENOMEM; path = strdup("/"); } else { err = -ENOENT; path = get_path(f, ino); } if (path) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = fuse_fs_statfs(f->fs, path, &buf); fuse_finish_interrupt(f, req, &d); free(path); } pthread_rwlock_unlock(&f->tree_lock); if (!err) fuse_reply_statfs(req, &buf); else reply_err(req, err);}static void fuse_lib_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, const char *value, size_t size, int flags){ struct fuse *f = req_fuse_prepare(req); char *path; int err; err = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = fuse_fs_setxattr(f->fs, path, name, value, size, flags); fuse_finish_interrupt(f, req, &d); free(path); } pthread_rwlock_unlock(&f->tree_lock); reply_err(req, err);}static int common_getxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, const char *name, char *value, size_t size){ int err; char *path; err = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = fuse_fs_getxattr(f->fs, path, name, value, size); fuse_finish_interrupt(f, req, &d); free(path); } pthread_rwlock_unlock(&f->tree_lock); return err;}static void fuse_lib_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, size_t size){ struct fuse *f = req_fuse_prepare(req); int res; if (size) { char *value = (char *) malloc(size); if (value == NULL) { reply_err(req, -ENOMEM); return; } res = common_getxattr(f, req, ino, name, value, size); if (res > 0) fuse_reply_buf(req, value, res); else reply_err(req, res); free(value); } else { res = common_getxattr(f, req, ino, name, NULL, 0); if (res >= 0) fuse_reply_xattr(req, res); else reply_err(req, res); }}static int common_listxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, char *list, size_t size){ char *path; int err; err = -ENOENT; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (path != NULL) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = fus
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -