fuse_init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_init.h"
  20. #include "fuse_options.h"
  21. #include "fuse_context_handle.h"
  22. #include "fuse_connect.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. // Hacked up function to basically do:
  27. // protectedpaths = split(options.protected,':');
  28. void init_protectedpaths(dfs_context *dfs) {
  29. char *tmp = options.protected;
  30. // handle degenerate case up front.
  31. if (tmp == NULL || 0 == *tmp) {
  32. dfs->protectedpaths = (char**)malloc(sizeof(char*));
  33. dfs->protectedpaths[0] = NULL;
  34. return;
  35. }
  36. assert(tmp);
  37. if (options.debug) {
  38. print_options();
  39. }
  40. int i = 0;
  41. while (tmp && (NULL != (tmp = index(tmp,':')))) {
  42. tmp++; // pass the ,
  43. i++;
  44. }
  45. i++; // for the last entry
  46. i++; // for the final NULL
  47. dfs->protectedpaths = (char**)malloc(sizeof(char*)*i);
  48. assert(dfs->protectedpaths);
  49. tmp = options.protected;
  50. int j = 0;
  51. while (NULL != tmp && j < i) {
  52. int length;
  53. char *eos = index(tmp,':');
  54. if (NULL != eos) {
  55. length = eos - tmp; // length of this value
  56. } else {
  57. length = strlen(tmp);
  58. }
  59. dfs->protectedpaths[j] = (char*)malloc(sizeof(char)*length+1);
  60. assert(dfs->protectedpaths[j]);
  61. strncpy(dfs->protectedpaths[j], tmp, length);
  62. dfs->protectedpaths[j][length] = '\0';
  63. if (eos) {
  64. tmp = eos + 1;
  65. } else {
  66. tmp = NULL;
  67. }
  68. j++;
  69. }
  70. dfs->protectedpaths[j] = NULL;
  71. }
  72. static void dfsPrintOptions(FILE *fp, const struct options *o)
  73. {
  74. fprintf(fp, "[ protected=%s, nn_uri=%s, nn_port=%d, "
  75. "debug=%d, read_only=%d, initchecks=%d, "
  76. "no_permissions=%d, usetrash=%d, entry_timeout=%d, "
  77. "attribute_timeout=%d, rdbuffer_size=%Zd, direct_io=%d ]",
  78. (o->protected ? o->protected : "(NULL)"), o->nn_uri, o->nn_port,
  79. o->debug, o->read_only, o->initchecks,
  80. o->no_permissions, o->usetrash, o->entry_timeout,
  81. o->attribute_timeout, o->rdbuffer_size, o->direct_io);
  82. }
  83. void *dfs_init(void)
  84. {
  85. //
  86. // Create a private struct of data we will pass to fuse here and which
  87. // will then be accessible on every call.
  88. //
  89. dfs_context *dfs = (dfs_context*)malloc(sizeof(dfs_context));
  90. if (NULL == dfs) {
  91. ERROR("FATAL: could not malloc dfs_context");
  92. exit(1);
  93. }
  94. // initialize the context
  95. dfs->debug = options.debug;
  96. dfs->read_only = options.read_only;
  97. dfs->usetrash = options.usetrash;
  98. dfs->protectedpaths = NULL;
  99. dfs->rdbuffer_size = options.rdbuffer_size;
  100. dfs->direct_io = options.direct_io;
  101. fprintf(stderr, "Mounting with options ");
  102. dfsPrintOptions(stderr, &options);
  103. fprintf(stderr, "\n");
  104. init_protectedpaths(dfs);
  105. assert(dfs->protectedpaths != NULL);
  106. if (dfs->rdbuffer_size <= 0) {
  107. DEBUG("dfs->rdbuffersize <= 0 = %ld", dfs->rdbuffer_size);
  108. dfs->rdbuffer_size = 32768;
  109. }
  110. return (void*)dfs;
  111. }
  112. void dfs_destroy(void *ptr)
  113. {
  114. TRACE("destroy")
  115. }