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 };