paul@601 | 1 | /* |
paul@601 | 2 | * List filesystem objects using the client library. |
paul@601 | 3 | * |
paul@601 | 4 | * Originally developed as fsaccess/op_list_objects.c, this should eventually |
paul@601 | 5 | * use C library functions instead of client library functions. |
paul@601 | 6 | * |
paul@601 | 7 | * Copyright (C) 2019, 2022, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@601 | 8 | * |
paul@601 | 9 | * This program is free software; you can redistribute it and/or |
paul@601 | 10 | * modify it under the terms of the GNU General Public License as |
paul@601 | 11 | * published by the Free Software Foundation; either version 2 of |
paul@601 | 12 | * the License, or (at your option) any later version. |
paul@601 | 13 | * |
paul@601 | 14 | * This program is distributed in the hope that it will be useful, |
paul@601 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@601 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@601 | 17 | * GNU General Public License for more details. |
paul@601 | 18 | * |
paul@601 | 19 | * You should have received a copy of the GNU General Public License |
paul@601 | 20 | * along with this program; if not, write to the Free Software |
paul@601 | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@601 | 22 | * Boston, MA 02110-1301, USA |
paul@601 | 23 | */ |
paul@601 | 24 | |
paul@653 | 25 | #include <dirent.h> |
paul@601 | 26 | #include <stdio.h> |
paul@601 | 27 | #include <stdlib.h> |
paul@601 | 28 | #include <string.h> |
paul@601 | 29 | |
paul@601 | 30 | #include <sys/stat.h> |
paul@601 | 31 | #include <sys/types.h> |
paul@601 | 32 | |
paul@601 | 33 | #include <e2access/format.h> /* get_permission_string */ |
paul@601 | 34 | #include <e2access/path.h> |
paul@653 | 35 | #include <fsclient/client.h> |
paul@601 | 36 | #include <systypes/format.h> |
paul@601 | 37 | |
paul@601 | 38 | #define FMTnlinkd "%" pFMTnlink "d" |
paul@601 | 39 | |
paul@601 | 40 | |
paul@601 | 41 | |
paul@601 | 42 | /* Show object details. */ |
paul@601 | 43 | |
paul@601 | 44 | static void _show_object(const char *basename, struct stat *st) |
paul@601 | 45 | { |
paul@653 | 46 | printf("%s%s %5d %5d %6ld " FMTnlinkd " %s\n", |
paul@601 | 47 | S_ISDIR(st->st_mode) ? "d" : "-", |
paul@601 | 48 | get_permission_string(st->st_mode), |
paul@601 | 49 | st->st_uid, |
paul@601 | 50 | st->st_gid, |
paul@601 | 51 | st->st_size, |
paul@601 | 52 | st->st_nlink, |
paul@601 | 53 | basename); |
paul@601 | 54 | } |
paul@601 | 55 | |
paul@601 | 56 | /* Show an object in a directory. */ |
paul@601 | 57 | |
paul@601 | 58 | static int _show_dirent(const char *dirname, struct dirent *dirent) |
paul@601 | 59 | { |
paul@601 | 60 | struct stat st; |
paul@601 | 61 | char path[strlen(dirname) + 1 + strlen(dirent->d_name) + 1]; |
paul@601 | 62 | |
paul@601 | 63 | sprintf(path, "%s/%s", dirname, dirent->d_name); |
paul@601 | 64 | |
paul@601 | 65 | if (client_stat(path, &st)) |
paul@601 | 66 | return 1; |
paul@601 | 67 | |
paul@601 | 68 | _show_object(dirent->d_name, &st); |
paul@601 | 69 | |
paul@601 | 70 | return 0; |
paul@601 | 71 | } |
paul@601 | 72 | |
paul@601 | 73 | /* List a directory or file. */ |
paul@601 | 74 | |
paul@601 | 75 | static int _list_object(const char *path) |
paul@601 | 76 | { |
paul@601 | 77 | struct stat st; |
paul@601 | 78 | file_t *reader; |
paul@601 | 79 | struct dirent *dirent; |
paul@601 | 80 | |
paul@601 | 81 | if (client_stat(path, &st)) |
paul@601 | 82 | return 1; |
paul@601 | 83 | |
paul@601 | 84 | if (S_ISDIR(st.st_mode)) |
paul@601 | 85 | { |
paul@601 | 86 | reader = client_opendir(path); |
paul@601 | 87 | |
paul@653 | 88 | if (!client_opened(reader)) |
paul@653 | 89 | { |
paul@653 | 90 | client_close(reader); |
paul@601 | 91 | return 1; |
paul@653 | 92 | } |
paul@601 | 93 | |
paul@601 | 94 | /* Show the directory entries. */ |
paul@601 | 95 | |
paul@601 | 96 | while ((dirent = client_readdir(reader)) != NULL) |
paul@601 | 97 | { |
paul@601 | 98 | if (_show_dirent(path, dirent)) |
paul@601 | 99 | { |
paul@601 | 100 | free(dirent); |
paul@601 | 101 | return 1; |
paul@601 | 102 | } |
paul@601 | 103 | |
paul@601 | 104 | free(dirent); |
paul@601 | 105 | } |
paul@601 | 106 | |
paul@601 | 107 | client_close(reader); |
paul@601 | 108 | } |
paul@601 | 109 | else |
paul@601 | 110 | _show_object(path_basename(path), &st); |
paul@601 | 111 | |
paul@601 | 112 | return 0; |
paul@601 | 113 | } |
paul@601 | 114 | |
paul@603 | 115 | /* List an object in the filesystem. */ |
paul@601 | 116 | |
paul@601 | 117 | static int list_object(const char *path) |
paul@601 | 118 | { |
paul@601 | 119 | /* Emit each object's name. */ |
paul@601 | 120 | |
paul@653 | 121 | printf("%s\n", path); |
paul@601 | 122 | |
paul@601 | 123 | /* List individual files or directories. */ |
paul@601 | 124 | |
paul@601 | 125 | if (_list_object(path)) |
paul@601 | 126 | { |
paul@601 | 127 | /* NOTE: Should write via a standard error stream provided in the same |
paul@601 | 128 | way as the output stream. */ |
paul@601 | 129 | |
paul@601 | 130 | fprintf(stderr, "Failed to list object: %s\n", path); |
paul@601 | 131 | return 1; |
paul@601 | 132 | } |
paul@601 | 133 | |
paul@601 | 134 | return 0; |
paul@601 | 135 | } |
paul@601 | 136 | |
paul@603 | 137 | /* List objects in the filesystem. */ |
paul@601 | 138 | |
paul@601 | 139 | int main(int argc, char *argv[]) |
paul@601 | 140 | { |
paul@601 | 141 | int i; |
paul@601 | 142 | |
paul@601 | 143 | /* List the top level without any argument. */ |
paul@601 | 144 | |
paul@601 | 145 | if (argc < 2) |
paul@601 | 146 | { |
paul@601 | 147 | if (list_object("")) |
paul@601 | 148 | return 1; |
paul@601 | 149 | } |
paul@601 | 150 | else |
paul@601 | 151 | { |
paul@601 | 152 | /* Otherwise, list all specified objects. */ |
paul@601 | 153 | |
paul@601 | 154 | for (i = 1; i < argc; i++) |
paul@601 | 155 | { |
paul@601 | 156 | if (list_object(argv[i])) |
paul@601 | 157 | return 1; |
paul@601 | 158 | } |
paul@601 | 159 | } |
paul@601 | 160 | |
paul@601 | 161 | return 0; |
paul@601 | 162 | } |
paul@601 | 163 | |
paul@601 | 164 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@601 | 165 | */ |