fuse_impls_readdir.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_impls.h"
  20. #include "fuse_stat_struct.h"
  21. #include "fuse_connect.h"
  22. int dfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  23. off_t offset, struct fuse_file_info *fi)
  24. {
  25. TRACE1("readdir", path)
  26. dfs_context *dfs = (dfs_context*)fuse_get_context()->private_data;
  27. assert(dfs);
  28. assert(path);
  29. assert(buf);
  30. hdfsFS userFS = doConnectAsUser(dfs->nn_uri, dfs->nn_port);
  31. if (userFS == NULL) {
  32. ERROR("Could not connect");
  33. return -EIO;
  34. }
  35. // Read dirents. Calling a variant that just returns the final path
  36. // component (HDFS-975) would save us from parsing it out below.
  37. int numEntries = 0;
  38. hdfsFileInfo *info = hdfsListDirectory(userFS, path, &numEntries);
  39. int ret = 0;
  40. // NULL means either the directory doesn't exist or maybe IO error.
  41. if (NULL == info) {
  42. ret = (errno > 0) ? -errno : -ENOENT;
  43. goto cleanup;
  44. }
  45. int i ;
  46. for (i = 0; i < numEntries; i++) {
  47. if (NULL == info[i].mName) {
  48. ERROR("Path %s info[%d].mName is NULL", path, i);
  49. continue;
  50. }
  51. struct stat st;
  52. fill_stat_structure(&info[i], &st);
  53. // Find the final path component
  54. const char *str = strrchr(info[i].mName, '/');
  55. if (NULL == str) {
  56. ERROR("Invalid URI %s", info[i].mName);
  57. continue;
  58. }
  59. str++;
  60. // pack this entry into the fuse buffer
  61. int res = 0;
  62. if ((res = filler(buf,str,&st,0)) != 0) {
  63. ERROR("Readdir filler failed: %d\n",res);
  64. }
  65. }
  66. // insert '.' and '..'
  67. const char *const dots [] = { ".",".."};
  68. for (i = 0 ; i < 2 ; i++)
  69. {
  70. struct stat st;
  71. memset(&st, 0, sizeof(struct stat));
  72. // set to 0 to indicate not supported for directory because we cannot (efficiently) get this info for every subdirectory
  73. st.st_nlink = 0;
  74. // setup stat size and acl meta data
  75. st.st_size = 512;
  76. st.st_blksize = 512;
  77. st.st_blocks = 1;
  78. st.st_mode = (S_IFDIR | 0777);
  79. st.st_uid = default_id;
  80. st.st_gid = default_id;
  81. // todo fix below times
  82. st.st_atime = 0;
  83. st.st_mtime = 0;
  84. st.st_ctime = 0;
  85. const char *const str = dots[i];
  86. // flatten the info using fuse's function into a buffer
  87. int res = 0;
  88. if ((res = filler(buf,str,&st,0)) != 0) {
  89. ERROR("Readdir filler failed: %d\n",res);
  90. }
  91. }
  92. // free the info pointers
  93. hdfsFreeFileInfo(info,numEntries);
  94. cleanup:
  95. if (doDisconnect(userFS)) {
  96. ret = -EIO;
  97. ERROR("Failed to disconnect %d", errno);
  98. }
  99. return ret;
  100. }