fuse_stat_struct.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <math.h>
  19. #include <pthread.h>
  20. #include <grp.h>
  21. #include <pwd.h>
  22. #include "fuse_dfs.h"
  23. #include "fuse_stat_struct.h"
  24. #include "fuse_context_handle.h"
  25. /*
  26. * getpwuid and getgrgid return static structs so we safeguard the contents
  27. * while retrieving fields using the 2 structs below.
  28. * NOTE: if using both, always get the passwd struct firt!
  29. */
  30. extern pthread_mutex_t passwdstruct_mutex;
  31. extern pthread_mutex_t groupstruct_mutex;
  32. const int default_id = 99; // nobody - not configurable since soon uids in dfs, yeah!
  33. const int blksize = 512;
  34. /**
  35. * Converts from a hdfs hdfsFileInfo to a POSIX stat struct
  36. *
  37. */
  38. int fill_stat_structure(hdfsFileInfo *info, struct stat *st)
  39. {
  40. assert(st);
  41. assert(info);
  42. // initialize the stat structure
  43. memset(st, 0, sizeof(struct stat));
  44. // by default: set to 0 to indicate not supported for directory because we cannot (efficiently) get this info for every subdirectory
  45. st->st_nlink = (info->mKind == kObjectKindDirectory) ? 0 : 1;
  46. uid_t owner_id = default_id;
  47. if (info->mOwner != NULL) {
  48. //
  49. // Critical section - protect from concurrent calls in different threads since
  50. // the struct below is static.
  51. // (no returns until end)
  52. //
  53. pthread_mutex_lock(&passwdstruct_mutex);
  54. struct passwd *passwd_info = getpwnam(info->mOwner);
  55. owner_id = passwd_info == NULL ? default_id : passwd_info->pw_uid;
  56. //
  57. // End critical section
  58. //
  59. pthread_mutex_unlock(&passwdstruct_mutex);
  60. }
  61. gid_t group_id = default_id;
  62. if (info->mGroup != NULL) {
  63. //
  64. // Critical section - protect from concurrent calls in different threads since
  65. // the struct below is static.
  66. // (no returns until end)
  67. //
  68. pthread_mutex_lock(&groupstruct_mutex);
  69. struct group *grp = getgrnam(info->mGroup);
  70. group_id = grp == NULL ? default_id : grp->gr_gid;
  71. //
  72. // End critical section
  73. //
  74. pthread_mutex_unlock(&groupstruct_mutex);
  75. }
  76. short perm = (info->mKind == kObjectKindDirectory) ? (S_IFDIR | 0777) : (S_IFREG | 0666);
  77. if (info->mPermissions > 0) {
  78. perm = (info->mKind == kObjectKindDirectory) ? S_IFDIR: S_IFREG ;
  79. perm |= info->mPermissions;
  80. }
  81. // set stat metadata
  82. st->st_size = (info->mKind == kObjectKindDirectory) ? 4096 : info->mSize;
  83. st->st_blksize = blksize;
  84. st->st_blocks = ceil(st->st_size/st->st_blksize);
  85. st->st_mode = perm;
  86. st->st_uid = owner_id;
  87. st->st_gid = group_id;
  88. st->st_atime = info->mLastAccess;
  89. st->st_mtime = info->mLastMod;
  90. st->st_ctime = info->mLastMod;
  91. return 0;
  92. }