L4Re/departure

Changeset

231:ded81f25f1aa
2022-01-04 Paul Boddie raw files shortlog changelog graph Added unlinking and removal functionality.
libe2access/include/e2access/image.h (file) libe2access/lib/src/image.c (file)
     1.1 --- a/libe2access/include/e2access/image.h	Tue Jan 04 19:22:01 2022 +0100
     1.2 +++ b/libe2access/include/e2access/image.h	Tue Jan 04 23:27:53 2022 +0100
     1.3 @@ -67,6 +67,14 @@
     1.4  
     1.5  errcode_t image_stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *st);
     1.6  
     1.7 +errcode_t image_remove_by_inode(ext2_filsys fs, ext2_ino_t ino);
     1.8 +
     1.9 +errcode_t image_unlink_by_name(ext2_filsys fs, ext2_ino_t ino_parent,
    1.10 +                               const char *basename);
    1.11 +
    1.12 +errcode_t image_unlink_by_inode(ext2_filsys fs, ext2_ino_t ino_parent,
    1.13 +                                ext2_ino_t ino);
    1.14 +
    1.15  int _image_isdir(ext2_filsys fs, ext2_ino_t ino);
    1.16  
    1.17  int image_isdir(ext2_filsys fs, const char *name);
     2.1 --- a/libe2access/lib/src/image.c	Tue Jan 04 19:22:01 2022 +0100
     2.2 +++ b/libe2access/lib/src/image.c	Tue Jan 04 23:27:53 2022 +0100
     2.3 @@ -195,7 +195,6 @@
     2.4                                       void *),
     2.5                           void *data)
     2.6  {
     2.7 -    const char *path_orig = path;
     2.8      char *buf;
     2.9      ext2_ino_t ino;
    2.10      errcode_t retval;
    2.11 @@ -344,6 +343,70 @@
    2.12      return 0;
    2.13  }
    2.14  
    2.15 +/* Remove an inode. */
    2.16 +
    2.17 +errcode_t image_remove_by_inode(ext2_filsys fs, ext2_ino_t ino)
    2.18 +{
    2.19 +    struct ext2_inode_large inode;
    2.20 +    errcode_t err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *) &inode,
    2.21 +                                           sizeof(inode));
    2.22 +
    2.23 +    /* Handle invalid inodes, ignore unreferenced inodes. */
    2.24 +
    2.25 +    if (err)
    2.26 +        return err;
    2.27 +
    2.28 +    if (!inode.i_links_count)
    2.29 +        return 0;
    2.30 +
    2.31 +    /* Decrement the reference count. With no more references to the inode,
    2.32 +       remove its resources. */
    2.33 +
    2.34 +    inode.i_links_count--;
    2.35 +
    2.36 +    if (!inode.i_links_count)
    2.37 +    {
    2.38 +        err = ext2fs_free_ext_attr(fs, ino, &inode);
    2.39 +
    2.40 +        if (!err)
    2.41 +        {
    2.42 +            /* Deallocate blocks, if appropriate. ~0ULL as the end represents
    2.43 +               truncation. */
    2.44 +
    2.45 +            if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *) &inode))
    2.46 +            {
    2.47 +                err = ext2fs_punch(fs, ino, (struct ext2_inode *) &inode, NULL,
    2.48 +                                   0, ~0ULL);
    2.49 +
    2.50 +                /* Update allocation statistics. */
    2.51 +
    2.52 +                if (!err)
    2.53 +                    ext2fs_inode_alloc_stats2(fs, ino, -1,
    2.54 +                                  LINUX_S_ISDIR(inode.i_mode));
    2.55 +            }
    2.56 +        }
    2.57 +    }
    2.58 +
    2.59 +    return ext2fs_write_inode_full(fs, ino, (struct ext2_inode *) &inode,
    2.60 +                                   sizeof(inode));
    2.61 +}
    2.62 +
    2.63 +/* Unlink a directory entry by name. */
    2.64 +
    2.65 +errcode_t image_unlink_by_name(ext2_filsys fs, ext2_ino_t ino_parent,
    2.66 +                               const char *basename)
    2.67 +{
    2.68 +    return ext2fs_unlink(fs, ino_parent, basename, 0, 0);
    2.69 +}
    2.70 +
    2.71 +/* Unlink a directory entry by inode number. */
    2.72 +
    2.73 +errcode_t image_unlink_by_inode(ext2_filsys fs, ext2_ino_t ino_parent,
    2.74 +                                ext2_ino_t ino)
    2.75 +{
    2.76 +    return ext2fs_unlink(fs, ino_parent, 0, ino, 0);
    2.77 +}
    2.78 +
    2.79  /* Test object types in the filesystem image. */
    2.80  
    2.81  int _image_isdir(ext2_filsys fs, ext2_ino_t ino)