# HG changeset patch # User Paul Boddie # Date 1644876446 -3600 # Node ID c609cb01213c7e809fa3554903ec367838011b8a # Parent add33265bcf4fd5608b069b3b925eaff62ed89d8 Simplified slightly and introduced consistent output message format. diff -r add33265bcf4 -r c609cb01213c libe2access/host/e2access.c --- a/libe2access/host/e2access.c Mon Feb 14 22:21:03 2022 +0100 +++ b/libe2access/host/e2access.c Mon Feb 14 23:07:26 2022 +0100 @@ -1,7 +1,7 @@ /* * Access a filesystem. * - * Copyright (C) 2019 Paul Boddie + * Copyright (C) 2019, 2022 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -54,6 +54,8 @@ struct metadata md; +/* Parse program options. */ + static int parse_options(int argc, char *argv[]) { int opt; @@ -81,7 +83,7 @@ break; default: - fprintf(stderr, "Option %s not recognised.\n", argv[optind]); + fprintf(stderr, "Option not recognised: %s\n", argv[optind]); return -1; } } @@ -273,7 +275,7 @@ if (!_image_isdir(fs, ino_target) || !path_is_leafname(target_remaining)) { - printf("Target %s not found.\n", target); + fprintf(stderr, "Target not found: %s\n", target); return 1; } @@ -292,13 +294,13 @@ { if (argc > 2) { - printf("Target %s can only be a file when copying a single file.\n", target); + fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); return 1; } } else if (!_image_isdir(fs, ino_target)) { - printf("Target %s is not a directory.\n", target); + fprintf(stderr, "Target is not a directory: %s\n", target); return 1; } @@ -320,14 +322,17 @@ { if (!target_is_new) { - printf("Target %s cannot be created since it already exists.\n", target); + fprintf(stderr, "Target cannot be created since it already exists: %s\n", target); return 1; } /* Obtain the metadata. */ if (lstat(argv[i], &st)) + { + fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]); return 1; + } retval = image_make_dir(fs, ino_target, basename, st.st_mode & ~md.mask, @@ -337,7 +342,7 @@ if (retval) { - printf("Failed to create directory %s (%d).\n", argv[i], retval); + fprintf(stderr, "Failed to create directory: %s\n", argv[i]); return 1; } } @@ -355,7 +360,10 @@ /* Populate the inode details. */ if (lstat(argv[i], &st)) + { + fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]); return 1; + } retval = image_create_file(fs, ino_target, basename, st.st_mode & ~md.mask, @@ -364,7 +372,7 @@ &ino_file); if (retval) { - printf("Failed to create file %s (%d).\n", argv[i], retval); + fprintf(stderr, "Failed to create file: %s\n", argv[i]); return 1; } @@ -375,7 +383,7 @@ if (copy_file_in(argv[i], fs, ino_file, flags)) { - printf("Failed to write to %s.\n", argv[i]); + fprintf(stderr, "Failed to write to file: %s\n", argv[i]); return 1; } } @@ -413,7 +421,7 @@ target_is_file = 1; else { - printf("Target %s is not a directory.\n", target); + fprintf(stderr, "Target is not a directory: %s\n", target); return 1; } @@ -421,7 +429,7 @@ if (argc > 2) { - printf("Target %s can only be a file when copying a single file.\n", target); + fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target); return 1; } } @@ -434,7 +442,7 @@ { if (!image_isfile(fs, argv[i])) { - printf("Source %s is not a file.\n", argv[i]); + fprintf(stderr, "Source is not a file: %s\n", argv[i]); return 1; } } @@ -446,11 +454,14 @@ path = argv[i]; if (image_find_path(fs, &path, &ino_file)) + { + fprintf(stderr, "Failed to find file: %s\n", argv[i]); return 1; + } if (copy_file_out(target, argv[i], target_is_file, fs, ino_file)) { - printf("Failed to read from %s.\n", argv[i]); + fprintf(stderr, "Failed to read from file: %s\n", argv[i]); return 1; } @@ -486,7 +497,10 @@ data.fs = fs; if (image_list_dir(fs, path, _list_dir_proc, &data)) + { + fprintf(stderr, "Failed to list directory: %s\n", path); return 1; + } } return 0; @@ -505,13 +519,14 @@ for (i = 0; i < argc; i++) { path = argv[i]; - if (!*path) - continue; /* Search for the remaining components. */ - if (!image_find_path(fs, &path, &ino)) - continue; + if ((!*path) || !image_find_path(fs, &path, &ino)) + { + fprintf(stderr, "Path exists: %s\n", argv[i]); + return 1; + } /* From the first unrecognised component, make the remaining directories. */ @@ -520,7 +535,10 @@ 0777 & ~md.mask, md.have_uid ? md.uid : 0, md.have_gid ? md.gid : 0)) + { + fprintf(stderr, "Failed to make directory: %s\n", argv[i]); return 1; + } } return 0; @@ -539,28 +557,38 @@ for (i = 0; i < argc; i++) { path = argv[i]; - if (!*path) - continue; + + /* Detect missing objects. */ - /* Ignore missing objects. */ - - if (!image_exists(fs, path)) - continue; + if ((!*path) || !image_exists(fs, path)) + { + fprintf(stderr, "Not found: %s\n", path); + return 1; + } /* Insist on a directory. */ if (!image_isdir(fs, path)) + { + fprintf(stderr, "Not a directory: %s\n", path); return 1; + } /* Remove the directory. */ if (image_remove_by_path(fs, path)) + { + fprintf(stderr, "Could not remove directory: %s\n", path); return 1; + } /* Unlink the directory. */ if (image_unlink_by_path(fs, path)) + { + fprintf(stderr, "Could not unlink directory: %s\n", path); return 1; + } } return 0; @@ -634,7 +662,7 @@ if (num_args < 3) { - printf(help_text, argv[0]); + fprintf(stderr, help_text, argv[0]); return 1; } @@ -645,7 +673,7 @@ retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs); if (retval) { - printf("Could not open filesystem using %s\n", fsname); + fprintf(stderr, "Could not open filesystem: %s\n", fsname); return 1; } @@ -654,7 +682,7 @@ retval = ext2fs_read_bitmaps(fs); if (retval) { - printf("Could not read bitmaps from %s\n", fsname); + fprintf(stderr, "Could not read bitmaps: %s\n", fsname); return 1; } @@ -669,13 +697,15 @@ if (!strcmp(operation, op->name)) { exitcode = op->fn(fs, num_args, args); + if (exitcode) + fprintf(stderr, "Operation failed: %s\n", operation); break; } } if (op->name == NULL) { - printf("Operation %s is not recognised.\n", operation); + fprintf(stderr, "Operation not recognised: %s\n", operation); exitcode = 1; } @@ -684,14 +714,14 @@ retval = ext2fs_flush(fs); if (retval) { - printf("Error flushing filesystem in %s\n", fsname); + fprintf(stderr, "Error flushing filesystem: %s\n", fsname); exitcode = 1; } retval = ext2fs_close(fs); if (retval) { - printf("Error closing filesystem in %s\n", fsname); + fprintf(stderr, "Error closing filesystem: %s\n", fsname); exitcode = 1; }