fuse_impls_write.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_connect.h"
  19. #include "fuse_dfs.h"
  20. #include "fuse_impls.h"
  21. #include "fuse_file_handle.h"
  22. int dfs_write(const char *path, const char *buf, size_t size,
  23. off_t offset, struct fuse_file_info *fi)
  24. {
  25. TRACE1("write", path)
  26. // retrieve dfs specific data
  27. dfs_context *dfs = (dfs_context*)fuse_get_context()->private_data;
  28. int ret = 0;
  29. // check params and the context var
  30. assert(path);
  31. assert(dfs);
  32. assert('/' == *path);
  33. assert(fi);
  34. dfs_fh *fh = (dfs_fh*)fi->fh;
  35. assert(fh);
  36. hdfsFile file_handle = (hdfsFile)fh->hdfsFH;
  37. assert(file_handle);
  38. //
  39. // Critical section - make the sanity check (tell to see the writes are sequential) and the actual write
  40. // (no returns until end)
  41. //
  42. pthread_mutex_lock(&fh->mutex);
  43. tSize length = 0;
  44. hdfsFS fs = hdfsConnGetFs(fh->conn);
  45. tOffset cur_offset = hdfsTell(fs, file_handle);
  46. if (cur_offset != offset) {
  47. ERROR("User trying to random access write to a file %d != %d for %s",
  48. (int)cur_offset, (int)offset, path);
  49. ret = -ENOTSUP;
  50. } else {
  51. length = hdfsWrite(fs, file_handle, buf, size);
  52. if (length <= 0) {
  53. ERROR("Could not write all bytes for %s %d != %d (errno=%d)",
  54. path, length, (int)size, errno);
  55. if (errno == 0 || errno == EINTERNAL) {
  56. ret = -EIO;
  57. } else {
  58. ret = -errno;
  59. }
  60. }
  61. if (length != size) {
  62. ERROR("Could not write all bytes for %s %d != %d (errno=%d)",
  63. path, length, (int)size, errno);
  64. }
  65. }
  66. //
  67. // Critical section end
  68. //
  69. pthread_mutex_unlock(&fh->mutex);
  70. return ret == 0 ? length : ret;
  71. }