#include <defs.h>
#include <string.h>
#include <vfs.h>
#include <inode.h>
#include <error.h>
#include <assert.h>
static int
get_device(char *path, char **subpath, struct inode **node_store) {
int i, slash = -1, colon = -1;
for (i = 0; path[i] != '\0'; i ++) {
if (path[i] == ':') { colon = i; break; }
if (path[i] == '/') { slash = i; break; }
}
if (colon < 0 && slash != 0) {
*subpath = path;
return vfs_get_curdir(node_store);
}
if (colon > 0) {
path[colon] = '\0';
while (path[++ colon] == '/');
*subpath = path + colon;
return vfs_get_root(path, node_store);
}
int ret;
if (*path == '/') {
if ((ret = vfs_get_bootfs(node_store)) != 0) {
return ret;
}
}
else {
assert(*path == ':');
struct inode *node;
if ((ret = vfs_get_curdir(&node)) != 0) {
return ret;
}
assert(node->in_fs != NULL);
*node_store = fsop_get_root(node->in_fs);
vop_ref_dec(node);
}
while (*(++ path) == '/');
*subpath = path;
return 0;
}
int
vfs_lookup(char *path, struct inode **node_store) {
int ret;
struct inode *node;
if ((ret = get_device(path, &path, &node)) != 0) {
return ret;
}
if (*path != '\0') {
ret = vop_lookup(node, path, node_store);
vop_ref_dec(node);
return ret;
}
*node_store = node;
return 0;
}
int
vfs_lookup_parent(char *path, struct inode **node_store, char **endp){
int ret;
struct inode *node;
if ((ret = get_device(path, &path, &node)) != 0) {
return ret;
}
*endp = path;
*node_store = node;
return 0;
}