fuse_options.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_context_handle.h"
  19. #include "fuse_dfs.h"
  20. #include "fuse_options.h"
  21. #include <getopt.h>
  22. #include <stdlib.h>
  23. #define OLD_HDFS_URI_LOCATION "dfs://"
  24. #define NEW_HDFS_URI_LOCATION "hdfs://"
  25. void print_options() {
  26. printf("options:\n"
  27. "\tprotected=%s\n"
  28. "\tserver=%s\n"
  29. "\tport=%d\n"
  30. "\tdebug=%d\n"
  31. "\tread_only=%d\n"
  32. "\tusetrash=%d\n"
  33. "\tentry_timeout=%d\n"
  34. "\tattribute_timeout=%d\n"
  35. "\tprivate=%d\n"
  36. "\trdbuffer_size=%d (KBs)\n",
  37. options.protected, options.nn_uri, options.nn_port, options.debug,
  38. options.read_only, options.usetrash, options.entry_timeout,
  39. options.attribute_timeout, options.private,
  40. (int)options.rdbuffer_size / 1024);
  41. }
  42. const char *program;
  43. /** macro to define options */
  44. #define DFSFS_OPT_KEY(t, p, v) { t, offsetof(struct options, p), v }
  45. void print_usage(const char *pname)
  46. {
  47. printf("USAGE: %s [debug] [--help] [--version] "
  48. "[-oprotected=<colon_seped_list_of_paths] [rw] [-onotrash] "
  49. "[-ousetrash] [-obig_writes] [-oprivate (single user)] [ro] "
  50. "[-oserver=<hadoop_servername>] [-oport=<hadoop_port>] "
  51. "[-oentry_timeout=<secs>] [-oattribute_timeout=<secs>] "
  52. "[-odirect_io] [-onopoermissions] [-o<other fuse option>] "
  53. "<mntpoint> [fuse options]\n", pname);
  54. printf("NOTE: debugging option for fuse is -debug\n");
  55. }
  56. /** keys for FUSE_OPT_ options */
  57. enum
  58. {
  59. KEY_VERSION,
  60. KEY_HELP,
  61. KEY_USETRASH,
  62. KEY_NOTRASH,
  63. KEY_RO,
  64. KEY_RW,
  65. KEY_PRIVATE,
  66. KEY_BIGWRITES,
  67. KEY_DEBUG,
  68. KEY_INITCHECKS,
  69. KEY_NOPERMISSIONS,
  70. KEY_DIRECTIO,
  71. };
  72. struct fuse_opt dfs_opts[] =
  73. {
  74. DFSFS_OPT_KEY("server=%s", nn_uri, 0),
  75. DFSFS_OPT_KEY("entry_timeout=%d", entry_timeout, 0),
  76. DFSFS_OPT_KEY("attribute_timeout=%d", attribute_timeout, 0),
  77. DFSFS_OPT_KEY("protected=%s", protected, 0),
  78. DFSFS_OPT_KEY("port=%d", nn_port, 0),
  79. DFSFS_OPT_KEY("rdbuffer=%d", rdbuffer_size,0),
  80. FUSE_OPT_KEY("private", KEY_PRIVATE),
  81. FUSE_OPT_KEY("ro", KEY_RO),
  82. FUSE_OPT_KEY("debug", KEY_DEBUG),
  83. FUSE_OPT_KEY("initchecks", KEY_INITCHECKS),
  84. FUSE_OPT_KEY("nopermissions", KEY_NOPERMISSIONS),
  85. FUSE_OPT_KEY("big_writes", KEY_BIGWRITES),
  86. FUSE_OPT_KEY("rw", KEY_RW),
  87. FUSE_OPT_KEY("usetrash", KEY_USETRASH),
  88. FUSE_OPT_KEY("notrash", KEY_NOTRASH),
  89. FUSE_OPT_KEY("direct_io", KEY_DIRECTIO),
  90. FUSE_OPT_KEY("-v", KEY_VERSION),
  91. FUSE_OPT_KEY("--version", KEY_VERSION),
  92. FUSE_OPT_KEY("-h", KEY_HELP),
  93. FUSE_OPT_KEY("--help", KEY_HELP),
  94. FUSE_OPT_END
  95. };
  96. int dfs_options(void *data, const char *arg, int key, struct fuse_args *outargs)
  97. {
  98. (void) data;
  99. int nn_uri_len;
  100. switch (key) {
  101. case FUSE_OPT_KEY_OPT:
  102. INFO("Ignoring option %s", arg);
  103. return 1;
  104. case KEY_VERSION:
  105. INFO("%s %s\n", program, _FUSE_DFS_VERSION);
  106. exit(0);
  107. case KEY_HELP:
  108. print_usage(program);
  109. exit(0);
  110. case KEY_USETRASH:
  111. options.usetrash = 1;
  112. break;
  113. case KEY_NOTRASH:
  114. options.usetrash = 0;
  115. break;
  116. case KEY_RO:
  117. options.read_only = 1;
  118. break;
  119. case KEY_RW:
  120. options.read_only = 0;
  121. break;
  122. case KEY_PRIVATE:
  123. options.private = 1;
  124. break;
  125. case KEY_DEBUG:
  126. fuse_opt_add_arg(outargs, "-d");
  127. options.debug = 1;
  128. break;
  129. case KEY_INITCHECKS:
  130. options.initchecks = 1;
  131. break;
  132. case KEY_NOPERMISSIONS:
  133. options.no_permissions = 1;
  134. break;
  135. case KEY_DIRECTIO:
  136. options.direct_io = 1;
  137. break;
  138. case KEY_BIGWRITES:
  139. #ifdef FUSE_CAP_BIG_WRITES
  140. fuse_opt_add_arg(outargs, "-obig_writes");
  141. #endif
  142. break;
  143. default: {
  144. // try and see if the arg is a URI
  145. if (!strstr(arg, "://")) {
  146. if (strcmp(arg,"ro") == 0) {
  147. options.read_only = 1;
  148. } else if (strcmp(arg,"rw") == 0) {
  149. options.read_only = 0;
  150. } else {
  151. INFO("Adding FUSE arg %s", arg);
  152. fuse_opt_add_arg(outargs, arg);
  153. return 0;
  154. }
  155. } else {
  156. if (options.nn_uri) {
  157. INFO("Ignoring option %s because '-server' was already "
  158. "specified!", arg);
  159. return 1;
  160. }
  161. if (strstr(arg, OLD_HDFS_URI_LOCATION) == arg) {
  162. // For historical reasons, we let people refer to hdfs:// as dfs://
  163. nn_uri_len = strlen(NEW_HDFS_URI_LOCATION) +
  164. strlen(arg + strlen(OLD_HDFS_URI_LOCATION)) + 1;
  165. options.nn_uri = malloc(nn_uri_len);
  166. snprintf(options.nn_uri, nn_uri_len, "%s%s", NEW_HDFS_URI_LOCATION,
  167. arg + strlen(OLD_HDFS_URI_LOCATION));
  168. } else {
  169. options.nn_uri = strdup(arg);
  170. }
  171. }
  172. }
  173. }
  174. return 0;
  175. }