1.1 --- a/libe2access/host/e2access.c Thu Feb 17 00:33:14 2022 +0100
1.2 +++ b/libe2access/host/e2access.c Thu Feb 17 00:37:13 2022 +0100
1.3 @@ -493,9 +493,9 @@
1.4 return 0;
1.5 }
1.6
1.7 -/* Remove directories from the filesystem image. */
1.8 +/* Remove objects from the filesystem image. */
1.9
1.10 -int remove_dirs(ext2_filsys fs, int argc, char *argv[])
1.11 +int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only)
1.12 {
1.13 int i;
1.14 const char *path;
1.15 @@ -515,35 +515,46 @@
1.16 return 1;
1.17 }
1.18
1.19 - /* Insist on a directory. */
1.20 + /* Insist on a directory if specified. */
1.21 +
1.22 + if (dir_only)
1.23 + {
1.24 + if (!image_isdir(fs, path))
1.25 + {
1.26 + fprintf(stderr, "Not a directory: %s\n", path);
1.27 + return 1;
1.28 + }
1.29 +
1.30 + /* Test for an empty directory. */
1.31
1.32 - if (!image_isdir(fs, path))
1.33 + if (image_dir_empty_by_path(fs, path, &ino))
1.34 + {
1.35 + fprintf(stderr, "Directory not empty: %s\n", path);
1.36 + return 1;
1.37 + }
1.38 + }
1.39 +
1.40 + /* Otherwise, insist on a non-directory. */
1.41 +
1.42 + else if (image_isdir_by_path(fs, path, &ino))
1.43 {
1.44 - fprintf(stderr, "Not a directory: %s\n", path);
1.45 + fprintf(stderr, "Cannot remove a directory: %s\n", path);
1.46 return 1;
1.47 }
1.48
1.49 - /* Test for an empty directory. */
1.50 + /* Unlink the object. */
1.51
1.52 - if (image_dir_empty_by_path(fs, path, &ino))
1.53 + if (image_unlink_by_path(fs, path))
1.54 {
1.55 - fprintf(stderr, "Directory not empty: %s\n", path);
1.56 + fprintf(stderr, "Could not unlink object: %s\n", path);
1.57 return 1;
1.58 }
1.59
1.60 - /* Unlink the directory. */
1.61 -
1.62 - if (image_unlink_by_path(fs, path))
1.63 - {
1.64 - fprintf(stderr, "Could not unlink directory: %s\n", path);
1.65 - return 1;
1.66 - }
1.67 -
1.68 - /* Remove the directory. */
1.69 + /* Remove the object. */
1.70
1.71 if (image_remove_by_inode(fs, ino))
1.72 {
1.73 - fprintf(stderr, "Could not remove directory: %s\n", path);
1.74 + fprintf(stderr, "Could not remove object: %s\n", path);
1.75 return 1;
1.76 }
1.77 }
1.78 @@ -551,6 +562,20 @@
1.79 return 0;
1.80 }
1.81
1.82 +/* Remove directories from the filesystem image. */
1.83 +
1.84 +int remove_dirs(ext2_filsys fs, int argc, char *argv[])
1.85 +{
1.86 + return _remove(fs, argc, argv, 1);
1.87 +}
1.88 +
1.89 +/* Remove non-directories from the filesystem image. */
1.90 +
1.91 +int remove_non_dirs(ext2_filsys fs, int argc, char *argv[])
1.92 +{
1.93 + return _remove(fs, argc, argv, 0);
1.94 +}
1.95 +
1.96
1.97
1.98 /* Help message. */
1.99 @@ -573,6 +598,7 @@
1.100 copy-out Copy files from the image into a directory\n\
1.101 ls List files and directories within the image\n\
1.102 mkdir Make directories within the image\n\
1.103 + rm Remove non-directory objects from the image\n\
1.104 rmdir Remove directories from the image\n\
1.105 ";
1.106
1.107 @@ -589,6 +615,7 @@
1.108 {"copy-out", copy_out},
1.109 {"ls", list},
1.110 {"mkdir", make_dirs},
1.111 + {"rm", remove_non_dirs},
1.112 {"rmdir", remove_dirs},
1.113 {NULL, NULL},
1.114 };
2.1 --- a/libe2access/include/e2access/image.h Thu Feb 17 00:33:14 2022 +0100
2.2 +++ b/libe2access/include/e2access/image.h Thu Feb 17 00:37:13 2022 +0100
2.3 @@ -111,10 +111,14 @@
2.4
2.5 int image_isdir(ext2_filsys fs, const char *name);
2.6
2.7 +int image_isdir_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino);
2.8 +
2.9 int _image_isfile(ext2_filsys fs, ext2_ino_t ino);
2.10
2.11 int image_isfile(ext2_filsys fs, const char *name);
2.12
2.13 +int image_isfile_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino);
2.14 +
2.15 #ifdef __cplusplus
2.16 }
2.17 #endif
3.1 --- a/libe2access/lib/src/image.c Thu Feb 17 00:33:14 2022 +0100
3.2 +++ b/libe2access/lib/src/image.c Thu Feb 17 00:37:13 2022 +0100
3.3 @@ -726,10 +726,15 @@
3.4 {
3.5 ext2_ino_t ino;
3.6
3.7 - if (image_find_path(fs, &name, &ino))
3.8 + return image_isdir_by_path(fs, name, &ino);
3.9 +}
3.10 +
3.11 +int image_isdir_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino)
3.12 +{
3.13 + if (image_find_path(fs, &name, ino))
3.14 return 0;
3.15
3.16 - return _image_isdir(fs, ino);
3.17 + return _image_isdir(fs, *ino);
3.18 }
3.19
3.20 int _image_isfile(ext2_filsys fs, ext2_ino_t ino)
3.21 @@ -746,10 +751,15 @@
3.22 {
3.23 ext2_ino_t ino;
3.24
3.25 - if (image_find_path(fs, &name, &ino))
3.26 + return image_isfile_by_path(fs, name, &ino);
3.27 +}
3.28 +
3.29 +int image_isfile_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino)
3.30 +{
3.31 + if (image_find_path(fs, &name, ino))
3.32 return 0;
3.33
3.34 - return _image_isfile(fs, ino);
3.35 + return _image_isfile(fs, *ino);
3.36 }
3.37
3.38 /* vim: tabstop=4 expandtab shiftwidth=4