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