Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / libraries / nacl_io / memfs / mem_fs.cc
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
3 // found in the LICENSE file.
4
5 #include "nacl_io/memfs/mem_fs.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9
10 #include <string>
11
12 #include "nacl_io/dir_node.h"
13 #include "nacl_io/filesystem.h"
14 #include "nacl_io/memfs/mem_fs_node.h"
15 #include "nacl_io/node.h"
16 #include "nacl_io/osstat.h"
17 #include "nacl_io/osunistd.h"
18 #include "nacl_io/path.h"
19 #include "sdk_util/auto_lock.h"
20 #include "sdk_util/ref_object.h"
21
22 namespace nacl_io {
23
24 MemFs::MemFs() : root_(NULL) {
25 }
26
27 Error MemFs::Init(const FsInitArgs& args) {
28   Error error = Filesystem::Init(args);
29   if (error)
30     return error;
31
32   root_.reset(new DirNode(this, S_IRALL | S_IWALL | S_IXALL));
33   error = root_->Init(0);
34   if (error) {
35     root_.reset(NULL);
36     return error;
37   }
38   return 0;
39 }
40
41 Error MemFs::FindNode(const Path& path, int type, ScopedNode* out_node) {
42   out_node->reset(NULL);
43   ScopedNode node = root_;
44
45   // If there is no root there, we have an error.
46   if (node == NULL)
47     return ENOTDIR;
48
49   // We are expecting an "absolute" path from this mount point.
50   if (!path.IsAbsolute())
51     return EINVAL;
52
53   // Starting at the root, traverse the path parts.
54   for (size_t index = 1; node && index < path.Size(); index++) {
55     // If not a directory, then we have an error so return.
56     if (!node->IsaDir())
57       return ENOTDIR;
58
59     // Find the child node
60     Error error = node->FindChild(path.Part(index), &node);
61     if (error)
62       return error;
63   }
64
65   // If a directory is expected, but it's not a directory, then fail.
66   if ((type & S_IFDIR) && !node->IsaDir())
67     return ENOTDIR;
68
69   // If a file is expected, but it's not a file, then fail.
70   if ((type & S_IFREG) && node->IsaDir())
71     return EISDIR;
72
73   // We now have a valid object of the expected type, so return it.
74   *out_node = node;
75   return 0;
76 }
77
78 Error MemFs::OpenWithMode(const Path& path, int open_flags, mode_t mode,
79                           ScopedNode* out_node) {
80   out_node->reset(NULL);
81   ScopedNode node;
82
83   Error error = FindNode(path, 0, &node);
84   if (error) {
85     // If the node does not exist and we can't create it, fail
86     if ((open_flags & O_CREAT) == 0)
87       return ENOENT;
88
89     // Now first find the parent directory to see if we can add it
90     ScopedNode parent;
91     error = FindNode(path.Parent(), S_IFDIR, &parent);
92     if (error)
93       return error;
94
95     node.reset(new MemFsNode(this));
96     error = node->Init(open_flags);
97     if (error)
98       return error;
99     node->SetMode(mode);
100
101     error = parent->AddChild(path.Basename(), node);
102     if (error)
103       return error;
104
105   } else {
106     // Opening an existing file.
107
108     // If we were expected to create it exclusively, fail
109     if (open_flags & O_EXCL)
110       return EEXIST;
111
112     if (open_flags & O_TRUNC)
113       node->FTruncate(0);
114   }
115
116   *out_node = node;
117   return 0;
118 }
119
120 Error MemFs::Mkdir(const Path& path, int mode) {
121   // We expect a Filesystem "absolute" path
122   if (!path.IsAbsolute())
123     return ENOENT;
124
125   // The root of the filesystem is already created by the filesystem
126   if (path.Size() == 1)
127     return EEXIST;
128
129   ScopedNode parent;
130   int error = FindNode(path.Parent(), S_IFDIR, &parent);
131   if (error)
132     return error;
133
134   ScopedNode node;
135   error = parent->FindChild(path.Basename(), &node);
136   if (!error)
137     return EEXIST;
138
139   if (error != ENOENT)
140     return error;
141
142   // Allocate a node, with a RefCount of 1.  If added to the parent
143   // it will get ref counted again.  In either case, release the
144   // recount we have on exit.
145   node.reset(new DirNode(this, mode));
146   error = node->Init(0);
147   if (error)
148     return error;
149
150   return parent->AddChild(path.Basename(), node);
151 }
152
153 Error MemFs::Unlink(const Path& path) {
154   return RemoveInternal(path, REMOVE_FILE);
155 }
156
157 Error MemFs::Rmdir(const Path& path) {
158   return RemoveInternal(path, REMOVE_DIR);
159 }
160
161 Error MemFs::Remove(const Path& path) {
162   return RemoveInternal(path, REMOVE_ALL);
163 }
164
165 Error MemFs::Rename(const Path& src_path, const Path& target_path) {
166   ScopedNode src_node;
167   ScopedNode src_parent;
168   ScopedNode target_node;
169   ScopedNode target_parent;
170   int error = FindNode(src_path, 0, &src_node);
171   if (error)
172     return error;
173
174   // The source must exist
175   error = FindNode(src_path.Parent(), S_IFDIR, &src_parent);
176   if (error)
177     return error;
178
179   // The parent of the target must exist
180   error = FindNode(target_path.Parent(), 0, &target_parent);
181   if (error)
182     return error;
183
184   std::string target_name = target_path.Basename();
185
186   // The target itself need not exist but if it does there are
187   // certain restrictions
188   error = FindNode(target_path, 0, &target_node);
189   bool replacing_target = error == 0;
190   if (replacing_target) {
191     if (target_node->IsaDir()) {
192       // If the target is a direcotry it must be empty
193       if (target_node->ChildCount()) {
194         return ENOTEMPTY;
195       }
196
197       if (src_node->IsaDir()) {
198         // Replacing an existing directory.
199         RemoveInternal(target_path, REMOVE_ALL);
200       } else {
201         // Renaming into an existing directory.
202         target_name = src_path.Basename();
203         target_parent = target_node;
204       }
205     } else {
206       if (src_node->IsaDir())
207         // Can't replace a file with a direcotory
208         return EISDIR;
209
210       // Replacing an existing file.
211       target_parent->RemoveChild(target_path.Basename());
212     }
213   }
214
215   // Perform that actual rename. Simply re-parent the original source node
216   // onto its new parent node.
217   error = src_parent->RemoveChild(src_path.Basename());
218   if (error)
219     return error;
220
221   error = target_parent->AddChild(target_name, src_node);
222   if (error) {
223     // Re-parent the old target_node if we failed to add the new one.
224     if (replacing_target)
225       target_parent->AddChild(target_path.Basename(), target_node);
226     // Re-parent the src_node
227     target_parent->AddChild(target_path.Basename(), src_node);
228     return error;
229   }
230
231   return 0;
232 }
233
234 Error MemFs::RemoveInternal(const Path& path, int remove_type) {
235   bool dir_only = remove_type == REMOVE_DIR;
236   bool file_only = remove_type == REMOVE_FILE;
237   bool remove_dir = (remove_type & REMOVE_DIR) != 0;
238
239   if (dir_only) {
240     // We expect a Filesystem "absolute" path
241     if (!path.IsAbsolute())
242       return ENOENT;
243
244     // The root of the filesystem is already created by the filesystem
245     if (path.Size() == 1)
246       return EEXIST;
247   }
248
249   ScopedNode parent;
250   int error = FindNode(path.Parent(), S_IFDIR, &parent);
251   if (error)
252     return error;
253
254   // Verify we find a child which is a directory.
255   ScopedNode child;
256   error = parent->FindChild(path.Basename(), &child);
257   if (error)
258     return error;
259
260   if (dir_only && !child->IsaDir())
261     return ENOTDIR;
262
263   if (file_only && child->IsaDir())
264     return EISDIR;
265
266   if (remove_dir && child->ChildCount() > 0)
267     return ENOTEMPTY;
268
269   return parent->RemoveChild(path.Basename());
270 }
271
272 }  // namespace nacl_io