From 9f05686a1f910c57dfd070eb359edb2cea3c896d Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Mon, 12 Jun 2017 13:39:31 +0200 Subject: [PATCH] mufs: clean up code in resolve_path() --- source/fs/mufs/dir.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/source/fs/mufs/dir.c b/source/fs/mufs/dir.c index d5d459d..78f3661 100644 --- a/source/fs/mufs/dir.c +++ b/source/fs/mufs/dir.c @@ -95,22 +95,24 @@ err_free_dir: struct mufs_tree *resolve_path(struct mufs *fs, const char *path) { - struct mufs_dir *dir; - struct mufs_dirent entry; - struct mufs_tree *tree; + struct mufs_dir *dir = NULL; + struct mufs_dirent entry, *found = NULL; + struct mufs_tree *tree = NULL; char *s, *p, *end; int ret; if (!(s = mufs_abspath(path))) return NULL; - if (strcmp(s, "/") == 0) - return &fs->root; - - if (!(dir = open_dirent(fs, NULL))) + if (strcmp(s, "/") == 0) { + tree = &fs->root; goto err_free_s; + } for (p = s + 1; *p != '\0'; p = end) { + if (!(dir = open_dirent(fs, found))) + goto err_free_s; + end = p + strcspn(p, "/"); if (*end == '/') @@ -120,29 +122,22 @@ struct mufs_tree *resolve_path(struct mufs *fs, const char *path) if (strcmp(entry.path, p) != 0) continue; - mufs_closedir(dir); - - if (!(dir = open_dirent(fs, &entry))) - goto err_free_s; - + found = &entry; break; } if (ret < 0) - goto err_close_dir; - } + goto err_closedir; - tree = dir->tree; - - mufs_closedir(dir); - - return tree; + tree = dir->tree; + dir->tree = NULL; + } -err_close_dir: +err_closedir: mufs_closedir(dir); err_free_s: free(s); - return NULL; + return tree; } struct mufs_dir *mufs_opendir(struct mufs *fs, const char *path)