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