fuse_impls_chown.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_users.h"
  20. #include "fuse_impls.h"
  21. #include "fuse_connect.h"
  22. #include <stdlib.h>
  23. int dfs_chown(const char *path, uid_t uid, gid_t gid)
  24. {
  25. struct hdfsConn *conn = NULL;
  26. int ret = 0;
  27. char *user = NULL;
  28. char *group = NULL;
  29. TRACE1("chown", path)
  30. // retrieve dfs specific data
  31. dfs_context *dfs = (dfs_context*)fuse_get_context()->private_data;
  32. // check params and the context var
  33. assert(path);
  34. assert(dfs);
  35. assert('/' == *path);
  36. if ((uid == -1) && (gid == -1)) {
  37. ret = 0;
  38. goto cleanup;
  39. }
  40. if (uid != -1) {
  41. user = getUsername(uid);
  42. if (NULL == user) {
  43. ERROR("Could not lookup the user id string %d",(int)uid);
  44. ret = -EIO;
  45. goto cleanup;
  46. }
  47. }
  48. if (gid != -1) {
  49. group = getGroup(gid);
  50. if (group == NULL) {
  51. ERROR("Could not lookup the group id string %d",(int)gid);
  52. ret = -EIO;
  53. goto cleanup;
  54. }
  55. }
  56. ret = fuseConnectAsThreadUid(&conn);
  57. if (ret) {
  58. fprintf(stderr, "fuseConnectAsThreadUid: failed to open a libhdfs "
  59. "connection! error %d.\n", ret);
  60. ret = -EIO;
  61. goto cleanup;
  62. }
  63. if (hdfsChown(hdfsConnGetFs(conn), path, user, group)) {
  64. ret = errno;
  65. ERROR("Could not chown %s to %d:%d: error %d", path, (int)uid, gid, ret);
  66. ret = (ret > 0) ? -ret : -EIO;
  67. goto cleanup;
  68. }
  69. cleanup:
  70. if (conn) {
  71. hdfsConnRelease(conn);
  72. }
  73. free(user);
  74. free(group);
  75. return ret;
  76. }