OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/html5fs/html5_fs.h" | 5 #include "nacl_io/html5fs/html5_fs.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 24 matching lines...) Expand all Loading... | |
35 return Open(path, O_RDONLY, &node); | 35 return Open(path, O_RDONLY, &node); |
36 } | 36 } |
37 | 37 |
38 Error Html5Fs::Open(const Path& path, int open_flags, ScopedNode* out_node) { | 38 Error Html5Fs::Open(const Path& path, int open_flags, ScopedNode* out_node) { |
39 out_node->reset(NULL); | 39 out_node->reset(NULL); |
40 Error error = BlockUntilFilesystemOpen(); | 40 Error error = BlockUntilFilesystemOpen(); |
41 if (error) | 41 if (error) |
42 return error; | 42 return error; |
43 | 43 |
44 PP_Resource fileref = ppapi()->GetFileRefInterface()->Create( | 44 PP_Resource fileref = ppapi()->GetFileRefInterface()->Create( |
45 filesystem_resource_, path.Join().c_str()); | 45 filesystem_resource_, GetFullPath(path).Join().c_str()); |
46 if (!fileref) | 46 if (!fileref) |
47 return ENOENT; | 47 return ENOENT; |
48 | 48 |
49 ScopedNode node(new Html5FsNode(this, fileref)); | 49 ScopedNode node(new Html5FsNode(this, fileref)); |
50 error = node->Init(open_flags); | 50 error = node->Init(open_flags); |
51 if (error) | 51 if (error) |
52 return error; | 52 return error; |
53 | 53 |
54 *out_node = node; | 54 *out_node = node; |
55 return 0; | 55 return 0; |
56 } | 56 } |
57 | 57 |
58 Path Html5Fs::GetFullPath(const Path& path) { | |
59 Path full_path(path); | |
60 //if (!prefix_.empty()) | |
binji
2014/06/09 20:54:29
remove
Sam Clegg
2014/06/10 17:20:58
Done.
| |
61 full_path.Prepend(prefix_); | |
62 return full_path; | |
63 } | |
64 | |
58 Error Html5Fs::Unlink(const Path& path) { | 65 Error Html5Fs::Unlink(const Path& path) { |
59 return Remove(path); | 66 return RemoveInternal(path, REMOVE_FILE); |
60 } | 67 } |
61 | 68 |
62 Error Html5Fs::Mkdir(const Path& path, int permissions) { | 69 Error Html5Fs::Mkdir(const Path& path, int permissions) { |
63 Error error = BlockUntilFilesystemOpen(); | 70 Error error = BlockUntilFilesystemOpen(); |
64 if (error) | 71 if (error) |
65 return error; | 72 return error; |
66 | 73 |
67 // FileRef returns PP_ERROR_NOACCESS which is translated to EACCES if you | 74 // FileRef returns PP_ERROR_NOACCESS which is translated to EACCES if you |
68 // try to create the root directory. EEXIST is a better errno here. | 75 // try to create the root directory. EEXIST is a better errno here. |
69 if (path.Top()) | 76 if (path.Top()) |
70 return EEXIST; | 77 return EEXIST; |
71 | 78 |
72 ScopedResource fileref_resource( | 79 ScopedResource fileref_resource( |
73 ppapi(), | 80 ppapi(), |
74 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | 81 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, |
75 path.Join().c_str())); | 82 GetFullPath(path).Join().c_str())); |
76 if (!fileref_resource.pp_resource()) | 83 if (!fileref_resource.pp_resource()) |
77 return ENOENT; | 84 return ENOENT; |
78 | 85 |
79 int32_t result = ppapi()->GetFileRefInterface()->MakeDirectory( | 86 int32_t result = ppapi()->GetFileRefInterface()->MakeDirectory( |
80 fileref_resource.pp_resource(), PP_FALSE, PP_BlockUntilComplete()); | 87 fileref_resource.pp_resource(), PP_FALSE, PP_BlockUntilComplete()); |
81 if (result != PP_OK) | 88 if (result != PP_OK) |
82 return PPErrorToErrno(result); | 89 return PPErrorToErrno(result); |
83 | 90 |
84 return 0; | 91 return 0; |
85 } | 92 } |
86 | 93 |
87 Error Html5Fs::Rmdir(const Path& path) { | 94 Error Html5Fs::Rmdir(const Path& path) { |
88 return Remove(path); | 95 return RemoveInternal(path, REMOVE_DIR); |
89 } | 96 } |
90 | 97 |
91 Error Html5Fs::Remove(const Path& path) { | 98 Error Html5Fs::Remove(const Path& path) { |
99 return RemoveInternal(path, REMOVE_ALL); | |
100 } | |
101 | |
102 Error Html5Fs::RemoveInternal(const Path& path, int remove_type) { | |
92 Error error = BlockUntilFilesystemOpen(); | 103 Error error = BlockUntilFilesystemOpen(); |
93 if (error) | 104 if (error) |
94 return error; | 105 return error; |
95 | 106 |
96 ScopedResource fileref_resource( | 107 ScopedResource fileref_resource( |
97 ppapi(), | 108 ppapi(), |
98 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | 109 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, |
99 path.Join().c_str())); | 110 GetFullPath(path).Join().c_str())); |
100 if (!fileref_resource.pp_resource()) | 111 if (!fileref_resource.pp_resource()) |
101 return ENOENT; | 112 return ENOENT; |
102 | 113 |
114 // Check file type | |
115 if (remove_type != REMOVE_ALL) { | |
116 PP_FileInfo file_info; | |
117 int32_t query_result = ppapi()->GetFileRefInterface()->Query( | |
118 fileref_resource.pp_resource(), &file_info, PP_BlockUntilComplete()); | |
119 if (query_result != PP_OK) { | |
120 LOG_ERROR("Error querying file type"); | |
121 return EINVAL; | |
122 } | |
123 switch (file_info.type) { | |
124 case PP_FILETYPE_DIRECTORY: | |
125 if (!(remove_type & REMOVE_DIR)) | |
126 return EISDIR; | |
127 break; | |
128 case PP_FILETYPE_REGULAR: | |
129 if (!(remove_type & REMOVE_FILE)) | |
130 return ENOTDIR; | |
131 break; | |
132 default: | |
133 LOG_ERROR("Invalid file type: %d", file_info.type); | |
134 return EINVAL; | |
135 } | |
136 } | |
137 | |
103 int32_t result = ppapi()->GetFileRefInterface()->Delete( | 138 int32_t result = ppapi()->GetFileRefInterface()->Delete( |
104 fileref_resource.pp_resource(), PP_BlockUntilComplete()); | 139 fileref_resource.pp_resource(), PP_BlockUntilComplete()); |
105 if (result != PP_OK) | 140 if (result != PP_OK) |
106 return PPErrorToErrno(result); | 141 return PPErrorToErrno(result); |
107 | 142 |
108 return 0; | 143 return 0; |
109 } | 144 } |
110 | 145 |
111 Error Html5Fs::Rename(const Path& path, const Path& newpath) { | 146 Error Html5Fs::Rename(const Path& path, const Path& newpath) { |
112 Error error = BlockUntilFilesystemOpen(); | 147 Error error = BlockUntilFilesystemOpen(); |
113 if (error) | 148 if (error) |
114 return error; | 149 return error; |
115 | 150 |
151 const char* oldpath_full = GetFullPath(path).Join().c_str(); | |
116 ScopedResource fileref_resource( | 152 ScopedResource fileref_resource( |
117 ppapi(), | 153 ppapi(), |
118 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | 154 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, |
119 path.Join().c_str())); | 155 oldpath_full)); |
binji
2014/06/09 20:54:29
do these fit on the previous line now?
Sam Clegg
2014/06/10 17:20:58
Nope :(
| |
120 if (!fileref_resource.pp_resource()) | 156 if (!fileref_resource.pp_resource()) |
121 return ENOENT; | 157 return ENOENT; |
122 | 158 |
159 const char* newpath_full = GetFullPath(newpath).Join().c_str(); | |
123 ScopedResource new_fileref_resource( | 160 ScopedResource new_fileref_resource( |
124 ppapi(), | 161 ppapi(), |
125 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, | 162 ppapi()->GetFileRefInterface()->Create(filesystem_resource_, |
126 newpath.Join().c_str())); | 163 newpath_full)); |
127 if (!new_fileref_resource.pp_resource()) | 164 if (!new_fileref_resource.pp_resource()) |
128 return ENOENT; | 165 return ENOENT; |
129 | 166 |
130 int32_t result = | 167 int32_t result = |
131 ppapi()->GetFileRefInterface()->Rename(fileref_resource.pp_resource(), | 168 ppapi()->GetFileRefInterface()->Rename(fileref_resource.pp_resource(), |
132 new_fileref_resource.pp_resource(), | 169 new_fileref_resource.pp_resource(), |
133 PP_BlockUntilComplete()); | 170 PP_BlockUntilComplete()); |
134 if (result != PP_OK) | 171 if (result != PP_OK) |
135 return PPErrorToErrno(result); | 172 return PPErrorToErrno(result); |
136 | 173 |
(...skipping 20 matching lines...) Expand all Loading... | |
157 PP_FileSystemType filesystem_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT; | 194 PP_FileSystemType filesystem_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT; |
158 int64_t expected_size = 0; | 195 int64_t expected_size = 0; |
159 for (StringMap_t::const_iterator iter = args.string_map.begin(); | 196 for (StringMap_t::const_iterator iter = args.string_map.begin(); |
160 iter != args.string_map.end(); | 197 iter != args.string_map.end(); |
161 ++iter) { | 198 ++iter) { |
162 if (iter->first == "type") { | 199 if (iter->first == "type") { |
163 if (iter->second == "PERSISTENT") { | 200 if (iter->second == "PERSISTENT") { |
164 filesystem_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT; | 201 filesystem_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT; |
165 } else if (iter->second == "TEMPORARY") { | 202 } else if (iter->second == "TEMPORARY") { |
166 filesystem_type = PP_FILESYSTEMTYPE_LOCALTEMPORARY; | 203 filesystem_type = PP_FILESYSTEMTYPE_LOCALTEMPORARY; |
204 } else if (iter->second == "") { | |
binji
2014/06/09 20:54:29
why allow this? The default is LOCALPERSISTENT
Sam Clegg
2014/06/10 17:20:58
The unit tests rely on this.. maybe I should chang
binji
2014/06/10 18:00:58
I'd prefer it, it seems weird to add support for a
| |
205 filesystem_type = PP_FILESYSTEMTYPE_LOCALPERSISTENT; | |
206 } else { | |
207 LOG_ERROR("html5fs: unknown type: '%s'", iter->second.c_str()); | |
208 return EINVAL; | |
167 } | 209 } |
168 } else if (iter->first == "expected_size") { | 210 } else if (iter->first == "expected_size") { |
169 expected_size = strtoull(iter->second.c_str(), NULL, 10); | 211 expected_size = strtoull(iter->second.c_str(), NULL, 10); |
170 } else if (iter->first == "filesystem_resource") { | 212 } else if (iter->first == "filesystem_resource") { |
171 PP_Resource resource = strtoull(iter->second.c_str(), NULL, 10); | 213 PP_Resource resource = strtoull(iter->second.c_str(), NULL, 10); |
172 if (!ppapi_->GetFileSystemInterface()->IsFileSystem(resource)) | 214 if (!ppapi_->GetFileSystemInterface()->IsFileSystem(resource)) |
173 return EINVAL; | 215 return EINVAL; |
174 | 216 |
175 filesystem_resource_ = resource; | 217 filesystem_resource_ = resource; |
176 ppapi_->AddRefResource(filesystem_resource_); | 218 ppapi_->AddRefResource(filesystem_resource_); |
219 } else if (iter->first == "SOURCE") { | |
220 prefix_ = iter->second; | |
221 } else { | |
222 LOG_ERROR("html5fs: bad param: %s", iter->first.c_str()); | |
223 return EINVAL; | |
177 } | 224 } |
178 } | 225 } |
179 | 226 |
180 if (filesystem_resource_ != 0) { | 227 if (filesystem_resource_ != 0) { |
181 filesystem_open_has_result_ = true; | 228 filesystem_open_has_result_ = true; |
182 filesystem_open_error_ = PP_OK; | 229 filesystem_open_error_ = PP_OK; |
183 return 0; | 230 return 0; |
184 } | 231 } |
185 | 232 |
186 // Initialize filesystem. | 233 // Initialize filesystem. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 } | 280 } |
234 | 281 |
235 void Html5Fs::FilesystemOpenCallback(int32_t result) { | 282 void Html5Fs::FilesystemOpenCallback(int32_t result) { |
236 AUTO_LOCK(filesysem_open_lock_); | 283 AUTO_LOCK(filesysem_open_lock_); |
237 filesystem_open_has_result_ = true; | 284 filesystem_open_has_result_ = true; |
238 filesystem_open_error_ = PPErrorToErrno(result); | 285 filesystem_open_error_ = PPErrorToErrno(result); |
239 pthread_cond_signal(&filesystem_open_cond_); | 286 pthread_cond_signal(&filesystem_open_cond_); |
240 } | 287 } |
241 | 288 |
242 } // namespace nacl_io | 289 } // namespace nacl_io |
OLD | NEW |