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