paul@226 | 1 | /* |
paul@226 | 2 | * Access a filesystem. |
paul@226 | 3 | * |
paul@246 | 4 | * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@226 | 5 | * |
paul@226 | 6 | * This program is free software; you can redistribute it and/or |
paul@226 | 7 | * modify it under the terms of the GNU General Public License as |
paul@226 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@226 | 9 | * the License, or (at your option) any later version. |
paul@226 | 10 | * |
paul@226 | 11 | * This program is distributed in the hope that it will be useful, |
paul@226 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@226 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@226 | 14 | * GNU General Public License for more details. |
paul@226 | 15 | * |
paul@226 | 16 | * You should have received a copy of the GNU General Public License |
paul@226 | 17 | * along with this program; if not, write to the Free Software |
paul@226 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@226 | 19 | * Boston, MA 02110-1301, USA |
paul@226 | 20 | */ |
paul@226 | 21 | |
paul@226 | 22 | #include <stdio.h> |
paul@226 | 23 | #include <stdlib.h> |
paul@226 | 24 | #include <string.h> |
paul@241 | 25 | #include <unistd.h> |
paul@226 | 26 | |
paul@226 | 27 | #include <sys/types.h> |
paul@226 | 28 | #include <sys/stat.h> |
paul@264 | 29 | #include <sys/sysmacros.h> /* major, minor */ |
paul@226 | 30 | |
paul@226 | 31 | #include <ext2fs/ext2fs.h> |
paul@226 | 32 | |
paul@226 | 33 | #include "file.h" |
paul@226 | 34 | #include "format.h" |
paul@226 | 35 | #include "image.h" |
paul@263 | 36 | #include "input.h" |
paul@226 | 37 | #include "path.h" |
paul@250 | 38 | #include "utils.h" |
paul@226 | 39 | |
paul@226 | 40 | |
paul@226 | 41 | |
paul@226 | 42 | /* Copy buffer size. */ |
paul@226 | 43 | |
paul@226 | 44 | const int BUFSIZE = 4096; |
paul@226 | 45 | |
paul@262 | 46 | /* Maximum number of arguments in scripts. */ |
paul@262 | 47 | |
paul@262 | 48 | const int MAX_ARGS = 32; |
paul@262 | 49 | |
paul@226 | 50 | |
paul@226 | 51 | |
paul@241 | 52 | /* Alternative metadata set by options. */ |
paul@241 | 53 | |
paul@241 | 54 | struct metadata |
paul@241 | 55 | { |
paul@241 | 56 | uid_t uid; |
paul@241 | 57 | gid_t gid; |
paul@241 | 58 | mode_t mask; |
paul@241 | 59 | int have_uid, have_gid; |
paul@241 | 60 | }; |
paul@241 | 61 | |
paul@241 | 62 | struct metadata md; |
paul@241 | 63 | |
paul@246 | 64 | /* Parse program options. */ |
paul@246 | 65 | |
paul@241 | 66 | static int parse_options(int argc, char *argv[]) |
paul@241 | 67 | { |
paul@241 | 68 | int opt; |
paul@241 | 69 | |
paul@241 | 70 | md.have_uid = 0; |
paul@241 | 71 | md.have_gid = 0; |
paul@241 | 72 | md.mask = 0000; |
paul@241 | 73 | |
paul@241 | 74 | while ((opt = getopt(argc, argv, "g:m:u:")) != -1) |
paul@241 | 75 | { |
paul@241 | 76 | switch (opt) |
paul@241 | 77 | { |
paul@241 | 78 | case 'g': |
paul@241 | 79 | md.gid = atoi(optarg); |
paul@241 | 80 | md.have_gid = 1; |
paul@241 | 81 | break; |
paul@241 | 82 | |
paul@241 | 83 | case 'm': |
paul@241 | 84 | md.mask = strtol(optarg, NULL, 0); |
paul@241 | 85 | break; |
paul@241 | 86 | |
paul@241 | 87 | case 'u': |
paul@241 | 88 | md.uid = atoi(optarg); |
paul@241 | 89 | md.have_uid = 1; |
paul@241 | 90 | break; |
paul@241 | 91 | |
paul@241 | 92 | default: |
paul@246 | 93 | fprintf(stderr, "Option not recognised: %s\n", argv[optind]); |
paul@241 | 94 | return -1; |
paul@241 | 95 | } |
paul@241 | 96 | } |
paul@241 | 97 | |
paul@241 | 98 | return 0; |
paul@241 | 99 | } |
paul@241 | 100 | |
paul@241 | 101 | |
paul@241 | 102 | |
paul@226 | 103 | /* Copy a file into the filesystem image. */ |
paul@226 | 104 | |
paul@226 | 105 | int copy_file_in(const char *filename, ext2_filsys fs, ext2_ino_t ino_file, int flags) |
paul@226 | 106 | { |
paul@226 | 107 | int retval = 0; |
paul@226 | 108 | ext2_file_t file; |
paul@226 | 109 | |
paul@226 | 110 | /* Copying details. */ |
paul@226 | 111 | |
paul@226 | 112 | FILE *fp; |
paul@226 | 113 | char buf[BUFSIZE]; |
paul@226 | 114 | size_t got; |
paul@226 | 115 | unsigned int written; |
paul@226 | 116 | |
paul@226 | 117 | /* Open a file in the target directory. */ |
paul@226 | 118 | |
paul@226 | 119 | if (ext2fs_file_open(fs, ino_file, flags, &file)) |
paul@226 | 120 | return 1; |
paul@226 | 121 | |
paul@226 | 122 | /* Open the file in the source directory. */ |
paul@226 | 123 | |
paul@226 | 124 | fp = fopen(filename, "r"); |
paul@226 | 125 | |
paul@226 | 126 | /* Copy the file content. */ |
paul@226 | 127 | |
paul@226 | 128 | if (fp != NULL) |
paul@226 | 129 | { |
paul@226 | 130 | while (got = fread(buf, sizeof(char), BUFSIZE, fp)) |
paul@226 | 131 | { |
paul@226 | 132 | while (got) |
paul@226 | 133 | { |
paul@226 | 134 | if (ext2fs_file_write(file, buf, got, &written)) |
paul@226 | 135 | { |
paul@226 | 136 | retval = 1; |
paul@226 | 137 | goto close_files; |
paul@226 | 138 | } |
paul@226 | 139 | got -= written; |
paul@226 | 140 | } |
paul@226 | 141 | } |
paul@226 | 142 | } |
paul@226 | 143 | |
paul@226 | 144 | close_files: |
paul@226 | 145 | fclose(fp); |
paul@226 | 146 | ext2fs_file_flush(file); |
paul@226 | 147 | ext2fs_file_close(file); |
paul@226 | 148 | |
paul@226 | 149 | return retval; |
paul@226 | 150 | } |
paul@226 | 151 | |
paul@226 | 152 | /* Copy a file out of the filesystem image. */ |
paul@226 | 153 | |
paul@226 | 154 | int copy_file_out(const char *target, const char *filename, int target_is_file, |
paul@226 | 155 | ext2_filsys fs, ext2_ino_t ino_file) |
paul@226 | 156 | { |
paul@226 | 157 | int retval = 0; |
paul@226 | 158 | ext2_file_t file; |
paul@226 | 159 | |
paul@226 | 160 | /* Copying details. */ |
paul@226 | 161 | |
paul@226 | 162 | FILE *fp; |
paul@226 | 163 | char buf[BUFSIZE]; |
paul@226 | 164 | unsigned int got; |
paul@226 | 165 | size_t written; |
paul@226 | 166 | |
paul@226 | 167 | /* Open the file in the source directory. */ |
paul@226 | 168 | |
paul@226 | 169 | if (ext2fs_file_open(fs, ino_file, 0, &file)) |
paul@226 | 170 | return 1; |
paul@226 | 171 | |
paul@226 | 172 | /* Open a file in the target directory. */ |
paul@226 | 173 | |
paul@226 | 174 | if (target_is_file) |
paul@226 | 175 | fp = fopen(target, "w"); |
paul@226 | 176 | else |
paul@226 | 177 | fp = open_file_in_dir(target, path_basename(filename), "w"); |
paul@226 | 178 | |
paul@226 | 179 | /* Copy the file content. */ |
paul@226 | 180 | |
paul@226 | 181 | if (fp != NULL) |
paul@226 | 182 | { |
paul@226 | 183 | do |
paul@226 | 184 | { |
paul@226 | 185 | if (ext2fs_file_read(file, buf, BUFSIZE, &got)) |
paul@226 | 186 | { |
paul@226 | 187 | retval = 1; |
paul@226 | 188 | goto close_files; |
paul@226 | 189 | } |
paul@226 | 190 | |
paul@226 | 191 | while (got) |
paul@226 | 192 | { |
paul@226 | 193 | written = fwrite(buf, sizeof(char), got, fp); |
paul@226 | 194 | got -= written; |
paul@226 | 195 | } |
paul@226 | 196 | |
paul@226 | 197 | } while (got); |
paul@226 | 198 | } |
paul@226 | 199 | |
paul@226 | 200 | close_files: |
paul@226 | 201 | fclose(fp); |
paul@226 | 202 | ext2fs_file_close(file); |
paul@226 | 203 | |
paul@226 | 204 | return retval; |
paul@226 | 205 | } |
paul@226 | 206 | |
paul@226 | 207 | |
paul@226 | 208 | |
paul@275 | 209 | /* Make a new object using the given function. */ |
paul@275 | 210 | |
paul@275 | 211 | static int _make_object(ext2_filsys fs, const char *filename, |
paul@275 | 212 | ext2_ino_t ino_parent, const char *basename, |
paul@275 | 213 | ext2_ino_t *ino_target, |
paul@275 | 214 | errcode_t (*fn)(ext2_filsys, ext2_ino_t, const char *, |
paul@275 | 215 | __u16, __u16, __u16, ext2_ino_t *)) |
paul@275 | 216 | { |
paul@275 | 217 | errcode_t retval; |
paul@275 | 218 | struct stat st; |
paul@275 | 219 | |
paul@275 | 220 | /* Obtain the metadata. */ |
paul@275 | 221 | |
paul@275 | 222 | if (lstat(filename, &st)) |
paul@275 | 223 | { |
paul@275 | 224 | fprintf(stderr, "Failed to read object metadata: %s\n", filename); |
paul@275 | 225 | return 1; |
paul@275 | 226 | } |
paul@275 | 227 | |
paul@275 | 228 | retval = fn(fs, ino_parent, basename, st.st_mode & ~md.mask, |
paul@275 | 229 | md.have_uid ? md.uid : st.st_uid, |
paul@275 | 230 | md.have_gid ? md.gid : st.st_gid, |
paul@275 | 231 | ino_target); |
paul@275 | 232 | |
paul@275 | 233 | if (retval) |
paul@275 | 234 | { |
paul@275 | 235 | fprintf(stderr, "Failed to create object: %s\n", filename); |
paul@275 | 236 | return 1; |
paul@275 | 237 | } |
paul@275 | 238 | |
paul@275 | 239 | return 0; |
paul@275 | 240 | } |
paul@275 | 241 | |
paul@273 | 242 | /* Copy a source file from the external environment into the filesystem. */ |
paul@273 | 243 | |
paul@273 | 244 | static int _copy_in(ext2_filsys fs, const char *filename, ext2_ino_t ino_target, |
paul@273 | 245 | const char *basename) |
paul@273 | 246 | { |
paul@273 | 247 | errcode_t retval; |
paul@273 | 248 | int flags; |
paul@273 | 249 | |
paul@273 | 250 | /* By default, treat the target as a new object. */ |
paul@273 | 251 | |
paul@273 | 252 | ext2_ino_t ino_parent = ino_target; |
paul@273 | 253 | int target_is_new = 1; |
paul@273 | 254 | |
paul@273 | 255 | /* Source file details. */ |
paul@273 | 256 | |
paul@273 | 257 | struct stat st; |
paul@273 | 258 | |
paul@273 | 259 | /* Without a basename, the target exists and is either a directory, into |
paul@273 | 260 | which the source file shall be copied, or it is a file that shall be |
paul@273 | 261 | overwritten. */ |
paul@273 | 262 | |
paul@273 | 263 | if (basename == NULL) |
paul@273 | 264 | { |
paul@273 | 265 | basename = path_basename(filename); |
paul@273 | 266 | target_is_new = image_isdir_by_inode(fs, ino_target); |
paul@273 | 267 | } |
paul@273 | 268 | |
paul@273 | 269 | /* Directories are created with the same metadata. */ |
paul@273 | 270 | |
paul@273 | 271 | if (isdir(filename)) |
paul@273 | 272 | { |
paul@273 | 273 | if (!target_is_new) |
paul@273 | 274 | { |
paul@273 | 275 | fprintf(stderr, "Directory cannot be copied as it already exists: %s\n", filename); |
paul@273 | 276 | return 1; |
paul@273 | 277 | } |
paul@273 | 278 | |
paul@275 | 279 | if (_make_object(fs, filename, ino_parent, basename, &ino_target, |
paul@275 | 280 | image_make_dir)) |
paul@273 | 281 | return 1; |
paul@273 | 282 | } |
paul@273 | 283 | |
paul@273 | 284 | /* Files are copied. */ |
paul@273 | 285 | |
paul@273 | 286 | else if (isfile(filename)) |
paul@273 | 287 | { |
paul@273 | 288 | flags = EXT2_FILE_WRITE; |
paul@273 | 289 | |
paul@273 | 290 | /* Obtain the inode for the target file. */ |
paul@273 | 291 | |
paul@273 | 292 | if (target_is_new) |
paul@273 | 293 | { |
paul@275 | 294 | if (_make_object(fs, filename, ino_parent, basename, &ino_target, |
paul@275 | 295 | image_create_file)) |
paul@273 | 296 | return 1; |
paul@273 | 297 | |
paul@273 | 298 | flags |= EXT2_FILE_CREATE; |
paul@273 | 299 | } |
paul@273 | 300 | |
paul@273 | 301 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@273 | 302 | |
paul@273 | 303 | if (copy_file_in(filename, fs, ino_target, flags)) |
paul@273 | 304 | { |
paul@273 | 305 | fprintf(stderr, "Failed to copy file: %s\n", filename); |
paul@273 | 306 | return 1; |
paul@273 | 307 | } |
paul@273 | 308 | } |
paul@273 | 309 | } |
paul@273 | 310 | |
paul@226 | 311 | /* Copy source files from the external environment into the filesystem image. */ |
paul@226 | 312 | |
paul@226 | 313 | int copy_in(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 314 | { |
paul@226 | 315 | errcode_t retval; |
paul@273 | 316 | int i; |
paul@226 | 317 | |
paul@226 | 318 | /* Target filename details. */ |
paul@226 | 319 | |
paul@226 | 320 | const char *target = argv[argc - 1]; |
paul@273 | 321 | const char *target_remaining = target; |
paul@226 | 322 | const char *basename; |
paul@226 | 323 | |
paul@226 | 324 | /* Target file and directory details. */ |
paul@226 | 325 | |
paul@273 | 326 | int target_is_file; |
paul@273 | 327 | ext2_ino_t ino_target; |
paul@226 | 328 | |
paul@226 | 329 | /* Locate the target and test whether it is a file or a directory. */ |
paul@226 | 330 | |
paul@257 | 331 | if (image_resolve_by_path(fs, &target_remaining, &ino_target)) |
paul@226 | 332 | { |
paul@226 | 333 | /* Only a non-existent file in an existing directory is permitted. */ |
paul@226 | 334 | |
paul@258 | 335 | if (!image_isdir_by_inode(fs, ino_target) || !path_is_leafname(target_remaining)) |
paul@226 | 336 | { |
paul@246 | 337 | fprintf(stderr, "Target not found: %s\n", target); |
paul@226 | 338 | return 1; |
paul@226 | 339 | } |
paul@226 | 340 | |
paul@273 | 341 | /* Any absent target leaves the basename remaining. |
paul@273 | 342 | The resolved target is the parent directory. */ |
paul@273 | 343 | |
paul@273 | 344 | target_is_file = 0; |
paul@273 | 345 | basename = target_remaining; |
paul@226 | 346 | } |
paul@226 | 347 | else |
paul@226 | 348 | { |
paul@273 | 349 | /* Any present target can be a file or directory. |
paul@273 | 350 | The resolved target is the actual target. */ |
paul@273 | 351 | |
paul@258 | 352 | target_is_file = image_isfile_by_inode(fs, ino_target); |
paul@273 | 353 | basename = NULL; |
paul@226 | 354 | } |
paul@226 | 355 | |
paul@226 | 356 | /* Only permit a target file when one source file is given. */ |
paul@226 | 357 | |
paul@226 | 358 | if (target_is_file) |
paul@226 | 359 | { |
paul@226 | 360 | if (argc > 2) |
paul@226 | 361 | { |
paul@246 | 362 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@226 | 363 | return 1; |
paul@226 | 364 | } |
paul@226 | 365 | } |
paul@258 | 366 | else if (!image_isdir_by_inode(fs, ino_target)) |
paul@226 | 367 | { |
paul@246 | 368 | fprintf(stderr, "Target is not a directory: %s\n", target); |
paul@226 | 369 | return 1; |
paul@226 | 370 | } |
paul@226 | 371 | |
paul@226 | 372 | /* Copy each source object to the target directory. */ |
paul@226 | 373 | |
paul@226 | 374 | for (i = 0; i < argc - 1; i++) |
paul@273 | 375 | if (_copy_in(fs, argv[i], ino_target, basename)) |
paul@273 | 376 | return 1; |
paul@226 | 377 | |
paul@226 | 378 | return 0; |
paul@226 | 379 | } |
paul@226 | 380 | |
paul@226 | 381 | /* Copy source files out of the filesystem image into the external environment. */ |
paul@226 | 382 | |
paul@226 | 383 | int copy_out(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 384 | { |
paul@226 | 385 | /* Target filename details. */ |
paul@226 | 386 | |
paul@226 | 387 | char *target = argv[argc - 1]; |
paul@226 | 388 | int target_is_file; |
paul@226 | 389 | |
paul@226 | 390 | /* Target file and directory details. */ |
paul@226 | 391 | |
paul@226 | 392 | ext2_file_t file; |
paul@226 | 393 | ext2_ino_t ino_file; |
paul@226 | 394 | |
paul@226 | 395 | /* Source file details. */ |
paul@226 | 396 | |
paul@226 | 397 | const char *path; |
paul@226 | 398 | int i; |
paul@226 | 399 | |
paul@226 | 400 | /* Locate the target and test whether it is a directory. */ |
paul@226 | 401 | |
paul@226 | 402 | if (!isdir(target)) |
paul@226 | 403 | { |
paul@226 | 404 | /* Only a new or existing file in an existing directory is permitted. */ |
paul@226 | 405 | |
paul@226 | 406 | if (isfile(target) || isdir_dirname(target)) |
paul@226 | 407 | target_is_file = 1; |
paul@226 | 408 | else |
paul@226 | 409 | { |
paul@246 | 410 | fprintf(stderr, "Target is not a directory: %s\n", target); |
paul@226 | 411 | return 1; |
paul@226 | 412 | } |
paul@226 | 413 | |
paul@226 | 414 | /* Only permit a target file when one source file is given. */ |
paul@226 | 415 | |
paul@226 | 416 | if (argc > 2) |
paul@226 | 417 | { |
paul@246 | 418 | fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); |
paul@226 | 419 | return 1; |
paul@226 | 420 | } |
paul@226 | 421 | } |
paul@226 | 422 | else |
paul@226 | 423 | target_is_file = 0; |
paul@226 | 424 | |
paul@226 | 425 | /* For each source filename, test whether it references a file. */ |
paul@226 | 426 | |
paul@226 | 427 | for (i = 0; i < argc - 1; i++) |
paul@226 | 428 | { |
paul@258 | 429 | if (!image_isfile_by_path(fs, argv[i])) |
paul@226 | 430 | { |
paul@246 | 431 | fprintf(stderr, "Source is not a file: %s\n", argv[i]); |
paul@226 | 432 | return 1; |
paul@226 | 433 | } |
paul@226 | 434 | } |
paul@226 | 435 | |
paul@226 | 436 | /* Copy each source file to the target directory. */ |
paul@226 | 437 | |
paul@226 | 438 | for (i = 0; i < argc - 1; i++) |
paul@226 | 439 | { |
paul@226 | 440 | path = argv[i]; |
paul@226 | 441 | |
paul@257 | 442 | if (image_find_by_path(fs, path, &ino_file)) |
paul@246 | 443 | { |
paul@246 | 444 | fprintf(stderr, "Failed to find file: %s\n", argv[i]); |
paul@226 | 445 | return 1; |
paul@246 | 446 | } |
paul@226 | 447 | |
paul@226 | 448 | if (copy_file_out(target, argv[i], target_is_file, fs, ino_file)) |
paul@226 | 449 | { |
paul@246 | 450 | fprintf(stderr, "Failed to read from file: %s\n", argv[i]); |
paul@226 | 451 | return 1; |
paul@226 | 452 | } |
paul@226 | 453 | |
paul@226 | 454 | /* NOTE: Overwrite/update metadata where appropriate. */ |
paul@226 | 455 | } |
paul@226 | 456 | |
paul@226 | 457 | return 0; |
paul@226 | 458 | } |
paul@226 | 459 | |
paul@244 | 460 | /* List objects in the filesystem image. */ |
paul@226 | 461 | |
paul@244 | 462 | int list(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 463 | { |
paul@226 | 464 | int i; |
paul@244 | 465 | char *path; |
paul@226 | 466 | |
paul@226 | 467 | for (i = 0; i < argc; i++) |
paul@226 | 468 | { |
paul@226 | 469 | path = argv[i]; |
paul@226 | 470 | |
paul@244 | 471 | /* Emit each object. */ |
paul@226 | 472 | |
paul@226 | 473 | puts(path); |
paul@226 | 474 | |
paul@244 | 475 | /* List individual files or directories. */ |
paul@244 | 476 | |
paul@250 | 477 | if (utils_list_dir(fs, path)) |
paul@246 | 478 | { |
paul@262 | 479 | fprintf(stderr, "Failed to list object: %s\n", path); |
paul@226 | 480 | return 1; |
paul@246 | 481 | } |
paul@226 | 482 | } |
paul@226 | 483 | |
paul@226 | 484 | return 0; |
paul@226 | 485 | } |
paul@226 | 486 | |
paul@226 | 487 | /* Make directories in the filesystem image. */ |
paul@226 | 488 | |
paul@226 | 489 | int make_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@226 | 490 | { |
paul@226 | 491 | int i; |
paul@226 | 492 | const char *path; |
paul@226 | 493 | ext2_ino_t ino; |
paul@226 | 494 | |
paul@226 | 495 | /* Make each directory component in the given pathname. */ |
paul@226 | 496 | |
paul@226 | 497 | for (i = 0; i < argc; i++) |
paul@226 | 498 | { |
paul@226 | 499 | path = argv[i]; |
paul@226 | 500 | |
paul@226 | 501 | /* Search for the remaining components. */ |
paul@226 | 502 | |
paul@257 | 503 | if ((!*path) || !image_resolve_by_path(fs, &path, &ino)) |
paul@246 | 504 | { |
paul@246 | 505 | fprintf(stderr, "Path exists: %s\n", argv[i]); |
paul@246 | 506 | return 1; |
paul@246 | 507 | } |
paul@226 | 508 | |
paul@226 | 509 | /* From the first unrecognised component, make the remaining |
paul@226 | 510 | directories. */ |
paul@226 | 511 | |
paul@244 | 512 | if (image_make_dirs(fs, &path, ino, |
paul@244 | 513 | 0777 & ~md.mask, |
paul@244 | 514 | md.have_uid ? md.uid : 0, |
paul@244 | 515 | md.have_gid ? md.gid : 0)) |
paul@246 | 516 | { |
paul@246 | 517 | fprintf(stderr, "Failed to make directory: %s\n", argv[i]); |
paul@226 | 518 | return 1; |
paul@246 | 519 | } |
paul@226 | 520 | } |
paul@226 | 521 | |
paul@226 | 522 | return 0; |
paul@226 | 523 | } |
paul@226 | 524 | |
paul@255 | 525 | /* Remove objects from the filesystem image. */ |
paul@244 | 526 | |
paul@255 | 527 | int _remove(ext2_filsys fs, int argc, char *argv[], int dir_only) |
paul@244 | 528 | { |
paul@244 | 529 | int i; |
paul@244 | 530 | const char *path; |
paul@244 | 531 | ext2_ino_t ino; |
paul@244 | 532 | |
paul@244 | 533 | /* Remove each directory with the given pathname. */ |
paul@244 | 534 | |
paul@244 | 535 | for (i = 0; i < argc; i++) |
paul@244 | 536 | { |
paul@244 | 537 | path = argv[i]; |
paul@246 | 538 | |
paul@246 | 539 | /* Detect missing objects. */ |
paul@244 | 540 | |
paul@258 | 541 | if ((!*path) || image_find_by_path(fs, path, &ino)) |
paul@246 | 542 | { |
paul@246 | 543 | fprintf(stderr, "Not found: %s\n", path); |
paul@246 | 544 | return 1; |
paul@246 | 545 | } |
paul@244 | 546 | |
paul@255 | 547 | /* Insist on a directory if specified. */ |
paul@255 | 548 | |
paul@255 | 549 | if (dir_only) |
paul@255 | 550 | { |
paul@258 | 551 | if (!image_isdir_by_inode(fs, ino)) |
paul@255 | 552 | { |
paul@255 | 553 | fprintf(stderr, "Not a directory: %s\n", path); |
paul@255 | 554 | return 1; |
paul@255 | 555 | } |
paul@255 | 556 | |
paul@255 | 557 | /* Test for an empty directory. */ |
paul@244 | 558 | |
paul@258 | 559 | if (image_dir_empty_by_inode(fs, ino)) |
paul@255 | 560 | { |
paul@255 | 561 | fprintf(stderr, "Directory not empty: %s\n", path); |
paul@255 | 562 | return 1; |
paul@255 | 563 | } |
paul@255 | 564 | } |
paul@255 | 565 | |
paul@255 | 566 | /* Otherwise, insist on a non-directory. */ |
paul@255 | 567 | |
paul@258 | 568 | else if (image_isdir_by_inode(fs, ino)) |
paul@246 | 569 | { |
paul@255 | 570 | fprintf(stderr, "Cannot remove a directory: %s\n", path); |
paul@244 | 571 | return 1; |
paul@246 | 572 | } |
paul@244 | 573 | |
paul@255 | 574 | /* Unlink the object. */ |
paul@244 | 575 | |
paul@255 | 576 | if (image_unlink_by_path(fs, path)) |
paul@246 | 577 | { |
paul@255 | 578 | fprintf(stderr, "Could not unlink object: %s\n", path); |
paul@244 | 579 | return 1; |
paul@246 | 580 | } |
paul@244 | 581 | |
paul@255 | 582 | /* Remove the object. */ |
paul@249 | 583 | |
paul@249 | 584 | if (image_remove_by_inode(fs, ino)) |
paul@249 | 585 | { |
paul@255 | 586 | fprintf(stderr, "Could not remove object: %s\n", path); |
paul@249 | 587 | return 1; |
paul@249 | 588 | } |
paul@244 | 589 | } |
paul@244 | 590 | |
paul@244 | 591 | return 0; |
paul@244 | 592 | } |
paul@244 | 593 | |
paul@255 | 594 | /* Remove directories from the filesystem image. */ |
paul@255 | 595 | |
paul@255 | 596 | int remove_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@255 | 597 | { |
paul@255 | 598 | return _remove(fs, argc, argv, 1); |
paul@255 | 599 | } |
paul@255 | 600 | |
paul@255 | 601 | /* Remove non-directories from the filesystem image. */ |
paul@255 | 602 | |
paul@255 | 603 | int remove_non_dirs(ext2_filsys fs, int argc, char *argv[]) |
paul@255 | 604 | { |
paul@255 | 605 | return _remove(fs, argc, argv, 0); |
paul@255 | 606 | } |
paul@255 | 607 | |
paul@262 | 608 | /* Read operations from a script file. */ |
paul@262 | 609 | |
paul@262 | 610 | enum op_results |
paul@262 | 611 | { |
paul@262 | 612 | OP_SUCCESS = 0, |
paul@262 | 613 | OP_FAILED = 1, |
paul@262 | 614 | OP_UNKNOWN = 2, |
paul@262 | 615 | }; |
paul@262 | 616 | |
paul@262 | 617 | int handle_op_result(const char *operation, enum op_results op_result) |
paul@262 | 618 | { |
paul@262 | 619 | if (op_result == OP_UNKNOWN) |
paul@262 | 620 | { |
paul@262 | 621 | fprintf(stderr, "Operation not recognised: %s\n", operation); |
paul@262 | 622 | return 1; |
paul@262 | 623 | } |
paul@262 | 624 | else if (op_result == OP_FAILED) |
paul@262 | 625 | { |
paul@262 | 626 | fprintf(stderr, "Operation failed: %s\n", operation); |
paul@262 | 627 | return 1; |
paul@262 | 628 | } |
paul@262 | 629 | else |
paul@262 | 630 | return 0; |
paul@262 | 631 | } |
paul@262 | 632 | |
paul@262 | 633 | enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]); |
paul@262 | 634 | |
paul@262 | 635 | int run_script(ext2_filsys fs, int argc, char *argv[]) |
paul@262 | 636 | { |
paul@262 | 637 | FILE *fp; |
paul@262 | 638 | char buffer[BUFSIZE]; |
paul@262 | 639 | struct read_line_state state; |
paul@262 | 640 | enum op_results op_result; |
paul@262 | 641 | int num_args; |
paul@262 | 642 | char *args[MAX_ARGS]; |
paul@262 | 643 | int i; |
paul@262 | 644 | |
paul@262 | 645 | for (i = 0; i < argc; i++) |
paul@262 | 646 | { |
paul@262 | 647 | fp = fopen(argv[i], "r"); |
paul@262 | 648 | |
paul@262 | 649 | state.buffer = buffer; |
paul@262 | 650 | state.start = buffer; |
paul@262 | 651 | state.end = buffer; |
paul@263 | 652 | state.buffer_size = BUFSIZE; |
paul@262 | 653 | state.remaining = BUFSIZE - 1; |
paul@262 | 654 | |
paul@262 | 655 | while (read_line(fp, &state) != NULL) |
paul@262 | 656 | { |
paul@262 | 657 | parse_line(state.start, state.eolp, &num_args, args, MAX_ARGS); |
paul@262 | 658 | |
paul@262 | 659 | if (num_args > 1) |
paul@262 | 660 | { |
paul@262 | 661 | op_result = run_operation(fs, args[0], num_args - 1, &args[1]); |
paul@262 | 662 | |
paul@262 | 663 | if (handle_op_result(args[0], op_result)) |
paul@262 | 664 | return 1; |
paul@262 | 665 | } |
paul@262 | 666 | |
paul@262 | 667 | state.start = state.eolp + 1; |
paul@262 | 668 | } |
paul@262 | 669 | |
paul@262 | 670 | fclose(fp); |
paul@262 | 671 | } |
paul@262 | 672 | |
paul@262 | 673 | return 0; |
paul@262 | 674 | } |
paul@262 | 675 | |
paul@264 | 676 | /* Show statistics for files and directories. */ |
paul@264 | 677 | |
paul@264 | 678 | int stat_objects(ext2_filsys fs, int argc, char *argv[]) |
paul@264 | 679 | { |
paul@264 | 680 | int i; |
paul@264 | 681 | const char *path; |
paul@264 | 682 | ext2_ino_t ino; |
paul@264 | 683 | struct stat st; |
paul@264 | 684 | |
paul@264 | 685 | for (i = 0; i < argc; i++) |
paul@264 | 686 | { |
paul@264 | 687 | path = argv[i]; |
paul@264 | 688 | |
paul@264 | 689 | /* Detect missing objects. */ |
paul@264 | 690 | |
paul@264 | 691 | if (image_find_by_path(fs, path, &ino)) |
paul@264 | 692 | { |
paul@264 | 693 | fprintf(stderr, "Not found: %s\n", path); |
paul@264 | 694 | return 1; |
paul@264 | 695 | } |
paul@264 | 696 | |
paul@264 | 697 | /* Even though the statistics could be read directly out of ext2 data |
paul@264 | 698 | structures, it is convenient to use the standard stat structure. */ |
paul@264 | 699 | |
paul@264 | 700 | if (image_stat_inode(fs, ino, &st)) |
paul@264 | 701 | { |
paul@264 | 702 | fprintf(stderr, "Cannot stat object: %s\n", path); |
paul@264 | 703 | return 1; |
paul@264 | 704 | } |
paul@264 | 705 | |
paul@264 | 706 | /* Terse stat output: |
paul@264 | 707 | %n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C */ |
paul@264 | 708 | |
paul@264 | 709 | printf("%s %ld %ld %x %d %d ", |
paul@264 | 710 | path, st.st_size, st.st_blocks, st.st_mode, st.st_uid, st.st_gid); |
paul@264 | 711 | |
paul@264 | 712 | printf("%d %d %d %x %x ", |
paul@264 | 713 | st.st_dev, st.st_ino, st.st_nlink, |
paul@264 | 714 | major(st.st_rdev), minor(st.st_rdev)); |
paul@264 | 715 | |
paul@264 | 716 | printf("%d %d %d ", |
paul@264 | 717 | st.st_atim, st.st_mtim, st.st_ctim); |
paul@264 | 718 | |
paul@264 | 719 | /* NOTE: Arbitrary values: |
paul@264 | 720 | %W (creation time) given as 0 |
paul@264 | 721 | %o (I/O transfer size hint) given as 0 |
paul@264 | 722 | %C (SELinux security context) given as empty string */ |
paul@264 | 723 | |
paul@264 | 724 | printf("%d %d %s\n", |
paul@264 | 725 | 0, 0, ""); |
paul@264 | 726 | } |
paul@264 | 727 | |
paul@264 | 728 | return 0; |
paul@264 | 729 | } |
paul@262 | 730 | |
paul@244 | 731 | |
paul@244 | 732 | |
paul@244 | 733 | /* Help message. */ |
paul@241 | 734 | |
paul@241 | 735 | char help_text[] = "\ |
paul@241 | 736 | Usage: %s [ <options> ] <image file> <operation> <filename>...\n\ |
paul@241 | 737 | \n\ |
paul@241 | 738 | File ownership options:\n\ |
paul@241 | 739 | \n\ |
paul@241 | 740 | -g GID Set group identifier for new files\n\ |
paul@241 | 741 | -u UID Set user identifier for new files\n\ |
paul@241 | 742 | \n\ |
paul@241 | 743 | File permission options:\n\ |
paul@241 | 744 | \n\ |
paul@241 | 745 | -m MASK Set mode/permissions mask for new directories\n\ |
paul@244 | 746 | \n\ |
paul@262 | 747 | Transfer operations:\n\ |
paul@244 | 748 | \n\ |
paul@244 | 749 | copy-in Copy files into a directory within the image\n\ |
paul@244 | 750 | copy-out Copy files from the image into a directory\n\ |
paul@262 | 751 | \n\ |
paul@262 | 752 | Image operations:\n\ |
paul@262 | 753 | \n\ |
paul@244 | 754 | ls List files and directories within the image\n\ |
paul@244 | 755 | mkdir Make directories within the image\n\ |
paul@255 | 756 | rm Remove non-directory objects from the image\n\ |
paul@244 | 757 | rmdir Remove directories from the image\n\ |
paul@264 | 758 | stat Show statistics for files and directories\n\ |
paul@262 | 759 | \n\ |
paul@262 | 760 | Script operations:\n\ |
paul@262 | 761 | \n\ |
paul@262 | 762 | script Read operations from a script file\n\ |
paul@241 | 763 | "; |
paul@241 | 764 | |
paul@244 | 765 | /* Operations exposed by the program. */ |
paul@244 | 766 | |
paul@244 | 767 | struct operation |
paul@244 | 768 | { |
paul@244 | 769 | const char *name; |
paul@244 | 770 | int (*fn)(ext2_filsys, int, char *[]); |
paul@244 | 771 | }; |
paul@244 | 772 | |
paul@244 | 773 | static struct operation operations[] = { |
paul@244 | 774 | {"copy-in", copy_in}, |
paul@244 | 775 | {"copy-out", copy_out}, |
paul@244 | 776 | {"ls", list}, |
paul@244 | 777 | {"mkdir", make_dirs}, |
paul@255 | 778 | {"rm", remove_non_dirs}, |
paul@244 | 779 | {"rmdir", remove_dirs}, |
paul@262 | 780 | {"script", run_script}, |
paul@264 | 781 | {"stat", stat_objects}, |
paul@244 | 782 | {NULL, NULL}, |
paul@244 | 783 | }; |
paul@244 | 784 | |
paul@262 | 785 | /* Invocation of operations. */ |
paul@262 | 786 | |
paul@262 | 787 | enum op_results run_operation(ext2_filsys fs, const char *operation, int argc, char *argv[]) |
paul@262 | 788 | { |
paul@262 | 789 | struct operation *op; |
paul@262 | 790 | int exitcode; |
paul@262 | 791 | |
paul@262 | 792 | for (op = &operations[0]; op->name != NULL; op++) |
paul@262 | 793 | { |
paul@262 | 794 | if (!strcmp(operation, op->name)) |
paul@262 | 795 | { |
paul@262 | 796 | exitcode = op->fn(fs, argc, argv); |
paul@262 | 797 | if (exitcode) |
paul@262 | 798 | return OP_FAILED; |
paul@262 | 799 | break; |
paul@262 | 800 | } |
paul@262 | 801 | } |
paul@262 | 802 | |
paul@262 | 803 | if (op->name == NULL) |
paul@262 | 804 | return OP_UNKNOWN; |
paul@262 | 805 | |
paul@262 | 806 | return OP_SUCCESS; |
paul@262 | 807 | } |
paul@262 | 808 | |
paul@244 | 809 | /* Main program. */ |
paul@244 | 810 | |
paul@226 | 811 | int main(int argc, char *argv[]) |
paul@226 | 812 | { |
paul@226 | 813 | int flags = EXT2_FLAG_RW; // | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
paul@226 | 814 | ext2_filsys fs = NULL; |
paul@226 | 815 | errcode_t retval; |
paul@226 | 816 | int exitcode = 0; |
paul@226 | 817 | |
paul@241 | 818 | /* Program argument details. */ |
paul@241 | 819 | |
paul@241 | 820 | char **args; |
paul@262 | 821 | char *fsname, *filename; |
paul@241 | 822 | int num_args; |
paul@262 | 823 | enum op_results op_result; |
paul@241 | 824 | |
paul@241 | 825 | /* Parse program options and initialise the argument details. */ |
paul@241 | 826 | |
paul@241 | 827 | if (parse_options(argc, argv)) |
paul@241 | 828 | return 1; |
paul@241 | 829 | |
paul@241 | 830 | args = &argv[optind]; |
paul@241 | 831 | num_args = argc - optind; |
paul@241 | 832 | |
paul@241 | 833 | if (num_args < 3) |
paul@226 | 834 | { |
paul@246 | 835 | fprintf(stderr, help_text, argv[0]); |
paul@226 | 836 | return 1; |
paul@226 | 837 | } |
paul@226 | 838 | |
paul@241 | 839 | /* Open the filesystem image using the POSIX file access mechanism. */ |
paul@226 | 840 | |
paul@241 | 841 | fsname = args[0]; |
paul@226 | 842 | |
paul@226 | 843 | retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); |
paul@226 | 844 | if (retval) |
paul@226 | 845 | { |
paul@246 | 846 | fprintf(stderr, "Could not open filesystem: %s\n", fsname); |
paul@226 | 847 | return 1; |
paul@226 | 848 | } |
paul@226 | 849 | |
paul@226 | 850 | //fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
paul@226 | 851 | |
paul@226 | 852 | retval = ext2fs_read_bitmaps(fs); |
paul@226 | 853 | if (retval) |
paul@226 | 854 | { |
paul@246 | 855 | fprintf(stderr, "Could not read bitmaps: %s\n", fsname); |
paul@226 | 856 | return 1; |
paul@226 | 857 | } |
paul@226 | 858 | |
paul@226 | 859 | /* Perform the requested operation. */ |
paul@226 | 860 | |
paul@262 | 861 | op_result = run_operation(fs, args[1], num_args - 2, &args[2]); |
paul@262 | 862 | exitcode = handle_op_result(args[1], op_result); |
paul@226 | 863 | |
paul@226 | 864 | /* Close the filesystem image. */ |
paul@226 | 865 | |
paul@226 | 866 | retval = ext2fs_flush(fs); |
paul@226 | 867 | if (retval) |
paul@226 | 868 | { |
paul@246 | 869 | fprintf(stderr, "Error flushing filesystem: %s\n", fsname); |
paul@226 | 870 | exitcode = 1; |
paul@226 | 871 | } |
paul@226 | 872 | |
paul@226 | 873 | retval = ext2fs_close(fs); |
paul@226 | 874 | if (retval) |
paul@226 | 875 | { |
paul@246 | 876 | fprintf(stderr, "Error closing filesystem: %s\n", fsname); |
paul@226 | 877 | exitcode = 1; |
paul@226 | 878 | } |
paul@226 | 879 | |
paul@226 | 880 | ext2fs_free(fs); |
paul@226 | 881 | return exitcode; |
paul@226 | 882 | } |
paul@241 | 883 | |
paul@241 | 884 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@241 | 885 | */ |