1.1 --- a/e2access.c Mon Mar 25 15:50:45 2019 +0100
1.2 +++ b/e2access.c Mon Mar 25 15:51:46 2019 +0100
1.3 @@ -181,82 +181,6 @@
1.4
1.5
1.6
1.7 -/* Copy source files out of the filesystem image into the external environment. */
1.8 -
1.9 -int copy_out(ext2_filsys fs, int argc, char *argv[])
1.10 -{
1.11 - /* Target filename details. */
1.12 -
1.13 - char *target = argv[argc - 1];
1.14 - int target_is_file;
1.15 -
1.16 - /* Target file and directory details. */
1.17 -
1.18 - ext2_file_t file;
1.19 - ext2_ino_t ino_file;
1.20 -
1.21 - /* Source file details. */
1.22 -
1.23 - const char *path;
1.24 - int i;
1.25 -
1.26 - /* Locate the target and test whether it is a directory. */
1.27 -
1.28 - if (!isdir(target))
1.29 - {
1.30 - /* Only a new or existing file in an existing directory is permitted. */
1.31 -
1.32 - if (isfile(target) || isdir_dirname(target))
1.33 - target_is_file = 1;
1.34 - else
1.35 - {
1.36 - printf("Target %s is not a directory.\n", target);
1.37 - return 1;
1.38 - }
1.39 -
1.40 - /* Only permit a target file when one source file is given. */
1.41 -
1.42 - if (argc > 2)
1.43 - {
1.44 - printf("Target %s can only be a file when copying a single file.\n", target);
1.45 - return 1;
1.46 - }
1.47 - }
1.48 - else
1.49 - target_is_file = 0;
1.50 -
1.51 - /* For each source filename, test whether it references a file. */
1.52 -
1.53 - for (i = 0; i < argc - 1; i++)
1.54 - {
1.55 - if (!image_isfile(fs, argv[i]))
1.56 - {
1.57 - printf("Source %s is not a file.\n", argv[i]);
1.58 - return 1;
1.59 - }
1.60 - }
1.61 -
1.62 - /* Copy each source file to the target directory. */
1.63 -
1.64 - for (i = 0; i < argc - 1; i++)
1.65 - {
1.66 - path = argv[i];
1.67 -
1.68 - if (image_find_path(fs, &path, &ino_file))
1.69 - return 1;
1.70 -
1.71 - if (copy_file_out(target, argv[i], target_is_file, fs, ino_file))
1.72 - {
1.73 - printf("Failed to read from %s.\n", argv[i]);
1.74 - return 1;
1.75 - }
1.76 -
1.77 - /* NOTE: Overwrite/update metadata where appropriate. */
1.78 - }
1.79 -
1.80 - return 0;
1.81 -}
1.82 -
1.83 /* Copy source files from the external environment into the filesystem image. */
1.84
1.85 int copy_in(ext2_filsys fs, int argc, char *argv[])
1.86 @@ -367,6 +291,82 @@
1.87 return 0;
1.88 }
1.89
1.90 +/* Copy source files out of the filesystem image into the external environment. */
1.91 +
1.92 +int copy_out(ext2_filsys fs, int argc, char *argv[])
1.93 +{
1.94 + /* Target filename details. */
1.95 +
1.96 + char *target = argv[argc - 1];
1.97 + int target_is_file;
1.98 +
1.99 + /* Target file and directory details. */
1.100 +
1.101 + ext2_file_t file;
1.102 + ext2_ino_t ino_file;
1.103 +
1.104 + /* Source file details. */
1.105 +
1.106 + const char *path;
1.107 + int i;
1.108 +
1.109 + /* Locate the target and test whether it is a directory. */
1.110 +
1.111 + if (!isdir(target))
1.112 + {
1.113 + /* Only a new or existing file in an existing directory is permitted. */
1.114 +
1.115 + if (isfile(target) || isdir_dirname(target))
1.116 + target_is_file = 1;
1.117 + else
1.118 + {
1.119 + printf("Target %s is not a directory.\n", target);
1.120 + return 1;
1.121 + }
1.122 +
1.123 + /* Only permit a target file when one source file is given. */
1.124 +
1.125 + if (argc > 2)
1.126 + {
1.127 + printf("Target %s can only be a file when copying a single file.\n", target);
1.128 + return 1;
1.129 + }
1.130 + }
1.131 + else
1.132 + target_is_file = 0;
1.133 +
1.134 + /* For each source filename, test whether it references a file. */
1.135 +
1.136 + for (i = 0; i < argc - 1; i++)
1.137 + {
1.138 + if (!image_isfile(fs, argv[i]))
1.139 + {
1.140 + printf("Source %s is not a file.\n", argv[i]);
1.141 + return 1;
1.142 + }
1.143 + }
1.144 +
1.145 + /* Copy each source file to the target directory. */
1.146 +
1.147 + for (i = 0; i < argc - 1; i++)
1.148 + {
1.149 + path = argv[i];
1.150 +
1.151 + if (image_find_path(fs, &path, &ino_file))
1.152 + return 1;
1.153 +
1.154 + if (copy_file_out(target, argv[i], target_is_file, fs, ino_file))
1.155 + {
1.156 + printf("Failed to read from %s.\n", argv[i]);
1.157 + return 1;
1.158 + }
1.159 +
1.160 + /* NOTE: Overwrite/update metadata where appropriate. */
1.161 + }
1.162 +
1.163 + return 0;
1.164 +}
1.165 +
1.166 /* List directories in the filesystem image. */
1.167
1.168 int list_dirs(ext2_filsys fs, int argc, char *argv[])