# HG changeset patch # User Paul Boddie # Date 1644795859 -3600 # Node ID fedb83ec8daebd89cd417c0bbd587733e7b400fa # Parent d7d0949c25fccaef8e111e16336ef89a499a06a9 Added path existence, removal and unlinking functions. diff -r d7d0949c25fc -r fedb83ec8dae libe2access/include/e2access/image.h --- a/libe2access/include/e2access/image.h Mon Feb 14 00:42:55 2022 +0100 +++ b/libe2access/include/e2access/image.h Mon Feb 14 00:44:19 2022 +0100 @@ -29,6 +29,8 @@ extern "C" { #endif +/* Filesystem operations. */ + errcode_t image_create_file(ext2_filsys fs, ext2_ino_t ino_target, const char *basename, __u16 mode, __u16 uid, __u16 gid, ext2_ino_t *ino_file); @@ -66,6 +68,8 @@ errcode_t image_remove_by_inode(ext2_filsys fs, ext2_ino_t ino); +errcode_t image_remove_by_path(ext2_filsys fs, const char *path); + errcode_t image_rename(ext2_filsys fs, ext2_ino_t source, ext2_ino_t source_parent, const char *source_basename, ext2_ino_t target_parent, const char *target_basename); @@ -78,9 +82,15 @@ errcode_t image_unlink_by_name(ext2_filsys fs, ext2_ino_t ino_parent, const char *basename); +errcode_t image_unlink_by_path(ext2_filsys fs, const char *path); + errcode_t image_unlink_by_inode(ext2_filsys fs, ext2_ino_t ino_parent, ext2_ino_t ino); +/* Presence and type tests. */ + +int image_exists(ext2_filsys fs, const char *name); + int _image_isdir(ext2_filsys fs, ext2_ino_t ino); int image_isdir(ext2_filsys fs, const char *name); diff -r d7d0949c25fc -r fedb83ec8dae libe2access/lib/src/image.c --- a/libe2access/lib/src/image.c Mon Feb 14 00:42:55 2022 +0100 +++ b/libe2access/lib/src/image.c Mon Feb 14 00:44:19 2022 +0100 @@ -84,6 +84,8 @@ return 0; } + + /* Create an inode for a file. */ errcode_t image_create_file(ext2_filsys fs, ext2_ino_t ino_target, @@ -404,6 +406,20 @@ sizeof(inode)); } +/* Remove a directory entry using its full path. */ + +errcode_t image_remove_by_path(ext2_filsys fs, const char *path) +{ + const char *remaining = path; + ext2_ino_t ino; + errcode_t retval = image_find_path(fs, &remaining, &ino); + + if (retval) + return retval; + + return image_remove_by_inode(fs, ino); +} + /* Rename a file. */ errcode_t image_rename(ext2_filsys fs, ext2_ino_t source, @@ -529,7 +545,26 @@ errcode_t image_unlink_by_name(ext2_filsys fs, ext2_ino_t ino_parent, const char *basename) { - /* NOTE: This might do more work for a directory. */ + return ext2fs_unlink(fs, ino_parent, basename, 0, 0); +} + +/* Unlink a directory entry by full path. */ + +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; + + strcpy(_path, path); + basename = path_split(_path); + + retval = image_find_path(fs, &remaining, &ino_parent); + + if (retval) + return retval; return ext2fs_unlink(fs, ino_parent, basename, 0, 0); } @@ -539,12 +574,19 @@ errcode_t image_unlink_by_inode(ext2_filsys fs, ext2_ino_t ino_parent, ext2_ino_t ino) { - /* NOTE: This might do more work for a directory. */ - return ext2fs_unlink(fs, ino_parent, 0, ino, 0); } -/* Test object types in the filesystem image. */ + + +/* Test object presence and types in the filesystem image. */ + +int image_exists(ext2_filsys fs, const char *name) +{ + ext2_ino_t ino; + + return !image_find_path(fs, &name, &ino); +} int _image_isdir(ext2_filsys fs, ext2_ino_t ino) {