# HG changeset patch # User Paul Boddie # Date 1645123351 -3600 # Node ID ccbbee67d9fc3b875b8f41e0a0f72099ec608d83 # Parent b8f26cc013ed441de6ad022daebd065cbfcb3717 Renamed various functions and introduced an image_find_by_path function with simpler parameters than the previous image_find_path function, now renamed to image_resolve_by_path. diff -r b8f26cc013ed -r ccbbee67d9fc libe2access/host/e2access.c --- a/libe2access/host/e2access.c Thu Feb 17 19:41:23 2022 +0100 +++ b/libe2access/host/e2access.c Thu Feb 17 19:42:31 2022 +0100 @@ -226,7 +226,7 @@ /* Locate the target and test whether it is a file or a directory. */ - if (image_find_path(fs, &target_remaining, &ino_target)) + if (image_resolve_by_path(fs, &target_remaining, &ino_target)) { /* Only a non-existent file in an existing directory is permitted. */ @@ -410,7 +410,7 @@ { path = argv[i]; - if (image_find_path(fs, &path, &ino_file)) + if (image_find_by_path(fs, path, &ino_file)) { fprintf(stderr, "Failed to find file: %s\n", argv[i]); return 1; @@ -471,7 +471,7 @@ /* Search for the remaining components. */ - if ((!*path) || !image_find_path(fs, &path, &ino)) + if ((!*path) || !image_resolve_by_path(fs, &path, &ino)) { fprintf(stderr, "Path exists: %s\n", argv[i]); return 1; diff -r b8f26cc013ed -r ccbbee67d9fc libe2access/include/e2access/image.h --- a/libe2access/include/e2access/image.h Thu Feb 17 19:41:23 2022 +0100 +++ b/libe2access/include/e2access/image.h Thu Feb 17 19:42:31 2022 +0100 @@ -49,16 +49,18 @@ int image_file_type(int mode); -errcode_t image_find_next(ext2_filsys fs, ext2_ino_t ino_dir, - const char **basename, char *buf, ext2_ino_t *ino); +errcode_t image_find_by_path(ext2_filsys fs, const char *path, ext2_ino_t *ino); -errcode_t image_find_path(ext2_filsys fs, const char **pathname, - ext2_ino_t *ino); +errcode_t image_resolve_by_path(ext2_filsys fs, const char **path, + ext2_ino_t *ino); + +errcode_t image_resolve_next(ext2_filsys fs, ext2_ino_t ino_dir, + const char **basename, char *buf, ext2_ino_t *ino); errcode_t image_find_file(ext2_filsys fs, const char *dirname, const char *basename, ext2_ino_t *ino); -errcode_t image_inode(ext2_filsys fs, const char *pathname, +errcode_t image_inode(ext2_filsys fs, const char *path, struct ext2_inode *inode); errcode_t image_inode_refcount_update(ext2_filsys fs, ext2_ino_t ino, @@ -77,7 +79,7 @@ const char **basename, __u16 mode, __u16 uid, __u16 gid, ext2_ino_t *ino); -errcode_t image_make_dirs(ext2_filsys fs, const char **pathname, +errcode_t image_make_dirs(ext2_filsys fs, const char **path, ext2_ino_t ino_dir, __u16 mode, __u16 uid, __u16 gid); diff -r b8f26cc013ed -r ccbbee67d9fc libe2access/lib/src/image.c --- a/libe2access/lib/src/image.c Thu Feb 17 19:41:23 2022 +0100 +++ b/libe2access/lib/src/image.c Thu Feb 17 19:42:31 2022 +0100 @@ -125,8 +125,7 @@ errcode_t (*op)(ext2_filsys, ext2_ino_t), ext2_ino_t *ino) { - const char *remaining = path; - errcode_t retval = image_find_path(fs, &remaining, ino); + errcode_t retval = image_find_by_path(fs, path, ino); if (retval) return retval; @@ -226,30 +225,17 @@ } } -/* Find an object in the given directory with the given name in the filesystem - image, updating the name reference to refer to the next component. */ - -errcode_t image_find_next(ext2_filsys fs, ext2_ino_t ino_dir, - const char **basename, char *buf, ext2_ino_t *ino) -{ - const char *end = path_component_end(*basename); - errcode_t retval; +/* Find an object with the given path in the filesystem image. */ - /* Find the basename in the directory. */ - - retval = ext2fs_lookup(fs, ino_dir, *basename, end - *basename, buf, ino); - - /* Update the current component. */ - - if (!retval) - *basename = path_component_next(end); - - return retval; +errcode_t image_find_by_path(ext2_filsys fs, const char *path, ext2_ino_t *ino) +{ + return image_resolve_by_path(fs, &path, ino); } -/* Find an object with the given pathname in the filesystem image. */ +/* Find an object with the given path in the filesystem image. */ -errcode_t image_find_path(ext2_filsys fs, const char **pathname, ext2_ino_t *ino) +errcode_t image_resolve_by_path(ext2_filsys fs, const char **path, + ext2_ino_t *ino) { char *buf; ext2_ino_t ino_dir; @@ -261,10 +247,10 @@ /* Skip any leading root marker. */ - if (**pathname == '/') - (*pathname)++; + if (**path == '/') + (*path)++; - if (!**pathname) + if (!**path) *ino = EXT2_ROOT_INO; /* Start at the root. */ @@ -273,9 +259,9 @@ /* With any remaining path, find the next component. */ - while (**pathname) + while (**path) { - retval = image_find_next(fs, ino_dir, pathname, buf, ino); + retval = image_resolve_next(fs, ino_dir, path, buf, ino); if (retval) { *ino = ino_dir; @@ -293,31 +279,52 @@ } /* Find an object in the given directory with the given name in the filesystem + image, updating the name reference to refer to the next component. */ + +errcode_t image_resolve_next(ext2_filsys fs, ext2_ino_t ino_dir, + const char **basename, char *buf, ext2_ino_t *ino) +{ + const char *end = path_component_end(*basename); + errcode_t retval; + + /* Find the basename in the directory. */ + + retval = ext2fs_lookup(fs, ino_dir, *basename, end - *basename, buf, ino); + + /* Update the current component. */ + + if (!retval) + *basename = path_component_next(end); + + return retval; +} + +/* Find an object in the given directory with the given name in the filesystem image. */ errcode_t image_find_file(ext2_filsys fs, const char *dirname, const char *basename, ext2_ino_t *ino) { - char pathname[strlen(dirname) + strlen(basename) + 2]; - const char *s = pathname; + char path[strlen(dirname) + strlen(basename) + 2]; - strcpy(pathname, dirname); - strcat(pathname, "/"); - strcat(pathname, basename); + strcpy(path, dirname); + strcat(path, "/"); + strcat(path, basename); - return image_find_path(fs, &s, ino); + return image_find_by_path(fs, path, ino); } -/* Obtain the inode for the object with the given pathname in the filesystem +/* Obtain the inode for the object with the given path in the filesystem image. */ -errcode_t image_inode(ext2_filsys fs, const char *pathname, +errcode_t image_inode(ext2_filsys fs, const char *path, struct ext2_inode *inode) { ext2_ino_t ino; errcode_t retval; - retval = image_find_path(fs, &pathname, &ino); + retval = image_find_by_path(fs, path, &ino); + if (retval) return retval; @@ -363,7 +370,8 @@ /* Locate the object and test whether it is a directory. */ - retval = image_find_path(fs, &path, &ino); + retval = image_find_by_path(fs, path, &ino); + if (retval) { ext2fs_free_mem(&buf); @@ -376,6 +384,7 @@ /* List the directory contents. */ retval = ext2fs_dir_iterate(fs, ino, 0, buf, proc, data); + if (retval) { ext2fs_free_mem(&buf); @@ -451,15 +460,15 @@ /* Make directories descending to the given path in the filesystem image. */ -errcode_t image_make_dirs(ext2_filsys fs, const char **pathname, +errcode_t image_make_dirs(ext2_filsys fs, const char **path, ext2_ino_t ino_dir, __u16 mode, __u16 uid, __u16 gid) { ext2_ino_t ino; errcode_t retval; - while (**pathname) + while (**path) { - retval = image_make_next_dir(fs, ino_dir, pathname, mode, uid, gid, &ino); + retval = image_make_next_dir(fs, ino_dir, path, mode, uid, gid, &ino); if (retval) return retval; @@ -668,7 +677,6 @@ errcode_t image_unlink_by_path(ext2_filsys fs, const char *path) { char _path[strlen(path) + 1]; - const char *remaining = _path; char *basename; ext2_ino_t ino_parent; errcode_t retval; @@ -682,7 +690,7 @@ if (basename != _path) { - retval = image_find_path(fs, &remaining, &ino_parent); + retval = image_find_by_path(fs, _path, &ino_parent); if (retval) return retval; @@ -709,7 +717,7 @@ { ext2_ino_t ino; - return !image_find_path(fs, &name, &ino); + return !image_find_by_path(fs, name, &ino); } int _image_isdir(ext2_filsys fs, ext2_ino_t ino) @@ -731,7 +739,7 @@ int image_isdir_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino) { - if (image_find_path(fs, &name, ino)) + if (image_find_by_path(fs, name, ino)) return 0; return _image_isdir(fs, *ino); @@ -756,7 +764,7 @@ int image_isfile_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino) { - if (image_find_path(fs, &name, ino)) + if (image_find_by_path(fs, name, ino)) return 0; return _image_isfile(fs, *ino);