1 // Copyright 2015 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.
5 #include "components/filesystem/files_impl.h"
10 #include <sys/types.h>
13 #include "base/files/file_path.h"
14 #include "base/files/scoped_file.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "components/filesystem/directory_impl.h"
26 base::ScopedFD
CreateAndOpenTemporaryDirectory(
27 scoped_ptr
<base::ScopedTempDir
>* temp_dir
) {
28 (*temp_dir
).reset(new base::ScopedTempDir());
29 CHECK((*temp_dir
)->CreateUniqueTempDir());
31 base::ScopedFD
temp_dir_fd(HANDLE_EINTR(
32 open((*temp_dir
)->path().value().c_str(), O_RDONLY
| O_DIRECTORY
, 0)));
33 PCHECK(temp_dir_fd
.is_valid());
34 DVLOG(1) << "Made a temporary directory: " << (*temp_dir
)->path().value();
35 return temp_dir_fd
.Pass();
39 base::ScopedFD
OpenMojoDebugDirectory() {
40 const char* home_dir_name
= getenv("HOME");
41 if (!home_dir_name
|| !home_dir_name
[0]) {
42 LOG(ERROR
) << "HOME not set";
43 return base::ScopedFD();
45 base::FilePath mojo_debug_dir_name
=
46 base::FilePath(home_dir_name
).Append("MojoDebug");
47 return base::ScopedFD(HANDLE_EINTR(
48 open(mojo_debug_dir_name
.value().c_str(), O_RDONLY
| O_DIRECTORY
, 0)));
54 FilesImpl::FilesImpl(ApplicationConnection
* connection
,
55 InterfaceRequest
<Files
> request
)
56 : binding_(this, request
.Pass()) {
57 // TODO(vtl): record other app's URL
60 FilesImpl::~FilesImpl() {
63 void FilesImpl::OpenFileSystem(const mojo::String
& file_system
,
64 InterfaceRequest
<Directory
> directory
,
65 const OpenFileSystemCallback
& callback
) {
66 base::ScopedFD dir_fd
;
67 // Set only if the |DirectoryImpl| will own a temporary directory.
68 scoped_ptr
<base::ScopedTempDir
> temp_dir
;
69 if (file_system
.is_null()) {
70 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=!
71 dir_fd
.reset(CreateAndOpenTemporaryDirectory(&temp_dir
).release());
73 } else if (file_system
.get() == std::string("debug")) {
75 LOG(WARNING
) << "~/MojoDebug only available in Debug builds";
77 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=!
78 dir_fd
.reset(OpenMojoDebugDirectory().release());
80 if (!dir_fd
.is_valid()) {
81 LOG(ERROR
) << "~/MojoDebug unavailable";
82 callback
.Run(ERROR_UNAVAILABLE
);
86 LOG(ERROR
) << "Unknown file system: " << file_system
.get();
87 callback
.Run(ERROR_UNIMPLEMENTED
);
91 new DirectoryImpl(directory
.Pass(), dir_fd
.Pass(), temp_dir
.Pass());
92 callback
.Run(ERROR_OK
);