⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fuse.c

📁 UNIX/LINUX下面的用户文件系统
💻 C
📖 第 1 页 / 共 5 页
字号:
        return -ENOSYS;}int fuse_fs_lock(struct fuse_fs *fs, const char *path,                 struct fuse_file_info *fi, int cmd, struct flock *lock){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.lock)        return fs->op.lock(path, fi, cmd, lock);    else        return -ENOSYS;}int fuse_fs_chown(struct fuse_fs *fs, const char *path, uid_t uid, gid_t gid){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.chown)        return fs->op.chown(path, uid, gid);    else        return -ENOSYS;}int fuse_fs_truncate(struct fuse_fs *fs, const char *path, off_t size){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.truncate)        return fs->op.truncate(path, size);    else        return -ENOSYS;}int fuse_fs_ftruncate(struct fuse_fs *fs, const char *path, off_t size,                      struct fuse_file_info *fi){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.ftruncate)        return fs->op.ftruncate(path, size, fi);    else if (fs->op.truncate)        return fs->op.truncate(path, size);    else        return -ENOSYS;}int fuse_fs_utimens(struct fuse_fs *fs, const char *path,                    const struct timespec tv[2]){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.utimens)        return fs->op.utimens(path, tv);    else if(fs->op.utime) {        struct utimbuf buf;        buf.actime = tv[0].tv_sec;        buf.modtime = tv[1].tv_sec;        return fs->op.utime(path, &buf);    } else        return -ENOSYS;}int fuse_fs_access(struct fuse_fs *fs, const char *path, int mask){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.access)        return fs->op.access(path, mask);    else        return -ENOSYS;}int fuse_fs_readlink(struct fuse_fs *fs, const char *path, char *buf,                     size_t len){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.readlink)        return fs->op.readlink(path, buf, len);    else        return -ENOSYS;}int fuse_fs_mknod(struct fuse_fs *fs, const char *path, mode_t mode,                  dev_t rdev){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.mknod)        return fs->op.mknod(path, mode, rdev);    else        return -ENOSYS;}int fuse_fs_mkdir(struct fuse_fs *fs, const char *path, mode_t mode){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.mkdir)        return fs->op.mkdir(path, mode);    else        return -ENOSYS;}int fuse_fs_setxattr(struct fuse_fs *fs, const char *path, const char *name,                     const char *value, size_t size, int flags){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.setxattr)        return fs->op.setxattr(path, name, value, size, flags);    else        return -ENOSYS;}int fuse_fs_getxattr(struct fuse_fs *fs, const char *path, const char *name,                     char *value, size_t size){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.getxattr)        return fs->op.getxattr(path, name, value, size);    else        return -ENOSYS;}int fuse_fs_listxattr(struct fuse_fs *fs, const char *path, char *list,                      size_t size){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.listxattr)        return fs->op.listxattr(path, list, size);    else        return -ENOSYS;}int fuse_fs_bmap(struct fuse_fs *fs, const char *path, size_t blocksize,                 uint64_t *idx){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.bmap)        return fs->op.bmap(path, blocksize, idx);    else        return -ENOSYS;}int fuse_fs_removexattr(struct fuse_fs *fs, const char *path, const char *name){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.removexattr)        return fs->op.removexattr(path, name);    else        return -ENOSYS;}static int is_open(struct fuse *f, fuse_ino_t dir, const char *name){    struct node *node;    int isopen = 0;    pthread_mutex_lock(&f->lock);    node = lookup_node(f, dir, name);    if (node && node->open_count > 0)        isopen = 1;    pthread_mutex_unlock(&f->lock);    return isopen;}static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname,                         char *newname, size_t bufsize){    struct stat buf;    struct node *node;    struct node *newnode;    char *newpath;    int res;    int failctr = 10;    do {        pthread_mutex_lock(&f->lock);        node = lookup_node(f, dir, oldname);        if (node == NULL) {            pthread_mutex_unlock(&f->lock);            return NULL;        }        do {            f->hidectr ++;            snprintf(newname, bufsize, ".fuse_hidden%08x%08x",                     (unsigned int) node->nodeid, f->hidectr);            newnode = lookup_node(f, dir, newname);        } while(newnode);        pthread_mutex_unlock(&f->lock);        newpath = get_path_name(f, dir, newname);        if (!newpath)            break;        res = fuse_fs_getattr(f->fs, newpath, &buf);        if (res == -ENOENT)            break;        free(newpath);        newpath = NULL;    } while(res == 0 && --failctr);    return newpath;}static int hide_node(struct fuse *f, const char *oldpath,                     fuse_ino_t dir, const char *oldname){    char newname[64];    char *newpath;    int err = -EBUSY;    newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));    if (newpath) {        err = fuse_fs_rename(f->fs, oldpath, newpath);        if (!err)            err = rename_node(f, dir, oldname, dir, newname, 1);        free(newpath);    }    return err;}static int mtime_eq(const struct stat *stbuf, const struct timespec *ts){    return stbuf->st_mtime == ts->tv_sec && ST_MTIM_NSEC(stbuf) == ts->tv_nsec;}#ifndef CLOCK_MONOTONIC#define CLOCK_MONOTONIC CLOCK_REALTIME#endifstatic void curr_time(struct timespec *now){    static clockid_t clockid = CLOCK_MONOTONIC;    int res = clock_gettime(clockid, now);    if (res == -1 && errno == EINVAL) {        clockid = CLOCK_REALTIME;        res = clock_gettime(clockid, now);    }    if (res == -1) {        perror("fuse: clock_gettime");        abort();    }}static void update_stat(struct node *node, const struct stat *stbuf){    if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) ||                              stbuf->st_size != node->size))        node->cache_valid = 0;    node->mtime.tv_sec = stbuf->st_mtime;    node->mtime.tv_nsec = ST_MTIM_NSEC(stbuf);    node->size = stbuf->st_size;    curr_time(&node->stat_updated);}static int lookup_path(struct fuse *f, fuse_ino_t nodeid,                       const char *name, const char *path,                       struct fuse_entry_param *e, struct fuse_file_info *fi){    int res;    memset(e, 0, sizeof(struct fuse_entry_param));    if (fi)        res = fuse_fs_fgetattr(f->fs, path, &e->attr, fi);    else        res = fuse_fs_getattr(f->fs, path, &e->attr);    if (res == 0) {        struct node *node;        node = find_node(f, nodeid, name);        if (node == NULL)            res = -ENOMEM;        else {            e->ino = node->nodeid;            e->generation = node->generation;            e->entry_timeout = f->conf.entry_timeout;            e->attr_timeout = f->conf.attr_timeout;            if (f->conf.auto_cache) {                pthread_mutex_lock(&f->lock);                update_stat(node, &e->attr);                pthread_mutex_unlock(&f->lock);            }            set_stat(f, e->ino, &e->attr);            if (f->conf.debug)                fprintf(stderr, "   NODEID: %lu\n", (unsigned long) e->ino);        }    }    return res;}static struct fuse_context_i *fuse_get_context_internal(void){    struct fuse_context_i *c;    c = (struct fuse_context_i *) pthread_getspecific(fuse_context_key);    if (c == NULL) {        c = (struct fuse_context_i *) malloc(sizeof(struct fuse_context_i));        if (c == NULL) {            /* This is hard to deal with properly, so just abort.  If               memory is so low that the context cannot be allocated,               there's not much hope for the filesystem anyway */             fprintf(stderr, "fuse: failed to allocate thread specific data\n");            abort();        }        pthread_setspecific(fuse_context_key, c);    }    return c;}static void fuse_freecontext(void *data){    free(data);}static int fuse_create_context_key(void){    int err = 0;    pthread_mutex_lock(&fuse_context_lock);    if (!fuse_context_ref) {        err = pthread_key_create(&fuse_context_key, fuse_freecontext);        if (err) {            fprintf(stderr, "fuse: failed to create thread specific key: %s\n",                    strerror(err));            pthread_mutex_unlock(&fuse_context_lock);            return -1;        }    }    fuse_context_ref++;    pthread_mutex_unlock(&fuse_context_lock);    return 0;}static void fuse_delete_context_key(void){    pthread_mutex_lock(&fuse_context_lock);    fuse_context_ref--;    if (!fuse_context_ref) {        free(pthread_getspecific(fuse_context_key));        pthread_key_delete(fuse_context_key);    }    pthread_mutex_unlock(&fuse_context_lock);}static struct fuse *req_fuse_prepare(fuse_req_t req){    struct fuse_context_i *c = fuse_get_context_internal();    const struct fuse_ctx *ctx = fuse_req_ctx(req);    c->req = req;    c->ctx.fuse = req_fuse(req);    c->ctx.uid = ctx->uid;    c->ctx.gid = ctx->gid;    c->ctx.pid = ctx->pid;    return c->ctx.fuse;}static inline void reply_err(fuse_req_t req, int err){    /* fuse_reply_err() uses non-negated errno values */    fuse_reply_err(req, -err);}static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e,                        int err){    if (!err) {        struct fuse *f = req_fuse(req);        if (fuse_reply_entry(req, e) == -ENOENT)            forget_node(f, e->ino, 1);    } else        reply_err(req, err);}void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.init)        fs->user_data = fs->op.init(conn);}static void fuse_lib_init(void *data, struct fuse_conn_info *conn){    struct fuse *f = (struct fuse *) data;    struct fuse_context_i *c = fuse_get_context_internal();    memset(c, 0, sizeof(*c));    c->ctx.fuse = f;    fuse_fs_init(f->fs, conn);}void fuse_fs_destroy(struct fuse_fs *fs){    fuse_get_context()->private_data = fs->user_data;    if (fs->op.destroy)        fs->op.destroy(fs->user_data);    if (fs->m)        fuse_put_module(fs->m);    free(fs);}static void fuse_lib_destroy(void *data){    struct fuse *f = (struct fuse *) data;    struct fuse_context_i *c = fuse_get_context_internal();    memset(c, 0, sizeof(*c));    c->ctx.fuse = f;    fuse_fs_destroy(f->fs);    f->fs = NULL;}static void fuse_lib_lookup(fuse_req_t req, fuse_ino_t parent,                            const char *name){    struct fuse *f = req_fuse_prepare(req);    struct fuse_entry_param e;    char *path;    int err;    err = -ENOENT;    pthread_rwlock_rdlock(&f->tree_lock);    path = get_path_name(f, parent, name);    if (path != NULL) {        struct fuse_intr_data d;        if (f->conf.debug)            fprintf(stderr, "LOOKUP %s\n", path);        fuse_prepare_interrupt(f, req, &d);         err = lookup_path(f, parent, name, path, &e, NULL);        if (err == -ENOENT && f->conf.negative_timeout != 0.0) {            e.ino = 0;            e.entry_timeout = f->conf.negative_timeout;            err = 0;        }        fuse_finish_interrupt(f, req, &d);        free(path);    }    pthread_rwlock_unlock(&f->tree_lock);    reply_entry(req, &e, err);}static void fuse_lib_forget(fuse_req_t req, fuse_ino_t ino,                            unsigned long nlookup){    struct fuse *f = req_fuse(req);    if (f->conf.debug)        fprintf(stderr, "FORGET %llu/%lu\n", (unsigned long long)ino, nlookup);    forget_node(f, ino, nlookup);    fuse_reply_none(req);}static void fuse_lib_getattr(fuse_req_t req, fuse_ino_t ino,                             struct fuse_file_info *fi){    struct fuse *f = req_fuse_prepare(req);    struct stat buf;    char *path;    int err;    (void) fi;    memset(&buf, 0, sizeof(buf));    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_getattr(f->fs, path, &buf);        fuse_finish_interrupt(f, req, &d);        free(path);    }    pthread_rwlock_unlock(&f->tree_lock);    if (!err) {        if (f->conf.auto_cache) {            pthread_mutex_lock(&f->lock);            update_stat(get_node(f, ino), &buf);            pthread_mutex_unlock(&f->lock);        }        set_stat(f, ino, &buf);        fuse_reply_attr(req, &buf, f->conf.attr_timeout);    } else        reply_err(req, err);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -