fuse_impls_truncate.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_connect.h"
  21. /**
  22. * For now implement truncate here and only for size == 0.
  23. * Weak implementation in that we just delete the file and
  24. * then re-create it, but don't set the user, group, and times to the old
  25. * file's metadata.
  26. */
  27. int dfs_truncate(const char *path, off_t size)
  28. {
  29. struct hdfsConn *conn = NULL;
  30. hdfsFS fs;
  31. dfs_context *dfs = (dfs_context*)fuse_get_context()->private_data;
  32. TRACE1("truncate", path)
  33. assert(path);
  34. assert('/' == *path);
  35. assert(dfs);
  36. if (size != 0) {
  37. return 0;
  38. }
  39. int ret = dfs_unlink(path);
  40. if (ret != 0) {
  41. return ret;
  42. }
  43. ret = fuseConnectAsThreadUid(&conn);
  44. if (ret) {
  45. fprintf(stderr, "fuseConnectAsThreadUid: failed to open a libhdfs "
  46. "connection! error %d.\n", ret);
  47. ret = -EIO;
  48. goto cleanup;
  49. }
  50. fs = hdfsConnGetFs(conn);
  51. int flags = O_WRONLY | O_CREAT;
  52. hdfsFile file;
  53. if ((file = (hdfsFile)hdfsOpenFile(fs, path, flags, 0, 0, 0)) == NULL) {
  54. ERROR("Could not connect open file %s", path);
  55. ret = -EIO;
  56. goto cleanup;
  57. }
  58. if (hdfsCloseFile(fs, file) != 0) {
  59. ERROR("Could not close file %s", path);
  60. ret = -EIO;
  61. goto cleanup;
  62. }
  63. cleanup:
  64. if (conn) {
  65. hdfsConnRelease(conn);
  66. }
  67. return ret;
  68. }