L4Re/departure

Annotated libe2access/host/op_remove.c

635:18f77ccd5ea8
8 months ago Paul Boddie Updated dataspace size type usage.
paul@278 1
/*
paul@278 2
 * Remove objects from a filesystem.
paul@278 3
 *
paul@278 4
 * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
paul@278 5
 *
paul@278 6
 * This program is free software; you can redistribute it and/or
paul@278 7
 * modify it under the terms of the GNU General Public License as
paul@278 8
 * published by the Free Software Foundation; either version 2 of
paul@278 9
 * the License, or (at your option) any later version.
paul@278 10
 *
paul@278 11
 * This program is distributed in the hope that it will be useful,
paul@278 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@278 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@278 14
 * GNU General Public License for more details.
paul@278 15
 *
paul@278 16
 * You should have received a copy of the GNU General Public License
paul@278 17
 * along with this program; if not, write to the Free Software
paul@278 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@278 19
 * Boston, MA  02110-1301, USA
paul@278 20
 */
paul@278 21
paul@278 22
#include <stdio.h>
paul@278 23
paul@278 24
#include <ext2fs/ext2fs.h>
paul@278 25
paul@278 26
#include "image.h"
paul@278 27
paul@278 28
paul@278 29
paul@278 30
/* Remove objects from the filesystem image. */
paul@278 31
paul@278 32
int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only)
paul@278 33
{
paul@278 34
    int i;
paul@278 35
    const char *path;
paul@278 36
    ext2_ino_t ino;
paul@278 37
paul@278 38
    /* Remove each directory with the given pathname. */
paul@278 39
paul@278 40
    for (i = 0; i < argc; i++)
paul@278 41
    {
paul@278 42
        path = argv[i];
paul@278 43
paul@278 44
        /* Detect missing objects. */
paul@278 45
paul@278 46
        if ((!*path) || image_find_by_path(fs, path, &ino))
paul@278 47
        {
paul@278 48
            fprintf(stderr, "Not found: %s\n", path);
paul@278 49
            return 1;
paul@278 50
        }
paul@278 51
paul@278 52
        /* Insist on a directory if specified. */
paul@278 53
paul@278 54
        if (dir_only)
paul@278 55
        {
paul@278 56
            if (!image_isdir_by_inode(fs, ino))
paul@278 57
            {
paul@278 58
                fprintf(stderr, "Not a directory: %s\n", path);
paul@278 59
                return 1;
paul@278 60
            }
paul@278 61
paul@278 62
            /* Test for an empty directory. */
paul@278 63
paul@278 64
            if (image_dir_empty_by_inode(fs, ino))
paul@278 65
            {
paul@278 66
                fprintf(stderr, "Directory not empty: %s\n", path);
paul@278 67
                return 1;
paul@278 68
            }
paul@278 69
        }
paul@278 70
paul@278 71
        /* Otherwise, insist on a non-directory. */
paul@278 72
paul@278 73
        else if (image_isdir_by_inode(fs, ino))
paul@278 74
        {
paul@278 75
            fprintf(stderr, "Cannot remove a directory: %s\n", path);
paul@278 76
            return 1;
paul@278 77
        }
paul@278 78
paul@278 79
        /* Unlink the object. */
paul@278 80
paul@278 81
        if (image_unlink_by_path(fs, path))
paul@278 82
        {
paul@278 83
            fprintf(stderr, "Could not unlink object: %s\n", path);
paul@278 84
            return 1;
paul@278 85
        }
paul@278 86
paul@278 87
        /* Remove the object. */
paul@278 88
paul@278 89
        if (image_remove_by_inode(fs, ino))
paul@278 90
        {
paul@278 91
            fprintf(stderr, "Could not remove object: %s\n", path);
paul@278 92
            return 1;
paul@278 93
        }
paul@278 94
    }
paul@278 95
paul@278 96
    return 0;
paul@278 97
}
paul@278 98
paul@278 99
/* Remove directories from the filesystem image. */
paul@278 100
paul@278 101
int remove_dirs(ext2_filsys fs, int argc, char *argv[])
paul@278 102
{
paul@278 103
    return _remove(fs, argc, argv, 1);
paul@278 104
}
paul@278 105
paul@278 106
/* Remove non-directories from the filesystem image. */
paul@278 107
paul@278 108
int remove_non_dirs(ext2_filsys fs, int argc, char *argv[])
paul@278 109
{
paul@278 110
    return _remove(fs, argc, argv, 0);
paul@278 111
}
paul@278 112
paul@278 113
/* vim: tabstop=4 expandtab shiftwidth=4
paul@278 114
*/