用户空间文件系统“FUSE”
(2013-09-05 13:16:43)
标签:
it |
分类: IT |
用户空间文件系统(Filesystem in
Userspace,简称FUSE)是操作系统中的概念,指完全在用户态实现的文件系统。
dfs_context *dfs =
(dfs_context*)fuse_get_context()->private_data;
assert(dfs != NULL);
assert(dfs->protectedpaths);
int i ;
for (i = 0;
dfs->protectedpaths[i]; i++) {
if (strcmp(path,
dfs->protectedpaths[i]) == 0) {
return 1;
}
}
return 0;
.getattr =
dfs_getattr,
.access =
dfs_access,
.readdir =
dfs_readdir,
.destroy =
dfs_destroy,
.init
= dfs_init,
.open
= dfs_open,
.read
= dfs_read,
.symlink =
dfs_symlink,
.statfs =
dfs_statfs,
.mkdir
= dfs_mkdir,
.rmdir
= dfs_rmdir,
.rename =
dfs_rename,
.unlink =
dfs_unlink,
.release =
dfs_release,
.create =
dfs_create,
.write
= dfs_write,
.flush
= dfs_flush,
.mknod
= dfs_mknod,
.utimens =
dfs_utimens,
.chmod
= dfs_chmod,
.chown
= dfs_chown,
.truncate =
dfs_truncate,
int ret;
umask(0);
extern const char *program;
program = argv[0];
struct fuse_args args =
FUSE_ARGS_INIT(argc, argv);
memset(&options, 0,
sizeof(struct options));
options.rdbuffer_size =
10*1024*1024;
options.attribute_timeout =
60;
options.entry_timeout =
60;
if (-1 ==
fuse_opt_parse(&args, &options,
dfs_opts, dfs_options)) {
return
-1;
}
if (!options.private) {
fuse_opt_add_arg(&args,
"-oallow_other");
}
if (!options.no_permissions)
{
fuse_opt_add_arg(&args,
"-odefault_permissions");
}
if (options.read_only) {
fuse_opt_add_arg(&args, "-r");
}
{
char
buf[80];
snprintf(buf,
sizeof buf,
"-oattr_timeout=%d",options.attribute_timeout);
fuse_opt_add_arg(&args, buf);
snprintf(buf,
sizeof buf,
"-oentry_timeout=%d",options.entry_timeout);
fuse_opt_add_arg(&args, buf);
}
if (options.nn_uri == NULL)
{
print_usage(argv[0]);
exit(EXIT_SUCCESS);
}
ret = fuse_main(args.argc,
args.argv, &dfs_oper, NULL);
fuse_opt_free_args(&args);
return ret;
Linux “mount”命令支持
FUSE,所以我们会看到一些基于FUSE的工具,比如sshfs,ftpfs,可以把远程mount成一个磁盘目录来操作。
hadoop中的分布式文件系统hdfs,也提供了fuse-dfs(hadoop\hadoop-hdfs-project\hadoop-hdfs\src\main\native\fuse-dfs)。
下面是fuse_dfs.c,重点句加粗。
#include
"fuse_dfs.h"
#include
"fuse_options.h"
#include
"fuse_impls.h"
#include
"fuse_init.h"
#include
"fuse_connect.h"
#include
#include
#include
int
is_protected(const char *path) {
}
static
struct fuse_operations dfs_oper = {
};
int main(int
argc, char *argv[])
{
}
当然他的具体实现,是通过libhdfs(hadoop\hadoop-hdfs-project\hadoop-hdfs\src\main\native\libhdfs)完成的,libhdfs是hadoop的c访问接口,也可以说是jndi访问接口。
总体来说,重写fuse_operations,可以扩展mount其他东西。
windows版本的FUSE,是http://dokan-dev.net/en/。