1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/fsaccess/op_list_objects.c Mon Mar 07 23:54:48 2022 +0100
1.3 @@ -0,0 +1,133 @@
1.4 +/*
1.5 + * List filesystem objects using the client library.
1.6 + *
1.7 + * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <stdio.h>
1.26 +#include <stdlib.h>
1.27 +#include <string.h>
1.28 +
1.29 +#include <sys/stat.h>
1.30 +#include <sys/types.h>
1.31 +
1.32 +#include <e2access/format.h> /* get_permission_string */
1.33 +#include <e2access/path.h>
1.34 +#include <fsclient/client.h>
1.35 +
1.36 +#include "ops.h"
1.37 +
1.38 +
1.39 +
1.40 +/* Show object details. */
1.41 +
1.42 +static void _show_object(const char *basename, struct stat *st)
1.43 +{
1.44 + printf("%s%s %5d %5d %6ld %6ld %s\n",
1.45 + S_ISDIR(st->st_mode) ? "d" : "-",
1.46 + get_permission_string(st->st_mode),
1.47 + st->st_uid,
1.48 + st->st_gid,
1.49 + st->st_size,
1.50 + st->st_nlink,
1.51 + basename);
1.52 +}
1.53 +
1.54 +/* Show an object in a directory. */
1.55 +
1.56 +static int _show_dirent(const char *dirname, struct dirent *dirent)
1.57 +{
1.58 + struct stat st;
1.59 + char path[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
1.60 +
1.61 + sprintf(path, "%s/%s", dirname, dirent->d_name);
1.62 +
1.63 + if (client_stat(path, &st))
1.64 + return 1;
1.65 +
1.66 + _show_object(dirent->d_name, &st);
1.67 +
1.68 + return 0;
1.69 +}
1.70 +
1.71 +/* List a directory or file. */
1.72 +
1.73 +static int _list_object(const char *path)
1.74 +{
1.75 + struct stat st;
1.76 + file_t *reader;
1.77 + struct dirent *dirent;
1.78 +
1.79 + if (client_stat(path, &st))
1.80 + return 1;
1.81 +
1.82 + if (S_ISDIR(st.st_mode))
1.83 + {
1.84 + reader = client_opendir(path);
1.85 +
1.86 + if (reader == NULL)
1.87 + return 1;
1.88 +
1.89 + /* Show the directory entries. */
1.90 +
1.91 + while ((dirent = client_readdir(reader)) != NULL)
1.92 + {
1.93 + if (_show_dirent(path, dirent))
1.94 + {
1.95 + free(dirent);
1.96 + return 1;
1.97 + }
1.98 +
1.99 + free(dirent);
1.100 + }
1.101 + }
1.102 + else
1.103 + _show_object(path_basename(path), &st);
1.104 +
1.105 + return 0;
1.106 +}
1.107 +
1.108 +/* List objects in the filesystem image. */
1.109 +
1.110 +int list_objects(int argc, char *argv[])
1.111 +{
1.112 + int i;
1.113 + char *path;
1.114 +
1.115 + for (i = 0; i < argc; i++)
1.116 + {
1.117 + path = argv[i];
1.118 +
1.119 + /* Emit each object. */
1.120 +
1.121 + puts(path);
1.122 +
1.123 + /* List individual files or directories. */
1.124 +
1.125 + if (_list_object(path))
1.126 + {
1.127 + fprintf(stderr, "Failed to list object: %s\n", path);
1.128 + return 1;
1.129 + }
1.130 + }
1.131 +
1.132 + return 0;
1.133 +}
1.134 +
1.135 +/* vim: tabstop=4 expandtab shiftwidth=4
1.136 +*/