L4Re/departure

Annotated fsaccess/op_list_objects.c

391:bc65615a8fed
2022-06-30 Paul Boddie Added missing structure members. mmap-region-flags
paul@283 1
/*
paul@283 2
 * List filesystem objects using the client library.
paul@283 3
 *
paul@283 4
 * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
paul@283 5
 *
paul@283 6
 * This program is free software; you can redistribute it and/or
paul@283 7
 * modify it under the terms of the GNU General Public License as
paul@283 8
 * published by the Free Software Foundation; either version 2 of
paul@283 9
 * the License, or (at your option) any later version.
paul@283 10
 *
paul@283 11
 * This program is distributed in the hope that it will be useful,
paul@283 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@283 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@283 14
 * GNU General Public License for more details.
paul@283 15
 *
paul@283 16
 * You should have received a copy of the GNU General Public License
paul@283 17
 * along with this program; if not, write to the Free Software
paul@283 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@283 19
 * Boston, MA  02110-1301, USA
paul@283 20
 */
paul@283 21
paul@283 22
#include <stdio.h>
paul@283 23
#include <stdlib.h>
paul@283 24
#include <string.h>
paul@283 25
paul@283 26
#include <sys/stat.h>
paul@283 27
#include <sys/types.h>
paul@283 28
paul@283 29
#include <e2access/format.h>    /* get_permission_string */
paul@283 30
#include <e2access/path.h>
paul@283 31
#include <fsclient/client.h>
paul@283 32
paul@283 33
#include "ops.h"
paul@283 34
paul@283 35
paul@283 36
paul@283 37
/* Show object details. */
paul@283 38
paul@283 39
static void _show_object(const char *basename, struct stat *st)
paul@283 40
{
paul@283 41
    printf("%s%s   %5d %5d      %6ld %6ld %s\n",
paul@283 42
        S_ISDIR(st->st_mode) ? "d" : "-",
paul@283 43
        get_permission_string(st->st_mode),
paul@283 44
        st->st_uid,
paul@283 45
        st->st_gid,
paul@283 46
        st->st_size,
paul@283 47
        st->st_nlink,
paul@283 48
        basename);
paul@283 49
}
paul@283 50
paul@283 51
/* Show an object in a directory. */
paul@283 52
paul@283 53
static int _show_dirent(const char *dirname, struct dirent *dirent)
paul@283 54
{
paul@283 55
    struct stat st;
paul@283 56
    char path[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
paul@283 57
paul@283 58
    sprintf(path, "%s/%s", dirname, dirent->d_name);
paul@283 59
paul@283 60
    if (client_stat(path, &st))
paul@283 61
        return 1;
paul@283 62
paul@283 63
    _show_object(dirent->d_name, &st);
paul@283 64
paul@283 65
    return 0;
paul@283 66
}
paul@283 67
paul@283 68
/* List a directory or file. */
paul@283 69
paul@283 70
static int _list_object(const char *path)
paul@283 71
{
paul@283 72
    struct stat st;
paul@283 73
    file_t *reader;
paul@283 74
    struct dirent *dirent;
paul@283 75
paul@283 76
    if (client_stat(path, &st))
paul@283 77
        return 1;
paul@283 78
paul@283 79
    if (S_ISDIR(st.st_mode))
paul@283 80
    {
paul@283 81
        reader = client_opendir(path);
paul@283 82
paul@283 83
        if (reader == NULL)
paul@283 84
            return 1;
paul@283 85
paul@283 86
        /* Show the directory entries. */
paul@283 87
paul@283 88
        while ((dirent = client_readdir(reader)) != NULL)
paul@283 89
        {
paul@283 90
            if (_show_dirent(path, dirent))
paul@283 91
            {
paul@283 92
                free(dirent);
paul@283 93
                return 1;
paul@283 94
            }
paul@283 95
paul@283 96
            free(dirent);
paul@283 97
        }
paul@298 98
paul@298 99
        client_close(reader);
paul@283 100
    }
paul@283 101
    else
paul@283 102
        _show_object(path_basename(path), &st);
paul@283 103
paul@283 104
    return 0;
paul@283 105
}
paul@283 106
paul@283 107
/* List objects in the filesystem image. */
paul@283 108
paul@283 109
int list_objects(int argc, char *argv[])
paul@283 110
{
paul@283 111
    int i;
paul@283 112
    char *path;
paul@283 113
paul@283 114
    for (i = 0; i < argc; i++)
paul@283 115
    {
paul@283 116
        path = argv[i];
paul@283 117
paul@283 118
        /* Emit each object. */
paul@283 119
paul@283 120
        puts(path);
paul@283 121
paul@283 122
        /* List individual files or directories. */
paul@283 123
paul@283 124
        if (_list_object(path))
paul@283 125
        {
paul@283 126
            fprintf(stderr, "Failed to list object: %s\n", path);
paul@283 127
            return 1;
paul@283 128
        }
paul@283 129
    }
paul@283 130
paul@283 131
    return 0;
paul@283 132
}
paul@283 133
paul@283 134
/* vim: tabstop=4 expandtab shiftwidth=4
paul@283 135
*/