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