Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc

Issue 10829027: [NaCl SDK] Add nacl_mounts to NaCl SDK build. Experimental for now. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix for windows Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include <dirent.h> 5 #include "nacl_mounts/mount_mem.h"
6
6 #include <errno.h> 7 #include <errno.h>
7 #include <fcntl.h> 8 #include <fcntl.h>
8 #include <sys/stat.h>
9 #include <string> 9 #include <string>
10 #include <unistd.h>
11 10
12 #include "mount.h" 11 #include "nacl_mounts/mount.h"
13 #include "mount_mem.h" 12 #include "nacl_mounts/mount_node.h"
14 #include "mount_node.h" 13 #include "nacl_mounts/mount_node_dir.h"
15 #include "mount_node_dir.h" 14 #include "nacl_mounts/mount_node_mem.h"
16 #include "mount_node_mem.h" 15 #include "nacl_mounts/osstat.h"
17 #include "path.h" 16 #include "nacl_mounts/path.h"
18 17 #include "utils/auto_lock.h"
19 #include "auto_lock.h" 18 #include "utils/ref_object.h"
20 #include "ref_object.h"
21 19
22 // TODO(noelallen) : Grab/Redefine these in the kernel object once available. 20 // TODO(noelallen) : Grab/Redefine these in the kernel object once available.
23 #define USR_ID 1002 21 #define USR_ID 1002
24 #define GRP_ID 1003 22 #define GRP_ID 1003
25 23
26 MountMem::MountMem() 24 MountMem::MountMem()
27 : MountFactory(), 25 : root_(NULL),
28 root_(NULL),
29 max_ino_(0) { 26 max_ino_(0) {
30 } 27 }
31 28
32 bool MountMem::Init(int dev, StringMap_t& args) { 29 bool MountMem::Init(int dev, StringMap_t& args) {
33 dev_ = dev; 30 dev_ = dev;
34 root_ = AllocatePath(_S_IREAD | _S_IWRITE); 31 root_ = AllocatePath(S_IREAD | S_IWRITE);
35 return (bool) (root_ != NULL); 32 return (bool) (root_ != NULL);
36 } 33 }
37 34
38 void MountMem::Destroy() { 35 void MountMem::Destroy() {
39 if (root_) 36 if (root_)
40 root_->Release(); 37 root_->Release();
41 root_ = NULL; 38 root_ = NULL;
42 } 39 }
43 40
44 MountNode* MountMem::AllocatePath(int mode) { 41 MountNode* MountMem::AllocatePath(int mode) {
45 ino_t ino = AllocateINO(); 42 ino_t ino = AllocateINO();
46 43
47 MountNode *ptr = new MountNodeDir(this, ino, dev_); 44 MountNode *ptr = new MountNodeDir(this, ino, dev_);
48 if (!ptr->Init(mode, 1002, 1003)) { 45 if (!ptr->Init(mode, USR_ID, GRP_ID)) {
49 ptr->Release(); 46 ptr->Release();
50 FreeINO(ino); 47 FreeINO(ino);
51 return NULL; 48 return NULL;
52 } 49 }
53 return ptr; 50 return ptr;
54 } 51 }
55 52
56 MountNode* MountMem::AllocateData(int mode) { 53 MountNode* MountMem::AllocateData(int mode) {
57 ino_t ino = AllocateINO(); 54 ino_t ino = AllocateINO();
58 55
59 MountNode* ptr = new MountNodeMem(this, ino, dev_); 56 MountNode* ptr = new MountNodeMem(this, ino, dev_);
60 if (!ptr->Init(mode, getuid(), getgid())) { 57 //if (!ptr->Init(mode, getuid(), getgid())) {
58 if (!ptr->Init(mode, USR_ID, GRP_ID)) {
61 ptr->Release(); 59 ptr->Release();
62 FreeINO(ino); 60 FreeINO(ino);
63 return NULL; 61 return NULL;
64 } 62 }
65 return ptr; 63 return ptr;
66 } 64 }
67 65
68 void MountMem::ReleaseNode(MountNode* node) { 66 void MountMem::ReleaseNode(MountNode* node) {
69 node->Release(); 67 node->Release();
70 } 68 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 // If not a directory, then we have an error so return. 110 // If not a directory, then we have an error so return.
113 if (!node->IsaDir()) { 111 if (!node->IsaDir()) {
114 errno = ENOTDIR; 112 errno = ENOTDIR;
115 return NULL; 113 return NULL;
116 } 114 }
117 115
118 // Find the child node 116 // Find the child node
119 node = node->FindChild(path.Part(index)); 117 node = node->FindChild(path.Part(index));
120 } 118 }
121 119
122 // node should be root, a found child, or a failed 'FindChild' 120 // node should be root, a found child, or a failed 'FindChild'
123 // which already has the correct errno set. 121 // which already has the correct errno set.
124 if (NULL == node) return NULL; 122 if (NULL == node) return NULL;
125 123
126 // If a directory is expected, but it's not a directory, then fail. 124 // If a directory is expected, but it's not a directory, then fail.
127 if ((type & _S_IFDIR) && !node->IsaDir()) { 125 if ((type & S_IFDIR) && !node->IsaDir()) {
128 errno = ENOTDIR; 126 errno = ENOTDIR;
129 return NULL; 127 return NULL;
130 } 128 }
131 129
132 // If a file is expected, but it's not a file, then fail. 130 // If a file is expected, but it's not a file, then fail.
133 if ((type & _S_IFREG) && node->IsaDir()) { 131 if ((type & S_IFREG) && node->IsaDir()) {
134 errno = EISDIR; 132 errno = EISDIR;
135 return NULL; 133 return NULL;
136 } 134 }
137 135
138 // We now have a valid object of the expected type, so return it. 136 // We now have a valid object of the expected type, so return it.
139 return node; 137 return node;
140 } 138 }
141 139
142 MountNode* MountMem::Open(const Path& path, int mode) { 140 MountNode* MountMem::Open(const Path& path, int mode) {
143 AutoLock lock(&lock_); 141 AutoLock lock(&lock_);
144 MountNode* node = FindNode(path); 142 MountNode* node = FindNode(path);
145 143
146 if (NULL == node) { 144 if (NULL == node) {
147 // Now first find the parent directory to see if we can add it 145 // Now first find the parent directory to see if we can add it
148 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); 146 MountNode* parent = FindNode(path.Parent(), S_IFDIR);
149 if (NULL == parent) return NULL; 147 if (NULL == parent) return NULL;
150 148
151 // If the node does not exist and we can't create it, fail 149 // If the node does not exist and we can't create it, fail
152 if ((mode & O_CREAT) == 0) return NULL; 150 if ((mode & O_CREAT) == 0) return NULL;
153 151
154 // Otherwise, create it with a single refernece 152 // Otherwise, create it with a single refernece
155 mode = OpenModeToPermission(mode); 153 mode = OpenModeToPermission(mode);
156 node = AllocateData(mode); 154 node = AllocateData(mode);
157 if (NULL == node) return NULL; 155 if (NULL == node) return NULL;
158 156
159 if (parent->AddChild(path.Filename(), node) == -1) { 157 if (parent->AddChild(path.Basename(), node) == -1) {
160 // Or if it fails, release it 158 // Or if it fails, release it
161 node->Release(); 159 node->Release();
162 return NULL; 160 return NULL;
163 } 161 }
164 return node; 162 return node;
165 } 163 }
166 164
167 // If we were expected to create it exclusively, fail 165 // If we were expected to create it exclusively, fail
168 if (mode & O_EXCL) { 166 if (mode & O_EXCL) {
169 errno = EEXIST; 167 errno = EEXIST;
(...skipping 15 matching lines...) Expand all
185 183
186 int MountMem::Close(MountNode* node) { 184 int MountMem::Close(MountNode* node) {
187 AutoLock lock(&lock_); 185 AutoLock lock(&lock_);
188 node->Close(); 186 node->Close();
189 ReleaseNode(node); 187 ReleaseNode(node);
190 return 0; 188 return 0;
191 } 189 }
192 190
193 int MountMem::Unlink(const Path& path) { 191 int MountMem::Unlink(const Path& path) {
194 AutoLock lock(&lock_); 192 AutoLock lock(&lock_);
195 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); 193 MountNode* parent = FindNode(path.Parent(), S_IFDIR);
196 194
197 if (NULL == parent) return -1; 195 if (NULL == parent) return -1;
198 196
199 MountNode* child = parent->FindChild(path.Filename()); 197 MountNode* child = parent->FindChild(path.Basename());
200 if (NULL == child) { 198 if (NULL == child) {
201 errno = ENOENT; 199 errno = ENOENT;
202 return -1; 200 return -1;
203 } 201 }
204 if (child->IsaDir()) { 202 if (child->IsaDir()) {
205 errno = EISDIR; 203 errno = EISDIR;
206 return -1; 204 return -1;
207 } 205 }
208 return parent->RemoveChild(path.Filename()); 206 return parent->RemoveChild(path.Basename());
209 } 207 }
210 208
211 int MountMem::Mkdir(const Path& path, int mode) { 209 int MountMem::Mkdir(const Path& path, int mode) {
212 AutoLock lock(&lock_); 210 AutoLock lock(&lock_);
213 211
214 // We expect a Mount "absolute" path 212 // We expect a Mount "absolute" path
215 if (!path.IsAbsolute()) { 213 if (!path.IsAbsolute()) {
216 errno = ENOENT; 214 errno = ENOENT;
217 return -1; 215 return -1;
218 } 216 }
219 217
220 // The root of the mount is already created by the mount 218 // The root of the mount is already created by the mount
221 if (path.Size() == 1) { 219 if (path.Size() == 1) {
222 errno = EEXIST; 220 errno = EEXIST;
223 return -1; 221 return -1;
224 } 222 }
225 223
226 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); 224 MountNode* parent = FindNode(path.Parent(), S_IFDIR);
227 MountNode* node; 225 MountNode* node;
228 226
229 // If we failed to find the parent, the error code is already set. 227 // If we failed to find the parent, the error code is already set.
230 if (NULL == parent) return -1; 228 if (NULL == parent) return -1;
231 229
232 node = parent->FindChild(path.Filename()); 230 node = parent->FindChild(path.Basename());
233 if (NULL != node) { 231 if (NULL != node) {
234 errno = EEXIST; 232 errno = EEXIST;
235 return -1; 233 return -1;
236 } 234 }
237 235
238 // Otherwise, create a new node and attempt to add it 236 // Otherwise, create a new node and attempt to add it
239 mode = OpenModeToPermission(mode); 237 mode = OpenModeToPermission(mode);
240 238
241 // Allocate a node, with a RefCount of 1. If added to the parent 239 // Allocate a node, with a RefCount of 1. If added to the parent
242 // it will get ref counted again. In either case, release the 240 // it will get ref counted again. In either case, release the
243 // recount we have on exit. 241 // recount we have on exit.
244 node = AllocatePath(_S_IREAD | _S_IWRITE); 242 node = AllocatePath(S_IREAD | S_IWRITE);
245 if (NULL == node) return -1; 243 if (NULL == node) return -1;
246 244
247 if (parent->AddChild(path.Filename(), node) == -1) { 245 if (parent->AddChild(path.Basename(), node) == -1) {
248 node->Release(); 246 node->Release();
249 return -1; 247 return -1;
250 } 248 }
251 249
252 node->Release(); 250 node->Release();
253 return 0; 251 return 0;
254 } 252 }
255 253
256 int MountMem::Rmdir(const Path& path) { 254 int MountMem::Rmdir(const Path& path) {
257 AutoLock lock(&lock_); 255 AutoLock lock(&lock_);
258 256
259 // We expect a Mount "absolute" path 257 // We expect a Mount "absolute" path
260 if (!path.IsAbsolute()) { 258 if (!path.IsAbsolute()) {
261 errno = ENOENT; 259 errno = ENOENT;
262 return -1; 260 return -1;
263 } 261 }
264 262
265 // The root of the mount is already created by the mount 263 // The root of the mount is already created by the mount
266 if (path.Size() == 1) { 264 if (path.Size() == 1) {
267 errno = EEXIST; 265 errno = EEXIST;
268 return -1; 266 return -1;
269 } 267 }
270 268
271 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); 269 MountNode* parent = FindNode(path.Parent(), S_IFDIR);
272 MountNode* node; 270 MountNode* node;
273 271
274 // If we failed to find the parent, the error code is already set. 272 // If we failed to find the parent, the error code is already set.
275 if (NULL == parent) return -1; 273 if (NULL == parent) return -1;
276 274
277 // Verify we find a child which is also a directory 275 // Verify we find a child which is also a directory
278 node = parent->FindChild(path.Filename()); 276 node = parent->FindChild(path.Basename());
279 if (NULL == node) { 277 if (NULL == node) {
280 errno = ENOENT; 278 errno = ENOENT;
281 return -1; 279 return -1;
282 } 280 }
283 if (!node->IsaDir()) { 281 if (!node->IsaDir()) {
284 errno = ENOTDIR; 282 errno = ENOTDIR;
285 return -1; 283 return -1;
286 } 284 }
287 if (node->ChildCount() > 0) { 285 if (node->ChildCount() > 0) {
288 errno = ENOTEMPTY; 286 errno = ENOTEMPTY;
289 return -1; 287 return -1;
290 } 288 }
291 return parent->RemoveChild(path.Filename()); 289 return parent->RemoveChild(path.Basename());
292 } 290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698