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)22fidl::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.release(), channel.reset_and_get_address()); 35 if (status != ZX_OK) { 36 ZX_DLOG(ERROR, status) << "fdio_fd_transfer"; 37 return fidl::InterfaceHandle<::fuchsia::io::Directory>(); 38 } 39 40 return fidl::InterfaceHandle<::fuchsia::io::Directory>(std::move(channel)); 41 } 42 43 } // namespace 44 45 const char kPersistedDataDirectoryPath[] = "/data"; 46 const char kPersistedCacheDirectoryPath[] = "/cache"; 47 const char kServiceDirectoryPath[] = "/svc"; 48 const char kPackageRootDirectoryPath[] = "/pkg"; 49 OpenDirectoryHandle(const base::FilePath & path)50fidl::InterfaceHandle<::fuchsia::io::Directory> OpenDirectoryHandle( 51 const base::FilePath& path) { 52 return OpenDirectoryHandleInternal(path, true); 53 } 54 OpenWritableDirectoryHandle(const base::FilePath & path)55fidl::InterfaceHandle<::fuchsia::io::Directory> OpenWritableDirectoryHandle( 56 const base::FilePath& path) { 57 return OpenDirectoryHandleInternal(path, false); 58 } 59 60 } // namespace base 61