OLD | NEW |
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 #include "nacl_io/mount_html5fs.h" | 6 #include "nacl_io/mount_html5fs.h" |
7 | 7 |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <ppapi/c/pp_completion_callback.h> | 9 #include <ppapi/c/pp_completion_callback.h> |
10 #include <ppapi/c/pp_errors.h> | 10 #include <ppapi/c/pp_errors.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <string.h> | 12 #include <string.h> |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include "nacl_io/mount_node_html5fs.h" | 14 #include "nacl_io/mount_node_html5fs.h" |
15 #include "utils/auto_lock.h" | 15 #include "utils/auto_lock.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 #if defined(WIN32) | 19 #if defined(WIN32) |
20 int64_t strtoull(const char* nptr, char** endptr, int base) { | 20 int64_t strtoull(const char* nptr, char** endptr, int base) { |
21 return _strtoui64(nptr, endptr, base); | 21 return _strtoui64(nptr, endptr, base); |
22 } | 22 } |
23 #endif | 23 #endif |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 MountNode *MountHtml5Fs::Open(const Path& path, int mode) { | 27 MountNode *MountHtml5Fs::Open(const Path& path, int mode) { |
28 if (!IsFilesystemOpen()) | 28 if (BlockUntilFilesystemOpen() != PP_OK) |
29 return NULL; | 29 return NULL; |
30 | 30 |
31 PP_Resource fileref = ppapi()->GetFileRefInterface()->Create( | 31 PP_Resource fileref = ppapi()->GetFileRefInterface()->Create( |
32 filesystem_resource_, path.Join().c_str()); | 32 filesystem_resource_, path.Join().c_str()); |
33 if (!fileref) | 33 if (!fileref) |
34 return NULL; | 34 return NULL; |
35 | 35 |
36 MountNodeHtml5Fs* node = new MountNodeHtml5Fs(this, fileref); | 36 MountNodeHtml5Fs* node = new MountNodeHtml5Fs(this, fileref); |
37 if (!node->Init(mode)) { | 37 if (!node->Init(mode)) { |
38 node->Release(); | 38 node->Release(); |
39 return NULL; | 39 return NULL; |
40 } | 40 } |
41 | 41 |
42 return node; | 42 return node; |
43 } | 43 } |
44 | 44 |
45 int MountHtml5Fs::Unlink(const Path& path) { | 45 int MountHtml5Fs::Unlink(const Path& path) { |
46 return Remove(path); | 46 return Remove(path); |
47 } | 47 } |
48 | 48 |
49 int MountHtml5Fs::Mkdir(const Path& path, int permissions) { | 49 int MountHtml5Fs::Mkdir(const Path& path, int permissions) { |
50 if (!IsFilesystemOpen()) { | 50 if (BlockUntilFilesystemOpen() != PP_OK) { |
51 errno = EINVAL; | 51 errno = ENODEV; |
52 return -1; | 52 return -1; |
53 } | 53 } |
54 | 54 |
55 ScopedResource fileref_resource( | 55 ScopedResource fileref_resource( |
56 ppapi(), ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | 56 ppapi(), ppapi()->GetFileRefInterface()->Create(filesystem_resource_, |
57 path.Join().c_str())); | 57 path.Join().c_str())); |
58 if (!fileref_resource.pp_resource()) { | 58 if (!fileref_resource.pp_resource()) { |
59 errno = EINVAL; | 59 errno = EINVAL; |
60 return -1; | 60 return -1; |
61 } | 61 } |
62 | 62 |
63 int32_t result = ppapi()->GetFileRefInterface()->MakeDirectory( | 63 int32_t result = ppapi()->GetFileRefInterface()->MakeDirectory( |
64 fileref_resource.pp_resource(), PP_FALSE, PP_BlockUntilComplete()); | 64 fileref_resource.pp_resource(), PP_FALSE, PP_BlockUntilComplete()); |
65 if (result != PP_OK) { | 65 if (result != PP_OK) { |
66 errno = PPErrorToErrno(result); | 66 errno = PPErrorToErrno(result); |
67 return -1; | 67 return -1; |
68 } | 68 } |
69 | 69 |
70 return 0; | 70 return 0; |
71 } | 71 } |
72 | 72 |
73 int MountHtml5Fs::Rmdir(const Path& path) { | 73 int MountHtml5Fs::Rmdir(const Path& path) { |
74 return Remove(path); | 74 return Remove(path); |
75 } | 75 } |
76 | 76 |
77 int MountHtml5Fs::Remove(const Path& path) { | 77 int MountHtml5Fs::Remove(const Path& path) { |
78 if (!IsFilesystemOpen()) { | 78 if (BlockUntilFilesystemOpen() != PP_OK) { |
79 errno = EINVAL; | 79 errno = ENODEV; |
80 return -1; | 80 return -1; |
81 } | 81 } |
82 | 82 |
83 ScopedResource fileref_resource( | 83 ScopedResource fileref_resource( |
84 ppapi(), ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | 84 ppapi(), ppapi()->GetFileRefInterface()->Create(filesystem_resource_, |
85 path.Join().c_str())); | 85 path.Join().c_str())); |
86 if (!fileref_resource.pp_resource()) { | 86 if (!fileref_resource.pp_resource()) { |
87 errno = EINVAL; | 87 errno = EINVAL; |
88 return -1; | 88 return -1; |
89 } | 89 } |
90 | 90 |
91 int32_t result = ppapi()->GetFileRefInterface()->Delete( | 91 int32_t result = ppapi()->GetFileRefInterface()->Delete( |
92 fileref_resource.pp_resource(), | 92 fileref_resource.pp_resource(), |
93 PP_BlockUntilComplete()); | 93 PP_BlockUntilComplete()); |
94 if (result != PP_OK) { | 94 if (result != PP_OK) { |
95 errno = PPErrorToErrno(result); | 95 errno = PPErrorToErrno(result); |
96 return -1; | 96 return -1; |
97 } | 97 } |
98 | 98 |
99 return 0; | 99 return 0; |
100 } | 100 } |
101 | 101 |
102 | 102 |
103 MountHtml5Fs::MountHtml5Fs() | 103 MountHtml5Fs::MountHtml5Fs() |
104 : filesystem_resource_(0), | 104 : filesystem_resource_(0), |
105 filesystem_open_(false) { | 105 filesystem_open_has_result_(false), |
| 106 filesystem_open_result_(PP_OK) { |
106 } | 107 } |
107 | 108 |
108 bool MountHtml5Fs::Init(int dev, StringMap_t& args, PepperInterface* ppapi) { | 109 bool MountHtml5Fs::Init(int dev, StringMap_t& args, PepperInterface* ppapi) { |
109 if (!Mount::Init(dev, args, ppapi)) | 110 if (!Mount::Init(dev, args, ppapi)) |
110 return false; | 111 return false; |
111 | 112 |
112 if (!ppapi) | 113 if (!ppapi) |
113 return false; | 114 return false; |
114 | 115 |
115 // Parse mount args. | 116 // Parse mount args. |
(...skipping 12 matching lines...) Expand all Loading... |
128 } | 129 } |
129 } | 130 } |
130 | 131 |
131 // Initialize filesystem. | 132 // Initialize filesystem. |
132 filesystem_resource_ = ppapi->GetFileSystemInterface()->Create( | 133 filesystem_resource_ = ppapi->GetFileSystemInterface()->Create( |
133 ppapi_->GetInstance(), filesystem_type); | 134 ppapi_->GetInstance(), filesystem_type); |
134 | 135 |
135 if (filesystem_resource_ == 0) | 136 if (filesystem_resource_ == 0) |
136 return false; | 137 return false; |
137 | 138 |
138 // Open the filesystem. Don't block, this could be called from the main | 139 // We can't block the main thread, so make an asynchronous call if on main |
139 // thread. | 140 // thread. If we are off-main-thread, then don't make an asynchronous call; |
140 ppapi->GetFileSystemInterface()->Open(filesystem_resource_, expected_size, | 141 // otherwise we require a message loop. |
| 142 bool main_thread = ppapi->IsMainThread(); |
| 143 PP_CompletionCallback cc = main_thread ? |
141 PP_MakeCompletionCallback(&MountHtml5Fs::FilesystemOpenCallbackThunk, | 144 PP_MakeCompletionCallback(&MountHtml5Fs::FilesystemOpenCallbackThunk, |
142 this)); | 145 this) : |
| 146 PP_BlockUntilComplete(); |
143 | 147 |
144 return true; | 148 int32_t result = ppapi->GetFileSystemInterface()->Open( |
| 149 filesystem_resource_, expected_size, cc); |
| 150 |
| 151 if (!main_thread) { |
| 152 filesystem_open_has_result_ = true; |
| 153 filesystem_open_result_ = result; |
| 154 |
| 155 return filesystem_open_result_ == PP_OK; |
| 156 } else { |
| 157 // We have to assume the call to Open will succeed; there is no better |
| 158 // result to return here. |
| 159 return true; |
| 160 } |
145 } | 161 } |
146 | 162 |
147 void MountHtml5Fs::Destroy() { | 163 void MountHtml5Fs::Destroy() { |
148 ppapi_->ReleaseResource(filesystem_resource_); | 164 ppapi_->ReleaseResource(filesystem_resource_); |
149 } | 165 } |
150 | 166 |
151 bool MountHtml5Fs::IsFilesystemOpen() { | 167 int32_t MountHtml5Fs::BlockUntilFilesystemOpen() { |
152 AutoLock lock(&lock_); | 168 AutoLock lock(&lock_); |
153 return filesystem_open_; | 169 while (!filesystem_open_has_result_) { |
| 170 pthread_cond_wait(&filesystem_open_cond_, &lock_); |
| 171 } |
| 172 return filesystem_open_result_; |
154 } | 173 } |
155 | 174 |
156 // static | 175 // static |
157 void MountHtml5Fs::FilesystemOpenCallbackThunk(void* user_data, | 176 void MountHtml5Fs::FilesystemOpenCallbackThunk(void* user_data, |
158 int32_t result) { | 177 int32_t result) { |
159 MountHtml5Fs* self = static_cast<MountHtml5Fs*>(user_data); | 178 MountHtml5Fs* self = static_cast<MountHtml5Fs*>(user_data); |
160 self->FilesystemOpenCallback(result); | 179 self->FilesystemOpenCallback(result); |
161 } | 180 } |
162 | 181 |
163 void MountHtml5Fs::FilesystemOpenCallback(int32_t result) { | 182 void MountHtml5Fs::FilesystemOpenCallback(int32_t result) { |
164 AutoLock lock(&lock_); | 183 AutoLock lock(&lock_); |
165 filesystem_open_ = result == PP_OK; | 184 filesystem_open_has_result_ = true; |
| 185 filesystem_open_result_ = result; |
| 186 pthread_cond_signal(&filesystem_open_cond_); |
166 } | 187 } |
OLD | NEW |