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@601 | 25 | #include <stdio.h> |
paul@601 | 26 | #include <stdlib.h> |
paul@601 | 27 | #include <string.h> |
paul@601 | 28 | |
paul@601 | 29 | #include <sys/stat.h> |
paul@601 | 30 | #include <sys/types.h> |
paul@601 | 31 | |
paul@601 | 32 | #include <e2access/format.h> /* get_permission_string */ |
paul@601 | 33 | #include <e2access/path.h> |
paul@601 | 34 | #include <systypes/format.h> |
paul@601 | 35 | |
paul@601 | 36 | #define FMTnlinkd "%" pFMTnlink "d" |
paul@601 | 37 | |
paul@601 | 38 | |
paul@601 | 39 | |
paul@601 | 40 | /* NOTE: For inclusion in the C library: stream acquisition and access. */ |
paul@601 | 41 | |
paul@601 | 42 | #include <fsclient/client.h> |
paul@601 | 43 | #include <systypes/env.h> |
paul@601 | 44 | #include <systypes/fcntl.h> |
paul@601 | 45 | |
paul@601 | 46 | file_t *output; |
paul@601 | 47 | |
paul@601 | 48 | |
paul@601 | 49 | |
paul@601 | 50 | /* Show object details. */ |
paul@601 | 51 | |
paul@601 | 52 | static void _show_object(const char *basename, struct stat *st) |
paul@601 | 53 | { |
paul@601 | 54 | char buffer[strlen(basename) + 64]; |
paul@601 | 55 | |
paul@601 | 56 | sprintf(buffer, "%s%s %5d %5d %6ld " FMTnlinkd " %s\n", |
paul@601 | 57 | S_ISDIR(st->st_mode) ? "d" : "-", |
paul@601 | 58 | get_permission_string(st->st_mode), |
paul@601 | 59 | st->st_uid, |
paul@601 | 60 | st->st_gid, |
paul@601 | 61 | st->st_size, |
paul@601 | 62 | st->st_nlink, |
paul@601 | 63 | basename); |
paul@601 | 64 | |
paul@601 | 65 | client_write(output, buffer, strlen(buffer)); |
paul@601 | 66 | } |
paul@601 | 67 | |
paul@601 | 68 | /* Show an object in a directory. */ |
paul@601 | 69 | |
paul@601 | 70 | static int _show_dirent(const char *dirname, struct dirent *dirent) |
paul@601 | 71 | { |
paul@601 | 72 | struct stat st; |
paul@601 | 73 | char path[strlen(dirname) + 1 + strlen(dirent->d_name) + 1]; |
paul@601 | 74 | |
paul@601 | 75 | sprintf(path, "%s/%s", dirname, dirent->d_name); |
paul@601 | 76 | |
paul@601 | 77 | if (client_stat(path, &st)) |
paul@601 | 78 | return 1; |
paul@601 | 79 | |
paul@601 | 80 | _show_object(dirent->d_name, &st); |
paul@601 | 81 | |
paul@601 | 82 | return 0; |
paul@601 | 83 | } |
paul@601 | 84 | |
paul@601 | 85 | /* List a directory or file. */ |
paul@601 | 86 | |
paul@601 | 87 | static int _list_object(const char *path) |
paul@601 | 88 | { |
paul@601 | 89 | struct stat st; |
paul@601 | 90 | file_t *reader; |
paul@601 | 91 | struct dirent *dirent; |
paul@601 | 92 | |
paul@601 | 93 | if (client_stat(path, &st)) |
paul@601 | 94 | return 1; |
paul@601 | 95 | |
paul@601 | 96 | if (S_ISDIR(st.st_mode)) |
paul@601 | 97 | { |
paul@601 | 98 | reader = client_opendir(path); |
paul@601 | 99 | |
paul@601 | 100 | if (reader == NULL) |
paul@601 | 101 | return 1; |
paul@601 | 102 | |
paul@601 | 103 | /* Show the directory entries. */ |
paul@601 | 104 | |
paul@601 | 105 | while ((dirent = client_readdir(reader)) != NULL) |
paul@601 | 106 | { |
paul@601 | 107 | if (_show_dirent(path, dirent)) |
paul@601 | 108 | { |
paul@601 | 109 | free(dirent); |
paul@601 | 110 | return 1; |
paul@601 | 111 | } |
paul@601 | 112 | |
paul@601 | 113 | free(dirent); |
paul@601 | 114 | } |
paul@601 | 115 | |
paul@601 | 116 | client_close(reader); |
paul@601 | 117 | } |
paul@601 | 118 | else |
paul@601 | 119 | _show_object(path_basename(path), &st); |
paul@601 | 120 | |
paul@601 | 121 | return 0; |
paul@601 | 122 | } |
paul@601 | 123 | |
paul@603 | 124 | /* List an object in the filesystem. */ |
paul@601 | 125 | |
paul@601 | 126 | static int list_object(const char *path) |
paul@601 | 127 | { |
paul@601 | 128 | /* Emit each object's name. */ |
paul@601 | 129 | |
paul@601 | 130 | client_write(output, path, strlen(path)); |
paul@601 | 131 | client_write(output, "\n", 1); |
paul@601 | 132 | |
paul@601 | 133 | /* List individual files or directories. */ |
paul@601 | 134 | |
paul@601 | 135 | if (_list_object(path)) |
paul@601 | 136 | { |
paul@601 | 137 | /* NOTE: Should write via a standard error stream provided in the same |
paul@601 | 138 | way as the output stream. */ |
paul@601 | 139 | |
paul@601 | 140 | fprintf(stderr, "Failed to list object: %s\n", path); |
paul@601 | 141 | return 1; |
paul@601 | 142 | } |
paul@601 | 143 | |
paul@601 | 144 | return 0; |
paul@601 | 145 | } |
paul@601 | 146 | |
paul@603 | 147 | /* List objects in the filesystem. */ |
paul@601 | 148 | |
paul@601 | 149 | int main(int argc, char *argv[]) |
paul@601 | 150 | { |
paul@601 | 151 | int i; |
paul@601 | 152 | |
paul@601 | 153 | /* NOTE: For inclusion in the C library: stream acquisition and access. */ |
paul@601 | 154 | |
paul@601 | 155 | output = client_get_stream(ENV_OUTPUT_STREAM_NAME, O_WRONLY); |
paul@601 | 156 | |
paul@601 | 157 | /* List the top level without any argument. */ |
paul@601 | 158 | |
paul@601 | 159 | if (argc < 2) |
paul@601 | 160 | { |
paul@601 | 161 | if (list_object("")) |
paul@601 | 162 | return 1; |
paul@601 | 163 | } |
paul@601 | 164 | else |
paul@601 | 165 | { |
paul@601 | 166 | /* Otherwise, list all specified objects. */ |
paul@601 | 167 | |
paul@601 | 168 | for (i = 1; i < argc; i++) |
paul@601 | 169 | { |
paul@601 | 170 | if (list_object(argv[i])) |
paul@601 | 171 | return 1; |
paul@601 | 172 | } |
paul@601 | 173 | } |
paul@601 | 174 | |
paul@601 | 175 | /* NOTE: For inclusion in the C library: stream acquisition and access. */ |
paul@601 | 176 | |
paul@601 | 177 | client_flush(output); |
paul@601 | 178 | |
paul@601 | 179 | return 0; |
paul@601 | 180 | } |
paul@601 | 181 | |
paul@601 | 182 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@601 | 183 | */ |