1.1 --- a/libe2access/host/e2access.c Fri Mar 04 00:07:50 2022 +0100
1.2 +++ b/libe2access/host/e2access.c Fri Mar 04 23:41:29 2022 +0100
1.3 @@ -206,6 +206,25 @@
1.4
1.5
1.6
1.7 +/* Make a new directory in the external environment. */
1.8 +
1.9 +static int _make_directory(const char *target, const char *basename,
1.10 + ext2_filsys fs, ext2_ino_t ino)
1.11 +{
1.12 + errcode_t retval;
1.13 + struct ext2_inode inode;
1.14 + char target_path[strlen(target) + strlen(basename) + 2];
1.15 +
1.16 + sprintf(target_path, "%s/%s", target, basename);
1.17 +
1.18 + retval = ext2fs_read_inode(fs, ino, &inode);
1.19 +
1.20 + if (retval)
1.21 + return retval;
1.22 +
1.23 + return mkdir(target_path, inode.i_mode);
1.24 +}
1.25 +
1.26 /* Make a new object using the given function. */
1.27
1.28 static int _make_object(ext2_filsys fs, const char *filename,
1.29 @@ -378,25 +397,60 @@
1.30 return 0;
1.31 }
1.32
1.33 +/* Copy a file from the filesystem into the external environment. */
1.34 +
1.35 +static int _copy_out(ext2_filsys fs, const char *source, const char *target,
1.36 + int target_is_file)
1.37 +{
1.38 + ext2_ino_t ino_source;
1.39 +
1.40 + if (image_find_by_path(fs, source, &ino_source))
1.41 + {
1.42 + fprintf(stderr, "Failed to find file: %s\n", source);
1.43 + return 1;
1.44 + }
1.45 +
1.46 + /* Test whether the filename references a file. */
1.47 +
1.48 + if (image_isdir_by_path(fs, source))
1.49 + {
1.50 + if (_make_directory(target, path_basename(source), fs, ino_source))
1.51 + {
1.52 + fprintf(stderr, "Failed to make directory: %s/%s\n", target,
1.53 + path_basename(source));
1.54 + return 1;
1.55 + }
1.56 + }
1.57 + else if (image_isfile_by_path(fs, source))
1.58 + {
1.59 + if (copy_file_out(target, source, target_is_file, fs, ino_source))
1.60 + {
1.61 + fprintf(stderr, "Failed to read from file: %s\n", source);
1.62 + return 1;
1.63 + }
1.64 + }
1.65 + else
1.66 + {
1.67 + fprintf(stderr, "Object type not supported: %s\n", source);
1.68 + return 1;
1.69 + }
1.70 +
1.71 + /* NOTE: Overwrite/update metadata where appropriate. */
1.72 +
1.73 + return 0;
1.74 +}
1.75 +
1.76 /* Copy source files out of the filesystem image into the external environment. */
1.77
1.78 int copy_out(ext2_filsys fs, int argc, char *argv[])
1.79 {
1.80 + int i;
1.81 +
1.82 /* Target filename details. */
1.83
1.84 char *target = argv[argc - 1];
1.85 int target_is_file;
1.86
1.87 - /* Target file and directory details. */
1.88 -
1.89 - ext2_file_t file;
1.90 - ext2_ino_t ino_file;
1.91 -
1.92 - /* Source file details. */
1.93 -
1.94 - const char *path;
1.95 - int i;
1.96 -
1.97 /* Locate the target and test whether it is a directory. */
1.98
1.99 if (!isdir(target))
1.100 @@ -422,37 +476,11 @@
1.101 else
1.102 target_is_file = 0;
1.103
1.104 - /* For each source filename, test whether it references a file. */
1.105 -
1.106 - for (i = 0; i < argc - 1; i++)
1.107 - {
1.108 - if (!image_isfile_by_path(fs, argv[i]))
1.109 - {
1.110 - fprintf(stderr, "Source is not a file: %s\n", argv[i]);
1.111 - return 1;
1.112 - }
1.113 - }
1.114 -
1.115 /* Copy each source file to the target directory. */
1.116
1.117 for (i = 0; i < argc - 1; i++)
1.118 - {
1.119 - path = argv[i];
1.120 -
1.121 - if (image_find_by_path(fs, path, &ino_file))
1.122 - {
1.123 - fprintf(stderr, "Failed to find file: %s\n", argv[i]);
1.124 + if (_copy_out(fs, argv[i], target, target_is_file))
1.125 return 1;
1.126 - }
1.127 -
1.128 - if (copy_file_out(target, argv[i], target_is_file, fs, ino_file))
1.129 - {
1.130 - fprintf(stderr, "Failed to read from file: %s\n", argv[i]);
1.131 - return 1;
1.132 - }
1.133 -
1.134 - /* NOTE: Overwrite/update metadata where appropriate. */
1.135 - }
1.136
1.137 return 0;
1.138 }