1 /* 2 * Test directory reading operations. 3 * 4 * Copyright (C) 2020, 2021, 2022, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <stdio.h> 26 #include <string.h> 27 #include <stdlib.h> 28 #include <sys/sysmacros.h> /* major, minor */ 29 30 #include <fsclient/client.h> 31 #include <systypes/fcntl.h> 32 #include <systypes/format.h> 33 34 #define FMT64d "%" pFMT64 "d" 35 #define FMTnlinkd "%" pFMTnlink "d" 36 37 38 39 int main(int argc, char *argv[]) 40 { 41 if (argc < 3) 42 { 43 printf("Need a directory name and an expected number of entries.\n"); 44 return 1; 45 } 46 47 char *filename = argv[1]; 48 int entries = atoi(argv[2]); 49 50 printf("Opening %s...\n", filename); 51 52 file_t *reader = client_opendir(filename); 53 54 if (!client_opened(reader)) 55 { 56 client_close(reader); 57 printf("Could not read from directory.\n"); 58 return 1; 59 } 60 61 printf("Reading...\n"); 62 63 struct dirent *dirent; 64 int found = 0; 65 66 while ((dirent = client_readdir(reader)) != NULL) 67 { 68 char path[strlen(filename) + 1 + strlen(dirent->d_name) + 1]; 69 struct stat st; 70 71 printf("> %s\n", dirent->d_name); 72 73 /* Obtain file metadata. */ 74 75 sprintf(path, "%s/%s", filename, dirent->d_name); 76 77 if (client_stat(path, &st)) 78 { 79 client_close(reader); 80 printf("Could not obtain metadata for file: %s\n", dirent->d_name); 81 return 1; 82 } 83 84 /* NOTE: From e2access... */ 85 86 /* Terse stat output: 87 %n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C */ 88 89 printf("%s %ld %ld %x %d %d ", 90 path, st.st_size, st.st_blocks, st.st_mode, st.st_uid, st.st_gid); 91 92 printf(FMT64d " %ld " FMTnlinkd " %x %x ", 93 st.st_dev, st.st_ino, st.st_nlink, 94 major(st.st_rdev), minor(st.st_rdev)); 95 96 printf("%ld %ld %ld ", 97 st.st_atim.tv_sec, st.st_mtim.tv_sec, st.st_ctim.tv_sec); 98 99 /* NOTE: Arbitrary values: 100 %W (creation time) given as 0 101 %o (I/O transfer size hint) given as 0 102 %C (SELinux security context) given as empty string */ 103 104 printf("%d %d %s\n", 105 0, 0, ""); 106 107 free(dirent); 108 found++; 109 } 110 111 printf("Directory shown.\n"); 112 printf("Found expected number of entries: %s\n", found == entries ? "True" : "False"); 113 114 /* Open again, reading a single entry only. */ 115 116 reader = client_opendir(filename); 117 118 if (!client_opened(reader)) 119 { 120 client_close(reader); 121 printf("Could not read from directory.\n"); 122 return 1; 123 } 124 125 char *name = NULL; 126 127 dirent = client_readdir(reader); 128 129 if (dirent != NULL) 130 { 131 name = strdup(dirent->d_name); 132 printf("> %s\n", dirent->d_name); 133 free(dirent); 134 } 135 136 printf("Entry shown.\n"); 137 138 /* Rewind to show the same entry. */ 139 140 char *name2 = NULL; 141 142 client_rewinddir(reader); 143 144 dirent = client_readdir(reader); 145 146 if (dirent != NULL) 147 { 148 name2 = strdup(dirent->d_name); 149 printf("> %s\n", dirent->d_name); 150 free(dirent); 151 } 152 153 printf("Entry shown and identical: %s\n", (name != NULL) && (name2 != NULL) && !strcmp(name, name2) ? "True" : "False"); 154 155 printf("End of test.\n"); 156 return 0; 157 } 158 159 // vim: tabstop=2 expandtab shiftwidth=2