From 302ccf616617f8f80d6d46e2140b8936c812e4a7 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Mon, 21 Mar 2011 17:39:29 +1100 Subject: [PATCH] Add 'remainder' arg to lafs_lookup_path This allows us to find where to create something. If 'remainder' is provided, the tail of the path that couldn't be found is placed there, and the last directory that was found is returned. Signed-off-by: NeilBrown --- include/lafs/lafs.h | 2 +- lib/lafs_lookup_path.c | 15 ++++++++++----- tools/lafs.c | 6 +++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/lafs/lafs.h b/include/lafs/lafs.h index 926c13f..7f60a38 100644 --- a/include/lafs/lafs.h +++ b/include/lafs/lafs.h @@ -94,7 +94,7 @@ struct lafs_iblk *lafs_leaf_find(struct lafs_ino *inode, u32 addr, int adopt, u32 *next); u32 lafs_dir_lookup(struct lafs_ino *dir, char *name, int len); struct lafs_ino *lafs_lookup_path(struct lafs_ino *root, struct lafs_ino *cwd, - char *path); + char *path, char **remainder); diff --git a/lib/lafs_lookup_path.c b/lib/lafs_lookup_path.c index cf29cb6..439638f 100644 --- a/lib/lafs_lookup_path.c +++ b/lib/lafs_lookup_path.c @@ -1,7 +1,7 @@ #include struct lafs_ino *lafs_lookup_path(struct lafs_ino *root, struct lafs_ino *cwd, - char *path) + char *path, char **remainder) { if (!path) return cwd; @@ -14,16 +14,21 @@ struct lafs_ino *lafs_lookup_path(struct lafs_ino *root, struct lafs_ino *cwd, while (cwd && *path) { char *p = path; int len; - u32 inum; + u32 inum = 0; while (*path && *path != '/') path++; len = path - p; while (*path == '/') path++; - inum = lafs_dir_lookup(cwd, p, len); - if (!inum) - return NULL; + if (cwd->type == TypeDir) + inum = lafs_dir_lookup(cwd, p, len); + if (!inum) { + if (!remainder) + return NULL; + *remainder = p; + return cwd; + } cwd = lafs_get_inode(cwd->filesys, inum); } return cwd; diff --git a/tools/lafs.c b/tools/lafs.c index 18c29db..437b080 100644 --- a/tools/lafs.c +++ b/tools/lafs.c @@ -580,7 +580,7 @@ static char *internal_gen(const char *prefix, int state) /*pre is the directory, prefix is the component prefix */ inode = lafs_get_itable(gen_state->lafs); inode = lafs_get_inode(inode, 2); - inode = lafs_lookup_path(inode, inode, pre); + inode = lafs_lookup_path(inode, inode, pre, NULL); index = -1; } else { if (index + 1 == 0) { @@ -1283,7 +1283,7 @@ static void c_show_inode(struct state *st, void **args) } else { char *path = args[2]; inode = lafs_get_inode(inode, 2); - inode = lafs_lookup_path(inode, inode, path); + inode = lafs_lookup_path(inode, inode, path, NULL); if (!inode) { printf("show inode: cannot find inode for %s\n", path); @@ -1507,7 +1507,7 @@ static void c_ls(struct state *st, void **args) ino = lafs_get_itable(st->lafs); ino = lafs_get_inode(ino, 2); if (path) - ino = lafs_lookup_path(ino, ino, path); + ino = lafs_lookup_path(ino, ino, path, NULL); if (!ino) { printf("ls: cannot find inode for %s\n", path?:"/"); -- 2.39.5