file_utils.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/fuchsia/file_utils.h"
  5. #include <fcntl.h>
  6. #include <lib/fdio/fd.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <tuple>
  11. #include <utility>
  12. #include "base/files/scoped_file.h"
  13. #include "base/fuchsia/fuchsia_logging.h"
  14. namespace base {
  15. namespace {
  16. fidl::InterfaceHandle<::fuchsia::io::Directory> OpenDirectoryHandleInternal(
  17. const base::FilePath& path,
  18. bool read_only) {
  19. ScopedFD fd(open(path.value().c_str(),
  20. O_DIRECTORY | (read_only ? O_RDONLY : O_RDWR)));
  21. if (!fd.is_valid()) {
  22. DPLOG(ERROR) << "Failed to open " << path;
  23. return fidl::InterfaceHandle<::fuchsia::io::Directory>();
  24. }
  25. zx::channel channel;
  26. zx_status_t status =
  27. fdio_fd_transfer(fd.get(), channel.reset_and_get_address());
  28. if (status != ZX_ERR_UNAVAILABLE)
  29. std::ignore = fd.release();
  30. if (status != ZX_OK) {
  31. ZX_DLOG(ERROR, status) << "fdio_fd_transfer";
  32. return fidl::InterfaceHandle<::fuchsia::io::Directory>();
  33. }
  34. return fidl::InterfaceHandle<::fuchsia::io::Directory>(std::move(channel));
  35. }
  36. } // namespace
  37. const char kPersistedDataDirectoryPath[] = "/data";
  38. const char kPersistedCacheDirectoryPath[] = "/cache";
  39. const char kServiceDirectoryPath[] = "/svc";
  40. const char kPackageRootDirectoryPath[] = "/pkg";
  41. fidl::InterfaceHandle<::fuchsia::io::Directory> OpenDirectoryHandle(
  42. const base::FilePath& path) {
  43. return OpenDirectoryHandleInternal(path, true);
  44. }
  45. fidl::InterfaceHandle<::fuchsia::io::Directory> OpenWritableDirectoryHandle(
  46. const base::FilePath& path) {
  47. return OpenDirectoryHandleInternal(path, false);
  48. }
  49. } // namespace base