1.1 --- a/libe2access/host/e2access.c Mon Feb 14 22:21:03 2022 +0100
1.2 +++ b/libe2access/host/e2access.c Mon Feb 14 23:07:26 2022 +0100
1.3 @@ -1,7 +1,7 @@
1.4 /*
1.5 * Access a filesystem.
1.6 *
1.7 - * Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk>
1.8 + * Copyright (C) 2019, 2022 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -54,6 +54,8 @@
1.13
1.14 struct metadata md;
1.15
1.16 +/* Parse program options. */
1.17 +
1.18 static int parse_options(int argc, char *argv[])
1.19 {
1.20 int opt;
1.21 @@ -81,7 +83,7 @@
1.22 break;
1.23
1.24 default:
1.25 - fprintf(stderr, "Option %s not recognised.\n", argv[optind]);
1.26 + fprintf(stderr, "Option not recognised: %s\n", argv[optind]);
1.27 return -1;
1.28 }
1.29 }
1.30 @@ -273,7 +275,7 @@
1.31
1.32 if (!_image_isdir(fs, ino_target) || !path_is_leafname(target_remaining))
1.33 {
1.34 - printf("Target %s not found.\n", target);
1.35 + fprintf(stderr, "Target not found: %s\n", target);
1.36 return 1;
1.37 }
1.38
1.39 @@ -292,13 +294,13 @@
1.40 {
1.41 if (argc > 2)
1.42 {
1.43 - printf("Target %s can only be a file when copying a single file.\n", target);
1.44 + fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target);
1.45 return 1;
1.46 }
1.47 }
1.48 else if (!_image_isdir(fs, ino_target))
1.49 {
1.50 - printf("Target %s is not a directory.\n", target);
1.51 + fprintf(stderr, "Target is not a directory: %s\n", target);
1.52 return 1;
1.53 }
1.54
1.55 @@ -320,14 +322,17 @@
1.56 {
1.57 if (!target_is_new)
1.58 {
1.59 - printf("Target %s cannot be created since it already exists.\n", target);
1.60 + fprintf(stderr, "Target cannot be created since it already exists: %s\n", target);
1.61 return 1;
1.62 }
1.63
1.64 /* Obtain the metadata. */
1.65
1.66 if (lstat(argv[i], &st))
1.67 + {
1.68 + fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]);
1.69 return 1;
1.70 + }
1.71
1.72 retval = image_make_dir(fs, ino_target, basename,
1.73 st.st_mode & ~md.mask,
1.74 @@ -337,7 +342,7 @@
1.75
1.76 if (retval)
1.77 {
1.78 - printf("Failed to create directory %s (%d).\n", argv[i], retval);
1.79 + fprintf(stderr, "Failed to create directory: %s\n", argv[i]);
1.80 return 1;
1.81 }
1.82 }
1.83 @@ -355,7 +360,10 @@
1.84 /* Populate the inode details. */
1.85
1.86 if (lstat(argv[i], &st))
1.87 + {
1.88 + fprintf(stderr, "Failed to read object metadata: %s\n", argv[i]);
1.89 return 1;
1.90 + }
1.91
1.92 retval = image_create_file(fs, ino_target, basename,
1.93 st.st_mode & ~md.mask,
1.94 @@ -364,7 +372,7 @@
1.95 &ino_file);
1.96 if (retval)
1.97 {
1.98 - printf("Failed to create file %s (%d).\n", argv[i], retval);
1.99 + fprintf(stderr, "Failed to create file: %s\n", argv[i]);
1.100 return 1;
1.101 }
1.102
1.103 @@ -375,7 +383,7 @@
1.104
1.105 if (copy_file_in(argv[i], fs, ino_file, flags))
1.106 {
1.107 - printf("Failed to write to %s.\n", argv[i]);
1.108 + fprintf(stderr, "Failed to write to file: %s\n", argv[i]);
1.109 return 1;
1.110 }
1.111 }
1.112 @@ -413,7 +421,7 @@
1.113 target_is_file = 1;
1.114 else
1.115 {
1.116 - printf("Target %s is not a directory.\n", target);
1.117 + fprintf(stderr, "Target is not a directory: %s\n", target);
1.118 return 1;
1.119 }
1.120
1.121 @@ -421,7 +429,7 @@
1.122
1.123 if (argc > 2)
1.124 {
1.125 - printf("Target %s can only be a file when copying a single file.\n", target);
1.126 + fprintf(stderr, "Target can only be a file when copying a single file: %s\n", target);
1.127 return 1;
1.128 }
1.129 }
1.130 @@ -434,7 +442,7 @@
1.131 {
1.132 if (!image_isfile(fs, argv[i]))
1.133 {
1.134 - printf("Source %s is not a file.\n", argv[i]);
1.135 + fprintf(stderr, "Source is not a file: %s\n", argv[i]);
1.136 return 1;
1.137 }
1.138 }
1.139 @@ -446,11 +454,14 @@
1.140 path = argv[i];
1.141
1.142 if (image_find_path(fs, &path, &ino_file))
1.143 + {
1.144 + fprintf(stderr, "Failed to find file: %s\n", argv[i]);
1.145 return 1;
1.146 + }
1.147
1.148 if (copy_file_out(target, argv[i], target_is_file, fs, ino_file))
1.149 {
1.150 - printf("Failed to read from %s.\n", argv[i]);
1.151 + fprintf(stderr, "Failed to read from file: %s\n", argv[i]);
1.152 return 1;
1.153 }
1.154
1.155 @@ -486,7 +497,10 @@
1.156 data.fs = fs;
1.157
1.158 if (image_list_dir(fs, path, _list_dir_proc, &data))
1.159 + {
1.160 + fprintf(stderr, "Failed to list directory: %s\n", path);
1.161 return 1;
1.162 + }
1.163 }
1.164
1.165 return 0;
1.166 @@ -505,13 +519,14 @@
1.167 for (i = 0; i < argc; i++)
1.168 {
1.169 path = argv[i];
1.170 - if (!*path)
1.171 - continue;
1.172
1.173 /* Search for the remaining components. */
1.174
1.175 - if (!image_find_path(fs, &path, &ino))
1.176 - continue;
1.177 + if ((!*path) || !image_find_path(fs, &path, &ino))
1.178 + {
1.179 + fprintf(stderr, "Path exists: %s\n", argv[i]);
1.180 + return 1;
1.181 + }
1.182
1.183 /* From the first unrecognised component, make the remaining
1.184 directories. */
1.185 @@ -520,7 +535,10 @@
1.186 0777 & ~md.mask,
1.187 md.have_uid ? md.uid : 0,
1.188 md.have_gid ? md.gid : 0))
1.189 + {
1.190 + fprintf(stderr, "Failed to make directory: %s\n", argv[i]);
1.191 return 1;
1.192 + }
1.193 }
1.194
1.195 return 0;
1.196 @@ -539,28 +557,38 @@
1.197 for (i = 0; i < argc; i++)
1.198 {
1.199 path = argv[i];
1.200 - if (!*path)
1.201 - continue;
1.202 +
1.203 + /* Detect missing objects. */
1.204
1.205 - /* Ignore missing objects. */
1.206 -
1.207 - if (!image_exists(fs, path))
1.208 - continue;
1.209 + if ((!*path) || !image_exists(fs, path))
1.210 + {
1.211 + fprintf(stderr, "Not found: %s\n", path);
1.212 + return 1;
1.213 + }
1.214
1.215 /* Insist on a directory. */
1.216
1.217 if (!image_isdir(fs, path))
1.218 + {
1.219 + fprintf(stderr, "Not a directory: %s\n", path);
1.220 return 1;
1.221 + }
1.222
1.223 /* Remove the directory. */
1.224
1.225 if (image_remove_by_path(fs, path))
1.226 + {
1.227 + fprintf(stderr, "Could not remove directory: %s\n", path);
1.228 return 1;
1.229 + }
1.230
1.231 /* Unlink the directory. */
1.232
1.233 if (image_unlink_by_path(fs, path))
1.234 + {
1.235 + fprintf(stderr, "Could not unlink directory: %s\n", path);
1.236 return 1;
1.237 + }
1.238 }
1.239
1.240 return 0;
1.241 @@ -634,7 +662,7 @@
1.242
1.243 if (num_args < 3)
1.244 {
1.245 - printf(help_text, argv[0]);
1.246 + fprintf(stderr, help_text, argv[0]);
1.247 return 1;
1.248 }
1.249
1.250 @@ -645,7 +673,7 @@
1.251 retval = ext2fs_open(fsname, flags, 0, 0, unix_io_manager, &fs);
1.252 if (retval)
1.253 {
1.254 - printf("Could not open filesystem using %s\n", fsname);
1.255 + fprintf(stderr, "Could not open filesystem: %s\n", fsname);
1.256 return 1;
1.257 }
1.258
1.259 @@ -654,7 +682,7 @@
1.260 retval = ext2fs_read_bitmaps(fs);
1.261 if (retval)
1.262 {
1.263 - printf("Could not read bitmaps from %s\n", fsname);
1.264 + fprintf(stderr, "Could not read bitmaps: %s\n", fsname);
1.265 return 1;
1.266 }
1.267
1.268 @@ -669,13 +697,15 @@
1.269 if (!strcmp(operation, op->name))
1.270 {
1.271 exitcode = op->fn(fs, num_args, args);
1.272 + if (exitcode)
1.273 + fprintf(stderr, "Operation failed: %s\n", operation);
1.274 break;
1.275 }
1.276 }
1.277
1.278 if (op->name == NULL)
1.279 {
1.280 - printf("Operation %s is not recognised.\n", operation);
1.281 + fprintf(stderr, "Operation not recognised: %s\n", operation);
1.282 exitcode = 1;
1.283 }
1.284
1.285 @@ -684,14 +714,14 @@
1.286 retval = ext2fs_flush(fs);
1.287 if (retval)
1.288 {
1.289 - printf("Error flushing filesystem in %s\n", fsname);
1.290 + fprintf(stderr, "Error flushing filesystem: %s\n", fsname);
1.291 exitcode = 1;
1.292 }
1.293
1.294 retval = ext2fs_close(fs);
1.295 if (retval)
1.296 {
1.297 - printf("Error closing filesystem in %s\n", fsname);
1.298 + fprintf(stderr, "Error closing filesystem: %s\n", fsname);
1.299 exitcode = 1;
1.300 }
1.301