1.1 --- a/test_files/Control Mon Jan 15 01:18:41 2024 +0100
1.2 +++ b/test_files/Control Mon Jan 15 23:44:56 2024 +0100
1.3 @@ -1,3 +1,3 @@
1.4 provides: fstest_files
1.5 -requires: libc libstdc++ libexec libfsclient libsystypes l4re_c-util
1.6 +requires: libc libstdc++ libexec libfsclient libsystypes l4re_c-util libe2access
1.7 maintainer: paul@boddie.org.uk
2.1 --- a/test_files/programs/Makefile Mon Jan 15 01:18:41 2024 +0100
2.2 +++ b/test_files/programs/Makefile Mon Jan 15 23:44:56 2024 +0100
2.3 @@ -1,7 +1,7 @@
2.4 PKGDIR ?= ..
2.5 L4DIR ?= $(PKGDIR)/../../..
2.6
2.7 -TARGET = clip dstest_exec_payload
2.8 +TARGET = clip dstest_exec_payload ls
2.9
2.10 MODE = static
2.11
2.12 @@ -9,6 +9,8 @@
2.13
2.14 SRC_C_dstest_exec_payload = dstest_exec_payload.c
2.15
2.16 -REQUIRES_LIBS = libc libfsclient l4re_c-util libsystypes
2.17 +SRC_C_ls = ls.c
2.18 +
2.19 +REQUIRES_LIBS = libc libfsclient l4re_c-util libsystypes libe2access
2.20
2.21 include $(L4DIR)/mk/prog.mk
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/test_files/programs/ls.c Mon Jan 15 23:44:56 2024 +0100
3.3 @@ -0,0 +1,183 @@
3.4 +/*
3.5 + * List filesystem objects using the client library.
3.6 + *
3.7 + * Originally developed as fsaccess/op_list_objects.c, this should eventually
3.8 + * use C library functions instead of client library functions.
3.9 + *
3.10 + * Copyright (C) 2019, 2022, 2024 Paul Boddie <paul@boddie.org.uk>
3.11 + *
3.12 + * This program is free software; you can redistribute it and/or
3.13 + * modify it under the terms of the GNU General Public License as
3.14 + * published by the Free Software Foundation; either version 2 of
3.15 + * the License, or (at your option) any later version.
3.16 + *
3.17 + * This program is distributed in the hope that it will be useful,
3.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.20 + * GNU General Public License for more details.
3.21 + *
3.22 + * You should have received a copy of the GNU General Public License
3.23 + * along with this program; if not, write to the Free Software
3.24 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.25 + * Boston, MA 02110-1301, USA
3.26 + */
3.27 +
3.28 +#include <stdio.h>
3.29 +#include <stdlib.h>
3.30 +#include <string.h>
3.31 +
3.32 +#include <sys/stat.h>
3.33 +#include <sys/types.h>
3.34 +
3.35 +#include <e2access/format.h> /* get_permission_string */
3.36 +#include <e2access/path.h>
3.37 +#include <systypes/format.h>
3.38 +
3.39 +#define FMTnlinkd "%" pFMTnlink "d"
3.40 +
3.41 +
3.42 +
3.43 +/* NOTE: For inclusion in the C library: stream acquisition and access. */
3.44 +
3.45 +#include <fsclient/client.h>
3.46 +#include <systypes/env.h>
3.47 +#include <systypes/fcntl.h>
3.48 +
3.49 +file_t *output;
3.50 +
3.51 +
3.52 +
3.53 +/* Show object details. */
3.54 +
3.55 +static void _show_object(const char *basename, struct stat *st)
3.56 +{
3.57 + char buffer[strlen(basename) + 64];
3.58 +
3.59 + sprintf(buffer, "%s%s %5d %5d %6ld " FMTnlinkd " %s\n",
3.60 + S_ISDIR(st->st_mode) ? "d" : "-",
3.61 + get_permission_string(st->st_mode),
3.62 + st->st_uid,
3.63 + st->st_gid,
3.64 + st->st_size,
3.65 + st->st_nlink,
3.66 + basename);
3.67 +
3.68 + client_write(output, buffer, strlen(buffer));
3.69 +}
3.70 +
3.71 +/* Show an object in a directory. */
3.72 +
3.73 +static int _show_dirent(const char *dirname, struct dirent *dirent)
3.74 +{
3.75 + struct stat st;
3.76 + char path[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
3.77 +
3.78 + sprintf(path, "%s/%s", dirname, dirent->d_name);
3.79 +
3.80 + if (client_stat(path, &st))
3.81 + return 1;
3.82 +
3.83 + _show_object(dirent->d_name, &st);
3.84 +
3.85 + return 0;
3.86 +}
3.87 +
3.88 +/* List a directory or file. */
3.89 +
3.90 +static int _list_object(const char *path)
3.91 +{
3.92 + struct stat st;
3.93 + file_t *reader;
3.94 + struct dirent *dirent;
3.95 +
3.96 + if (client_stat(path, &st))
3.97 + return 1;
3.98 +
3.99 + if (S_ISDIR(st.st_mode))
3.100 + {
3.101 + reader = client_opendir(path);
3.102 +
3.103 + if (reader == NULL)
3.104 + return 1;
3.105 +
3.106 + /* Show the directory entries. */
3.107 +
3.108 + while ((dirent = client_readdir(reader)) != NULL)
3.109 + {
3.110 + if (_show_dirent(path, dirent))
3.111 + {
3.112 + free(dirent);
3.113 + return 1;
3.114 + }
3.115 +
3.116 + free(dirent);
3.117 + }
3.118 +
3.119 + client_close(reader);
3.120 + }
3.121 + else
3.122 + _show_object(path_basename(path), &st);
3.123 +
3.124 + return 0;
3.125 +}
3.126 +
3.127 +/* List an object in the filesystem image. */
3.128 +
3.129 +static int list_object(const char *path)
3.130 +{
3.131 + /* Emit each object's name. */
3.132 +
3.133 + client_write(output, path, strlen(path));
3.134 + client_write(output, "\n", 1);
3.135 +
3.136 + /* List individual files or directories. */
3.137 +
3.138 + if (_list_object(path))
3.139 + {
3.140 + /* NOTE: Should write via a standard error stream provided in the same
3.141 + way as the output stream. */
3.142 +
3.143 + fprintf(stderr, "Failed to list object: %s\n", path);
3.144 + return 1;
3.145 + }
3.146 +
3.147 + return 0;
3.148 +}
3.149 +
3.150 +/* List objects in the filesystem image. */
3.151 +
3.152 +int main(int argc, char *argv[])
3.153 +{
3.154 + int i;
3.155 +
3.156 + /* NOTE: For inclusion in the C library: stream acquisition and access. */
3.157 +
3.158 + output = client_get_stream(ENV_OUTPUT_STREAM_NAME, O_WRONLY);
3.159 +
3.160 + /* List the top level without any argument. */
3.161 +
3.162 + if (argc < 2)
3.163 + {
3.164 + if (list_object(""))
3.165 + return 1;
3.166 + }
3.167 + else
3.168 + {
3.169 + /* Otherwise, list all specified objects. */
3.170 +
3.171 + for (i = 1; i < argc; i++)
3.172 + {
3.173 + if (list_object(argv[i]))
3.174 + return 1;
3.175 + }
3.176 + }
3.177 +
3.178 + /* NOTE: For inclusion in the C library: stream acquisition and access. */
3.179 +
3.180 + client_flush(output);
3.181 +
3.182 + return 0;
3.183 +}
3.184 +
3.185 +/* vim: tabstop=4 expandtab shiftwidth=4
3.186 +*/