# HG changeset patch # User Paul Boddie # Date 1656340251 -7200 # Node ID 3d935a7f97857ac9b8c6b7a70012235eb17227c1 # Parent a6752a91fd633883254a7394affe9c585fb40a9c Added a test of file removal semantics. diff -r a6752a91fd63 -r 3d935a7f9785 conf/dstest_file_remove.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/conf/dstest_file_remove.cfg Mon Jun 27 16:30:51 2022 +0200 @@ -0,0 +1,51 @@ +-- vim:set ft=lua: + +local L4 = require("L4"); + +local l = L4.default_loader; + +local pipe_server = l:new_channel(); + +l:startv({ + caps = { + server = pipe_server:svr(), + }, + log = { "pipes", "r" }, + }, + "rom/dstest_pipe_server", "10"); + +local block_server = l:new_channel(); + +l:startv({ + caps = { + server = block_server:svr(), + }, + log = { "blocksvr", "r" }, + }, + "rom/dstest_block_server", "10"); + +local ext2svr = l:new_channel(); + +l:startv({ + caps = { + blocksvr = block_server, + pipes = pipe_server, + ext2svr = ext2svr:svr(), + }, + log = { "ext2svr", "y" }, + }, + "rom/dstest_ext2_server", "blocksvr", "rom/e2test.fs", "10", "ext2svr"); + +-- Obtain user filesystems with umask 0022 (18). + +local open_for_user = 6; +local ext2svr_paulb = L4.cast(L4.Proto.Factory, ext2svr):create(open_for_user, 1000, 1000, 18); + +l:startv({ + caps = { + server = ext2svr_paulb, + }, + log = { "client", "g" }, + }, + -- program, file to create and remove + "rom/dstest_file_remove", "home/paulb/to_remove"); diff -r a6752a91fd63 -r 3d935a7f9785 conf/dstest_file_remove.list --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/conf/dstest_file_remove.list Mon Jun 27 16:30:51 2022 +0200 @@ -0,0 +1,28 @@ +entry dstest_file_remove +roottask moe rom/dstest_file_remove.cfg +module dstest_file_remove.cfg +module e2test.fs +module l4re +module ned +module dstest_file_remove +module dstest_ext2_server +module dstest_block_server +module dstest_pipe_server +module lib4re-c.so +module lib4re-c-util.so +module lib4re.so +module lib4re-util.so +module libc_be_l4refile.so +module libc_be_l4re.so +module libc_be_socket_noop.so +module libc_support_misc.so +module libdl.so +module libipc.so +module libl4sys-direct.so +module libl4sys.so +module libl4util.so +module libld-l4.so +module libpthread.so +module libstdc++.so +module libsupc++.so +module libuc_c.so diff -r a6752a91fd63 -r 3d935a7f9785 tests/Makefile --- a/tests/Makefile Mon Jun 27 16:30:36 2022 +0200 +++ b/tests/Makefile Mon Jun 27 16:30:51 2022 +0200 @@ -9,6 +9,7 @@ dstest_file_monitor \ dstest_file_readdir \ dstest_file_readdir_concurrent \ + dstest_file_remove \ dstest_file_rename \ dstest_host_client \ dstest_pipe_client \ @@ -65,6 +66,8 @@ SRC_CC_dstest_file_readdir_concurrent = dstest_file_readdir_concurrent.cc +SRC_CC_dstest_file_remove = dstest_file_remove.cc + SRC_CC_dstest_file_rename = dstest_file_rename.cc SRC_CC_dstest_host_client = dstest_host_client.cc diff -r a6752a91fd63 -r 3d935a7f9785 tests/dstest_file_remove.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/dstest_file_remove.cc Mon Jun 27 16:30:51 2022 +0200 @@ -0,0 +1,146 @@ +/* + * Test removal operations. + * + * Copyright (C) 2020, 2021, 2022 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include + +#include +#include +#include + +#include +#include + + + +static void read_from_file(file_t *file) +{ + char buf[20]; + offset_t nread; + + client_seek(file, 0, SEEK_SET); + + printf("Reading...\n"); + + nread = client_read(file, buf, 20); + + printf("Read %ld bytes...\n", nread); + fwrite(buf, sizeof(char), nread, stdout); + fputs("\n", stdout); +} + +static void write_to_file(file_t *file, const char *buf) +{ + offset_t nwritten; + + client_seek(file, 0, SEEK_SET); + + printf("Writing...\n"); + + nwritten = client_write(file, buf, strlen(buf)); + + printf("Wrote %ld bytes: ", nwritten); + fwrite(buf, sizeof(char), nwritten, stdout); + fputs("\n", stdout); +} + +int main(int argc, char *argv[]) +{ + if (argc < 2) + { + printf("Need a file to remove.\n"); + return 1; + } + + char *filename = argv[1]; + + printf("Creating %s...\n", filename); + + file_t *file = client_open(filename, O_WRONLY | O_CREAT); + + write_to_file(file, "An existing file."); + + client_close(file); + + printf("Opening %s...\n", filename); + + file = client_open(filename, O_RDONLY); + + if (file == NULL) + { + printf("Could not open file: %s\n", filename); + return 1; + } + + /* Read from the file, then remove the file, then read again. */ + + read_from_file(file); + + printf("Removing...\n"); + + long err = client_remove(filename); + + if (err) + { + printf("Could not remove file: %s\n", filename); + return 1; + } + + printf("Reading again...\n"); + + read_from_file(file); + + /* Open the file again. */ + + file_t *file_new = client_open(filename, O_RDONLY); + + if (file_new != NULL) + { + printf("File should be absent: %s\n", filename); + return 1; + } + + file_new = client_open(filename, O_RDWR | O_CREAT); + + if (file_new == NULL) + { + printf("File should be present: %s\n", filename); + return 1; + } + + write_to_file(file_new, "New file!"); + + /* Read from the initial file and new file. */ + + read_from_file(file); + read_from_file(file_new); + + /* Close the files. */ + + client_close(file); + client_close(file_new); + + printf("End of tests.\n"); + + return 0; +} + +// vim: tabstop=2 expandtab shiftwidth=2