OLD | NEW |
| (Empty) |
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 | |
3 * found in the LICENSE file. | |
4 */ | |
5 | |
6 #include "nacl_mounts/mount_html5fs.h" | |
7 | |
8 #include <errno.h> | |
9 #include <ppapi/c/pp_completion_callback.h> | |
10 #include <ppapi/c/pp_errors.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
13 #include <algorithm> | |
14 #include "nacl_mounts/mount_node_html5fs.h" | |
15 #include "utils/auto_lock.h" | |
16 | |
17 namespace { | |
18 | |
19 #if defined(WIN32) | |
20 int64_t strtoull(const char* nptr, char** endptr, int base) { | |
21 return _strtoui64(nptr, endptr, base); | |
22 } | |
23 #endif | |
24 | |
25 } // namespace | |
26 | |
27 MountNode *MountHtml5Fs::Open(const Path& path, int mode) { | |
28 if (!IsFilesystemOpen()) | |
29 return NULL; | |
30 | |
31 PP_Resource fileref = ppapi()->GetFileRefInterface()->Create( | |
32 filesystem_resource_, path.Join().c_str()); | |
33 if (!fileref) | |
34 return NULL; | |
35 | |
36 MountNodeHtml5Fs* node = new MountNodeHtml5Fs(this, fileref); | |
37 if (!node->Init(mode)) { | |
38 node->Release(); | |
39 return NULL; | |
40 } | |
41 | |
42 return node; | |
43 } | |
44 | |
45 int MountHtml5Fs::Unlink(const Path& path) { | |
46 return Remove(path); | |
47 } | |
48 | |
49 int MountHtml5Fs::Mkdir(const Path& path, int permissions) { | |
50 if (!IsFilesystemOpen()) { | |
51 errno = EINVAL; | |
52 return -1; | |
53 } | |
54 | |
55 ScopedResource fileref_resource( | |
56 ppapi(), ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | |
57 path.Join().c_str())); | |
58 if (!fileref_resource.pp_resource()) { | |
59 errno = EINVAL; | |
60 return -1; | |
61 } | |
62 | |
63 int32_t result = ppapi()->GetFileRefInterface()->MakeDirectory( | |
64 fileref_resource.pp_resource(), PP_FALSE, PP_BlockUntilComplete()); | |
65 if (result != PP_OK) { | |
66 errno = PPErrorToErrno(result); | |
67 return -1; | |
68 } | |
69 | |
70 return 0; | |
71 } | |
72 | |
73 int MountHtml5Fs::Rmdir(const Path& path) { | |
74 return Remove(path); | |
75 } | |
76 | |
77 int MountHtml5Fs::Remove(const Path& path) { | |
78 if (!IsFilesystemOpen()) { | |
79 errno = EINVAL; | |
80 return -1; | |
81 } | |
82 | |
83 ScopedResource fileref_resource( | |
84 ppapi(), ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | |
85 path.Join().c_str())); | |
86 if (!fileref_resource.pp_resource()) { | |
87 errno = EINVAL; | |
88 return -1; | |
89 } | |
90 | |
91 int32_t result = ppapi()->GetFileRefInterface()->Delete( | |
92 fileref_resource.pp_resource(), | |
93 PP_BlockUntilComplete()); | |
94 if (result != PP_OK) { | |
95 errno = PPErrorToErrno(result); | |
96 return -1; | |
97 } | |
98 | |
99 return 0; | |
100 } | |
101 | |
102 | |
103 MountHtml5Fs::MountHtml5Fs() | |
104 : filesystem_resource_(0), | |
105 filesystem_open_(false) { | |
106 } | |
107 | |
108 bool MountHtml5Fs::Init(int dev, StringMap_t& args, PepperInterface* ppapi) { | |
109 if (!Mount::Init(dev, args, ppapi)) | |
110 return false; | |
111 | |
112 if (!ppapi) | |
113 return false; | |
114 | |
115 // Parse mount args. | |
116 PP_FileSystemType filesystem_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT; | |
117 int64_t expected_size = 0; | |
118 for (StringMap_t::iterator iter = args.begin(), end = args.end(); iter != end; | |
119 ++iter) { | |
120 if (iter->first == "type") { | |
121 if (iter->second == "PERSISTENT") { | |
122 filesystem_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT; | |
123 } else if (iter->second == "TEMPORARY") { | |
124 filesystem_type = PP_FILESYSTEMTYPE_LOCALTEMPORARY; | |
125 } | |
126 } else if (iter->first == "expected_size") { | |
127 expected_size = strtoull(iter->second.c_str(), NULL, 10); | |
128 } | |
129 } | |
130 | |
131 // Initialize filesystem. | |
132 filesystem_resource_ = ppapi->GetFileSystemInterface()->Create( | |
133 ppapi_->GetInstance(), filesystem_type); | |
134 | |
135 if (filesystem_resource_ == 0) | |
136 return false; | |
137 | |
138 // Open the filesystem. Don't block, this could be called from the main | |
139 // thread. | |
140 ppapi->GetFileSystemInterface()->Open(filesystem_resource_, expected_size, | |
141 PP_MakeCompletionCallback(&MountHtml5Fs::FilesystemOpenCallbackThunk, | |
142 this)); | |
143 | |
144 return true; | |
145 } | |
146 | |
147 void MountHtml5Fs::Destroy() { | |
148 ppapi_->ReleaseResource(filesystem_resource_); | |
149 } | |
150 | |
151 bool MountHtml5Fs::IsFilesystemOpen() { | |
152 AutoLock lock(&lock_); | |
153 return filesystem_open_; | |
154 } | |
155 | |
156 // static | |
157 void MountHtml5Fs::FilesystemOpenCallbackThunk(void* user_data, | |
158 int32_t result) { | |
159 MountHtml5Fs* self = static_cast<MountHtml5Fs*>(user_data); | |
160 self->FilesystemOpenCallback(result); | |
161 } | |
162 | |
163 void MountHtml5Fs::FilesystemOpenCallback(int32_t result) { | |
164 AutoLock lock(&lock_); | |
165 filesystem_open_ = result == PP_OK; | |
166 } | |
OLD | NEW |