paul@236 | 1 | /* |
paul@236 | 2 | * Test renaming operations. |
paul@236 | 3 | * |
paul@236 | 4 | * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@236 | 5 | * |
paul@236 | 6 | * This program is free software; you can redistribute it and/or |
paul@236 | 7 | * modify it under the terms of the GNU General Public License as |
paul@236 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@236 | 9 | * the License, or (at your option) any later version. |
paul@236 | 10 | * |
paul@236 | 11 | * This program is distributed in the hope that it will be useful, |
paul@236 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@236 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@236 | 14 | * GNU General Public License for more details. |
paul@236 | 15 | * |
paul@236 | 16 | * You should have received a copy of the GNU General Public License |
paul@236 | 17 | * along with this program; if not, write to the Free Software |
paul@236 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@236 | 19 | * Boston, MA 02110-1301, USA |
paul@236 | 20 | */ |
paul@236 | 21 | |
paul@236 | 22 | #include <l4/re/env.h> |
paul@236 | 23 | #include <l4/sys/err.h> |
paul@236 | 24 | |
paul@236 | 25 | #include <stdio.h> |
paul@236 | 26 | #include <string.h> |
paul@236 | 27 | #include <stdlib.h> |
paul@236 | 28 | |
paul@236 | 29 | #include <fsclient/client.h> |
paul@236 | 30 | #include <systypes/fcntl.h> |
paul@236 | 31 | |
paul@236 | 32 | |
paul@236 | 33 | |
paul@271 | 34 | /* Show a directory's contents and determine if all expected files are |
paul@271 | 35 | present. */ |
paul@271 | 36 | |
paul@271 | 37 | static int show_directory(const char *filename, int expected) |
paul@236 | 38 | { |
paul@271 | 39 | file_t *reader = client_opendir(filename); |
paul@271 | 40 | |
paul@402 | 41 | if (!client_opened(reader)) |
paul@236 | 42 | { |
paul@402 | 43 | client_close(reader); |
paul@271 | 44 | printf("Could not read from directory.\n"); |
paul@236 | 45 | return 1; |
paul@236 | 46 | } |
paul@236 | 47 | |
paul@271 | 48 | printf("Reading...\n"); |
paul@271 | 49 | |
paul@271 | 50 | struct dirent *dirent; |
paul@271 | 51 | int renamed = 0; |
paul@271 | 52 | |
paul@271 | 53 | while ((dirent = client_readdir(reader)) != NULL) |
paul@271 | 54 | { |
paul@271 | 55 | if (!strncmp(dirent->d_name, "renamed-", 8)) |
paul@271 | 56 | renamed++; |
paul@271 | 57 | |
paul@271 | 58 | printf("> %s\n", dirent->d_name); |
paul@271 | 59 | free(dirent); |
paul@271 | 60 | } |
paul@271 | 61 | |
paul@271 | 62 | printf("Directory shown.\n"); |
paul@271 | 63 | |
paul@271 | 64 | printf("Renamed files: %d (%d)\n", renamed, expected); |
paul@271 | 65 | |
paul@271 | 66 | if (renamed != expected) |
paul@271 | 67 | return 1; |
paul@271 | 68 | |
paul@271 | 69 | return 0; |
paul@271 | 70 | } |
paul@271 | 71 | |
paul@271 | 72 | int main(int argc, char *argv[]) |
paul@271 | 73 | { |
paul@271 | 74 | if (argc < 3) |
paul@271 | 75 | { |
paul@271 | 76 | printf("Need a directory containing files and a new directory name.\n"); |
paul@271 | 77 | return 1; |
paul@271 | 78 | } |
paul@271 | 79 | |
paul@271 | 80 | char *filename = argv[1], *newdir = argv[2]; |
paul@236 | 81 | |
paul@236 | 82 | printf("Opening %s...\n", filename); |
paul@236 | 83 | |
paul@237 | 84 | file_t *reader = client_opendir(filename); |
paul@236 | 85 | |
paul@402 | 86 | if (!client_opened(reader)) |
paul@236 | 87 | { |
paul@402 | 88 | client_close(reader); |
paul@236 | 89 | printf("Could not read from directory.\n"); |
paul@236 | 90 | return 1; |
paul@236 | 91 | } |
paul@236 | 92 | |
paul@236 | 93 | printf("Reading...\n"); |
paul@236 | 94 | |
paul@236 | 95 | struct dirent *dirent; |
paul@236 | 96 | |
paul@236 | 97 | while ((dirent = client_readdir(reader)) != NULL) |
paul@236 | 98 | { |
paul@236 | 99 | printf("> %s\n", dirent->d_name); |
paul@236 | 100 | free(dirent); |
paul@236 | 101 | } |
paul@236 | 102 | |
paul@236 | 103 | printf("Directory shown.\n"); |
paul@236 | 104 | |
paul@236 | 105 | /* Rename some files. */ |
paul@236 | 106 | |
paul@236 | 107 | char source[strlen(filename) + strlen("/file-XXXX.txt") + 10]; |
paul@236 | 108 | char target[strlen(filename) + strlen("/file-XXXX.txt") + 10]; |
paul@260 | 109 | int filenum, to_rename = 100; |
paul@271 | 110 | long err; |
paul@236 | 111 | |
paul@260 | 112 | for (filenum = 1; filenum <= to_rename; filenum++) |
paul@236 | 113 | { |
paul@236 | 114 | sprintf(source, "%s/file-%d.txt", filename, filenum); |
paul@236 | 115 | sprintf(target, "%s/renamed-%d.txt", filename, filenum); |
paul@236 | 116 | |
paul@271 | 117 | err = client_rename(source, target); |
paul@236 | 118 | |
paul@236 | 119 | if (err) |
paul@236 | 120 | { |
paul@236 | 121 | printf("Could not rename file: %s\n", source); |
paul@236 | 122 | return 1; |
paul@236 | 123 | } |
paul@236 | 124 | } |
paul@236 | 125 | |
paul@236 | 126 | /* Show the new listing. */ |
paul@236 | 127 | |
paul@271 | 128 | if (show_directory(filename, to_rename)) |
paul@271 | 129 | return 1; |
paul@271 | 130 | |
paul@271 | 131 | /* Create a directory and move files into it. */ |
paul@271 | 132 | |
paul@271 | 133 | err = client_mkdir(newdir, 0755); |
paul@271 | 134 | |
paul@271 | 135 | if (err) |
paul@271 | 136 | { |
paul@271 | 137 | printf("Could not make directory: %s\n", newdir); |
paul@271 | 138 | return 1; |
paul@271 | 139 | } |
paul@271 | 140 | |
paul@237 | 141 | reader = client_opendir(filename); |
paul@236 | 142 | |
paul@402 | 143 | if (!client_opened(reader)) |
paul@236 | 144 | { |
paul@402 | 145 | client_close(reader); |
paul@236 | 146 | printf("Could not read from directory.\n"); |
paul@236 | 147 | return 1; |
paul@236 | 148 | } |
paul@236 | 149 | |
paul@271 | 150 | printf("Renaming...\n"); |
paul@260 | 151 | |
paul@236 | 152 | while ((dirent = client_readdir(reader)) != NULL) |
paul@236 | 153 | { |
paul@260 | 154 | if (!strncmp(dirent->d_name, "renamed-", 8)) |
paul@271 | 155 | { |
paul@271 | 156 | sprintf(source, "%s/%s", filename, dirent->d_name); |
paul@271 | 157 | sprintf(target, "%s/%s", newdir, dirent->d_name); |
paul@271 | 158 | |
paul@271 | 159 | err = client_rename(source, target); |
paul@260 | 160 | |
paul@271 | 161 | if (err) |
paul@271 | 162 | { |
paul@271 | 163 | printf("Could not rename file: %s\n", source); |
paul@271 | 164 | return 1; |
paul@271 | 165 | } |
paul@271 | 166 | } |
paul@271 | 167 | |
paul@236 | 168 | free(dirent); |
paul@236 | 169 | } |
paul@236 | 170 | |
paul@271 | 171 | /* Show the new directory's listing. */ |
paul@236 | 172 | |
paul@271 | 173 | if (show_directory(newdir, to_rename)) |
paul@271 | 174 | return 1; |
paul@271 | 175 | |
paul@402 | 176 | printf("End of test.\n"); |
paul@236 | 177 | return 0; |
paul@236 | 178 | } |
paul@236 | 179 | |
paul@236 | 180 | // vim: tabstop=2 expandtab shiftwidth=2 |