fuse_options.c 5.4 KB

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