OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/filesystem/files_impl.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <stdlib.h> |
| 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> |
| 11 #include <unistd.h> |
| 12 |
| 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" |
| 20 |
| 21 namespace mojo { |
| 22 namespace files { |
| 23 |
| 24 namespace { |
| 25 |
| 26 base::ScopedFD CreateAndOpenTemporaryDirectory( |
| 27 scoped_ptr<base::ScopedTempDir>* temp_dir) { |
| 28 (*temp_dir).reset(new base::ScopedTempDir()); |
| 29 CHECK((*temp_dir)->CreateUniqueTempDir()); |
| 30 |
| 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(); |
| 36 } |
| 37 |
| 38 #ifndef NDEBUG |
| 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(); |
| 44 } |
| 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))); |
| 49 } |
| 50 #endif |
| 51 |
| 52 } // namespace |
| 53 |
| 54 FilesImpl::FilesImpl(ApplicationConnection* connection, |
| 55 InterfaceRequest<Files> request) |
| 56 : binding_(this, request.Pass()) { |
| 57 // TODO(vtl): record other app's URL |
| 58 } |
| 59 |
| 60 FilesImpl::~FilesImpl() { |
| 61 } |
| 62 |
| 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()); |
| 72 DCHECK(temp_dir); |
| 73 } else if (file_system.get() == std::string("debug")) { |
| 74 #ifdef NDEBUG |
| 75 LOG(WARNING) << "~/MojoDebug only available in Debug builds"; |
| 76 #else |
| 77 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=! |
| 78 dir_fd.reset(OpenMojoDebugDirectory().release()); |
| 79 #endif |
| 80 if (!dir_fd.is_valid()) { |
| 81 LOG(ERROR) << "~/MojoDebug unavailable"; |
| 82 callback.Run(ERROR_UNAVAILABLE); |
| 83 return; |
| 84 } |
| 85 } else { |
| 86 LOG(ERROR) << "Unknown file system: " << file_system.get(); |
| 87 callback.Run(ERROR_UNIMPLEMENTED); |
| 88 return; |
| 89 } |
| 90 |
| 91 new DirectoryImpl(directory.Pass(), dir_fd.Pass(), temp_dir.Pass()); |
| 92 callback.Run(ERROR_OK); |
| 93 } |
| 94 |
| 95 } // namespace files |
| 96 } // namespace mojo |
OLD | NEW |