1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/dstest_file_remove.cc Mon Jun 27 16:30:51 2022 +0200
1.3 @@ -0,0 +1,146 @@
1.4 +/*
1.5 + * Test removal operations.
1.6 + *
1.7 + * Copyright (C) 2020, 2021, 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 <l4/re/env.h>
1.26 +#include <l4/sys/err.h>
1.27 +
1.28 +#include <stdio.h>
1.29 +#include <string.h>
1.30 +#include <stdlib.h>
1.31 +
1.32 +#include <fsclient/client.h>
1.33 +#include <systypes/fcntl.h>
1.34 +
1.35 +
1.36 +
1.37 +static void read_from_file(file_t *file)
1.38 +{
1.39 + char buf[20];
1.40 + offset_t nread;
1.41 +
1.42 + client_seek(file, 0, SEEK_SET);
1.43 +
1.44 + printf("Reading...\n");
1.45 +
1.46 + nread = client_read(file, buf, 20);
1.47 +
1.48 + printf("Read %ld bytes...\n", nread);
1.49 + fwrite(buf, sizeof(char), nread, stdout);
1.50 + fputs("\n", stdout);
1.51 +}
1.52 +
1.53 +static void write_to_file(file_t *file, const char *buf)
1.54 +{
1.55 + offset_t nwritten;
1.56 +
1.57 + client_seek(file, 0, SEEK_SET);
1.58 +
1.59 + printf("Writing...\n");
1.60 +
1.61 + nwritten = client_write(file, buf, strlen(buf));
1.62 +
1.63 + printf("Wrote %ld bytes: ", nwritten);
1.64 + fwrite(buf, sizeof(char), nwritten, stdout);
1.65 + fputs("\n", stdout);
1.66 +}
1.67 +
1.68 +int main(int argc, char *argv[])
1.69 +{
1.70 + if (argc < 2)
1.71 + {
1.72 + printf("Need a file to remove.\n");
1.73 + return 1;
1.74 + }
1.75 +
1.76 + char *filename = argv[1];
1.77 +
1.78 + printf("Creating %s...\n", filename);
1.79 +
1.80 + file_t *file = client_open(filename, O_WRONLY | O_CREAT);
1.81 +
1.82 + write_to_file(file, "An existing file.");
1.83 +
1.84 + client_close(file);
1.85 +
1.86 + printf("Opening %s...\n", filename);
1.87 +
1.88 + file = client_open(filename, O_RDONLY);
1.89 +
1.90 + if (file == NULL)
1.91 + {
1.92 + printf("Could not open file: %s\n", filename);
1.93 + return 1;
1.94 + }
1.95 +
1.96 + /* Read from the file, then remove the file, then read again. */
1.97 +
1.98 + read_from_file(file);
1.99 +
1.100 + printf("Removing...\n");
1.101 +
1.102 + long err = client_remove(filename);
1.103 +
1.104 + if (err)
1.105 + {
1.106 + printf("Could not remove file: %s\n", filename);
1.107 + return 1;
1.108 + }
1.109 +
1.110 + printf("Reading again...\n");
1.111 +
1.112 + read_from_file(file);
1.113 +
1.114 + /* Open the file again. */
1.115 +
1.116 + file_t *file_new = client_open(filename, O_RDONLY);
1.117 +
1.118 + if (file_new != NULL)
1.119 + {
1.120 + printf("File should be absent: %s\n", filename);
1.121 + return 1;
1.122 + }
1.123 +
1.124 + file_new = client_open(filename, O_RDWR | O_CREAT);
1.125 +
1.126 + if (file_new == NULL)
1.127 + {
1.128 + printf("File should be present: %s\n", filename);
1.129 + return 1;
1.130 + }
1.131 +
1.132 + write_to_file(file_new, "New file!");
1.133 +
1.134 + /* Read from the initial file and new file. */
1.135 +
1.136 + read_from_file(file);
1.137 + read_from_file(file_new);
1.138 +
1.139 + /* Close the files. */
1.140 +
1.141 + client_close(file);
1.142 + client_close(file_new);
1.143 +
1.144 + printf("End of tests.\n");
1.145 +
1.146 + return 0;
1.147 +}
1.148 +
1.149 +// vim: tabstop=2 expandtab shiftwidth=2