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