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