1.1 --- a/libe2access/lib/src/image.c Thu Feb 17 19:41:23 2022 +0100
1.2 +++ b/libe2access/lib/src/image.c Thu Feb 17 19:42:31 2022 +0100
1.3 @@ -125,8 +125,7 @@
1.4 errcode_t (*op)(ext2_filsys, ext2_ino_t),
1.5 ext2_ino_t *ino)
1.6 {
1.7 - const char *remaining = path;
1.8 - errcode_t retval = image_find_path(fs, &remaining, ino);
1.9 + errcode_t retval = image_find_by_path(fs, path, ino);
1.10
1.11 if (retval)
1.12 return retval;
1.13 @@ -226,30 +225,17 @@
1.14 }
1.15 }
1.16
1.17 -/* Find an object in the given directory with the given name in the filesystem
1.18 - image, updating the name reference to refer to the next component. */
1.19 -
1.20 -errcode_t image_find_next(ext2_filsys fs, ext2_ino_t ino_dir,
1.21 - const char **basename, char *buf, ext2_ino_t *ino)
1.22 -{
1.23 - const char *end = path_component_end(*basename);
1.24 - errcode_t retval;
1.25 +/* Find an object with the given path in the filesystem image. */
1.26
1.27 - /* Find the basename in the directory. */
1.28 -
1.29 - retval = ext2fs_lookup(fs, ino_dir, *basename, end - *basename, buf, ino);
1.30 -
1.31 - /* Update the current component. */
1.32 -
1.33 - if (!retval)
1.34 - *basename = path_component_next(end);
1.35 -
1.36 - return retval;
1.37 +errcode_t image_find_by_path(ext2_filsys fs, const char *path, ext2_ino_t *ino)
1.38 +{
1.39 + return image_resolve_by_path(fs, &path, ino);
1.40 }
1.41
1.42 -/* Find an object with the given pathname in the filesystem image. */
1.43 +/* Find an object with the given path in the filesystem image. */
1.44
1.45 -errcode_t image_find_path(ext2_filsys fs, const char **pathname, ext2_ino_t *ino)
1.46 +errcode_t image_resolve_by_path(ext2_filsys fs, const char **path,
1.47 + ext2_ino_t *ino)
1.48 {
1.49 char *buf;
1.50 ext2_ino_t ino_dir;
1.51 @@ -261,10 +247,10 @@
1.52
1.53 /* Skip any leading root marker. */
1.54
1.55 - if (**pathname == '/')
1.56 - (*pathname)++;
1.57 + if (**path == '/')
1.58 + (*path)++;
1.59
1.60 - if (!**pathname)
1.61 + if (!**path)
1.62 *ino = EXT2_ROOT_INO;
1.63
1.64 /* Start at the root. */
1.65 @@ -273,9 +259,9 @@
1.66
1.67 /* With any remaining path, find the next component. */
1.68
1.69 - while (**pathname)
1.70 + while (**path)
1.71 {
1.72 - retval = image_find_next(fs, ino_dir, pathname, buf, ino);
1.73 + retval = image_resolve_next(fs, ino_dir, path, buf, ino);
1.74 if (retval)
1.75 {
1.76 *ino = ino_dir;
1.77 @@ -293,31 +279,52 @@
1.78 }
1.79
1.80 /* Find an object in the given directory with the given name in the filesystem
1.81 + image, updating the name reference to refer to the next component. */
1.82 +
1.83 +errcode_t image_resolve_next(ext2_filsys fs, ext2_ino_t ino_dir,
1.84 + const char **basename, char *buf, ext2_ino_t *ino)
1.85 +{
1.86 + const char *end = path_component_end(*basename);
1.87 + errcode_t retval;
1.88 +
1.89 + /* Find the basename in the directory. */
1.90 +
1.91 + retval = ext2fs_lookup(fs, ino_dir, *basename, end - *basename, buf, ino);
1.92 +
1.93 + /* Update the current component. */
1.94 +
1.95 + if (!retval)
1.96 + *basename = path_component_next(end);
1.97 +
1.98 + return retval;
1.99 +}
1.100 +
1.101 +/* Find an object in the given directory with the given name in the filesystem
1.102 image. */
1.103
1.104 errcode_t image_find_file(ext2_filsys fs, const char *dirname,
1.105 const char *basename, ext2_ino_t *ino)
1.106 {
1.107 - char pathname[strlen(dirname) + strlen(basename) + 2];
1.108 - const char *s = pathname;
1.109 + char path[strlen(dirname) + strlen(basename) + 2];
1.110
1.111 - strcpy(pathname, dirname);
1.112 - strcat(pathname, "/");
1.113 - strcat(pathname, basename);
1.114 + strcpy(path, dirname);
1.115 + strcat(path, "/");
1.116 + strcat(path, basename);
1.117
1.118 - return image_find_path(fs, &s, ino);
1.119 + return image_find_by_path(fs, path, ino);
1.120 }
1.121
1.122 -/* Obtain the inode for the object with the given pathname in the filesystem
1.123 +/* Obtain the inode for the object with the given path in the filesystem
1.124 image. */
1.125
1.126 -errcode_t image_inode(ext2_filsys fs, const char *pathname,
1.127 +errcode_t image_inode(ext2_filsys fs, const char *path,
1.128 struct ext2_inode *inode)
1.129 {
1.130 ext2_ino_t ino;
1.131 errcode_t retval;
1.132
1.133 - retval = image_find_path(fs, &pathname, &ino);
1.134 + retval = image_find_by_path(fs, path, &ino);
1.135 +
1.136 if (retval)
1.137 return retval;
1.138
1.139 @@ -363,7 +370,8 @@
1.140
1.141 /* Locate the object and test whether it is a directory. */
1.142
1.143 - retval = image_find_path(fs, &path, &ino);
1.144 + retval = image_find_by_path(fs, path, &ino);
1.145 +
1.146 if (retval)
1.147 {
1.148 ext2fs_free_mem(&buf);
1.149 @@ -376,6 +384,7 @@
1.150 /* List the directory contents. */
1.151
1.152 retval = ext2fs_dir_iterate(fs, ino, 0, buf, proc, data);
1.153 +
1.154 if (retval)
1.155 {
1.156 ext2fs_free_mem(&buf);
1.157 @@ -451,15 +460,15 @@
1.158
1.159 /* Make directories descending to the given path in the filesystem image. */
1.160
1.161 -errcode_t image_make_dirs(ext2_filsys fs, const char **pathname,
1.162 +errcode_t image_make_dirs(ext2_filsys fs, const char **path,
1.163 ext2_ino_t ino_dir, __u16 mode, __u16 uid, __u16 gid)
1.164 {
1.165 ext2_ino_t ino;
1.166 errcode_t retval;
1.167
1.168 - while (**pathname)
1.169 + while (**path)
1.170 {
1.171 - retval = image_make_next_dir(fs, ino_dir, pathname, mode, uid, gid, &ino);
1.172 + retval = image_make_next_dir(fs, ino_dir, path, mode, uid, gid, &ino);
1.173 if (retval)
1.174 return retval;
1.175
1.176 @@ -668,7 +677,6 @@
1.177 errcode_t image_unlink_by_path(ext2_filsys fs, const char *path)
1.178 {
1.179 char _path[strlen(path) + 1];
1.180 - const char *remaining = _path;
1.181 char *basename;
1.182 ext2_ino_t ino_parent;
1.183 errcode_t retval;
1.184 @@ -682,7 +690,7 @@
1.185
1.186 if (basename != _path)
1.187 {
1.188 - retval = image_find_path(fs, &remaining, &ino_parent);
1.189 + retval = image_find_by_path(fs, _path, &ino_parent);
1.190
1.191 if (retval)
1.192 return retval;
1.193 @@ -709,7 +717,7 @@
1.194 {
1.195 ext2_ino_t ino;
1.196
1.197 - return !image_find_path(fs, &name, &ino);
1.198 + return !image_find_by_path(fs, name, &ino);
1.199 }
1.200
1.201 int _image_isdir(ext2_filsys fs, ext2_ino_t ino)
1.202 @@ -731,7 +739,7 @@
1.203
1.204 int image_isdir_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino)
1.205 {
1.206 - if (image_find_path(fs, &name, ino))
1.207 + if (image_find_by_path(fs, name, ino))
1.208 return 0;
1.209
1.210 return _image_isdir(fs, *ino);
1.211 @@ -756,7 +764,7 @@
1.212
1.213 int image_isfile_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino)
1.214 {
1.215 - if (image_find_path(fs, &name, ino))
1.216 + if (image_find_by_path(fs, name, ino))
1.217 return 0;
1.218
1.219 return _image_isfile(fs, *ino);