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/files/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/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/files/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, 0))); | |
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 #ifndef NDEBUG | |
37 base::ScopedFD OpenMojoDebugDirectory() { | |
38 const char* home_dir_name = getenv("HOME"); | |
39 if (!home_dir_name || !home_dir_name[0]) { | |
40 LOG(ERROR) << "HOME not set"; | |
41 return base::ScopedFD(); | |
42 } | |
43 base::FilePath mojo_debug_dir_name = | |
44 base::FilePath(home_dir_name).Append("MojoDebug"); | |
45 return base::ScopedFD(HANDLE_EINTR( | |
46 open(mojo_debug_dir_name.value().c_str(), O_RDONLY | O_DIRECTORY, 0))); | |
47 } | |
48 #endif | |
49 | |
50 } // namespace | |
51 | |
52 FilesImpl::FilesImpl(ApplicationConnection* connection, | |
53 InterfaceRequest<Files> request) | |
54 : binding_(this, request.Pass()) { | |
55 // TODO(vtl): record other app's URL | |
56 } | |
57 | |
58 FilesImpl::~FilesImpl() { | |
59 } | |
60 | |
61 void FilesImpl::OpenFileSystem(FileSystem file_system, | |
62 InterfaceRequest<Directory> directory, | |
63 const Callback<void(Error)>& callback) { | |
64 base::ScopedFD dir_fd; | |
65 // Set only if the |DirectoryImpl| will own this directory (and should delete | |
66 // it on destruction). | |
67 base::FilePath owned_dir_name; | |
68 switch (file_system) { | |
69 case FILE_SYSTEM_TEMPORARY: | |
70 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=! | |
71 dir_fd.reset(CreateAndOpenTemporaryDirectory(&owned_dir_name).release()); | |
72 DCHECK(!owned_dir_name.empty()); | |
73 break; | |
74 case FILE_SYSTEM_DEBUG: | |
75 #ifdef NDEBUG | |
76 LOG(WARNING) << "~/MojoDebug only available in Debug builds"; | |
qsr
2015/03/02 12:46:40
You should call the callback, otherwise client wil
qsr
2015/03/03 11:56:44
You didn't change this -> I really think this shou
viettrungluu
2015/03/03 18:50:35
Oops, missed your previous comment. Sorry.
I had
| |
77 #else | |
78 // TODO(vtl): ScopedGeneric (hence ScopedFD) doesn't have an operator=! | |
79 dir_fd.reset(OpenMojoDebugDirectory().release()); | |
80 if (!dir_fd.is_valid()) { | |
81 LOG(ERROR) << "~/MojoDebug unavailable"; | |
82 callback.Run(ERROR_UNAVAILABLE); | |
83 return; | |
84 } | |
85 #endif | |
86 break; | |
87 default: | |
88 LOG(ERROR) << "Unknown file system type: " << file_system; | |
89 callback.Run(ERROR_UNIMPLEMENTED); | |
90 return; | |
91 } | |
92 | |
93 new DirectoryImpl(directory.Pass(), dir_fd.Pass(), owned_dir_name); | |
94 callback.Run(ERROR_OK); | |
95 } | |
96 | |
97 } // namespace files | |
98 } // namespace mojo | |
OLD | NEW |