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 | /* Register the reader for notification. */ |
paul@170 | 113 | |
paul@170 | 114 | return client_set_blocking(file, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED); |
paul@170 | 115 | } |
paul@170 | 116 | |
paul@170 | 117 | |
paul@170 | 118 | |
paul@158 | 119 | int main(int argc, char *argv[]) |
paul@158 | 120 | { |
paul@158 | 121 | if (argc < 2) |
paul@158 | 122 | { |
paul@160 | 123 | printf("Need a directory name and an optional user identifier (if used with a filesystem).\n"); |
paul@160 | 124 | return 1; |
paul@160 | 125 | } |
paul@160 | 126 | |
paul@160 | 127 | char *filename = argv[1]; |
paul@160 | 128 | bool have_uid = (argc > 2) && strlen(argv[2]); |
paul@160 | 129 | sys_uid_t uid = have_uid ? atoi(argv[2]) : 0; |
paul@170 | 130 | file_t *file = open_file(filename, have_uid, uid); |
paul@160 | 131 | |
paul@160 | 132 | if (file == NULL) |
paul@160 | 133 | { |
paul@160 | 134 | printf("Could not obtain directory.\n"); |
paul@158 | 135 | return 1; |
paul@158 | 136 | } |
paul@158 | 137 | |
paul@170 | 138 | long err = open_directory(file); |
paul@158 | 139 | |
paul@160 | 140 | if (err) |
paul@158 | 141 | { |
paul@160 | 142 | printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); |
paul@158 | 143 | return 1; |
paul@158 | 144 | } |
paul@158 | 145 | |
paul@170 | 146 | struct dirent *dirent; |
paul@160 | 147 | |
paul@170 | 148 | while ((dirent = read_directory_entry(file)) != NULL) |
paul@170 | 149 | { |
paul@170 | 150 | printf("> %s\n", dirent->d_name); |
paul@170 | 151 | free(dirent); |
paul@160 | 152 | } |
paul@158 | 153 | |
paul@158 | 154 | printf("Directory shown.\n"); |
paul@158 | 155 | |
paul@170 | 156 | /* Open again, reading a single entry only. */ |
paul@170 | 157 | |
paul@170 | 158 | file = open_file(filename, have_uid, uid); |
paul@170 | 159 | |
paul@170 | 160 | if (file == NULL) |
paul@170 | 161 | { |
paul@170 | 162 | printf("Could not obtain directory.\n"); |
paul@170 | 163 | return 1; |
paul@170 | 164 | } |
paul@170 | 165 | |
paul@170 | 166 | err = open_directory(file); |
paul@170 | 167 | |
paul@170 | 168 | if (err) |
paul@170 | 169 | { |
paul@170 | 170 | printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); |
paul@170 | 171 | return 1; |
paul@170 | 172 | } |
paul@170 | 173 | |
paul@170 | 174 | dirent = read_directory_entry(file); |
paul@170 | 175 | |
paul@170 | 176 | if (dirent != NULL) |
paul@170 | 177 | { |
paul@170 | 178 | printf("> %s\n", dirent->d_name); |
paul@170 | 179 | free(dirent); |
paul@170 | 180 | } |
paul@170 | 181 | |
paul@170 | 182 | printf("Entry shown.\n"); |
paul@170 | 183 | |
paul@158 | 184 | return 0; |
paul@158 | 185 | } |
paul@158 | 186 | |
paul@158 | 187 | // vim: tabstop=2 expandtab shiftwidth=2 |