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@236 | 34 | int main(int argc, char *argv[]) |
paul@236 | 35 | { |
paul@236 | 36 | if (argc < 2) |
paul@236 | 37 | { |
paul@237 | 38 | printf("Need a directory name.\n"); |
paul@236 | 39 | return 1; |
paul@236 | 40 | } |
paul@236 | 41 | |
paul@236 | 42 | char *filename = argv[1]; |
paul@236 | 43 | |
paul@236 | 44 | printf("Opening %s...\n", filename); |
paul@236 | 45 | |
paul@237 | 46 | file_t *reader = client_opendir(filename); |
paul@236 | 47 | |
paul@236 | 48 | if (reader == NULL) |
paul@236 | 49 | { |
paul@236 | 50 | printf("Could not read from directory.\n"); |
paul@236 | 51 | return 1; |
paul@236 | 52 | } |
paul@236 | 53 | |
paul@236 | 54 | printf("Reading...\n"); |
paul@236 | 55 | |
paul@236 | 56 | struct dirent *dirent; |
paul@236 | 57 | |
paul@236 | 58 | while ((dirent = client_readdir(reader)) != NULL) |
paul@236 | 59 | { |
paul@236 | 60 | printf("> %s\n", dirent->d_name); |
paul@236 | 61 | free(dirent); |
paul@236 | 62 | } |
paul@236 | 63 | |
paul@236 | 64 | printf("Directory shown.\n"); |
paul@236 | 65 | |
paul@236 | 66 | /* Rename some files. */ |
paul@236 | 67 | |
paul@236 | 68 | char source[strlen(filename) + strlen("/file-XXXX.txt") + 10]; |
paul@236 | 69 | char target[strlen(filename) + strlen("/file-XXXX.txt") + 10]; |
paul@260 | 70 | int filenum, to_rename = 100; |
paul@236 | 71 | |
paul@260 | 72 | for (filenum = 1; filenum <= to_rename; filenum++) |
paul@236 | 73 | { |
paul@236 | 74 | sprintf(source, "%s/file-%d.txt", filename, filenum); |
paul@236 | 75 | sprintf(target, "%s/renamed-%d.txt", filename, filenum); |
paul@236 | 76 | |
paul@237 | 77 | long err = client_rename(source, target); |
paul@236 | 78 | |
paul@236 | 79 | if (err) |
paul@236 | 80 | { |
paul@236 | 81 | printf("Could not rename file: %s\n", source); |
paul@236 | 82 | return 1; |
paul@236 | 83 | } |
paul@236 | 84 | } |
paul@236 | 85 | |
paul@236 | 86 | /* Show the new listing. */ |
paul@236 | 87 | |
paul@237 | 88 | reader = client_opendir(filename); |
paul@236 | 89 | |
paul@236 | 90 | if (reader == NULL) |
paul@236 | 91 | { |
paul@236 | 92 | printf("Could not read from directory.\n"); |
paul@236 | 93 | return 1; |
paul@236 | 94 | } |
paul@236 | 95 | |
paul@236 | 96 | printf("Reading...\n"); |
paul@236 | 97 | |
paul@260 | 98 | int renamed = 0; |
paul@260 | 99 | |
paul@236 | 100 | while ((dirent = client_readdir(reader)) != NULL) |
paul@236 | 101 | { |
paul@260 | 102 | if (!strncmp(dirent->d_name, "renamed-", 8)) |
paul@260 | 103 | renamed++; |
paul@260 | 104 | |
paul@236 | 105 | printf("> %s\n", dirent->d_name); |
paul@236 | 106 | free(dirent); |
paul@236 | 107 | } |
paul@236 | 108 | |
paul@236 | 109 | printf("Directory shown.\n"); |
paul@236 | 110 | |
paul@260 | 111 | printf("Renamed files: %d (%d)\n", renamed, to_rename); |
paul@260 | 112 | |
paul@236 | 113 | return 0; |
paul@236 | 114 | } |
paul@236 | 115 | |
paul@236 | 116 | // vim: tabstop=2 expandtab shiftwidth=2 |