📄 fuse.c
字号:
} if (!err) { if (f->conf.direct_io) fi->direct_io = 1; if (f->conf.kernel_cache) fi->keep_cache = 1; /* see comment for open */ pthread_mutex_lock(&f->lock); if (fuse_reply_create(req, &e, fi) == -ENOENT) { /* The open syscall was interrupted, so it must be cancelled */ pthread_mutex_unlock(&f->lock); if(f->op.release) fuse_do_release(f, req, path, fi); forget_node(f, e.ino, 1); } else { get_node(f, e.ino)->open_count++; pthread_mutex_unlock(&f->lock); } } else reply_err(req, err); if (path) free(path); pthread_rwlock_unlock(&f->tree_lock);}static double diff_timespec(const struct timespec *t1, const struct timespec *t2){ return (t1->tv_sec - t2->tv_sec) + ((double) t1->tv_nsec - (double) t2->tv_nsec) / 1000000000.0;}static void open_auto_cache(struct fuse *f, fuse_req_t req, fuse_ino_t ino, const char *path, struct fuse_file_info *fi){ struct node *node; pthread_mutex_lock(&f->lock); node = get_node(f, ino); if (node->cache_valid) { struct timespec now; curr_time(&now); if (diff_timespec(&now, &node->stat_updated) > f->conf.ac_attr_timeout) { struct stat stbuf; int err; pthread_mutex_unlock(&f->lock); if (f->op.fgetattr) err = fuse_do_fgetattr(f, req, path, &stbuf, fi); else err = fuse_do_getattr(f, req, path, &stbuf); pthread_mutex_lock(&f->lock); if (!err) update_stat(node, &stbuf); else node->cache_valid = 0; } } if (node->cache_valid) fi->keep_cache = 1; node->cache_valid = 1; pthread_mutex_unlock(&f->lock);}static void fuse_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi){ struct fuse *f = req_fuse_prepare(req); char *path = NULL; int err = 0; pthread_rwlock_rdlock(&f->tree_lock); if (f->op.open) { err = -ENOENT; path = get_path(f, ino); if (path != NULL) err = fuse_compat_open(f, req, path, fi); } if (!err) { if (f->conf.debug) { printf("OPEN[%llu] flags: 0x%x\n", (unsigned long long) fi->fh, fi->flags); fflush(stdout); } if (f->conf.direct_io) fi->direct_io = 1; if (f->conf.kernel_cache) fi->keep_cache = 1; if (f->conf.auto_cache) open_auto_cache(f, req, ino, path, fi); /* The reply and the open count increase needs to be under a single locked section, otherwise a release could come in between and screw up the accounting */ pthread_mutex_lock(&f->lock); if (fuse_reply_open(req, fi) == -ENOENT) { /* The open syscall was interrupted, so it must be cancelled */ pthread_mutex_unlock(&f->lock); if(f->op.release && path != NULL) fuse_compat_release(f, req, path, fi); } else { get_node(f, ino)->open_count++; pthread_mutex_unlock(&f->lock); } } else reply_err(req, err); if (path) free(path); pthread_rwlock_unlock(&f->tree_lock);}static void fuse_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) { if (f->conf.debug) { printf("READ[%llu] %lu bytes from %llu\n", (unsigned long long) fi->fh, (unsigned long) size, (unsigned long long) off); fflush(stdout); } res = -ENOSYS; if (f->op.read) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); res = f->op.read(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) { printf(" READ[%llu] %u bytes\n", (unsigned long long) fi->fh, res); fflush(stdout); } 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_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) { if (f->conf.debug) { printf("WRITE%s[%llu] %lu bytes to %llu\n", fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, (unsigned long) size, (unsigned long long) off); fflush(stdout); } res = -ENOSYS; if (f->op.write) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); res = f->op.write(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) { printf(" WRITE%s[%llu] %u bytes\n", fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, res); fflush(stdout); } 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_release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi){ struct fuse *f = req_fuse_prepare(req); char *path; struct node *node; int unlink_hidden = 0; int err = 0; pthread_rwlock_rdlock(&f->tree_lock); path = get_path(f, ino); if (f->conf.debug) { printf("RELEASE%s[%llu] flags: 0x%x\n", fi->flush ? "+FLUSH" : "", (unsigned long long) fi->fh, fi->flags); fflush(stdout); } if (fi->flush && path && f->op.flush) err = fuse_do_flush(f, req, path, fi); if (f->op.release) fuse_compat_release(f, req, path, fi); pthread_mutex_lock(&f->lock); node = get_node(f, ino); assert(node->open_count > 0); --node->open_count; if (node->is_hidden && !node->open_count) { unlink_hidden = 1; node->is_hidden = 0; } pthread_mutex_unlock(&f->lock); if(unlink_hidden && path) fuse_do_unlink(f, req, path); if (path) free(path); pthread_rwlock_unlock(&f->tree_lock); reply_err(req, err);}static void fuse_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) { if (f->conf.debug) { printf("FSYNC[%llu]\n", (unsigned long long) fi->fh); fflush(stdout); } err = -ENOSYS; if (f->op.fsync) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = f->op.fsync(path, datasync, fi); fuse_finish_interrupt(f, req, &d); } free(path); } pthread_rwlock_unlock(&f->tree_lock); reply_err(req, err);}static struct fuse_dirhandle *get_dirhandle(const struct fuse_file_info *llfi, struct fuse_file_info *fi){ struct fuse_dirhandle *dh = (struct fuse_dirhandle *) (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_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *llfi){ struct fuse *f = req_fuse_prepare(req); struct fuse_dirhandle *dh; dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle)); if (dh == NULL) { reply_err(req, -ENOMEM); return; } memset(dh, 0, sizeof(struct fuse_dirhandle)); 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; if (f->op.opendir) { struct fuse_file_info fi; char *path; int err; 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) { err = fuse_compat_opendir(f, req, path, &fi); dh->fh = fi.fh; } if (!err) { if (fuse_reply_open(req, llfi) == -ENOENT) { /* The opendir syscall was interrupted, so it must be cancelled */ if(f->op.releasedir) fuse_do_releasedir(f, req, path, &fi); pthread_mutex_destroy(&dh->lock); free(dh); } } else { reply_err(req, err); free(dh); } free(path); pthread_rwlock_unlock(&f->tree_lock); } else fuse_reply_open(req, llfi);}static int extend_contents(struct fuse_dirhandle *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_common(struct fuse_dirhandle *dh, const char *name, const struct stat *statp, off_t off){ 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 fill_dir(void *buf, const char *name, const struct stat *stbuf, off_t off){ return fill_dir_common((struct fuse_dirhandle *) buf, name, stbuf, off);}static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type, ino_t ino){ struct stat stbuf; memset(&stbuf, 0, sizeof(stbuf)); stbuf.st_mode = type << 12; stbuf.st_ino = ino; fill_dir_common(dh, name, &stbuf, 0); return dh->error;}static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_dirhandle *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) { dh->len = 0; dh->error = 0; dh->needlen = size; dh->filled = 1; dh->req = req; err = -ENOSYS; if (f->op.readdir) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = f->op.readdir(path, dh, fill_dir, off, fi); fuse_finish_interrupt(f, req, &d); } else if (f->op.getdir) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); err = f->op.getdir(path, dh, fill_dir_old); 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_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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -