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 (reader == NULL) 42 { 43 printf("Could not read from directory.\n"); 44 return 1; 45 } 46 47 printf("Reading...\n"); 48 49 struct dirent *dirent; 50 int renamed = 0; 51 52 while ((dirent = client_readdir(reader)) != NULL) 53 { 54 if (!strncmp(dirent->d_name, "renamed-", 8)) 55 renamed++; 56 57 printf("> %s\n", dirent->d_name); 58 free(dirent); 59 } 60 61 printf("Directory shown.\n"); 62 63 printf("Renamed files: %d (%d)\n", renamed, expected); 64 65 if (renamed != expected) 66 return 1; 67 68 return 0; 69 } 70 71 int main(int argc, char *argv[]) 72 { 73 if (argc < 3) 74 { 75 printf("Need a directory containing files and a new directory name.\n"); 76 return 1; 77 } 78 79 char *filename = argv[1], *newdir = argv[2]; 80 81 printf("Opening %s...\n", filename); 82 83 file_t *reader = client_opendir(filename); 84 85 if (reader == NULL) 86 { 87 printf("Could not read from directory.\n"); 88 return 1; 89 } 90 91 printf("Reading...\n"); 92 93 struct dirent *dirent; 94 95 while ((dirent = client_readdir(reader)) != NULL) 96 { 97 printf("> %s\n", dirent->d_name); 98 free(dirent); 99 } 100 101 printf("Directory shown.\n"); 102 103 /* Rename some files. */ 104 105 char source[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 106 char target[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 107 int filenum, to_rename = 100; 108 long err; 109 110 for (filenum = 1; filenum <= to_rename; filenum++) 111 { 112 sprintf(source, "%s/file-%d.txt", filename, filenum); 113 sprintf(target, "%s/renamed-%d.txt", filename, filenum); 114 115 err = client_rename(source, target); 116 117 if (err) 118 { 119 printf("Could not rename file: %s\n", source); 120 return 1; 121 } 122 } 123 124 /* Show the new listing. */ 125 126 if (show_directory(filename, to_rename)) 127 return 1; 128 129 /* Create a directory and move files into it. */ 130 131 err = client_mkdir(newdir, 0755); 132 133 if (err) 134 { 135 printf("Could not make directory: %s\n", newdir); 136 return 1; 137 } 138 139 reader = client_opendir(filename); 140 141 if (reader == NULL) 142 { 143 printf("Could not read from directory.\n"); 144 return 1; 145 } 146 147 printf("Renaming...\n"); 148 149 while ((dirent = client_readdir(reader)) != NULL) 150 { 151 if (!strncmp(dirent->d_name, "renamed-", 8)) 152 { 153 sprintf(source, "%s/%s", filename, dirent->d_name); 154 sprintf(target, "%s/%s", newdir, dirent->d_name); 155 156 err = client_rename(source, target); 157 158 if (err) 159 { 160 printf("Could not rename file: %s\n", source); 161 return 1; 162 } 163 } 164 165 free(dirent); 166 } 167 168 /* Show the new directory's listing. */ 169 170 if (show_directory(newdir, to_rename)) 171 return 1; 172 173 printf("End of tests.\n"); 174 175 return 0; 176 } 177 178 // vim: tabstop=2 expandtab shiftwidth=2