1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/dstest_file_rename.cc Thu Jan 27 23:45:17 2022 +0100
1.3 @@ -0,0 +1,161 @@
1.4 +/*
1.5 + * Test renaming 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 file_t *open_directory(char *filename, bool have_uid, sys_uid_t uid)
1.38 +{
1.39 + /* With a user, open a user-specific file opener. */
1.40 +
1.41 + if (have_uid)
1.42 + {
1.43 + l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022});
1.44 +
1.45 + if (l4_is_invalid_cap(opener))
1.46 + {
1.47 + printf("Could not obtain opener for file.\n");
1.48 + return NULL;
1.49 + }
1.50 +
1.51 + /* Invoke the open method to receive the file reference. */
1.52 +
1.53 + return client_opendir_using(filename, opener);
1.54 + }
1.55 + else
1.56 + {
1.57 + return client_opendir(filename);
1.58 + }
1.59 +}
1.60 +
1.61 +static long rename_file(char *source, char *target, bool have_uid, sys_uid_t uid)
1.62 +{
1.63 + /* With a user, open a user-specific file opener. */
1.64 +
1.65 + if (have_uid)
1.66 + {
1.67 + l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022});
1.68 +
1.69 + if (l4_is_invalid_cap(opener))
1.70 + {
1.71 + printf("Could not obtain opener for file.\n");
1.72 + return -L4_EIO;
1.73 + }
1.74 +
1.75 + /* Invoke the rename method to rename the file. */
1.76 +
1.77 + return client_rename_using(source, target, opener);
1.78 + }
1.79 + else
1.80 + {
1.81 + return client_rename(source, target);
1.82 + }
1.83 +}
1.84 +
1.85 +
1.86 +
1.87 +int main(int argc, char *argv[])
1.88 +{
1.89 + if (argc < 2)
1.90 + {
1.91 + printf("Need a directory name and an optional user identifier (if used with a filesystem).\n");
1.92 + return 1;
1.93 + }
1.94 +
1.95 + char *filename = argv[1];
1.96 + bool have_uid = (argc > 2) && strlen(argv[2]);
1.97 + sys_uid_t uid = have_uid ? atoi(argv[2]) : 0;
1.98 +
1.99 + printf("Opening %s...\n", filename);
1.100 +
1.101 + file_t *reader = open_directory(filename, have_uid, uid);
1.102 +
1.103 + if (reader == NULL)
1.104 + {
1.105 + printf("Could not read from directory.\n");
1.106 + return 1;
1.107 + }
1.108 +
1.109 + printf("Reading...\n");
1.110 +
1.111 + struct dirent *dirent;
1.112 +
1.113 + while ((dirent = client_readdir(reader)) != NULL)
1.114 + {
1.115 + printf("> %s\n", dirent->d_name);
1.116 + free(dirent);
1.117 + }
1.118 +
1.119 + printf("Directory shown.\n");
1.120 +
1.121 + /* Rename some files. */
1.122 +
1.123 + char source[strlen(filename) + strlen("/file-XXXX.txt") + 10];
1.124 + char target[strlen(filename) + strlen("/file-XXXX.txt") + 10];
1.125 + int filenum;
1.126 +
1.127 + for (filenum = 1; filenum < 100; filenum++)
1.128 + {
1.129 + sprintf(source, "%s/file-%d.txt", filename, filenum);
1.130 + sprintf(target, "%s/renamed-%d.txt", filename, filenum);
1.131 +
1.132 + long err = rename_file(source, target, have_uid, uid);
1.133 +
1.134 + if (err)
1.135 + {
1.136 + printf("Could not rename file: %s\n", source);
1.137 + return 1;
1.138 + }
1.139 + }
1.140 +
1.141 + /* Show the new listing. */
1.142 +
1.143 + reader = open_directory(filename, have_uid, uid);
1.144 +
1.145 + if (reader == NULL)
1.146 + {
1.147 + printf("Could not read from directory.\n");
1.148 + return 1;
1.149 + }
1.150 +
1.151 + printf("Reading...\n");
1.152 +
1.153 + while ((dirent = client_readdir(reader)) != NULL)
1.154 + {
1.155 + printf("> %s\n", dirent->d_name);
1.156 + free(dirent);
1.157 + }
1.158 +
1.159 + printf("Directory shown.\n");
1.160 +
1.161 + return 0;
1.162 +}
1.163 +
1.164 +// vim: tabstop=2 expandtab shiftwidth=2