paul@158 | 1 | /* |
paul@160 | 2 | * Test directory reading operations. |
paul@158 | 3 | * |
paul@160 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@158 | 5 | * |
paul@158 | 6 | * This program is free software; you can redistribute it and/or |
paul@158 | 7 | * modify it under the terms of the GNU General Public License as |
paul@158 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@158 | 9 | * the License, or (at your option) any later version. |
paul@158 | 10 | * |
paul@158 | 11 | * This program is distributed in the hope that it will be useful, |
paul@158 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@158 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@158 | 14 | * GNU General Public License for more details. |
paul@158 | 15 | * |
paul@158 | 16 | * You should have received a copy of the GNU General Public License |
paul@158 | 17 | * along with this program; if not, write to the Free Software |
paul@158 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@158 | 19 | * Boston, MA 02110-1301, USA |
paul@158 | 20 | */ |
paul@158 | 21 | |
paul@160 | 22 | #include <l4/re/env.h> |
paul@160 | 23 | #include <l4/sys/err.h> |
paul@160 | 24 | |
paul@158 | 25 | #include <dirent.h> |
paul@158 | 26 | #include <stdio.h> |
paul@160 | 27 | #include <string.h> |
paul@160 | 28 | #include <stdlib.h> |
paul@160 | 29 | |
paul@160 | 30 | #include <fsclient/client.h> |
paul@160 | 31 | #include <systypes/fcntl.h> |
paul@158 | 32 | |
paul@158 | 33 | |
paul@158 | 34 | |
paul@160 | 35 | #define DIRENT_CORE_SIZE (sizeof(struct dirent) - sizeof(((struct dirent *) 0)->d_name)) |
paul@160 | 36 | |
paul@170 | 37 | |
paul@170 | 38 | |
paul@170 | 39 | /* Return a directory entry. This must be freed by the caller after use. */ |
paul@170 | 40 | |
paul@170 | 41 | static struct dirent *read_directory_entry(file_t *file) |
paul@170 | 42 | { |
paul@170 | 43 | char buffer[DIRENT_CORE_SIZE]; |
paul@170 | 44 | offset_t nread = client_read(file, buffer, DIRENT_CORE_SIZE); |
paul@170 | 45 | |
paul@170 | 46 | /* Stop if no new structure can be successfully read. */ |
paul@170 | 47 | |
paul@170 | 48 | if (nread != DIRENT_CORE_SIZE) |
paul@170 | 49 | return NULL; |
paul@170 | 50 | |
paul@170 | 51 | struct dirent *dirent = (struct dirent *) buffer; |
paul@170 | 52 | offset_t remaining = dirent->d_reclen - DIRENT_CORE_SIZE; |
paul@170 | 53 | |
paul@170 | 54 | /* Allocate a buffer for the complete structure. */ |
paul@170 | 55 | |
paul@170 | 56 | char *entry = (char *) calloc(DIRENT_CORE_SIZE + remaining, sizeof(char)); |
paul@170 | 57 | |
paul@170 | 58 | if (entry == NULL) |
paul@170 | 59 | return NULL; |
paul@170 | 60 | |
paul@170 | 61 | /* Copy the start of the entry into a new buffer. */ |
paul@170 | 62 | |
paul@170 | 63 | memcpy(entry, buffer, DIRENT_CORE_SIZE); |
paul@170 | 64 | |
paul@170 | 65 | /* Append to the entry buffer. */ |
paul@170 | 66 | |
paul@170 | 67 | char *current = entry + DIRENT_CORE_SIZE; |
paul@170 | 68 | |
paul@170 | 69 | nread = client_read(file, current, remaining); |
paul@170 | 70 | |
paul@170 | 71 | /* Stop if no complete structure can be successfully read. */ |
paul@170 | 72 | |
paul@170 | 73 | if (nread != remaining) |
paul@170 | 74 | { |
paul@170 | 75 | free(entry); |
paul@170 | 76 | return NULL; |
paul@170 | 77 | } |
paul@170 | 78 | |
paul@170 | 79 | return (struct dirent *) entry; |
paul@170 | 80 | } |
paul@170 | 81 | |
paul@170 | 82 | |
paul@170 | 83 | |
paul@170 | 84 | static file_t *open_file(char *filename, bool have_uid, sys_uid_t uid) |
paul@170 | 85 | { |
paul@170 | 86 | /* With a user, open a user-specific file opener. */ |
paul@170 | 87 | |
paul@170 | 88 | if (have_uid) |
paul@170 | 89 | { |
paul@170 | 90 | l4_cap_idx_t opener = client_open_for_user((user_t) {uid, uid, 0022}); |
paul@170 | 91 | |
paul@170 | 92 | if (l4_is_invalid_cap(opener)) |
paul@170 | 93 | { |
paul@170 | 94 | printf("Could not obtain opener for file.\n"); |
paul@170 | 95 | return NULL; |
paul@170 | 96 | } |
paul@170 | 97 | |
paul@170 | 98 | /* Invoke the open method to receive the file reference. */ |
paul@170 | 99 | |
paul@170 | 100 | return client_open_using(filename, O_DIRECTORY, opener); |
paul@170 | 101 | } |
paul@170 | 102 | else |
paul@170 | 103 | { |
paul@170 | 104 | return client_open(filename, O_DIRECTORY); |
paul@170 | 105 | } |
paul@170 | 106 | } |
paul@170 | 107 | |
paul@170 | 108 | |
paul@170 | 109 | |
paul@170 | 110 | static long open_directory(file_t *file) |
paul@170 | 111 | { |
paul@170 | 112 | // NOTE: To be replaced by a proper mechanism identifying the nature of each |
paul@170 | 113 | // NOTE: obtained object. |
paul@170 | 114 | |
paul@170 | 115 | file->can_mmap = 0; |
paul@170 | 116 | file->has_size = 0; |
paul@170 | 117 | |
paul@170 | 118 | /* Register the reader for notification. */ |
paul@170 | 119 | |
paul@170 | 120 | return client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@170 | 121 | } |
paul@170 | 122 | |
paul@170 | 123 | |
paul@170 | 124 | |
paul@158 | 125 | int main(int argc, char *argv[]) |
paul@158 | 126 | { |
paul@158 | 127 | if (argc < 2) |
paul@158 | 128 | { |
paul@160 | 129 | printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); |
paul@160 | 130 | return 1; |
paul@160 | 131 | } |
paul@160 | 132 | |
paul@160 | 133 | char *filename = argv[1]; |
paul@160 | 134 | bool have_uid = (argc > 2) && strlen(argv[2]); |
paul@160 | 135 | sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; |
paul@170 | 136 | file_t *file = open_file(filename, have_uid, uid); |
paul@160 | 137 | |
paul@160 | 138 | if (file == NULL) |
paul@160 | 139 | { |
paul@160 | 140 | printf("Could not obtain directory.\n"); |
paul@158 | 141 | return 1; |
paul@158 | 142 | } |
paul@158 | 143 | |
paul@170 | 144 | long err = open_directory(file); |
paul@158 | 145 | |
paul@160 | 146 | if (err) |
paul@158 | 147 | { |
paul@160 | 148 | printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); |
paul@158 | 149 | return 1; |
paul@158 | 150 | } |
paul@158 | 151 | |
paul@170 | 152 | struct dirent *dirent; |
paul@160 | 153 | |
paul@170 | 154 | while ((dirent = read_directory_entry(file)) != NULL) |
paul@170 | 155 | { |
paul@170 | 156 | printf("> %s\n", dirent->d_name); |
paul@170 | 157 | free(dirent); |
paul@160 | 158 | } |
paul@158 | 159 | |
paul@158 | 160 | printf("Directory shown.\n"); |
paul@158 | 161 | |
paul@170 | 162 | /* Open again, reading a single entry only. */ |
paul@170 | 163 | |
paul@170 | 164 | file = open_file(filename, have_uid, uid); |
paul@170 | 165 | |
paul@170 | 166 | if (file == NULL) |
paul@170 | 167 | { |
paul@170 | 168 | printf("Could not obtain directory.\n"); |
paul@170 | 169 | return 1; |
paul@170 | 170 | } |
paul@170 | 171 | |
paul@170 | 172 | err = open_directory(file); |
paul@170 | 173 | |
paul@170 | 174 | if (err) |
paul@170 | 175 | { |
paul@170 | 176 | printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); |
paul@170 | 177 | return 1; |
paul@170 | 178 | } |
paul@170 | 179 | |
paul@170 | 180 | dirent = read_directory_entry(file); |
paul@170 | 181 | |
paul@170 | 182 | if (dirent != NULL) |
paul@170 | 183 | { |
paul@170 | 184 | printf("> %s\n", dirent->d_name); |
paul@170 | 185 | free(dirent); |
paul@170 | 186 | } |
paul@170 | 187 | |
paul@170 | 188 | printf("Entry shown.\n"); |
paul@170 | 189 | |
paul@158 | 190 | return 0; |
paul@158 | 191 | } |
paul@158 | 192 | |
paul@158 | 193 | // vim: tabstop=2 expandtab shiftwidth=2 |