fuse_dfs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "fuse_dfs.h"
  19. #include "fuse_options.h"
  20. #include "fuse_impls.h"
  21. #include "fuse_init.h"
  22. #include "fuse_connect.h"
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. int is_protected(const char *path) {
  27. dfs_context *dfs = (dfs_context*)fuse_get_context()->private_data;
  28. assert(dfs != NULL);
  29. assert(dfs->protectedpaths);
  30. int i ;
  31. for (i = 0; dfs->protectedpaths[i]; i++) {
  32. if (strcmp(path, dfs->protectedpaths[i]) == 0) {
  33. return 1;
  34. }
  35. }
  36. return 0;
  37. }
  38. static struct fuse_operations dfs_oper = {
  39. .getattr = dfs_getattr,
  40. .access = dfs_access,
  41. .readdir = dfs_readdir,
  42. .destroy = dfs_destroy,
  43. .init = dfs_init,
  44. .open = dfs_open,
  45. .read = dfs_read,
  46. .symlink = dfs_symlink,
  47. .statfs = dfs_statfs,
  48. .mkdir = dfs_mkdir,
  49. .rmdir = dfs_rmdir,
  50. .rename = dfs_rename,
  51. .unlink = dfs_unlink,
  52. .release = dfs_release,
  53. .create = dfs_create,
  54. .write = dfs_write,
  55. .flush = dfs_flush,
  56. .mknod = dfs_mknod,
  57. .utimens = dfs_utimens,
  58. .chmod = dfs_chmod,
  59. .chown = dfs_chown,
  60. .truncate = dfs_truncate,
  61. };
  62. int main(int argc, char *argv[])
  63. {
  64. int ret;
  65. umask(0);
  66. extern const char *program;
  67. program = argv[0];
  68. struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
  69. memset(&options, 0, sizeof(struct options));
  70. options.rdbuffer_size = 10*1024*1024;
  71. options.attribute_timeout = 60;
  72. options.entry_timeout = 60;
  73. options.max_background = 0;
  74. if (-1 == fuse_opt_parse(&args, &options, dfs_opts, dfs_options)) {
  75. return -1;
  76. }
  77. if (!options.private) {
  78. fuse_opt_add_arg(&args, "-oallow_other");
  79. }
  80. if (!options.no_permissions) {
  81. fuse_opt_add_arg(&args, "-odefault_permissions");
  82. }
  83. /*
  84. * FUSE already has a built-in parameter for mounting the filesystem as
  85. * read-only, -r. We defined our own parameter for doing this called -oro.
  86. * We support it by translating it into -r internally.
  87. * The kernel intercepts and returns an error message for any "write"
  88. * operations that the user attempts to perform on a read-only filesystem.
  89. * That means that we don't have to write any code to handle read-only mode.
  90. * See HDFS-4139 for more details.
  91. */
  92. if (options.read_only) {
  93. fuse_opt_add_arg(&args, "-r");
  94. }
  95. {
  96. char buf[80];
  97. snprintf(buf, sizeof buf, "-oattr_timeout=%d",options.attribute_timeout);
  98. fuse_opt_add_arg(&args, buf);
  99. snprintf(buf, sizeof buf, "-oentry_timeout=%d",options.entry_timeout);
  100. fuse_opt_add_arg(&args, buf);
  101. if (options.max_background > 0) {
  102. snprintf(buf, sizeof buf, "-omax_background=%d",options.max_background);
  103. fuse_opt_add_arg(&args, buf);
  104. }
  105. }
  106. if (options.nn_uri == NULL) {
  107. print_usage(argv[0]);
  108. exit(EXIT_SUCCESS);
  109. }
  110. /* Note: do not call any libhdfs functions until fuse_main has been invoked.
  111. *
  112. * fuse_main will daemonize this process, by calling fork(). This will cause
  113. * any extant threads to be destroyed, which could cause problems if
  114. * libhdfs has started some Java threads.
  115. *
  116. * Most initialization code should go in dfs_init, which is invoked after the
  117. * fork. See HDFS-3808 for details.
  118. */
  119. ret = fuse_main(args.argc, args.argv, &dfs_oper, NULL);
  120. fuse_opt_free_args(&args);
  121. return ret;
  122. }