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