1.1 --- a/libe2access/host/Makefile Wed Feb 16 01:25:39 2022 +0100
1.2 +++ b/libe2access/host/Makefile Wed Feb 16 17:55:47 2022 +0100
1.3 @@ -51,7 +51,9 @@
1.4 TEST_REMOVE_SRC = test_remove.c
1.5 TEST_REMOVE_OBJ = $(TEST_REMOVE_SRC:.c=.o)
1.6
1.7 -LIBE2ACCESS_SRC = $(foreach FILE,format.c image.c path.c,$(SRC)/$(FILE))
1.8 +LIBE2ACCESS_SRC_ORIG = format.c image.c path.c utils.c
1.9 +
1.10 +LIBE2ACCESS_SRC = $(foreach FILE,$(LIBE2ACCESS_SRC_ORIG),$(SRC)/$(FILE))
1.11 LIBE2ACCESS_OBJ = $(LIBE2ACCESS_SRC:.c=.o)
1.12
1.13 ALL_OBJ = $(E2ACCESS_OBJ) $(LIBE2ACCESS_OBJ) $(TEST_LISTING_OBJ) $(TEST_REMOVE_OBJ)
2.1 --- a/libe2access/host/e2access.c Wed Feb 16 01:25:39 2022 +0100
2.2 +++ b/libe2access/host/e2access.c Wed Feb 16 17:55:47 2022 +0100
2.3 @@ -33,6 +33,7 @@
2.4 #include "format.h"
2.5 #include "image.h"
2.6 #include "path.h"
2.7 +#include "utils.h"
2.8
2.9
2.10
2.11 @@ -93,50 +94,6 @@
2.12
2.13
2.14
2.15 -/* Show directory entries when iterating. */
2.16 -
2.17 -struct _list_dir_data
2.18 -{
2.19 - ext2_filsys fs;
2.20 - char *filename;
2.21 -};
2.22 -
2.23 -static int _list_dir_proc(struct ext2_dir_entry *dirent, int offset,
2.24 - int blocksize, char *buf, void *priv_data)
2.25 -{
2.26 - struct _list_dir_data *data = (struct _list_dir_data *) priv_data;
2.27 - ext2_filsys fs = data->fs;
2.28 - struct ext2_inode inode;
2.29 -
2.30 - /* Select any indicated filename. */
2.31 -
2.32 - if ((data->filename != NULL) && (strcmp(dirent->name, data->filename)))
2.33 - return 0;
2.34 -
2.35 - /* Obtain the inode details for metadata. */
2.36 -
2.37 - if (ext2fs_read_inode(fs, dirent->inode, &inode))
2.38 - return DIRENT_ABORT;
2.39 -
2.40 - /* Output details in the style of "ls -l" showing directory, permissions,
2.41 - owner, group and size information. */
2.42 -
2.43 - printf("%s%s %5d %5d %6d ",
2.44 - _image_isdir(fs, dirent->inode) ? "d" : "-",
2.45 - get_permission_string(inode.i_mode),
2.46 - inode.i_uid,
2.47 - inode.i_gid,
2.48 - EXT2_I_SIZE(&inode));
2.49 -
2.50 - /* Output the name which is presumably not necessarily null-terminated. */
2.51 -
2.52 - fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);
2.53 - fputc((int) '\n', stdout);
2.54 - return 0;
2.55 -}
2.56 -
2.57 -
2.58 -
2.59 /* Copy a file into the filesystem image. */
2.60
2.61 int copy_file_in(const char *filename, ext2_filsys fs, ext2_ino_t ino_file, int flags)
2.62 @@ -477,7 +434,6 @@
2.63 {
2.64 int i;
2.65 char *path;
2.66 - struct _list_dir_data data;
2.67
2.68 for (i = 0; i < argc; i++)
2.69 {
2.70 @@ -489,14 +445,7 @@
2.71
2.72 /* List individual files or directories. */
2.73
2.74 - if (image_isfile(fs, path))
2.75 - data.filename = path_split(path);
2.76 - else
2.77 - data.filename = NULL;
2.78 -
2.79 - data.fs = fs;
2.80 -
2.81 - if (image_list_dir(fs, path, _list_dir_proc, &data))
2.82 + if (utils_list_dir(fs, path))
2.83 {
2.84 fprintf(stderr, "Failed to list directory: %s\n", path);
2.85 return 1;
3.1 --- a/libe2access/host/test_listing.c Wed Feb 16 01:25:39 2022 +0100
3.2 +++ b/libe2access/host/test_listing.c Wed Feb 16 17:55:47 2022 +0100
3.3 @@ -1,7 +1,7 @@
3.4 /*
3.5 * Test filesystem listing concurrency limitations.
3.6 *
3.7 - * Copyright (C) 2019, 2021 Paul Boddie <paul@boddie.org.uk>
3.8 + * Copyright (C) 2019, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
3.9 *
3.10 * This program is free software; you can redistribute it and/or
3.11 * modify it under the terms of the GNU General Public License as
3.12 @@ -32,6 +32,7 @@
3.13 #include "format.h"
3.14 #include "image.h"
3.15 #include "path.h"
3.16 +#include "utils.h"
3.17
3.18
3.19
3.20 @@ -40,33 +41,17 @@
3.21 static int image_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
3.22 int blocksize, char *buf, void *priv_data)
3.23 {
3.24 - ext2_filsys fs = (ext2_filsys) priv_data;
3.25 + struct utils_list_dir_data *data = (struct utils_list_dir_data *) priv_data;
3.26 + ext2_filsys fs = (ext2_filsys) data->fs;
3.27 struct ext2_inode inode;
3.28 ext2_file_t file;
3.29 __u64 size;
3.30 char *to_remove[] = {"file0061", "file0062", "file0063", "file0125", "file0126", "file0127", 0};
3.31 char **f;
3.32
3.33 - /* Obtain the inode details for metadata. */
3.34 -
3.35 - if (ext2fs_read_inode(fs, dirent->inode, &inode))
3.36 - return DIRENT_ABORT;
3.37 -
3.38 - /* Output details in the style of "ls -l" showing directory, permissions,
3.39 - owner, group and size information. */
3.40 + /* Call the formatting utility function. */
3.41
3.42 - printf("%06d: %s%s %5d %5d %6d ",
3.43 - offset,
3.44 - _image_isdir(fs, dirent->inode) ? "d" : "-",
3.45 - get_permission_string(inode.i_mode),
3.46 - inode.i_uid,
3.47 - inode.i_gid,
3.48 - EXT2_I_SIZE(&inode));
3.49 -
3.50 - /* Output the name which is presumably not necessarily null-terminated. */
3.51 -
3.52 - fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);
3.53 - fputc((int) '\n', stdout);
3.54 + utils_list_dir_proc(dirent, offset, blocksize, buf, priv_data);
3.55
3.56 /* Remove files at an awkward point in time. */
3.57
3.58 @@ -93,6 +78,7 @@
3.59 ext2_ino_t ino;
3.60 ext2_file_t file;
3.61 int i;
3.62 + struct utils_list_dir_data list_dir_data;
3.63
3.64 if (argc < 2)
3.65 {
3.66 @@ -145,8 +131,11 @@
3.67 ext2fs_file_close(file);
3.68 }
3.69
3.70 - image_list_dir(fs, "", image_list_dir_proc, fs);
3.71 - image_list_dir(fs, "", image_list_dir_proc, fs);
3.72 + list_dir_data.fs = fs;
3.73 + list_dir_data.filename = NULL;
3.74 +
3.75 + image_list_dir(fs, "", image_list_dir_proc, &list_dir_data);
3.76 + image_list_dir(fs, "", image_list_dir_proc, &list_dir_data);
3.77
3.78 /* Close the filesystem image. */
3.79
4.1 --- a/libe2access/host/test_remove.c Wed Feb 16 01:25:39 2022 +0100
4.2 +++ b/libe2access/host/test_remove.c Wed Feb 16 17:55:47 2022 +0100
4.3 @@ -1,7 +1,7 @@
4.4 /*
4.5 * Test file removal semantics.
4.6 *
4.7 - * Copyright (C) 2019, 2021 Paul Boddie <paul@boddie.org.uk>
4.8 + * Copyright (C) 2019, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
4.9 *
4.10 * This program is free software; you can redistribute it and/or
4.11 * modify it under the terms of the GNU General Public License as
4.12 @@ -32,6 +32,7 @@
4.13 #include "format.h"
4.14 #include "image.h"
4.15 #include "path.h"
4.16 +#include "utils.h"
4.17
4.18
4.19
4.20 @@ -45,42 +46,6 @@
4.21
4.22
4.23
4.24 -/* Show directory entries when iterating. */
4.25 -
4.26 -static int image_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
4.27 - int blocksize, char *buf, void *priv_data)
4.28 -{
4.29 - ext2_filsys fs = (ext2_filsys) priv_data;
4.30 - struct ext2_inode inode;
4.31 - ext2_file_t file;
4.32 - __u64 size;
4.33 -
4.34 - /* Obtain the inode details for metadata. */
4.35 -
4.36 - if (ext2fs_read_inode(fs, dirent->inode, &inode))
4.37 - return DIRENT_ABORT;
4.38 -
4.39 - /* Output details in the style of "ls -l" showing directory, permissions,
4.40 - owner, group and size information. */
4.41 -
4.42 - printf("%06d: %s%s %5d %5d %6d ",
4.43 - offset,
4.44 - _image_isdir(fs, dirent->inode) ? "d" : "-",
4.45 - get_permission_string(inode.i_mode),
4.46 - inode.i_uid,
4.47 - inode.i_gid,
4.48 - EXT2_I_SIZE(&inode));
4.49 -
4.50 - /* Output the name which is presumably not necessarily null-terminated. */
4.51 -
4.52 - fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);
4.53 - fputc((int) '\n', stdout);
4.54 -
4.55 - return 0;
4.56 -}
4.57 -
4.58 -
4.59 -
4.60 int main(int argc, char *argv[])
4.61 {
4.62 int flags = EXT2_FLAG_RW;
4.63 @@ -156,7 +121,7 @@
4.64 inos[i - 1] = ino;
4.65 }
4.66
4.67 - image_list_dir(fs, "", image_list_dir_proc, fs);
4.68 + utils_list_dir(fs, "");
4.69 printf("----\n");
4.70
4.71 /* Unlink the files. */
4.72 @@ -172,7 +137,7 @@
4.73 }
4.74 }
4.75
4.76 - image_list_dir(fs, "", image_list_dir_proc, fs);
4.77 + utils_list_dir(fs, "");
4.78 printf("----\n");
4.79
4.80 /* Access and close unlinked files. */
4.81 @@ -194,7 +159,7 @@
4.82 ext2fs_file_close(file);
4.83 }
4.84
4.85 - image_list_dir(fs, "", image_list_dir_proc, fs);
4.86 + utils_list_dir(fs, "");
4.87 printf("----\n");
4.88
4.89 /* Create some more files. */
4.90 @@ -222,7 +187,7 @@
4.91 ext2fs_file_close(file);
4.92 }
4.93
4.94 - image_list_dir(fs, "", image_list_dir_proc, fs);
4.95 + utils_list_dir(fs, "");
4.96 printf("----\n");
4.97
4.98 /* Re-open the original files and read from them. */
4.99 @@ -276,7 +241,7 @@
4.100 ext2fs_file_close(file);
4.101 }
4.102
4.103 - image_list_dir(fs, "", image_list_dir_proc, fs);
4.104 + utils_list_dir(fs, "");
4.105 printf("----\n");
4.106
4.107 /* Re-open the original files and read from them again. */
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/libe2access/include/e2access/utils.h Wed Feb 16 17:55:47 2022 +0100
5.3 @@ -0,0 +1,49 @@
5.4 +/*
5.5 + * Common utilities for filesystem users.
5.6 + *
5.7 + * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
5.8 + *
5.9 + * This program is free software; you can redistribute it and/or
5.10 + * modify it under the terms of the GNU General Public License as
5.11 + * published by the Free Software Foundation; either version 2 of
5.12 + * the License, or (at your option) any later version.
5.13 + *
5.14 + * This program is distributed in the hope that it will be useful,
5.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.17 + * GNU General Public License for more details.
5.18 + *
5.19 + * You should have received a copy of the GNU General Public License
5.20 + * along with this program; if not, write to the Free Software
5.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
5.22 + * Boston, MA 02110-1301, USA
5.23 + */
5.24 +
5.25 +#ifndef __UTILS_H__
5.26 +#define __UTILS_H__
5.27 +
5.28 +#include <ext2fs/ext2fs.h>
5.29 +
5.30 +#ifdef __cplusplus
5.31 +extern "C" {
5.32 +#endif
5.33 +
5.34 +struct utils_list_dir_data
5.35 +{
5.36 + ext2_filsys fs; /* filesystem */
5.37 + const char *filename; /* single filename to show */
5.38 +};
5.39 +
5.40 +errcode_t utils_list_dir(ext2_filsys fs, const char *path);
5.41 +
5.42 +int utils_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
5.43 + int blocksize, char *buf, void *priv_data);
5.44 +
5.45 +#ifdef __cplusplus
5.46 +}
5.47 +#endif
5.48 +
5.49 +#endif /* __UTILS_H__ */
5.50 +
5.51 +/* vim: tabstop=4 expandtab shiftwidth=4
5.52 +*/
6.1 --- a/libe2access/lib/src/Makefile Wed Feb 16 01:25:39 2022 +0100
6.2 +++ b/libe2access/lib/src/Makefile Wed Feb 16 17:55:47 2022 +0100
6.3 @@ -3,7 +3,7 @@
6.4
6.5 TARGET = libe2access.a libe2access.so
6.6 PC_FILENAME = libe2access
6.7 -SRC_C = access.c format.c image.c path.c
6.8 +SRC_C = access.c format.c image.c path.c utils.c
6.9
6.10 REQUIRES_LIBS = libext2fs libsystypes
6.11
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/libe2access/lib/src/utils.c Wed Feb 16 17:55:47 2022 +0100
7.3 @@ -0,0 +1,87 @@
7.4 +/*
7.5 + * Common utilities for filesystem users.
7.6 + *
7.7 + * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
7.8 + *
7.9 + * This program is free software; you can redistribute it and/or
7.10 + * modify it under the terms of the GNU General Public License as
7.11 + * published by the Free Software Foundation; either version 2 of
7.12 + * the License, or (at your option) any later version.
7.13 + *
7.14 + * This program is distributed in the hope that it will be useful,
7.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7.17 + * GNU General Public License for more details.
7.18 + *
7.19 + * You should have received a copy of the GNU General Public License
7.20 + * along with this program; if not, write to the Free Software
7.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
7.22 + * Boston, MA 02110-1301, USA
7.23 + */
7.24 +
7.25 +#include <stdio.h>
7.26 +#include <string.h>
7.27 +
7.28 +#include "format.h"
7.29 +#include "image.h"
7.30 +#include "path.h"
7.31 +#include "utils.h"
7.32 +
7.33 +
7.34 +
7.35 +/* Show a directory listing for the given path. */
7.36 +
7.37 +errcode_t utils_list_dir(ext2_filsys fs, const char *path)
7.38 +{
7.39 + struct utils_list_dir_data data;
7.40 +
7.41 + data.fs = fs;
7.42 +
7.43 + /* Select a specific object, if appropriate. */
7.44 +
7.45 + if (!image_isdir(fs, path))
7.46 + data.filename = path_basename(path);
7.47 + else
7.48 + data.filename = NULL;
7.49 +
7.50 + return image_list_dir(fs, path, utils_list_dir_proc, &data);
7.51 +}
7.52 +
7.53 +/* Show directory entries when iterating. */
7.54 +
7.55 +int utils_list_dir_proc(struct ext2_dir_entry *dirent, int offset,
7.56 + int blocksize, char *buf, void *priv_data)
7.57 +{
7.58 + struct utils_list_dir_data *data = (struct utils_list_dir_data *) priv_data;
7.59 + ext2_filsys fs = data->fs;
7.60 + struct ext2_inode inode;
7.61 +
7.62 + /* Select any indicated filename. */
7.63 +
7.64 + if ((data->filename != NULL) && (strcmp(dirent->name, data->filename)))
7.65 + return 0;
7.66 +
7.67 + /* Obtain the inode details for metadata. */
7.68 +
7.69 + if (ext2fs_read_inode(fs, dirent->inode, &inode))
7.70 + return DIRENT_ABORT;
7.71 +
7.72 + /* Output details in the style of "ls -l" showing directory, permissions,
7.73 + owner, group and size information. */
7.74 +
7.75 + printf("%s%s %5d %5d %6d ",
7.76 + _image_isdir(fs, dirent->inode) ? "d" : "-",
7.77 + get_permission_string(inode.i_mode),
7.78 + inode.i_uid,
7.79 + inode.i_gid,
7.80 + EXT2_I_SIZE(&inode));
7.81 +
7.82 + /* Output the name which is presumably not necessarily null-terminated. */
7.83 +
7.84 + fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);
7.85 + fputc((int) '\n', stdout);
7.86 + return 0;
7.87 +}
7.88 +
7.89 +/* vim: tabstop=4 expandtab shiftwidth=4
7.90 +*/