paul@250 | 1 | /* |
paul@250 | 2 | * Common utilities for filesystem users. |
paul@250 | 3 | * |
paul@250 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@250 | 5 | * |
paul@250 | 6 | * This program is free software; you can redistribute it and/or |
paul@250 | 7 | * modify it under the terms of the GNU General Public License as |
paul@250 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@250 | 9 | * the License, or (at your option) any later version. |
paul@250 | 10 | * |
paul@250 | 11 | * This program is distributed in the hope that it will be useful, |
paul@250 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@250 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@250 | 14 | * GNU General Public License for more details. |
paul@250 | 15 | * |
paul@250 | 16 | * You should have received a copy of the GNU General Public License |
paul@250 | 17 | * along with this program; if not, write to the Free Software |
paul@250 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@250 | 19 | * Boston, MA 02110-1301, USA |
paul@250 | 20 | */ |
paul@250 | 21 | |
paul@250 | 22 | #include <stdio.h> |
paul@250 | 23 | #include <string.h> |
paul@250 | 24 | |
paul@250 | 25 | #include "format.h" |
paul@250 | 26 | #include "image.h" |
paul@250 | 27 | #include "path.h" |
paul@250 | 28 | #include "utils.h" |
paul@250 | 29 | |
paul@250 | 30 | |
paul@250 | 31 | |
paul@250 | 32 | /* Show a directory listing for the given path. */ |
paul@250 | 33 | |
paul@250 | 34 | errcode_t utils_list_dir(ext2_filsys fs, const char *path) |
paul@250 | 35 | { |
paul@250 | 36 | struct utils_list_dir_data data; |
paul@250 | 37 | |
paul@250 | 38 | data.fs = fs; |
paul@250 | 39 | |
paul@250 | 40 | /* Select a specific object, if appropriate. */ |
paul@250 | 41 | |
paul@258 | 42 | if (!image_isdir_by_path(fs, path)) |
paul@250 | 43 | data.filename = path_basename(path); |
paul@250 | 44 | else |
paul@250 | 45 | data.filename = NULL; |
paul@250 | 46 | |
paul@252 | 47 | /* Do not show link counts by default. */ |
paul@252 | 48 | |
paul@252 | 49 | data.link_count = 0; |
paul@252 | 50 | |
paul@250 | 51 | return image_list_dir(fs, path, utils_list_dir_proc, &data); |
paul@250 | 52 | } |
paul@250 | 53 | |
paul@250 | 54 | /* Show directory entries when iterating. */ |
paul@250 | 55 | |
paul@250 | 56 | int utils_list_dir_proc(struct ext2_dir_entry *dirent, int offset, |
paul@250 | 57 | int blocksize, char *buf, void *priv_data) |
paul@250 | 58 | { |
paul@250 | 59 | struct utils_list_dir_data *data = (struct utils_list_dir_data *) priv_data; |
paul@250 | 60 | ext2_filsys fs = data->fs; |
paul@250 | 61 | struct ext2_inode inode; |
paul@250 | 62 | |
paul@254 | 63 | (void) offset; (void) blocksize; (void) buf; |
paul@254 | 64 | |
paul@250 | 65 | /* Select any indicated filename. */ |
paul@250 | 66 | |
paul@250 | 67 | if ((data->filename != NULL) && (strcmp(dirent->name, data->filename))) |
paul@250 | 68 | return 0; |
paul@250 | 69 | |
paul@250 | 70 | /* Obtain the inode details for metadata. */ |
paul@250 | 71 | |
paul@250 | 72 | if (ext2fs_read_inode(fs, dirent->inode, &inode)) |
paul@250 | 73 | return DIRENT_ABORT; |
paul@250 | 74 | |
paul@250 | 75 | /* Output details in the style of "ls -l" showing directory, permissions, |
paul@250 | 76 | owner, group and size information. */ |
paul@250 | 77 | |
paul@254 | 78 | printf("%s%s %5d %5d %6lld ", |
paul@258 | 79 | image_isdir_by_inode(fs, dirent->inode) ? "d" : "-", |
paul@250 | 80 | get_permission_string(inode.i_mode), |
paul@250 | 81 | inode.i_uid, |
paul@250 | 82 | inode.i_gid, |
paul@250 | 83 | EXT2_I_SIZE(&inode)); |
paul@250 | 84 | |
paul@252 | 85 | /* Show link counts if requested. */ |
paul@252 | 86 | |
paul@252 | 87 | if (data->link_count) |
paul@252 | 88 | printf("%6d ", inode.i_links_count); |
paul@252 | 89 | |
paul@250 | 90 | /* Output the name which is presumably not necessarily null-terminated. */ |
paul@250 | 91 | |
paul@250 | 92 | fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout); |
paul@250 | 93 | fputc((int) '\n', stdout); |
paul@250 | 94 | return 0; |
paul@250 | 95 | } |
paul@250 | 96 | |
paul@250 | 97 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@250 | 98 | */ |