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