OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "nacl_io/jsfs/js_fs_node.h" | 5 #include "nacl_io/jsfs/js_fs_node.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <limits.h> | 10 #include <limits.h> |
11 #include <string.h> | 11 #include <string.h> |
12 | 12 |
13 #include "nacl_io/jsfs/js_fs.h" | 13 #include "nacl_io/jsfs/js_fs.h" |
14 #include "nacl_io/kernel_handle.h" | 14 #include "nacl_io/kernel_handle.h" |
15 #include "nacl_io/log.h" | 15 #include "nacl_io/log.h" |
16 #include "nacl_io/osdirent.h" | 16 #include "nacl_io/osdirent.h" |
17 #include "nacl_io/pepper_interface.h" | 17 #include "nacl_io/pepper_interface.h" |
18 #include "sdk_util/macros.h" | 18 #include "sdk_util/macros.h" |
19 | 19 |
20 #define TRACE(format, ...) \ | |
21 LOG_TRACE("%s:%d: " format, __FILE__, __LINE__, ##__VA_ARGS__) | |
22 #define ERROR(format, ...) \ | |
23 LOG_ERROR("%s:%d: " format, __FILE__, __LINE__, ##__VA_ARGS__) | |
24 | |
25 | |
26 namespace nacl_io { | 20 namespace nacl_io { |
27 | 21 |
28 JsFsNode::JsFsNode(Filesystem* filesystem, int32_t fd) | 22 JsFsNode::JsFsNode(Filesystem* filesystem, int32_t fd) |
29 : Node(filesystem), | 23 : Node(filesystem), |
30 ppapi_(filesystem->ppapi()), | 24 ppapi_(filesystem->ppapi()), |
31 array_iface_(ppapi_->GetVarArrayInterface()), | 25 array_iface_(ppapi_->GetVarArrayInterface()), |
32 buffer_iface_(ppapi_->GetVarArrayBufferInterface()), | 26 buffer_iface_(ppapi_->GetVarArrayBufferInterface()), |
33 var_iface_(ppapi_->GetVarInterface()), | 27 var_iface_(ppapi_->GetVarInterface()), |
34 fd_(fd) { | 28 fd_(fd) { |
35 } | 29 } |
(...skipping 29 matching lines...) Expand all Loading... |
65 // GetStat cached the mode in stat_.st_mode. Forward to Node::CanOpen, | 59 // GetStat cached the mode in stat_.st_mode. Forward to Node::CanOpen, |
66 // which will check this mode against open_flags. | 60 // which will check this mode against open_flags. |
67 return Node::CanOpen(open_flags); | 61 return Node::CanOpen(open_flags); |
68 } | 62 } |
69 | 63 |
70 Error JsFsNode::GetStat(struct stat* stat) { | 64 Error JsFsNode::GetStat(struct stat* stat) { |
71 AUTO_LOCK(node_lock_); | 65 AUTO_LOCK(node_lock_); |
72 | 66 |
73 ScopedVar response(ppapi_); | 67 ScopedVar response(ppapi_); |
74 if (!SendRequestAndWait(&response, "%s%d", "cmd", "fstat", "fildes", fd_)) { | 68 if (!SendRequestAndWait(&response, "%s%d", "cmd", "fstat", "fildes", fd_)) { |
75 ERROR("Failed to send request."); | 69 LOG_ERROR("Failed to send request."); |
76 return EINVAL; | 70 return EINVAL; |
77 } | 71 } |
78 | 72 |
79 // TODO(binji): find out the size of bionic stat fields. | 73 // TODO(binji): find out the size of bionic stat fields. |
80 #if defined(__native_client__) && !defined(__BIONIC__) | 74 #if defined(__native_client__) && !defined(__BIONIC__) |
81 #if defined(__GLIBC__) | 75 #if defined(__GLIBC__) |
82 const char* format = "%d%lld%d%d%d%d%lld%lld%lld%lld%lld%lld%lld"; | 76 const char* format = "%d%lld%d%d%d%d%lld%lld%lld%lld%lld%lld%lld"; |
83 #else | 77 #else |
84 const char* format = "%d%lld%d%d%d%d%lld%lld%d%d%lld%lld%lld"; | 78 const char* format = "%d%lld%d%d%d%d%lld%lld%d%d%lld%lld%lld"; |
85 #endif | 79 #endif |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 "st_blksize", &stat->st_blksize, | 114 "st_blksize", &stat->st_blksize, |
121 "st_blocks", &stat->st_blocks, | 115 "st_blocks", &stat->st_blocks, |
122 "st_atime", &stat->st_atime, | 116 "st_atime", &stat->st_atime, |
123 "st_mtime", &stat->st_mtime, | 117 "st_mtime", &stat->st_mtime, |
124 "st_ctime", &stat->st_ctime); | 118 "st_ctime", &stat->st_ctime); |
125 | 119 |
126 if (result >= 1 && error) | 120 if (result >= 1 && error) |
127 return error; | 121 return error; |
128 | 122 |
129 if (result != 13) { | 123 if (result != 13) { |
130 ERROR( | 124 LOG_ERROR( |
131 "Expected \"st_*\" and \"error\" fields in response (should be 13 " | 125 "Expected \"st_*\" and \"error\" fields in response (should be 13 " |
132 "total)."); | 126 "total)."); |
133 return EINVAL; | 127 return EINVAL; |
134 } | 128 } |
135 | 129 |
136 stat->st_dev = filesystem()->dev(); | 130 stat->st_dev = filesystem()->dev(); |
137 | 131 |
138 return 0; | 132 return 0; |
139 } | 133 } |
140 | 134 |
141 Error JsFsNode::GetSize(off_t* out_size) { | 135 Error JsFsNode::GetSize(off_t* out_size) { |
142 *out_size = 0; | 136 *out_size = 0; |
143 | 137 |
144 struct stat statbuf; | 138 struct stat statbuf; |
145 Error error = GetStat(&statbuf); | 139 Error error = GetStat(&statbuf); |
146 if (error) | 140 if (error) |
147 return error; | 141 return error; |
148 | 142 |
149 *out_size = stat_.st_size; | 143 *out_size = stat_.st_size; |
150 return 0; | 144 return 0; |
151 } | 145 } |
152 | 146 |
153 Error JsFsNode::FSync() { | 147 Error JsFsNode::FSync() { |
154 AUTO_LOCK(node_lock_); | 148 AUTO_LOCK(node_lock_); |
155 | 149 |
156 ScopedVar response(ppapi_); | 150 ScopedVar response(ppapi_); |
157 if (!SendRequestAndWait(&response, "%s%d", "cmd", "fsync", "fildes", fd_)) { | 151 if (!SendRequestAndWait(&response, "%s%d", "cmd", "fsync", "fildes", fd_)) { |
158 ERROR("Failed to send request."); | 152 LOG_ERROR("Failed to send request."); |
159 return EINVAL; | 153 return EINVAL; |
160 } | 154 } |
161 | 155 |
162 return filesystem()->ErrorFromResponse(response); | 156 return filesystem()->ErrorFromResponse(response); |
163 } | 157 } |
164 | 158 |
165 Error JsFsNode::FTruncate(off_t length) { | 159 Error JsFsNode::FTruncate(off_t length) { |
166 AUTO_LOCK(node_lock_); | 160 AUTO_LOCK(node_lock_); |
167 | 161 |
168 ScopedVar response(ppapi_); | 162 ScopedVar response(ppapi_); |
169 if (!SendRequestAndWait(&response, | 163 if (!SendRequestAndWait(&response, |
170 "%s%d%lld", "cmd", "ftruncate", "fildes", fd_, "length", length)) { | 164 "%s%d%lld", "cmd", "ftruncate", "fildes", fd_, "length", length)) { |
171 ERROR("Failed to send request."); | 165 LOG_ERROR("Failed to send request."); |
172 return EINVAL; | 166 return EINVAL; |
173 } | 167 } |
174 | 168 |
175 return filesystem()->ErrorFromResponse(response); | 169 return filesystem()->ErrorFromResponse(response); |
176 } | 170 } |
177 | 171 |
178 Error JsFsNode::Read(const HandleAttr& attr, | 172 Error JsFsNode::Read(const HandleAttr& attr, |
179 void* buf, | 173 void* buf, |
180 size_t count, | 174 size_t count, |
181 int* out_bytes) { | 175 int* out_bytes) { |
182 AUTO_LOCK(node_lock_); | 176 AUTO_LOCK(node_lock_); |
183 | 177 |
184 *out_bytes = 0; | 178 *out_bytes = 0; |
185 | 179 |
186 ScopedVar response(ppapi_); | 180 ScopedVar response(ppapi_); |
187 if (!SendRequestAndWait(&response, "%s%d%u%lld", | 181 if (!SendRequestAndWait(&response, "%s%d%u%lld", |
188 "cmd", "pread", | 182 "cmd", "pread", |
189 "fildes", fd_, | 183 "fildes", fd_, |
190 "nbyte", count, | 184 "nbyte", count, |
191 "offset", attr.offs)) { | 185 "offset", attr.offs)) { |
192 ERROR("Failed to send request."); | 186 LOG_ERROR("Failed to send request."); |
193 return EINVAL; | 187 return EINVAL; |
194 } | 188 } |
195 | 189 |
196 int32_t error; | 190 int32_t error; |
197 | 191 |
198 PP_Var buf_var; | 192 PP_Var buf_var; |
199 int result = | 193 int result = |
200 ScanVar(response.pp_var(), "%d%p", "error", &error, "buf", &buf_var); | 194 ScanVar(response.pp_var(), "%d%p", "error", &error, "buf", &buf_var); |
201 ScopedVar scoped_buf_var(ppapi_, buf_var); | 195 ScopedVar scoped_buf_var(ppapi_, buf_var); |
202 | 196 |
203 if (result >= 1 && error) | 197 if (result >= 1 && error) |
204 return error; | 198 return error; |
205 | 199 |
206 if (result != 2) { | 200 if (result != 2) { |
207 ERROR("Expected \"error\" and \"buf\" fields in response."); | 201 LOG_ERROR("Expected \"error\" and \"buf\" fields in response."); |
208 return EINVAL; | 202 return EINVAL; |
209 } | 203 } |
210 | 204 |
211 if (buf_var.type != PP_VARTYPE_ARRAY_BUFFER) { | 205 if (buf_var.type != PP_VARTYPE_ARRAY_BUFFER) { |
212 ERROR("Expected \"buf\" to be an ArrayBuffer."); | 206 LOG_ERROR("Expected \"buf\" to be an ArrayBuffer."); |
213 return EINVAL; | 207 return EINVAL; |
214 } | 208 } |
215 | 209 |
216 uint32_t src_buf_len; | 210 uint32_t src_buf_len; |
217 if (!buffer_iface_->ByteLength(buf_var, &src_buf_len)) { | 211 if (!buffer_iface_->ByteLength(buf_var, &src_buf_len)) { |
218 ERROR("Unable to get byteLength of \"buf\"."); | 212 LOG_ERROR("Unable to get byteLength of \"buf\"."); |
219 return EINVAL; | 213 return EINVAL; |
220 } | 214 } |
221 | 215 |
222 if (src_buf_len > count) | 216 if (src_buf_len > count) |
223 src_buf_len = count; | 217 src_buf_len = count; |
224 | 218 |
225 void* src_buf = buffer_iface_->Map(buf_var); | 219 void* src_buf = buffer_iface_->Map(buf_var); |
226 if (src_buf == NULL) { | 220 if (src_buf == NULL) { |
227 ERROR("Unable to map \"buf\"."); | 221 LOG_ERROR("Unable to map \"buf\"."); |
228 return EINVAL; | 222 return EINVAL; |
229 } | 223 } |
230 | 224 |
231 memcpy(buf, src_buf, src_buf_len); | 225 memcpy(buf, src_buf, src_buf_len); |
232 *out_bytes = src_buf_len; | 226 *out_bytes = src_buf_len; |
233 | 227 |
234 buffer_iface_->Unmap(buf_var); | 228 buffer_iface_->Unmap(buf_var); |
235 | 229 |
236 return 0; | 230 return 0; |
237 } | 231 } |
238 | 232 |
239 Error JsFsNode::Write(const HandleAttr& attr, | 233 Error JsFsNode::Write(const HandleAttr& attr, |
240 const void* buf, | 234 const void* buf, |
241 size_t count, | 235 size_t count, |
242 int* out_bytes) { | 236 int* out_bytes) { |
243 AUTO_LOCK(node_lock_); | 237 AUTO_LOCK(node_lock_); |
244 | 238 |
245 *out_bytes = 0; | 239 *out_bytes = 0; |
246 | 240 |
247 PP_Var buf_var = buffer_iface_->Create(count); | 241 PP_Var buf_var = buffer_iface_->Create(count); |
248 ScopedVar scoped_buf_var(ppapi_, buf_var); | 242 ScopedVar scoped_buf_var(ppapi_, buf_var); |
249 | 243 |
250 if (buf_var.type != PP_VARTYPE_ARRAY_BUFFER) { | 244 if (buf_var.type != PP_VARTYPE_ARRAY_BUFFER) { |
251 ERROR("Unable to create \"buf\" var."); | 245 LOG_ERROR("Unable to create \"buf\" var."); |
252 return EINVAL; | 246 return EINVAL; |
253 } | 247 } |
254 | 248 |
255 void* dst_buf = buffer_iface_->Map(buf_var); | 249 void* dst_buf = buffer_iface_->Map(buf_var); |
256 if (dst_buf == NULL) { | 250 if (dst_buf == NULL) { |
257 ERROR("Unable to map \"buf\"."); | 251 LOG_ERROR("Unable to map \"buf\"."); |
258 return EINVAL; | 252 return EINVAL; |
259 } | 253 } |
260 | 254 |
261 memcpy(dst_buf, buf, count); | 255 memcpy(dst_buf, buf, count); |
262 | 256 |
263 buffer_iface_->Unmap(buf_var); | 257 buffer_iface_->Unmap(buf_var); |
264 | 258 |
265 ScopedVar response(ppapi_); | 259 ScopedVar response(ppapi_); |
266 if (!SendRequestAndWait(&response, "%s%d%p%u%lld", | 260 if (!SendRequestAndWait(&response, "%s%d%p%u%lld", |
267 "cmd", "pwrite", | 261 "cmd", "pwrite", |
268 "fildes", fd_, | 262 "fildes", fd_, |
269 "buf", &buf_var, | 263 "buf", &buf_var, |
270 "nbyte", count, | 264 "nbyte", count, |
271 "offset", attr.offs)) { | 265 "offset", attr.offs)) { |
272 ERROR("Failed to send request."); | 266 LOG_ERROR("Failed to send request."); |
273 return EINVAL; | 267 return EINVAL; |
274 } | 268 } |
275 | 269 |
276 int error; | 270 int error; |
277 uint32_t nwrote; | 271 uint32_t nwrote; |
278 int result = | 272 int result = |
279 ScanVar(response.pp_var(), "%d%u", "error", &error, "nwrote", &nwrote); | 273 ScanVar(response.pp_var(), "%d%u", "error", &error, "nwrote", &nwrote); |
280 | 274 |
281 if (result >= 1 && error) | 275 if (result >= 1 && error) |
282 return error; | 276 return error; |
283 | 277 |
284 if (result != 2) { | 278 if (result != 2) { |
285 ERROR("Expected \"error\" and \"nwrote\" fields in response."); | 279 LOG_ERROR("Expected \"error\" and \"nwrote\" fields in response."); |
286 return EINVAL; | 280 return EINVAL; |
287 } | 281 } |
288 | 282 |
289 *out_bytes = nwrote; | 283 *out_bytes = nwrote; |
290 return 0; | 284 return 0; |
291 } | 285 } |
292 | 286 |
293 Error JsFsNode::GetDents(size_t offs, | 287 Error JsFsNode::GetDents(size_t offs, |
294 struct dirent* pdir, | 288 struct dirent* pdir, |
295 size_t count, | 289 size_t count, |
296 int* out_bytes) { | 290 int* out_bytes) { |
297 AUTO_LOCK(node_lock_); | 291 AUTO_LOCK(node_lock_); |
298 | 292 |
299 *out_bytes = 0; | 293 *out_bytes = 0; |
300 | 294 |
301 // Round to the nearest sizeof(dirent) and ask for that. | 295 // Round to the nearest sizeof(dirent) and ask for that. |
302 size_t first = offs / sizeof(dirent); | 296 size_t first = offs / sizeof(dirent); |
303 size_t last = (offs + count + sizeof(dirent) - 1) / sizeof(dirent); | 297 size_t last = (offs + count + sizeof(dirent) - 1) / sizeof(dirent); |
304 | 298 |
305 ScopedVar response(ppapi_); | 299 ScopedVar response(ppapi_); |
306 if (!SendRequestAndWait(&response, "%s%d%u%u", | 300 if (!SendRequestAndWait(&response, "%s%d%u%u", |
307 "cmd", "getdents", | 301 "cmd", "getdents", |
308 "fildes", fd_, | 302 "fildes", fd_, |
309 "offs", first, | 303 "offs", first, |
310 "count", last - first)) { | 304 "count", last - first)) { |
311 ERROR("Failed to send request."); | 305 LOG_ERROR("Failed to send request."); |
312 return EINVAL; | 306 return EINVAL; |
313 } | 307 } |
314 | 308 |
315 int error; | 309 int error; |
316 PP_Var dirents_var; | 310 PP_Var dirents_var; |
317 int result = ScanVar( | 311 int result = ScanVar( |
318 response.pp_var(), "%d%p", "error", &error, "dirents", &dirents_var); | 312 response.pp_var(), "%d%p", "error", &error, "dirents", &dirents_var); |
319 | 313 |
320 ScopedVar scoped_dirents_var(ppapi_, dirents_var); | 314 ScopedVar scoped_dirents_var(ppapi_, dirents_var); |
321 | 315 |
322 if (result >= 1 && error) | 316 if (result >= 1 && error) |
323 return error; | 317 return error; |
324 | 318 |
325 if (result != 2) { | 319 if (result != 2) { |
326 ERROR("Expected \"error\" and \"dirents\" fields in response."); | 320 LOG_ERROR("Expected \"error\" and \"dirents\" fields in response."); |
327 return EINVAL; | 321 return EINVAL; |
328 } | 322 } |
329 | 323 |
330 if (dirents_var.type != PP_VARTYPE_ARRAY) { | 324 if (dirents_var.type != PP_VARTYPE_ARRAY) { |
331 ERROR("Expected \"dirents\" to be an Array."); | 325 LOG_ERROR("Expected \"dirents\" to be an Array."); |
332 return EINVAL; | 326 return EINVAL; |
333 } | 327 } |
334 | 328 |
335 uint32_t dirents_len = array_iface_->GetLength(dirents_var); | 329 uint32_t dirents_len = array_iface_->GetLength(dirents_var); |
336 uint32_t dirents_byte_len = dirents_len * sizeof(dirent); | 330 uint32_t dirents_byte_len = dirents_len * sizeof(dirent); |
337 | 331 |
338 // Allocate enough full dirents to copy from. This makes it easier if, for | 332 // Allocate enough full dirents to copy from. This makes it easier if, for |
339 // some reason, we are reading unaligned dirents. | 333 // some reason, we are reading unaligned dirents. |
340 dirent* dirents = static_cast<dirent*>(malloc(dirents_byte_len)); | 334 dirent* dirents = static_cast<dirent*>(malloc(dirents_byte_len)); |
341 | 335 |
342 for (uint32_t i = 0; i < dirents_len; ++i) { | 336 for (uint32_t i = 0; i < dirents_len; ++i) { |
343 PP_Var dirent_var = array_iface_->Get(dirents_var, i); | 337 PP_Var dirent_var = array_iface_->Get(dirents_var, i); |
344 PP_Var d_name_var; | 338 PP_Var d_name_var; |
345 result = ScanVar(dirent_var, | 339 result = ScanVar(dirent_var, |
346 "%lld%p", | 340 "%lld%p", |
347 "d_ino", &dirents[i].d_ino, | 341 "d_ino", &dirents[i].d_ino, |
348 "d_name", &d_name_var); | 342 "d_name", &d_name_var); |
349 ScopedVar scoped_dirent_var(ppapi_, dirent_var); | 343 ScopedVar scoped_dirent_var(ppapi_, dirent_var); |
350 ScopedVar scoped_d_name_var(ppapi_, d_name_var); | 344 ScopedVar scoped_d_name_var(ppapi_, d_name_var); |
351 | 345 |
352 if (result != 2) { | 346 if (result != 2) { |
353 ERROR("Expected dirent[%d] to have \"d_ino\" and \"d_name\".", i); | 347 LOG_ERROR("Expected dirent[%d] to have \"d_ino\" and \"d_name\".", i); |
354 free(dirents); | 348 free(dirents); |
355 return EINVAL; | 349 return EINVAL; |
356 } | 350 } |
357 | 351 |
358 uint32_t d_name_len; | 352 uint32_t d_name_len; |
359 const char* d_name = var_iface_->VarToUtf8(d_name_var, &d_name_len); | 353 const char* d_name = var_iface_->VarToUtf8(d_name_var, &d_name_len); |
360 | 354 |
361 dirents[i].d_off = sizeof(dirent); | 355 dirents[i].d_off = sizeof(dirent); |
362 dirents[i].d_reclen = sizeof(dirent); | 356 dirents[i].d_reclen = sizeof(dirent); |
363 strncpy(dirents[i].d_name, d_name, sizeof(dirents[i].d_name)); | 357 strncpy(dirents[i].d_name, d_name, sizeof(dirents[i].d_name)); |
364 } | 358 } |
365 | 359 |
366 size_t dirents_offs = offs - first * sizeof(dirent); | 360 size_t dirents_offs = offs - first * sizeof(dirent); |
367 if (dirents_offs + count > dirents_byte_len) | 361 if (dirents_offs + count > dirents_byte_len) |
368 count = dirents_byte_len - dirents_offs; | 362 count = dirents_byte_len - dirents_offs; |
369 | 363 |
370 memcpy(pdir, reinterpret_cast<const char*>(dirents) + dirents_offs, count); | 364 memcpy(pdir, reinterpret_cast<const char*>(dirents) + dirents_offs, count); |
371 *out_bytes = count; | 365 *out_bytes = count; |
372 | 366 |
373 free(dirents); | 367 free(dirents); |
374 return 0; | 368 return 0; |
375 } | 369 } |
376 | 370 |
377 } // namespace nacl_io | 371 } // namespace nacl_io |
OLD | NEW |