L4Re/departure

libe2access/lib/src/utils.c

250:5c301fd60418
2022-02-16 Paul Boddie Moved common directory listing functionality into a new utilities collection.
     1 /*     2  * Common utilities for filesystem users.     3  *     4  * Copyright (C) 2019, 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 <stdio.h>    23 #include <string.h>    24     25 #include "format.h"    26 #include "image.h"    27 #include "path.h"    28 #include "utils.h"    29     30     31     32 /* Show a directory listing for the given path. */    33     34 errcode_t utils_list_dir(ext2_filsys fs, const char *path)    35 {    36     struct utils_list_dir_data data;    37     38     data.fs = fs;    39     40     /* Select a specific object, if appropriate. */    41     42     if (!image_isdir(fs, path))    43         data.filename = path_basename(path);    44     else    45         data.filename = NULL;    46     47     return image_list_dir(fs, path, utils_list_dir_proc, &data);    48 }    49     50 /* Show directory entries when iterating. */    51     52 int utils_list_dir_proc(struct ext2_dir_entry *dirent, int offset,    53                         int blocksize, char *buf, void *priv_data)    54 {    55     struct utils_list_dir_data *data = (struct utils_list_dir_data *) priv_data;    56     ext2_filsys fs = data->fs;    57     struct ext2_inode inode;    58     59     /* Select any indicated filename. */    60     61     if ((data->filename != NULL) && (strcmp(dirent->name, data->filename)))    62         return 0;    63     64     /* Obtain the inode details for metadata. */    65     66     if (ext2fs_read_inode(fs, dirent->inode, &inode))    67         return DIRENT_ABORT;    68     69     /* Output details in the style of "ls -l" showing directory, permissions,    70        owner, group and size information. */    71     72     printf("%s%s   %5d %5d      %6d ",    73         _image_isdir(fs, dirent->inode) ? "d" : "-",    74         get_permission_string(inode.i_mode),    75         inode.i_uid,    76         inode.i_gid,    77         EXT2_I_SIZE(&inode));    78     79     /* Output the name which is presumably not necessarily null-terminated. */    80     81     fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);    82     fputc((int) '\n', stdout);    83     return 0;    84 }    85     86 /* vim: tabstop=4 expandtab shiftwidth=4    87 */