Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / base / fuchsia / file_utils.cc
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
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 <utility>
14
15 #include "base/files/scoped_file.h"
16 #include "base/fuchsia/fuchsia_logging.h"
17 #include "base/macros.h"
18
19 namespace base {
20 namespace fuchsia {
21
22 const char kPersistedDataDirectoryPath[] = "/data";
23 const char kServiceDirectoryPath[] = "/svc";
24 const char kPackageRootDirectoryPath[] = "/pkg";
25
26 fidl::InterfaceHandle<::fuchsia::io::Directory> OpenDirectory(
27     const base::FilePath& path) {
28   ScopedFD fd(open(path.value().c_str(), O_DIRECTORY | O_RDONLY));
29   if (!fd.is_valid()) {
30     DPLOG(ERROR) << "Failed to open " << path;
31     return fidl::InterfaceHandle<::fuchsia::io::Directory>();
32   }
33
34   zx::channel channel;
35   zx_status_t status =
36       fdio_fd_transfer(fd.get(), channel.reset_and_get_address());
37   if (status != ZX_ERR_UNAVAILABLE)
38     ignore_result(fd.release());
39   if (status != ZX_OK) {
40     ZX_DLOG(ERROR, status) << "fdio_fd_transfer";
41     return fidl::InterfaceHandle<::fuchsia::io::Directory>();
42   }
43
44   return fidl::InterfaceHandle<::fuchsia::io::Directory>(std::move(channel));
45 }
46
47 }  // namespace fuchsia
48 }  // namespace base