1.1 --- a/conf/dstest_fsaccess.cfg Wed Mar 16 22:44:46 2022 +0100
1.2 +++ b/conf/dstest_fsaccess.cfg Fri Mar 18 00:14:07 2022 +0100
1.3 @@ -47,5 +47,5 @@
1.4 },
1.5 log = { "client", "g" },
1.6 },
1.7 - -- program, directory to read
1.8 - "rom/dstest_fsaccess", "ls", "home/paulb/many");
1.9 + -- program, options, operation involving a script file
1.10 + "rom/dstest_fsaccess", "-m", "0022", "script", "rom/fsaccess.txt");
2.1 --- a/conf/dstest_fsaccess.list Wed Mar 16 22:44:46 2022 +0100
2.2 +++ b/conf/dstest_fsaccess.list Fri Mar 18 00:14:07 2022 +0100
2.3 @@ -2,6 +2,7 @@
2.4 roottask moe rom/dstest_fsaccess.cfg
2.5 module dstest_fsaccess.cfg
2.6 module e2test.fs
2.7 +module fsaccess.txt
2.8 module l4re
2.9 module ned
2.10 module dstest_fsaccess
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/conf/fsaccess.txt Fri Mar 18 00:14:07 2022 +0100
3.3 @@ -0,0 +1,5 @@
3.4 +ls home/paulb/many
3.5 +ls home/paulb
3.6 +mkdir home/paulb/newdir
3.7 +ls home/paulb
3.8 +ls home/paulb/newdir
4.1 --- a/fsaccess/Makefile Wed Mar 16 22:44:46 2022 +0100
4.2 +++ b/fsaccess/Makefile Fri Mar 18 00:14:07 2022 +0100
4.3 @@ -5,7 +5,9 @@
4.4
4.5 MODE = static
4.6
4.7 -SRC_C = fsaccess.c ops.c op_list_objects.c
4.8 +SRC_C = \
4.9 + fsaccess.c input.c session.c \
4.10 + ops.c op_list_objects.c op_make_dirs.c op_script.c
4.11
4.12 REQUIRES_LIBS = l4re_c-util libfsclient libmem libipc libsystypes libe2access_blockserver
4.13
5.1 --- a/fsaccess/fsaccess.c Wed Mar 16 22:44:46 2022 +0100
5.2 +++ b/fsaccess/fsaccess.c Fri Mar 18 00:14:07 2022 +0100
5.3 @@ -20,8 +20,10 @@
5.4 */
5.5
5.6 #include <stdio.h>
5.7 +#include <unistd.h>
5.8
5.9 #include "ops.h"
5.10 +#include "session.h"
5.11
5.12
5.13
5.14 @@ -30,11 +32,6 @@
5.15 char help_text[] = "\
5.16 Usage: %s [ <options> ] <operation> <filename>...\n\
5.17 \n\
5.18 -File ownership options:\n\
5.19 -\n\
5.20 - -g GID Set group identifier for new files\n\
5.21 - -u UID Set user identifier for new files\n\
5.22 -\n\
5.23 File permission options:\n\
5.24 \n\
5.25 -m MASK Set mode/permissions mask for new directories\n\
5.26 @@ -65,11 +62,13 @@
5.27 {"copy-out", copy_out},
5.28 #endif
5.29 {"ls", list_objects},
5.30 + {"mkdir", make_dirs},
5.31 #if 0
5.32 - {"mkdir", make_dirs},
5.33 {"rm", remove_non_dirs},
5.34 {"rmdir", remove_dirs},
5.35 +#endif
5.36 {"script", run_script},
5.37 +#if 0
5.38 {"stat", stat_objects},
5.39 #endif
5.40 {NULL, NULL},
5.41 @@ -85,8 +84,13 @@
5.42 int num_args;
5.43 enum op_results op_result;
5.44
5.45 - args = &argv[1];
5.46 - num_args = argc - 1;
5.47 + /* Parse program options and initialise the argument details. */
5.48 +
5.49 + if (parse_options(argc, argv))
5.50 + return 1;
5.51 +
5.52 + args = &argv[optind];
5.53 + num_args = argc - optind;
5.54
5.55 if (num_args < 2)
5.56 {
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/fsaccess/input.c Fri Mar 18 00:14:07 2022 +0100
6.3 @@ -0,0 +1,118 @@
6.4 +/*
6.5 + * Elementary input utilities.
6.6 + *
6.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
6.8 + *
6.9 + * This program is free software; you can redistribute it and/or
6.10 + * modify it under the terms of the GNU General Public License as
6.11 + * published by the Free Software Foundation; either version 2 of
6.12 + * the License, or (at your option) any later version.
6.13 + *
6.14 + * This program is distributed in the hope that it will be useful,
6.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6.17 + * GNU General Public License for more details.
6.18 + *
6.19 + * You should have received a copy of the GNU General Public License
6.20 + * along with this program; if not, write to the Free Software
6.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
6.22 + * Boston, MA 02110-1301, USA
6.23 + */
6.24 +
6.25 +#include <string.h>
6.26 +
6.27 +#include "input.h"
6.28 +
6.29 +
6.30 +
6.31 +/* Read a line from a file into the given buffer. */
6.32 +
6.33 +char *read_line(FILE *fp, struct read_line_state *state)
6.34 +{
6.35 + size_t nread;
6.36 +
6.37 + do
6.38 + {
6.39 + do
6.40 + {
6.41 + /* Search for a newline character in any available text. */
6.42 +
6.43 + if (state->end > state->start)
6.44 + {
6.45 + state->eolp = strchr(state->start, '\n');
6.46 +
6.47 + if (state->eolp != NULL)
6.48 + {
6.49 + *(state->eolp) = '\0';
6.50 + return state->eolp;
6.51 + }
6.52 + }
6.53 +
6.54 + /* Obtain more text if necessary. */
6.55 +
6.56 + nread = fread(state->end, sizeof(char), state->remaining, fp);
6.57 +
6.58 + /* Handle end of file condition. */
6.59 +
6.60 + if (!nread)
6.61 + {
6.62 + if (state->end > state->start)
6.63 + return state->end;
6.64 + else
6.65 + return NULL;
6.66 + }
6.67 +
6.68 + /* Zero-terminate the string for searching. */
6.69 +
6.70 + *(state->end + nread) = '\0';
6.71 +
6.72 + /* Advance the end of string and subtract remaining space. */
6.73 +
6.74 + state->end += nread;
6.75 + state->remaining -= nread;
6.76 + }
6.77 + while (state->remaining);
6.78 +
6.79 + /* Copy the remaining text to the start of the buffer. */
6.80 +
6.81 + if (state->start > state->buffer)
6.82 + {
6.83 + strcpy(state->buffer, state->start);
6.84 +
6.85 + state->end -= (state->start - state->buffer);
6.86 + state->start = state->buffer;
6.87 + state->remaining = state->buffer_size - 1 - (state->end - state->buffer);
6.88 + }
6.89 + }
6.90 + while (state->remaining);
6.91 +
6.92 + return NULL;
6.93 +}
6.94 +
6.95 +/* Parse the text in the given region, returning details of arguments. */
6.96 +
6.97 +void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args)
6.98 +{
6.99 + *num_args = 0;
6.100 +
6.101 + while ((start != NULL) && (start < end) && (*num_args < max_args))
6.102 + {
6.103 + args[*num_args] = start;
6.104 + (*num_args)++;
6.105 +
6.106 + /* NOTE: Only handling spaces as delimiters. */
6.107 +
6.108 + start = strchr(start, ' ');
6.109 +
6.110 + if (start != NULL)
6.111 + {
6.112 + *start = '\0';
6.113 +
6.114 + if (start < end)
6.115 + start++;
6.116 + }
6.117 + }
6.118 +}
6.119 +
6.120 +/* vim: tabstop=4 expandtab shiftwidth=4
6.121 +*/
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/fsaccess/input.h Fri Mar 18 00:14:07 2022 +0100
7.3 @@ -0,0 +1,38 @@
7.4 +/*
7.5 + * Elementary input utilities.
7.6 + *
7.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
7.8 + *
7.9 + * This program is free software; you can redistribute it and/or
7.10 + * modify it under the terms of the GNU General Public License as
7.11 + * published by the Free Software Foundation; either version 2 of
7.12 + * the License, or (at your option) any later version.
7.13 + *
7.14 + * This program is distributed in the hope that it will be useful,
7.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7.17 + * GNU General Public License for more details.
7.18 + *
7.19 + * You should have received a copy of the GNU General Public License
7.20 + * along with this program; if not, write to the Free Software
7.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
7.22 + * Boston, MA 02110-1301, USA
7.23 + */
7.24 +
7.25 +#pragma once
7.26 +
7.27 +#include <stdio.h>
7.28 +
7.29 +struct read_line_state
7.30 +{
7.31 + char *buffer, *start, *end, *eolp;
7.32 + size_t buffer_size, remaining;
7.33 +};
7.34 +
7.35 +void parse_line(char *start, char *end, int *num_args, char *args[],
7.36 + const int max_args);
7.37 +
7.38 +char *read_line(FILE *fp, struct read_line_state *state);
7.39 +
7.40 +/* vim: tabstop=4 expandtab shiftwidth=4
7.41 +*/
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/fsaccess/op_make_dirs.c Fri Mar 18 00:14:07 2022 +0100
8.3 @@ -0,0 +1,108 @@
8.4 +/*
8.5 + * Make a directory in the filesystem using the client library.
8.6 + *
8.7 + * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
8.8 + *
8.9 + * This program is free software; you can redistribute it and/or
8.10 + * modify it under the terms of the GNU General Public License as
8.11 + * published by the Free Software Foundation; either version 2 of
8.12 + * the License, or (at your option) any later version.
8.13 + *
8.14 + * This program is distributed in the hope that it will be useful,
8.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8.17 + * GNU General Public License for more details.
8.18 + *
8.19 + * You should have received a copy of the GNU General Public License
8.20 + * along with this program; if not, write to the Free Software
8.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
8.22 + * Boston, MA 02110-1301, USA
8.23 + */
8.24 +
8.25 +#include <stdio.h>
8.26 +#include <stdlib.h>
8.27 +#include <string.h>
8.28 +#include <unistd.h>
8.29 +
8.30 +#include <sys/stat.h>
8.31 +#include <sys/types.h>
8.32 +
8.33 +#include <e2access/path.h>
8.34 +#include <fsclient/client.h>
8.35 +
8.36 +#include "ops.h"
8.37 +#include "session.h"
8.38 +
8.39 +
8.40 +
8.41 +/* Alternative metadata set by options. */
8.42 +
8.43 +extern struct metadata md;
8.44 +
8.45 +
8.46 +
8.47 +static int _make_dir(const char *path)
8.48 +{
8.49 + char parent[strlen(path) + 1];
8.50 +
8.51 + /* Attempt to create the directory and return if successful. */
8.52 +
8.53 + long err = client_mkdir(path, 0777 & ~md.mask);
8.54 +
8.55 + if (!err)
8.56 + return 0;
8.57 +
8.58 + /* Without a usable parent directory, attempt to create the parent, and then
8.59 + attempt to create the directory again. */
8.60 +
8.61 + if (err == -L4_ENOENT)
8.62 + {
8.63 + strcpy(parent, path);
8.64 + path_split(parent);
8.65 +
8.66 + if (_make_dir(parent))
8.67 + return 1;
8.68 +
8.69 + return _make_dir(path);
8.70 + }
8.71 +
8.72 + fprintf(stderr, "Could not create directory: %s\n", path);
8.73 + return 1;
8.74 +}
8.75 +
8.76 +static int _make_dirs(const char *path)
8.77 +{
8.78 + struct stat st;
8.79 +
8.80 + /* Search for the remaining components. */
8.81 +
8.82 + long err = client_stat(path, &st);
8.83 +
8.84 + if (!err)
8.85 + {
8.86 + fprintf(stderr, "Path exists: %s\n", path);
8.87 + return 1;
8.88 + }
8.89 +
8.90 + /* Try and make the directory. */
8.91 +
8.92 + return _make_dir(path);
8.93 +}
8.94 +
8.95 +/* Make directories in the filesystem. */
8.96 +
8.97 +int make_dirs(int argc, char *argv[])
8.98 +{
8.99 + int i;
8.100 +
8.101 + /* Make each directory component in the given pathname. */
8.102 +
8.103 + for (i = 0; i < argc; i++)
8.104 + if (_make_dirs(argv[i]))
8.105 + return 1;
8.106 +
8.107 + return 0;
8.108 +}
8.109 +
8.110 +/* vim: tabstop=4 expandtab shiftwidth=4
8.111 +*/
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/fsaccess/op_script.c Fri Mar 18 00:14:07 2022 +0100
9.3 @@ -0,0 +1,84 @@
9.4 +/*
9.5 + * Run a script accessing a filesystem.
9.6 + *
9.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
9.8 + *
9.9 + * This program is free software; you can redistribute it and/or
9.10 + * modify it under the terms of the GNU General Public License as
9.11 + * published by the Free Software Foundation; either version 2 of
9.12 + * the License, or (at your option) any later version.
9.13 + *
9.14 + * This program is distributed in the hope that it will be useful,
9.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9.17 + * GNU General Public License for more details.
9.18 + *
9.19 + * You should have received a copy of the GNU General Public License
9.20 + * along with this program; if not, write to the Free Software
9.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
9.22 + * Boston, MA 02110-1301, USA
9.23 + */
9.24 +
9.25 +#include <stdio.h>
9.26 +#include <string.h>
9.27 +
9.28 +#include "input.h"
9.29 +#include "ops.h"
9.30 +
9.31 +
9.32 +
9.33 +/* Line buffer size. */
9.34 +
9.35 +static int BUFSIZE = 4096;
9.36 +
9.37 +/* Maximum number of arguments in scripts. */
9.38 +
9.39 +const int MAX_ARGS = 32;
9.40 +
9.41 +
9.42 +
9.43 +/* Read operations from a script file. */
9.44 +
9.45 +int run_script(int argc, char *argv[])
9.46 +{
9.47 + FILE *fp;
9.48 + char buffer[BUFSIZE];
9.49 + struct read_line_state state;
9.50 + enum op_results op_result;
9.51 + int num_args;
9.52 + char *args[MAX_ARGS];
9.53 + int i;
9.54 +
9.55 + for (i = 0; i < argc; i++)
9.56 + {
9.57 + fp = fopen(argv[i], "r");
9.58 +
9.59 + state.buffer = buffer;
9.60 + state.start = buffer;
9.61 + state.end = buffer;
9.62 + state.buffer_size = BUFSIZE;
9.63 + state.remaining = BUFSIZE - 1;
9.64 +
9.65 + while (read_line(fp, &state) != NULL)
9.66 + {
9.67 + parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS);
9.68 +
9.69 + if (num_args > 1)
9.70 + {
9.71 + op_result = run_operation(args[0], num_args - 1, &args[1]);
9.72 +
9.73 + if (handle_op_result(args[0], op_result))
9.74 + return 1;
9.75 + }
9.76 +
9.77 + state.start = state.eolp + 1;
9.78 + }
9.79 +
9.80 + fclose(fp);
9.81 + }
9.82 +
9.83 + return 0;
9.84 +}
9.85 +
9.86 +/* vim: tabstop=4 expandtab shiftwidth=4
9.87 +*/
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/fsaccess/session.c Fri Mar 18 00:14:07 2022 +0100
10.3 @@ -0,0 +1,63 @@
10.4 +/*
10.5 + * Session utilities.
10.6 + *
10.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
10.8 + *
10.9 + * This program is free software; you can redistribute it and/or
10.10 + * modify it under the terms of the GNU General Public License as
10.11 + * published by the Free Software Foundation; either version 2 of
10.12 + * the License, or (at your option) any later version.
10.13 + *
10.14 + * This program is distributed in the hope that it will be useful,
10.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10.17 + * GNU General Public License for more details.
10.18 + *
10.19 + * You should have received a copy of the GNU General Public License
10.20 + * along with this program; if not, write to the Free Software
10.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
10.22 + * Boston, MA 02110-1301, USA
10.23 + */
10.24 +
10.25 +#include <stdio.h>
10.26 +#include <stdlib.h>
10.27 +#include <string.h>
10.28 +#include <unistd.h>
10.29 +
10.30 +#include "session.h"
10.31 +
10.32 +
10.33 +
10.34 +/* Alternative metadata set by options. */
10.35 +
10.36 +struct metadata md;
10.37 +
10.38 +
10.39 +
10.40 +/* Parse program options. */
10.41 +
10.42 +int parse_options(int argc, char *argv[])
10.43 +{
10.44 + int opt;
10.45 +
10.46 + md.mask = 0000;
10.47 +
10.48 + while ((opt = getopt(argc, argv, "m:")) != -1)
10.49 + {
10.50 + switch (opt)
10.51 + {
10.52 + case 'm':
10.53 + md.mask = strtol(optarg, NULL, 0);
10.54 + break;
10.55 +
10.56 + default:
10.57 + fprintf(stderr, "Option not recognised: %s\n", argv[optind]);
10.58 + return -1;
10.59 + }
10.60 + }
10.61 +
10.62 + return 0;
10.63 +}
10.64 +
10.65 +/* vim: tabstop=4 expandtab shiftwidth=4
10.66 +*/
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/fsaccess/session.h Fri Mar 18 00:14:07 2022 +0100
11.3 @@ -0,0 +1,36 @@
11.4 +/*
11.5 + * Common session details.
11.6 + *
11.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
11.8 + *
11.9 + * This program is free software; you can redistribute it and/or
11.10 + * modify it under the terms of the GNU General Public License as
11.11 + * published by the Free Software Foundation; either version 2 of
11.12 + * the License, or (at your option) any later version.
11.13 + *
11.14 + * This program is distributed in the hope that it will be useful,
11.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11.17 + * GNU General Public License for more details.
11.18 + *
11.19 + * You should have received a copy of the GNU General Public License
11.20 + * along with this program; if not, write to the Free Software
11.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
11.22 + * Boston, MA 02110-1301, USA
11.23 + */
11.24 +
11.25 +#pragma once
11.26 +
11.27 +#include <sys/types.h>
11.28 +
11.29 +/* Alternative metadata set by options. */
11.30 +
11.31 +struct metadata
11.32 +{
11.33 + mode_t mask;
11.34 +};
11.35 +
11.36 +int parse_options(int argc, char *argv[]);
11.37 +
11.38 +/* vim: tabstop=4 expandtab shiftwidth=4
11.39 +*/