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 static file_t *open_directory(char *filename, bool have_uid, sys_uid_t uid) 35 { 36 /* With a user, open a user-specific file opener. */ 37 38 if (have_uid) 39 { 40 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 41 42 if (l4_is_invalid_cap(opener)) 43 { 44 printf("Could not obtain opener for file.\n"); 45 return NULL; 46 } 47 48 /* Invoke the open method to receive the file reference. */ 49 50 return client_opendir_using(filename, opener); 51 } 52 else 53 { 54 return client_opendir(filename); 55 } 56 } 57 58 static long rename_file(char *source, char *target, bool have_uid, sys_uid_t uid) 59 { 60 /* With a user, open a user-specific file opener. */ 61 62 if (have_uid) 63 { 64 l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); 65 66 if (l4_is_invalid_cap(opener)) 67 { 68 printf("Could not obtain opener for file.\n"); 69 return -L4_EIO; 70 } 71 72 /* Invoke the rename method to rename the file. */ 73 74 return client_rename_using(source, target, opener); 75 } 76 else 77 { 78 return client_rename(source, target); 79 } 80 } 81 82 83 84 int main(int argc, char *argv[]) 85 { 86 if (argc < 2) 87 { 88 printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); 89 return 1; 90 } 91 92 char *filename = argv[1]; 93 bool have_uid = (argc > 2) && strlen(argv[2]); 94 sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; 95 96 printf("Opening %s...\n", filename); 97 98 file_t *reader = open_directory(filename, have_uid, uid); 99 100 if (reader == NULL) 101 { 102 printf("Could not read from directory.\n"); 103 return 1; 104 } 105 106 printf("Reading...\n"); 107 108 struct dirent *dirent; 109 110 while ((dirent = client_readdir(reader)) != NULL) 111 { 112 printf("> %s\n", dirent->d_name); 113 free(dirent); 114 } 115 116 printf("Directory shown.\n"); 117 118 /* Rename some files. */ 119 120 char source[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 121 char target[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 122 int filenum; 123 124 for (filenum = 1; filenum < 100; filenum++) 125 { 126 sprintf(source, "%s/file-%d.txt", filename, filenum); 127 sprintf(target, "%s/renamed-%d.txt", filename, filenum); 128 129 long err = rename_file(source, target, have_uid, uid); 130 131 if (err) 132 { 133 printf("Could not rename file: %s\n", source); 134 return 1; 135 } 136 } 137 138 /* Show the new listing. */ 139 140 reader = open_directory(filename, have_uid, uid); 141 142 if (reader == NULL) 143 { 144 printf("Could not read from directory.\n"); 145 return 1; 146 } 147 148 printf("Reading...\n"); 149 150 while ((dirent = client_readdir(reader)) != NULL) 151 { 152 printf("> %s\n", dirent->d_name); 153 free(dirent); 154 } 155 156 printf("Directory shown.\n"); 157 158 return 0; 159 } 160 161 // vim: tabstop=2 expandtab shiftwidth=2