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 "services/file_manager/file_manager_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/file_util.h" |
| 15 #include "base/files/scoped_file.h" |
| 16 #include "base/logging.h" |
| 17 #include "base/posix/eintr_wrapper.h" |
| 18 #include "services/file_manager/directory_impl.h" |
| 19 |
| 20 namespace mojo { |
| 21 namespace files { |
| 22 |
| 23 namespace { |
| 24 |
| 25 base::ScopedFD CreateAndOpenTemporaryDirectory(base::FilePath* temp_dir_name) { |
| 26 DCHECK(temp_dir_name); |
| 27 CHECK(base::CreateNewTempDirectory(base::FilePath::StringType(), |
| 28 temp_dir_name)); |
| 29 base::ScopedFD temp_dir_fd(HANDLE_EINTR( |
| 30 open(temp_dir_name->value().c_str(), O_RDONLY, O_DIRECTORY))); |
| 31 PCHECK(temp_dir_fd.is_valid()); |
| 32 DVLOG(1) << "Made a temporary directory: " << temp_dir_name->value(); |
| 33 return temp_dir_fd.Pass(); |
| 34 } |
| 35 |
| 36 base::ScopedFD OpenMojoHomeDirectory() { |
| 37 const char* home_dir_name = getenv("HOME"); |
| 38 if (!home_dir_name || !home_dir_name[0]) { |
| 39 LOG(ERROR) << "HOME not set"; |
| 40 return base::ScopedFD(); |
| 41 } |
| 42 base::FilePath mojo_home_dir_name = |
| 43 base::FilePath(home_dir_name).Append("MojoHome"); |
| 44 return base::ScopedFD(HANDLE_EINTR( |
| 45 open(mojo_home_dir_name.value().c_str(), O_RDONLY, O_DIRECTORY))); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 FileManagerImpl::FileManagerImpl(ApplicationConnection* connection, |
| 51 InterfaceRequest<FileManager> request) |
| 52 : binding_(this, request.Pass()) { |
| 53 // TODO(vtl): record other app's URL |
| 54 } |
| 55 |
| 56 FileManagerImpl::~FileManagerImpl() { |
| 57 } |
| 58 |
| 59 void FileManagerImpl::OpenFileSystem(FileSystem file_system, |
| 60 InterfaceRequest<Directory> directory, |
| 61 const Callback<void(Error)>& callback) { |
| 62 base::ScopedFD dir_fd; |
| 63 // Set only if the |DirectoryImpl| will own this directory (and should delete |
| 64 // it on destruction). |
| 65 base::FilePath owned_dir_name; |
| 66 switch (file_system) { |
| 67 case FILE_SYSTEM_TEMPORARY: |
| 68 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=! |
| 69 dir_fd.reset(CreateAndOpenTemporaryDirectory(&owned_dir_name).release()); |
| 70 DCHECK(!owned_dir_name.empty()); |
| 71 break; |
| 72 case FILE_SYSTEM_USER: |
| 73 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=! |
| 74 dir_fd.reset(OpenMojoHomeDirectory().release()); |
| 75 if (!dir_fd.is_valid()) { |
| 76 LOG(ERROR) << "~/MojoHome unavailable"; |
| 77 callback.Run(ERROR_UNAVAILABLE); |
| 78 return; |
| 79 } |
| 80 break; |
| 81 default: |
| 82 LOG(ERROR) << "Unknown file system type: " << file_system; |
| 83 callback.Run(ERROR_UNIMPLEMENTED); |
| 84 return; |
| 85 } |
| 86 |
| 87 new DirectoryImpl(directory.Pass(), dir_fd.Pass(), owned_dir_name); |
| 88 callback.Run(ERROR_OK); |
| 89 } |
| 90 |
| 91 } // namespace files |
| 92 } // namespace mojo |
OLD | NEW |