@@ -108,165 +108,4 @@ size_t Symlink::getSize() {
108108 return target.size ();
109109}
110110
111- //
112- // Path Parsing utilities
113- //
114-
115- ParsedPath getParsedPath (std::vector<std::string> pathParts,
116- long & err,
117- std::shared_ptr<File> forbiddenAncestor,
118- std::optional<__wasi_fd_t > baseFD) {
119- std::shared_ptr<Directory> curr;
120- auto begin = pathParts.begin ();
121-
122- if (pathParts.empty ()) {
123- err = -ENOENT;
124- return ParsedPath{{}, nullptr };
125- }
126-
127- // Check if the first path element is '/', indicating an absolute path.
128- if (pathParts[0 ] == " /" ) {
129- curr = wasmFS.getRootDirectory ();
130- begin++;
131- // If the pathname is the root directory, return the root as the child.
132- if (pathParts.size () == 1 ) {
133- return ParsedPath{curr->locked (), curr};
134- }
135- } else {
136- // This is a relative path. It is either relative to the current working
137- // directory if no base FD is given, or if the base FD is the special value
138- // indicating the CWD.
139- if (baseFD && *baseFD != AT_FDCWD) {
140- auto lockedOpenDir = wasmFS.getLockedFileTable ()[*baseFD].locked ();
141- auto openDir = lockedOpenDir.getFile ();
142- if (!openDir->is <Directory>()) {
143- err = -EBADF;
144- return ParsedPath{{}, nullptr };
145- }
146- curr = openDir->dynCast <Directory>();
147- } else {
148- curr = wasmFS.getCWD ();
149- }
150- }
151-
152- for (auto pathPart = begin; pathPart != pathParts.end () - 1 ; ++pathPart) {
153- // Find the next entry in the current directory entry
154- #ifdef WASMFS_DEBUG
155- curr->locked ().printKeys ();
156- #endif
157- auto entry = curr->locked ().getEntry (*pathPart);
158-
159- if (forbiddenAncestor) {
160- if (entry == forbiddenAncestor) {
161- err = -EINVAL;
162- return ParsedPath{{}, nullptr };
163- }
164- }
165-
166- // An entry is defined in the current directory's entries vector.
167- if (!entry) {
168- err = -ENOENT;
169- return ParsedPath{{}, nullptr };
170- }
171-
172- curr = entry->dynCast <Directory>();
173-
174- // If file is nullptr, then the file was not a Directory.
175- // TODO: Change this to accommodate symlinks
176- if (!curr) {
177- err = -ENOTDIR;
178- return ParsedPath{{}, nullptr };
179- }
180-
181- #ifdef WASMFS_DEBUG
182- emscripten_console_log (*pathPart->c_str ());
183- #endif
184- }
185-
186- // Lock the parent once.
187- auto lockedCurr = curr->locked ();
188- auto child = lockedCurr.getEntry (*(pathParts.end () - 1 ));
189- return ParsedPath{std::move (lockedCurr), child};
190- }
191-
192- std::shared_ptr<Directory> getDir (std::vector<std::string>::iterator begin,
193- std::vector<std::string>::iterator end,
194- long & err,
195- std::shared_ptr<File> forbiddenAncestor) {
196-
197- std::shared_ptr<File> curr;
198- // Check if the first path element is '/', indicating an absolute path.
199- if (*begin == " /" ) {
200- curr = wasmFS.getRootDirectory ();
201- begin++;
202- } else {
203- curr = wasmFS.getCWD ();
204- }
205-
206- for (auto it = begin; it != end; ++it) {
207- auto directory = curr->dynCast <Directory>();
208-
209- // If file is nullptr, then the file was not a Directory.
210- // TODO: Change this to accommodate symlinks
211- if (!directory) {
212- err = -ENOTDIR;
213- return nullptr ;
214- }
215-
216- // Find the next entry in the current directory entry
217- #ifdef WASMFS_DEBUG
218- directory->locked ().printKeys ();
219- #endif
220- curr = directory->locked ().getEntry (*it);
221-
222- if (forbiddenAncestor) {
223- if (curr == forbiddenAncestor) {
224- err = -EINVAL;
225- return nullptr ;
226- }
227- }
228-
229- // Requested entry (file or directory)
230- if (!curr) {
231- err = -ENOENT;
232- return nullptr ;
233- }
234-
235- #ifdef WASMFS_DEBUG
236- emscripten_console_log (it->c_str ());
237- #endif
238- }
239-
240- auto currDirectory = curr->dynCast <Directory>();
241-
242- if (!currDirectory) {
243- err = -ENOTDIR;
244- return nullptr ;
245- }
246-
247- return currDirectory;
248- }
249-
250- // TODO: Check for trailing slash, i.e. /foo/bar.txt/
251- // Currently any trailing slash is ignored.
252- std::vector<std::string> splitPath (char * pathname) {
253- std::vector<std::string> pathParts;
254- char newPathName[strlen (pathname) + 1 ];
255- strcpy (newPathName, pathname);
256-
257- // TODO: Other path parsing edge cases.
258- char * current;
259- // Handle absolute path.
260- if (newPathName[0 ] == ' /' ) {
261- pathParts.push_back (" /" );
262- }
263-
264- current = strtok (newPathName, " /" );
265- while (current != NULL ) {
266- pathParts.push_back (current);
267- current = strtok (NULL , " /" );
268- }
269-
270- return pathParts;
271- }
272111} // namespace wasmfs
0 commit comments