# HG changeset patch # User Paul Boddie # Date 1645054633 -3600 # Node ID 8145967ef0123ba1c2475f3fd07da4811a1ea6b1 # Parent aadba73e788caa20d2888d87970746eae7d48c92 Added support for removing files with e2access. diff -r aadba73e788c -r 8145967ef012 libe2access/host/e2access.c --- a/libe2access/host/e2access.c Thu Feb 17 00:33:14 2022 +0100 +++ b/libe2access/host/e2access.c Thu Feb 17 00:37:13 2022 +0100 @@ -493,9 +493,9 @@ return 0; } -/* Remove directories from the filesystem image. */ +/* Remove objects from the filesystem image. */ -int remove_dirs(ext2_filsys fs, int argc, char *argv[]) +int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only) { int i; const char *path; @@ -515,35 +515,46 @@ return 1; } - /* Insist on a directory. */ + /* Insist on a directory if specified. */ + + if (dir_only) + { + if (!image_isdir(fs, path)) + { + fprintf(stderr, "Not a directory: %s\n", path); + return 1; + } + + /* Test for an empty directory. */ - if (!image_isdir(fs, path)) + if (image_dir_empty_by_path(fs, path, &ino)) + { + fprintf(stderr, "Directory not empty: %s\n", path); + return 1; + } + } + + /* Otherwise, insist on a non-directory. */ + + else if (image_isdir_by_path(fs, path, &ino)) { - fprintf(stderr, "Not a directory: %s\n", path); + fprintf(stderr, "Cannot remove a directory: %s\n", path); return 1; } - /* Test for an empty directory. */ + /* Unlink the object. */ - if (image_dir_empty_by_path(fs, path, &ino)) + if (image_unlink_by_path(fs, path)) { - fprintf(stderr, "Directory not empty: %s\n", path); + fprintf(stderr, "Could not unlink object: %s\n", path); return 1; } - /* Unlink the directory. */ - - if (image_unlink_by_path(fs, path)) - { - fprintf(stderr, "Could not unlink directory: %s\n", path); - return 1; - } - - /* Remove the directory. */ + /* Remove the object. */ if (image_remove_by_inode(fs, ino)) { - fprintf(stderr, "Could not remove directory: %s\n", path); + fprintf(stderr, "Could not remove object: %s\n", path); return 1; } } @@ -551,6 +562,20 @@ return 0; } +/* Remove directories from the filesystem image. */ + +int remove_dirs(ext2_filsys fs, int argc, char *argv[]) +{ + return _remove(fs, argc, argv, 1); +} + +/* Remove non-directories from the filesystem image. */ + +int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]) +{ + return _remove(fs, argc, argv, 0); +} + /* Help message. */ @@ -573,6 +598,7 @@ copy-out Copy files from the image into a directory\n\ ls List files and directories within the image\n\ mkdir Make directories within the image\n\ + rm Remove non-directory objects from the image\n\ rmdir Remove directories from the image\n\ "; @@ -589,6 +615,7 @@ {"copy-out", copy_out}, {"ls", list}, {"mkdir", make_dirs}, + {"rm", remove_non_dirs}, {"rmdir", remove_dirs}, {NULL, NULL}, }; diff -r aadba73e788c -r 8145967ef012 libe2access/include/e2access/image.h --- a/libe2access/include/e2access/image.h Thu Feb 17 00:33:14 2022 +0100 +++ b/libe2access/include/e2access/image.h Thu Feb 17 00:37:13 2022 +0100 @@ -111,10 +111,14 @@ int image_isdir(ext2_filsys fs, const char *name); +int image_isdir_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino); + int _image_isfile(ext2_filsys fs, ext2_ino_t ino); int image_isfile(ext2_filsys fs, const char *name); +int image_isfile_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino); + #ifdef __cplusplus } #endif diff -r aadba73e788c -r 8145967ef012 libe2access/lib/src/image.c --- a/libe2access/lib/src/image.c Thu Feb 17 00:33:14 2022 +0100 +++ b/libe2access/lib/src/image.c Thu Feb 17 00:37:13 2022 +0100 @@ -726,10 +726,15 @@ { ext2_ino_t ino; - if (image_find_path(fs, &name, &ino)) + return image_isdir_by_path(fs, name, &ino); +} + +int image_isdir_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino) +{ + if (image_find_path(fs, &name, ino)) return 0; - return _image_isdir(fs, ino); + return _image_isdir(fs, *ino); } int _image_isfile(ext2_filsys fs, ext2_ino_t ino) @@ -746,10 +751,15 @@ { ext2_ino_t ino; - if (image_find_path(fs, &name, &ino)) + return image_isfile_by_path(fs, name, &ino); +} + +int image_isfile_by_path(ext2_filsys fs, const char *name, ext2_ino_t *ino) +{ + if (image_find_path(fs, &name, ino)) return 0; - return _image_isfile(fs, ino); + return _image_isfile(fs, *ino); } /* vim: tabstop=4 expandtab shiftwidth=4