paul@278 | 1 | /* |
paul@280 | 2 | * Copy a file out of a filesystem. |
paul@278 | 3 | * |
paul@278 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@278 | 5 | * |
paul@278 | 6 | * This program is free software; you can redistribute it and/or |
paul@278 | 7 | * modify it under the terms of the GNU General Public License as |
paul@278 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@278 | 9 | * the License, or (at your option) any later version. |
paul@278 | 10 | * |
paul@278 | 11 | * This program is distributed in the hope that it will be useful, |
paul@278 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@278 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@278 | 14 | * GNU General Public License for more details. |
paul@278 | 15 | * |
paul@278 | 16 | * You should have received a copy of the GNU General Public License |
paul@278 | 17 | * along with this program; if not, write to the Free Software |
paul@278 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@278 | 19 | * Boston, MA 02110-1301, USA |
paul@278 | 20 | */ |
paul@278 | 21 | |
paul@278 | 22 | #include <stdio.h> |
paul@278 | 23 | #include <string.h> |
paul@278 | 24 | |
paul@278 | 25 | #include <sys/stat.h> |
paul@278 | 26 | |
paul@278 | 27 | #include <ext2fs/ext2fs.h> |
paul@278 | 28 | |
paul@278 | 29 | #include "file.h" |
paul@278 | 30 | #include "image.h" |
paul@278 | 31 | #include "path.h" |
paul@278 | 32 | #include "session.h" |
paul@278 | 33 | |
paul@278 | 34 | |
paul@278 | 35 | |
paul@278 | 36 | /* Copy buffer size. */ |
paul@278 | 37 | |
paul@281 | 38 | static int BUFSIZE = 4096; |
paul@278 | 39 | |
paul@278 | 40 | |
paul@278 | 41 | |
paul@278 | 42 | /* Alternative metadata set by options. */ |
paul@278 | 43 | |
paul@278 | 44 | extern struct metadata md; |
paul@278 | 45 | |
paul@278 | 46 | |
paul@278 | 47 | |
paul@278 | 48 | /* Copy a file out of the filesystem image. */ |
paul@278 | 49 | |
paul@278 | 50 | static int _copy_file_out(const char *target, const char *filename, |
paul@278 | 51 | int target_is_file, ext2_filsys fs, |
paul@278 | 52 | ext2_ino_t ino_file) |
paul@278 | 53 | { |
paul@278 | 54 | int retval = 0; |
paul@278 | 55 | ext2_file_t file; |
paul@278 | 56 | |
paul@278 | 57 | /* Copying details. */ |
paul@278 | 58 | |
paul@278 | 59 | FILE *fp; |
paul@278 | 60 | char buf[BUFSIZE]; |
paul@278 | 61 | unsigned int got; |
paul@278 | 62 | size_t written; |
paul@278 | 63 | |
paul@278 | 64 | /* Open the file in the source directory. */ |
paul@278 | 65 | |
paul@278 | 66 | if (ext2fs_file_open(fs, ino_file, 0, &file)) |
paul@278 | 67 | return 1; |
paul@278 | 68 | |
paul@278 | 69 | /* Open a file in the target directory. */ |
paul@278 | 70 | |
paul@278 | 71 | if (target_is_file) |
paul@278 | 72 | fp = fopen(target, "w"); |
paul@278 | 73 | else |
paul@278 | 74 | fp = open_file_in_dir(target, path_basename(filename), "w"); |
paul@278 | 75 | |
paul@278 | 76 | /* Copy the file content. */ |
paul@278 | 77 | |
paul@278 | 78 | if (fp != NULL) |
paul@278 | 79 | { |
paul@278 | 80 | do |
paul@278 | 81 | { |
paul@278 | 82 | if (ext2fs_file_read(file, buf, BUFSIZE, &got)) |
paul@278 | 83 | { |
paul@278 | 84 | retval = 1; |
paul@278 | 85 | goto close_files; |
paul@278 | 86 | } |
paul@278 | 87 | |
paul@278 | 88 | while (got) |
paul@278 | 89 | { |
paul@278 | 90 | written = fwrite(buf, sizeof(char), got, fp); |
paul@278 | 91 | got -= written; |
paul@278 | 92 | } |
paul@278 | 93 | |
paul@278 | 94 | } while (got); |
paul@278 | 95 | } |
paul@278 | 96 | |
paul@278 | 97 | close_files: |
paul@278 | 98 | fclose(fp); |
paul@278 | 99 | ext2fs_file_close(file); |
paul@278 | 100 | |
paul@278 | 101 | return retval; |
paul@278 | 102 | } |
paul@278 | 103 | |
paul@278 | 104 | |
paul@278 | 105 | |
paul@278 | 106 | /* Make a new directory in the external environment. */ |
paul@278 | 107 | |
paul@278 | 108 | static int _make_directory(const char *target, const char *basename, |
paul@278 | 109 | ext2_filsys fs, ext2_ino_t ino) |
paul@278 | 110 | { |
paul@278 | 111 | errcode_t retval; |
paul@278 | 112 | struct ext2_inode inode; |
paul@278 | 113 | char target_path[strlen(target) + strlen(basename) + 2]; |
paul@278 | 114 | |
paul@278 | 115 | sprintf(target_path, "%s/%s", target, basename); |
paul@278 | 116 | |
paul@278 | 117 | retval = ext2fs_read_inode(fs, ino, &inode); |
paul@278 | 118 | |
paul@278 | 119 | if (retval) |
paul@278 | 120 | return retval; |
paul@278 | 121 | |
paul@278 | 122 | return mkdir(target_path, inode.i_mode); |
paul@278 | 123 | } |
paul@278 | 124 | |
paul@278 | 125 | /* Copy a file from the filesystem into the external environment. */ |
paul@278 | 126 | |
paul@278 | 127 | static int _copy_out(ext2_filsys fs, const char *source, const char *target, |
paul@278 | 128 | int target_is_file) |
paul@278 | 129 | { |
paul@278 | 130 | ext2_ino_t ino_source; |
paul@278 | 131 | |
paul@278 | 132 | if (image_find_by_path(fs, source, &ino_source)) |
paul@278 | 133 | { |
paul@278 | 134 | fprintf(stderr, "Failed to find file: %s\n", source); |
paul@278 | 135 | return 1; |
paul@278 | 136 | } |
paul@278 | 137 | |
paul@278 | 138 | /* Test whether the filename references a file. */ |
paul@278 | 139 | |
paul@278 | 140 | if (image_isdir_by_path(fs, source)) |
paul@278 | 141 | { |
paul@278 | 142 | if (_make_directory(target, path_basename(source), fs, ino_source)) |
paul@278 | 143 | { |
paul@278 | 144 | fprintf(stderr, "Failed to make directory: %s/%s\n", target, |
paul@278 | 145 | path_basename(source)); |
paul@278 | 146 | return 1; |
paul@278 | 147 | } |
paul@278 | 148 | } |
paul@278 | 149 | else if (image_isfile_by_path(fs, source)) |
paul@278 | 150 | { |
paul@278 | 151 | if (_copy_file_out(target, source, target_is_file, fs, ino_source)) |
paul@278 | 152 | { |
paul@278 | 153 | fprintf(stderr, "Failed to read from file: %s\n", source); |
paul@278 | 154 | return 1; |
paul@278 | 155 | } |
paul@278 | 156 | } |
paul@278 | 157 | else |
paul@278 | 158 | { |
paul@278 | 159 | fprintf(stderr, "Object type not supported: %s\n", source); |
paul@278 | 160 | return 1; |
paul@278 | 161 | } |
paul@278 | 162 | |
paul@278 | 163 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@278 | 164 | |
paul@278 | 165 | return 0; |
paul@278 | 166 | } |
paul@278 | 167 | |
paul@278 | 168 | /* Copy source files out of the filesystem image into the external environment. */ |
paul@278 | 169 | |
paul@278 | 170 | int copy_out(ext2_filsys fs, int argc, char *argv[]) |
paul@278 | 171 | { |
paul@278 | 172 | int i; |
paul@278 | 173 | |
paul@278 | 174 | /* Target filename details. */ |
paul@278 | 175 | |
paul@278 | 176 | char *target = argv[argc - 1]; |
paul@278 | 177 | int target_is_file; |
paul@278 | 178 | |
paul@278 | 179 | /* Locate the target and test whether it is a directory. */ |
paul@278 | 180 | |
paul@278 | 181 | if (!isdir(target)) |
paul@278 | 182 | { |
paul@278 | 183 | /* Only a new or existing file in an existing directory is permitted. */ |
paul@278 | 184 | |
paul@278 | 185 | if (isfile(target) || isdir_dirname(target)) |
paul@278 | 186 | target_is_file = 1; |
paul@278 | 187 | else |
paul@278 | 188 | { |
paul@278 | 189 | fprintf(stderr, "Target is not a directory: %s\n", target); |
paul@278 | 190 | return 1; |
paul@278 | 191 | } |
paul@278 | 192 | |
paul@278 | 193 | /* Only permit a target file when one source file is given. */ |
paul@278 | 194 | |
paul@278 | 195 | if (argc > 2) |
paul@278 | 196 | { |
paul@278 | 197 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@278 | 198 | return 1; |
paul@278 | 199 | } |
paul@278 | 200 | } |
paul@278 | 201 | else |
paul@278 | 202 | target_is_file = 0; |
paul@278 | 203 | |
paul@278 | 204 | /* Copy each source file to the target directory. */ |
paul@278 | 205 | |
paul@278 | 206 | for (i = 0; i < argc - 1; i++) |
paul@278 | 207 | if (_copy_out(fs, argv[i], target, target_is_file)) |
paul@278 | 208 | return 1; |
paul@278 | 209 | |
paul@278 | 210 | return 0; |
paul@278 | 211 | } |
paul@278 | 212 | |
paul@278 | 213 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@278 | 214 | */ |