paul@278 | 1 | /* |
paul@278 | 2 | * Obtain object statistics from a filesystem. |
paul@278 | 3 | * |
paul@278 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@278 | 5 | * |
paul@278 | 6 | * This program is free software; you can redistribute it and/or |
paul@278 | 7 | * modify it under the terms of the GNU General Public License as |
paul@278 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@278 | 9 | * the License, or (at your option) any later version. |
paul@278 | 10 | * |
paul@278 | 11 | * This program is distributed in the hope that it will be useful, |
paul@278 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@278 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@278 | 14 | * GNU General Public License for more details. |
paul@278 | 15 | * |
paul@278 | 16 | * You should have received a copy of the GNU General Public License |
paul@278 | 17 | * along with this program; if not, write to the Free Software |
paul@278 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@278 | 19 | * Boston, MA 02110-1301, USA |
paul@278 | 20 | */ |
paul@278 | 21 | |
paul@278 | 22 | #include <stdio.h> |
paul@278 | 23 | |
paul@278 | 24 | #include <sys/stat.h> |
paul@278 | 25 | #include <sys/sysmacros.h> /* major, minor */ |
paul@278 | 26 | #include <sys/types.h> |
paul@278 | 27 | |
paul@278 | 28 | #include <ext2fs/ext2fs.h> |
paul@278 | 29 | |
paul@278 | 30 | #include "image.h" |
paul@278 | 31 | |
paul@278 | 32 | |
paul@278 | 33 | |
paul@278 | 34 | /* Show statistics for files and directories. */ |
paul@278 | 35 | |
paul@278 | 36 | int stat_objects(ext2_filsys fs, int argc, char *argv[]) |
paul@278 | 37 | { |
paul@278 | 38 | int i; |
paul@278 | 39 | const char *path; |
paul@278 | 40 | ext2_ino_t ino; |
paul@278 | 41 | struct stat st; |
paul@278 | 42 | |
paul@278 | 43 | for (i = 0; i < argc; i++) |
paul@278 | 44 | { |
paul@278 | 45 | path = argv[i]; |
paul@278 | 46 | |
paul@278 | 47 | /* Detect missing objects. */ |
paul@278 | 48 | |
paul@278 | 49 | if (image_find_by_path(fs, path, &ino)) |
paul@278 | 50 | { |
paul@278 | 51 | fprintf(stderr, "Not found: %s\n", path); |
paul@278 | 52 | return 1; |
paul@278 | 53 | } |
paul@278 | 54 | |
paul@278 | 55 | /* Even though the statistics could be read directly out of ext2 data |
paul@278 | 56 | structures, it is convenient to use the standard stat structure. */ |
paul@278 | 57 | |
paul@278 | 58 | if (image_stat_inode(fs, ino, &st)) |
paul@278 | 59 | { |
paul@278 | 60 | fprintf(stderr, "Cannot stat object: %s\n", path); |
paul@278 | 61 | return 1; |
paul@278 | 62 | } |
paul@278 | 63 | |
paul@278 | 64 | /* Terse stat output: |
paul@278 | 65 | %n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C */ |
paul@278 | 66 | |
paul@278 | 67 | printf("%s %ld %ld %x %d %d ", |
paul@278 | 68 | path, st.st_size, st.st_blocks, st.st_mode, st.st_uid, st.st_gid); |
paul@278 | 69 | |
paul@278 | 70 | printf("%d %d %d %x %x ", |
paul@278 | 71 | st.st_dev, st.st_ino, st.st_nlink, |
paul@278 | 72 | major(st.st_rdev), minor(st.st_rdev)); |
paul@278 | 73 | |
paul@278 | 74 | printf("%d %d %d ", |
paul@278 | 75 | st.st_atim, st.st_mtim, st.st_ctim); |
paul@278 | 76 | |
paul@278 | 77 | /* NOTE: Arbitrary values: |
paul@278 | 78 | %W (creation time) given as 0 |
paul@278 | 79 | %o (I/O transfer size hint) given as 0 |
paul@278 | 80 | %C (SELinux security context) given as empty string */ |
paul@278 | 81 | |
paul@278 | 82 | printf("%d %d %s\n", |
paul@278 | 83 | 0, 0, ""); |
paul@278 | 84 | } |
paul@278 | 85 | |
paul@278 | 86 | return 0; |
paul@278 | 87 | } |
paul@278 | 88 | |
paul@278 | 89 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@278 | 90 | */ |