paul@158 | 1 | /* |
paul@160 | 2 | * Test directory reading operations. |
paul@158 | 3 | * |
paul@237 | 4 | * Copyright (C) 2020, 2021, 2022 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 <stdio.h> |
paul@160 | 26 | #include <string.h> |
paul@160 | 27 | #include <stdlib.h> |
paul@266 | 28 | #include <sys/sysmacros.h> /* major, minor */ |
paul@160 | 29 | |
paul@160 | 30 | #include <fsclient/client.h> |
paul@160 | 31 | #include <systypes/fcntl.h> |
paul@463 | 32 | #include <systypes/format.h> |
paul@463 | 33 | |
paul@463 | 34 | #define FMT64d "%" pFMT64 "d" |
paul@464 | 35 | #define FMTnlinkd "%" pFMTnlink "d" |
paul@158 | 36 | |
paul@158 | 37 | |
paul@158 | 38 | |
paul@158 | 39 | int main(int argc, char *argv[]) |
paul@158 | 40 | { |
paul@402 | 41 | if (argc < 3) |
paul@158 | 42 | { |
paul@402 | 43 | printf("Need a directory name and an expected number of entries.\n"); |
paul@160 | 44 | return 1; |
paul@160 | 45 | } |
paul@160 | 46 | |
paul@160 | 47 | char *filename = argv[1]; |
paul@402 | 48 | int entries = atoi(argv[2]); |
paul@178 | 49 | |
paul@178 | 50 | printf("Opening %s...\n", filename); |
paul@178 | 51 | |
paul@237 | 52 | file_t *reader = client_opendir(filename); |
paul@202 | 53 | |
paul@402 | 54 | if (!client_opened(reader)) |
paul@202 | 55 | { |
paul@402 | 56 | client_close(reader); |
paul@202 | 57 | printf("Could not read from directory.\n"); |
paul@202 | 58 | return 1; |
paul@202 | 59 | } |
paul@202 | 60 | |
paul@178 | 61 | printf("Reading...\n"); |
paul@178 | 62 | |
paul@170 | 63 | struct dirent *dirent; |
paul@402 | 64 | int found = 0; |
paul@160 | 65 | |
paul@202 | 66 | while ((dirent = client_readdir(reader)) != NULL) |
paul@170 | 67 | { |
paul@266 | 68 | char path[strlen(filename) + 1 + strlen(dirent->d_name) + 1]; |
paul@266 | 69 | struct stat st; |
paul@266 | 70 | |
paul@170 | 71 | printf("> %s\n", dirent->d_name); |
paul@266 | 72 | |
paul@266 | 73 | /* Obtain file metadata. */ |
paul@266 | 74 | |
paul@266 | 75 | sprintf(path, "%s/%s", filename, dirent->d_name); |
paul@266 | 76 | |
paul@266 | 77 | if (client_stat(path, &st)) |
paul@266 | 78 | { |
paul@402 | 79 | client_close(reader); |
paul@266 | 80 | printf("Could not obtain metadata for file: %s\n", dirent->d_name); |
paul@266 | 81 | return 1; |
paul@266 | 82 | } |
paul@266 | 83 | |
paul@266 | 84 | /* NOTE: From e2access... */ |
paul@266 | 85 | |
paul@266 | 86 | /* Terse stat output: |
paul@266 | 87 | %n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C */ |
paul@266 | 88 | |
paul@266 | 89 | printf("%s %ld %ld %x %d %d ", |
paul@266 | 90 | path, st.st_size, st.st_blocks, st.st_mode, st.st_uid, st.st_gid); |
paul@266 | 91 | |
paul@464 | 92 | printf(FMT64d " %ld " FMTnlinkd " %x %x ", |
paul@266 | 93 | st.st_dev, st.st_ino, st.st_nlink, |
paul@266 | 94 | major(st.st_rdev), minor(st.st_rdev)); |
paul@266 | 95 | |
paul@269 | 96 | printf("%ld %ld %ld ", |
paul@269 | 97 | st.st_atim.tv_sec, st.st_mtim.tv_sec, st.st_ctim.tv_sec); |
paul@266 | 98 | |
paul@266 | 99 | /* NOTE: Arbitrary values: |
paul@266 | 100 | %W (creation time) given as 0 |
paul@266 | 101 | %o (I/O transfer size hint) given as 0 |
paul@266 | 102 | %C (SELinux security context) given as empty string */ |
paul@266 | 103 | |
paul@266 | 104 | printf("%d %d %s\n", |
paul@266 | 105 | 0, 0, ""); |
paul@266 | 106 | |
paul@170 | 107 | free(dirent); |
paul@402 | 108 | found++; |
paul@160 | 109 | } |
paul@158 | 110 | |
paul@158 | 111 | printf("Directory shown.\n"); |
paul@402 | 112 | printf("Found expected number of entries: %s\n", found == entries ? "True" : "False"); |
paul@158 | 113 | |
paul@170 | 114 | /* Open again, reading a single entry only. */ |
paul@170 | 115 | |
paul@237 | 116 | reader = client_opendir(filename); |
paul@202 | 117 | |
paul@402 | 118 | if (!client_opened(reader)) |
paul@202 | 119 | { |
paul@402 | 120 | client_close(reader); |
paul@202 | 121 | printf("Could not read from directory.\n"); |
paul@202 | 122 | return 1; |
paul@202 | 123 | } |
paul@202 | 124 | |
paul@202 | 125 | dirent = client_readdir(reader); |
paul@170 | 126 | |
paul@170 | 127 | if (dirent != NULL) |
paul@170 | 128 | { |
paul@170 | 129 | printf("> %s\n", dirent->d_name); |
paul@170 | 130 | free(dirent); |
paul@170 | 131 | } |
paul@170 | 132 | |
paul@170 | 133 | printf("Entry shown.\n"); |
paul@170 | 134 | |
paul@402 | 135 | printf("End of test.\n"); |
paul@158 | 136 | return 0; |
paul@158 | 137 | } |
paul@158 | 138 | |
paul@158 | 139 | // vim: tabstop=2 expandtab shiftwidth=2 |