| 1 | /* Generate a file containing some preset patterns.
|
|---|
| 2 | Print statistics for existing files.
|
|---|
| 3 |
|
|---|
| 4 | Copyright (C) 1995, 1996, 1997, 2001, 2003, 2004, 2005, 2006
|
|---|
| 5 | Free Software Foundation, Inc.
|
|---|
| 6 |
|
|---|
| 7 | François Pinard <[email protected]>, 1995.
|
|---|
| 8 | Sergey Poznyakoff <[email protected]>, 2004, 2005, 2006.
|
|---|
| 9 |
|
|---|
| 10 | This program is free software; you can redistribute it and/or modify
|
|---|
| 11 | it under the terms of the GNU General Public License as published by
|
|---|
| 12 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 13 | any later version.
|
|---|
| 14 |
|
|---|
| 15 | This program is distributed in the hope that it will be useful, but
|
|---|
| 16 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 18 | General Public License for more details.
|
|---|
| 19 |
|
|---|
| 20 | You should have received a copy of the GNU General Public License
|
|---|
| 21 | along with this program; if not, write to the Free Software Foundation,
|
|---|
| 22 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include <system.h>
|
|---|
| 26 | #include <signal.h>
|
|---|
| 27 | #include <stdarg.h>
|
|---|
| 28 | #include <argmatch.h>
|
|---|
| 29 | #include <argp.h>
|
|---|
| 30 | #include <argcv.h>
|
|---|
| 31 | #include <getdate.h>
|
|---|
| 32 | #include <setenv.h>
|
|---|
| 33 | #include <utimens.h>
|
|---|
| 34 | #include <inttostr.h>
|
|---|
| 35 | #define obstack_chunk_alloc malloc
|
|---|
| 36 | #define obstack_chunk_free free
|
|---|
| 37 | #include <obstack.h>
|
|---|
| 38 |
|
|---|
| 39 | #ifndef EXIT_SUCCESS
|
|---|
| 40 | # define EXIT_SUCCESS 0
|
|---|
| 41 | #endif
|
|---|
| 42 | #ifndef EXIT_FAILURE
|
|---|
| 43 | # define EXIT_FAILURE 1
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 | #if ! defined SIGCHLD && defined SIGCLD
|
|---|
| 47 | # define SIGCHLD SIGCLD
|
|---|
| 48 | #endif
|
|---|
| 49 |
|
|---|
| 50 | enum pattern
|
|---|
| 51 | {
|
|---|
| 52 | DEFAULT_PATTERN,
|
|---|
| 53 | ZEROS_PATTERN
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | /* The name this program was run with. */
|
|---|
| 57 | const char *program_name;
|
|---|
| 58 |
|
|---|
| 59 | /* Name of file to generate */
|
|---|
| 60 | static char *file_name;
|
|---|
| 61 |
|
|---|
| 62 | /* Name of the file-list file: */
|
|---|
| 63 | static char *files_from;
|
|---|
| 64 | static char filename_terminator = '\n';
|
|---|
| 65 |
|
|---|
| 66 | /* Length of file to generate. */
|
|---|
| 67 | static off_t file_length = 0;
|
|---|
| 68 | static off_t seek_offset = 0;
|
|---|
| 69 |
|
|---|
| 70 | /* Pattern to generate. */
|
|---|
| 71 | static enum pattern pattern = DEFAULT_PATTERN;
|
|---|
| 72 |
|
|---|
| 73 | /* Next checkpoint number */
|
|---|
| 74 | size_t checkpoint;
|
|---|
| 75 |
|
|---|
| 76 | enum genfile_mode
|
|---|
| 77 | {
|
|---|
| 78 | mode_generate,
|
|---|
| 79 | mode_sparse,
|
|---|
| 80 | mode_stat,
|
|---|
| 81 | mode_exec
|
|---|
| 82 | };
|
|---|
| 83 |
|
|---|
| 84 | enum genfile_mode mode = mode_generate;
|
|---|
| 85 |
|
|---|
| 86 | #define DEFAULT_STAT_FORMAT \
|
|---|
| 87 | "name,dev,ino,mode,nlink,uid,gid,size,blksize,blocks,atime,mtime,ctime"
|
|---|
| 88 |
|
|---|
| 89 | /* Format for --stat option */
|
|---|
| 90 | static char *stat_format = DEFAULT_STAT_FORMAT;
|
|---|
| 91 |
|
|---|
| 92 | /* Size of a block for sparse file */
|
|---|
| 93 | size_t block_size = 512;
|
|---|
| 94 |
|
|---|
| 95 | /* Block buffer for sparse file */
|
|---|
| 96 | char *buffer;
|
|---|
| 97 |
|
|---|
| 98 | /* Number of arguments and argument vector for mode == mode_exec */
|
|---|
| 99 | int exec_argc;
|
|---|
| 100 | char **exec_argv;
|
|---|
| 101 |
|
|---|
| 102 | /* Time for --touch option */
|
|---|
| 103 | struct timespec touch_time;
|
|---|
| 104 |
|
|---|
| 105 | /* Verbose mode */
|
|---|
| 106 | int verbose;
|
|---|
| 107 |
|
|---|
| 108 | const char *argp_program_version = "genfile (" PACKAGE ") " VERSION;
|
|---|
| 109 | const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
|
|---|
| 110 | static char doc[] = N_("genfile manipulates data files for GNU paxutils test suite.\n"
|
|---|
| 111 | "OPTIONS are:\n");
|
|---|
| 112 |
|
|---|
| 113 | #define OPT_CHECKPOINT 256
|
|---|
| 114 | #define OPT_TOUCH 257
|
|---|
| 115 | #define OPT_APPEND 258
|
|---|
| 116 | #define OPT_TRUNCATE 259
|
|---|
| 117 | #define OPT_EXEC 260
|
|---|
| 118 | #define OPT_DATE 261
|
|---|
| 119 | #define OPT_VERBOSE 262
|
|---|
| 120 | #define OPT_SEEK 263
|
|---|
| 121 |
|
|---|
| 122 | static struct argp_option options[] = {
|
|---|
| 123 | #define GRP 0
|
|---|
| 124 | {NULL, 0, NULL, 0,
|
|---|
| 125 | N_("File creation options:"), GRP},
|
|---|
| 126 | {"length", 'l', N_("SIZE"), 0,
|
|---|
| 127 | N_("Create file of the given SIZE"), GRP+1 },
|
|---|
| 128 | {"file", 'f', N_("NAME"), 0,
|
|---|
| 129 | N_("Write to file NAME, instead of standard output"), GRP+1},
|
|---|
| 130 | {"files-from", 'T', N_("FILE"), 0,
|
|---|
| 131 | N_("Read file names from FILE"), GRP+1},
|
|---|
| 132 | {"null", '0', NULL, 0,
|
|---|
| 133 | N_("-T reads null-terminated names"), GRP+1},
|
|---|
| 134 | {"pattern", 'p', N_("PATTERN"), 0,
|
|---|
| 135 | N_("Fill the file with the given PATTERN. PATTERN is 'default' or 'zeros'"),
|
|---|
| 136 | GRP+1 },
|
|---|
| 137 | {"block-size", 'b', N_("SIZE"), 0,
|
|---|
| 138 | N_("Size of a block for sparse file"), GRP+1},
|
|---|
| 139 | {"sparse", 's', NULL, 0,
|
|---|
| 140 | N_("Generate sparse file. Rest of the command line gives the file map."),
|
|---|
| 141 | GRP+1 },
|
|---|
| 142 | {"seek", OPT_SEEK, N_("OFFSET"), 0,
|
|---|
| 143 | N_("Seek to the given offset before writing data"),
|
|---|
| 144 | GRP+1 },
|
|---|
| 145 |
|
|---|
| 146 | #undef GRP
|
|---|
| 147 | #define GRP 10
|
|---|
| 148 | {NULL, 0, NULL, 0,
|
|---|
| 149 | N_("File statistics options:"), GRP},
|
|---|
| 150 |
|
|---|
| 151 | {"stat", 'S', N_("FORMAT"), OPTION_ARG_OPTIONAL,
|
|---|
| 152 | N_("Print contents of struct stat for each given file. Default FORMAT is: ")
|
|---|
| 153 | DEFAULT_STAT_FORMAT,
|
|---|
| 154 | GRP+1 },
|
|---|
| 155 |
|
|---|
| 156 | #undef GRP
|
|---|
| 157 | #define GRP 20
|
|---|
| 158 | {NULL, 0, NULL, 0,
|
|---|
| 159 | N_("Synchronous execution options:"), GRP},
|
|---|
| 160 |
|
|---|
| 161 | {"run", 'r', N_("COMMAND"), 0,
|
|---|
| 162 | N_("Execute given COMMAND. Useful with --checkpoint and one of --cut, --append, --touch"),
|
|---|
| 163 | GRP+1 },
|
|---|
| 164 | {"checkpoint", OPT_CHECKPOINT, N_("NUMBER"), 0,
|
|---|
| 165 | N_("Perform given action (see below) upon reaching checkpoint NUMBER"),
|
|---|
| 166 | GRP+1 },
|
|---|
| 167 | {"date", OPT_DATE, N_("STRING"), 0,
|
|---|
| 168 | N_("Set date for next --touch option"),
|
|---|
| 169 | GRP+1 },
|
|---|
| 170 | {"verbose", OPT_VERBOSE, NULL, 0,
|
|---|
| 171 | N_("Display executed checkpoints and exit status of COMMAND"),
|
|---|
| 172 | GRP+1 },
|
|---|
| 173 | #undef GRP
|
|---|
| 174 | #define GRP 30
|
|---|
| 175 | {NULL, 0, NULL, 0,
|
|---|
| 176 | N_("Synchronous execution actions. These are executed when checkpoint number given by --checkpoint option is reached."), GRP},
|
|---|
| 177 |
|
|---|
| 178 | {"cut", OPT_TRUNCATE, N_("FILE"), 0,
|
|---|
| 179 | N_("Truncate FILE to the size specified by previous --length option (or 0, if it is not given)"),
|
|---|
| 180 | GRP+1 },
|
|---|
| 181 | {"truncate", 0, NULL, OPTION_ALIAS, NULL, GRP+1 },
|
|---|
| 182 | {"append", OPT_APPEND, N_("FILE"), 0,
|
|---|
| 183 | N_("Append SIZE bytes to FILE. SIZE is given by previous --length option."),
|
|---|
| 184 | GRP+1 },
|
|---|
| 185 | {"touch", OPT_TOUCH, N_("FILE"), 0,
|
|---|
| 186 | N_("Update the access and modification times of FILE"),
|
|---|
| 187 | GRP+1 },
|
|---|
| 188 | {"exec", OPT_EXEC, N_("COMMAND"), 0,
|
|---|
| 189 | N_("Execute COMMAND"),
|
|---|
| 190 | GRP+1 },
|
|---|
| 191 | #undef GRP
|
|---|
| 192 | { NULL, }
|
|---|
| 193 | };
|
|---|
| 194 |
|
|---|
| 195 | static char const * const pattern_args[] = { "default", "zeros", 0 };
|
|---|
| 196 | static enum pattern const pattern_types[] = {DEFAULT_PATTERN, ZEROS_PATTERN};
|
|---|
| 197 |
|
|---|
| 198 | static int
|
|---|
| 199 | xlat_suffix (off_t *vp, const char *p)
|
|---|
| 200 | {
|
|---|
| 201 | off_t val = *vp;
|
|---|
| 202 |
|
|---|
| 203 | if (p[1])
|
|---|
| 204 | return 1;
|
|---|
| 205 | switch (p[0])
|
|---|
| 206 | {
|
|---|
| 207 | case 'g':
|
|---|
| 208 | case 'G':
|
|---|
| 209 | *vp *= 1024;
|
|---|
| 210 |
|
|---|
| 211 | case 'm':
|
|---|
| 212 | case 'M':
|
|---|
| 213 | *vp *= 1024;
|
|---|
| 214 |
|
|---|
| 215 | case 'k':
|
|---|
| 216 | case 'K':
|
|---|
| 217 | *vp *= 1024;
|
|---|
| 218 | break;
|
|---|
| 219 |
|
|---|
| 220 | default:
|
|---|
| 221 | return 1;
|
|---|
| 222 | }
|
|---|
| 223 | return *vp <= val;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | static off_t
|
|---|
| 227 | get_size (const char *str, int allow_zero)
|
|---|
| 228 | {
|
|---|
| 229 | const char *p;
|
|---|
| 230 | off_t v = 0;
|
|---|
| 231 |
|
|---|
| 232 | for (p = str; *p; p++)
|
|---|
| 233 | {
|
|---|
| 234 | int digit = *p - '0';
|
|---|
| 235 | off_t x = v * 10;
|
|---|
| 236 | if (9 < (unsigned) digit)
|
|---|
| 237 | {
|
|---|
| 238 | if (xlat_suffix (&v, p))
|
|---|
| 239 | error (EXIT_FAILURE, 0, _("Invalid size: %s"), str);
|
|---|
| 240 | else
|
|---|
| 241 | break;
|
|---|
| 242 | }
|
|---|
| 243 | else if (x / 10 != v)
|
|---|
| 244 | error (EXIT_FAILURE, 0, _("Number out of allowed range: %s"), str);
|
|---|
| 245 | v = x + digit;
|
|---|
| 246 | if (v < 0)
|
|---|
| 247 | error (EXIT_FAILURE, 0, _("Negative size: %s"), str);
|
|---|
| 248 | }
|
|---|
| 249 | return v;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | void
|
|---|
| 253 | verify_file (char *file_name)
|
|---|
| 254 | {
|
|---|
| 255 | if (file_name)
|
|---|
| 256 | {
|
|---|
| 257 | struct stat st;
|
|---|
| 258 |
|
|---|
| 259 | if (stat (file_name, &st))
|
|---|
| 260 | error (0, errno, _("stat(%s) failed"), file_name);
|
|---|
| 261 |
|
|---|
| 262 | if (st.st_size != file_length + seek_offset)
|
|---|
| 263 | {
|
|---|
| 264 | printf ("%lu %lu\n", (unsigned long)st.st_size , (unsigned long)file_length);
|
|---|
| 265 | exit (1);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | if (mode == mode_sparse && !ST_IS_SPARSE (st))
|
|---|
| 269 | exit (1);
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | struct action
|
|---|
| 274 | {
|
|---|
| 275 | struct action *next;
|
|---|
| 276 | size_t checkpoint;
|
|---|
| 277 | int action;
|
|---|
| 278 | char *name;
|
|---|
| 279 | off_t size;
|
|---|
| 280 | enum pattern pattern;
|
|---|
| 281 | struct timespec ts;
|
|---|
| 282 | };
|
|---|
| 283 |
|
|---|
| 284 | static struct action *action_list;
|
|---|
| 285 |
|
|---|
| 286 | void
|
|---|
| 287 | reg_action (int action, char *arg)
|
|---|
| 288 | {
|
|---|
| 289 | struct action *act = xmalloc (sizeof (*act));
|
|---|
| 290 | act->checkpoint = checkpoint;
|
|---|
| 291 | act->action = action;
|
|---|
| 292 | act->pattern = pattern;
|
|---|
| 293 | act->ts = touch_time;
|
|---|
| 294 | act->size = file_length;
|
|---|
| 295 | act->name = arg;
|
|---|
| 296 | act->next = action_list;
|
|---|
| 297 | action_list = act;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | static error_t
|
|---|
| 301 | parse_opt (int key, char *arg, struct argp_state *state)
|
|---|
| 302 | {
|
|---|
| 303 | switch (key)
|
|---|
| 304 | {
|
|---|
| 305 | case '0':
|
|---|
| 306 | filename_terminator = 0;
|
|---|
| 307 | break;
|
|---|
| 308 |
|
|---|
| 309 | case 'f':
|
|---|
| 310 | file_name = arg;
|
|---|
| 311 | break;
|
|---|
| 312 |
|
|---|
| 313 | case 'l':
|
|---|
| 314 | file_length = get_size (arg, 1);
|
|---|
| 315 | break;
|
|---|
| 316 |
|
|---|
| 317 | case 'p':
|
|---|
| 318 | pattern = XARGMATCH ("--pattern", arg, pattern_args, pattern_types);
|
|---|
| 319 | break;
|
|---|
| 320 |
|
|---|
| 321 | case 'b':
|
|---|
| 322 | block_size = get_size (arg, 0);
|
|---|
| 323 | break;
|
|---|
| 324 |
|
|---|
| 325 | case 's':
|
|---|
| 326 | mode = mode_sparse;
|
|---|
| 327 | break;
|
|---|
| 328 |
|
|---|
| 329 | case 'S':
|
|---|
| 330 | mode = mode_stat;
|
|---|
| 331 | if (arg)
|
|---|
| 332 | stat_format = arg;
|
|---|
| 333 | break;
|
|---|
| 334 |
|
|---|
| 335 | case 'r':
|
|---|
| 336 | mode = mode_exec;
|
|---|
| 337 | argcv_get (arg, "", NULL, &exec_argc, &exec_argv);
|
|---|
| 338 | break;
|
|---|
| 339 |
|
|---|
| 340 | case 'T':
|
|---|
| 341 | files_from = arg;
|
|---|
| 342 | break;
|
|---|
| 343 |
|
|---|
| 344 | case OPT_SEEK:
|
|---|
| 345 | seek_offset = get_size (arg, 0);
|
|---|
| 346 | break;
|
|---|
| 347 |
|
|---|
| 348 | case OPT_CHECKPOINT:
|
|---|
| 349 | {
|
|---|
| 350 | char *p;
|
|---|
| 351 |
|
|---|
| 352 | checkpoint = strtoul (arg, &p, 0);
|
|---|
| 353 | if (*p)
|
|---|
| 354 | argp_error (state, _("Error parsing number near `%s'"), p);
|
|---|
| 355 | }
|
|---|
| 356 | break;
|
|---|
| 357 |
|
|---|
| 358 | case OPT_DATE:
|
|---|
| 359 | if (!get_date (&touch_time, arg, NULL))
|
|---|
| 360 | argp_error (state, _("Unknown date format"));
|
|---|
| 361 | break;
|
|---|
| 362 |
|
|---|
| 363 | case OPT_APPEND:
|
|---|
| 364 | case OPT_TRUNCATE:
|
|---|
| 365 | case OPT_TOUCH:
|
|---|
| 366 | case OPT_EXEC:
|
|---|
| 367 | reg_action (key, arg);
|
|---|
| 368 | break;
|
|---|
| 369 |
|
|---|
| 370 | case OPT_VERBOSE:
|
|---|
| 371 | verbose++;
|
|---|
| 372 | break;
|
|---|
| 373 |
|
|---|
| 374 | default:
|
|---|
| 375 | return ARGP_ERR_UNKNOWN;
|
|---|
| 376 | }
|
|---|
| 377 | return 0;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | static struct argp argp = {
|
|---|
| 381 | options,
|
|---|
| 382 | parse_opt,
|
|---|
| 383 | N_("[ARGS...]"),
|
|---|
| 384 | doc,
|
|---|
| 385 | NULL,
|
|---|
| 386 | NULL,
|
|---|
| 387 | NULL
|
|---|
| 388 | };
|
|---|
| 389 |
|
|---|
| 390 | |
|---|
| 391 |
|
|---|
| 392 | void
|
|---|
| 393 | fill (FILE *fp, off_t length, enum pattern pattern)
|
|---|
| 394 | {
|
|---|
| 395 | off_t i;
|
|---|
| 396 |
|
|---|
| 397 | switch (pattern)
|
|---|
| 398 | {
|
|---|
| 399 | case DEFAULT_PATTERN:
|
|---|
| 400 | for (i = 0; i < length; i++)
|
|---|
| 401 | fputc (i & 255, fp);
|
|---|
| 402 | break;
|
|---|
| 403 |
|
|---|
| 404 | case ZEROS_PATTERN:
|
|---|
| 405 | for (i = 0; i < length; i++)
|
|---|
| 406 | fputc (0, fp);
|
|---|
| 407 | break;
|
|---|
| 408 | }
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | /* Generate Mode: usual files */
|
|---|
| 412 | static void
|
|---|
| 413 | generate_simple_file (char *filename)
|
|---|
| 414 | {
|
|---|
| 415 | FILE *fp;
|
|---|
| 416 |
|
|---|
| 417 | if (filename)
|
|---|
| 418 | {
|
|---|
| 419 | fp = fopen (filename, seek_offset ? "r+" : "w");
|
|---|
| 420 | if (!fp)
|
|---|
| 421 | error (EXIT_FAILURE, 0, _("cannot open `%s'"), filename);
|
|---|
| 422 | }
|
|---|
| 423 | else
|
|---|
| 424 | fp = stdout;
|
|---|
| 425 |
|
|---|
| 426 | if (fseeko (fp, seek_offset, 0))
|
|---|
| 427 | error (EXIT_FAILURE, 0, _("cannot seek: %s"), strerror (errno));
|
|---|
| 428 |
|
|---|
| 429 | fill (fp, file_length, pattern);
|
|---|
| 430 |
|
|---|
| 431 | fclose (fp);
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | /* A simplified version of the same function from tar */
|
|---|
| 435 | int
|
|---|
| 436 | read_name_from_file (FILE *fp, struct obstack *stk)
|
|---|
| 437 | {
|
|---|
| 438 | int c;
|
|---|
| 439 | size_t counter = 0;
|
|---|
| 440 |
|
|---|
| 441 | for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
|
|---|
| 442 | {
|
|---|
| 443 | if (c == 0)
|
|---|
| 444 | error (EXIT_FAILURE, 0, _("file name contains null character"));
|
|---|
| 445 | obstack_1grow (stk, c);
|
|---|
| 446 | counter++;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | obstack_1grow (stk, 0);
|
|---|
| 450 |
|
|---|
| 451 | return (counter == 0 && c == EOF);
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | void
|
|---|
| 455 | generate_files_from_list ()
|
|---|
| 456 | {
|
|---|
| 457 | FILE *fp = strcmp (files_from, "-") ? fopen (files_from, "r") : stdin;
|
|---|
| 458 | struct obstack stk;
|
|---|
| 459 |
|
|---|
| 460 | if (!fp)
|
|---|
| 461 | error (EXIT_FAILURE, errno, _("cannot open `%s'"), files_from);
|
|---|
| 462 |
|
|---|
| 463 | obstack_init (&stk);
|
|---|
| 464 | while (!read_name_from_file (fp, &stk))
|
|---|
| 465 | {
|
|---|
| 466 | char *name = obstack_finish (&stk);
|
|---|
| 467 | generate_simple_file (name);
|
|---|
| 468 | verify_file (name);
|
|---|
| 469 | obstack_free (&stk, name);
|
|---|
| 470 | }
|
|---|
| 471 | fclose (fp);
|
|---|
| 472 | obstack_free (&stk, NULL);
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | |
|---|
| 476 |
|
|---|
| 477 | /* Generate Mode: sparse files */
|
|---|
| 478 |
|
|---|
| 479 | static void
|
|---|
| 480 | mkhole (int fd, off_t displ)
|
|---|
| 481 | {
|
|---|
| 482 | if (lseek (fd, displ, SEEK_CUR) == -1)
|
|---|
| 483 | error (EXIT_FAILURE, errno, "lseek");
|
|---|
| 484 | ftruncate (fd, lseek (fd, 0, SEEK_CUR));
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | static void
|
|---|
| 488 | mksparse (int fd, off_t displ, char *marks)
|
|---|
| 489 | {
|
|---|
| 490 | if (lseek (fd, displ, SEEK_CUR) == -1)
|
|---|
| 491 | error (EXIT_FAILURE, errno, "lseek");
|
|---|
| 492 |
|
|---|
| 493 | for (; *marks; marks++)
|
|---|
| 494 | {
|
|---|
| 495 | memset (buffer, *marks, block_size);
|
|---|
| 496 | if (write (fd, buffer, block_size) != block_size)
|
|---|
| 497 | error (EXIT_FAILURE, errno, "write");
|
|---|
| 498 | }
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | static void
|
|---|
| 502 | generate_sparse_file (int argc, char **argv)
|
|---|
| 503 | {
|
|---|
| 504 | int i;
|
|---|
| 505 | int fd;
|
|---|
| 506 | int flags = O_CREAT|O_RDWR;
|
|---|
| 507 |
|
|---|
| 508 | if (!file_name)
|
|---|
| 509 | error (EXIT_FAILURE, 0,
|
|---|
| 510 | _("cannot generate sparse files on standard output, use --file option"));
|
|---|
| 511 | if (!seek_offset)
|
|---|
| 512 | flags |= O_TRUNC;
|
|---|
| 513 | fd = open (file_name, flags, 0644);
|
|---|
| 514 | if (fd < 0)
|
|---|
| 515 | error (EXIT_FAILURE, 0, _("cannot open `%s'"), file_name);
|
|---|
| 516 |
|
|---|
| 517 | buffer = xmalloc (block_size);
|
|---|
| 518 |
|
|---|
| 519 | file_length = 0;
|
|---|
| 520 |
|
|---|
| 521 | for (i = 0; i < argc; i += 2)
|
|---|
| 522 | {
|
|---|
| 523 | off_t displ = get_size (argv[i], 1);
|
|---|
| 524 | file_length += displ;
|
|---|
| 525 |
|
|---|
| 526 | if (i == argc-1)
|
|---|
| 527 | {
|
|---|
| 528 | mkhole (fd, displ);
|
|---|
| 529 | break;
|
|---|
| 530 | }
|
|---|
| 531 | else
|
|---|
| 532 | {
|
|---|
| 533 | file_length += block_size * strlen (argv[i+1]);
|
|---|
| 534 | mksparse (fd, displ, argv[i+1]);
|
|---|
| 535 | }
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | close (fd);
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | |
|---|
| 542 |
|
|---|
| 543 | /* Status Mode */
|
|---|
| 544 |
|
|---|
| 545 | void
|
|---|
| 546 | print_time (time_t t)
|
|---|
| 547 | {
|
|---|
| 548 | char buf[20]; /* ccyy-mm-dd HH:MM:SS\0 */
|
|---|
| 549 | strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", gmtime (&t));
|
|---|
| 550 | printf ("%s ", buf);
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | void
|
|---|
| 554 | print_stat (const char *name)
|
|---|
| 555 | {
|
|---|
| 556 | char *fmt, *p;
|
|---|
| 557 | struct stat st;
|
|---|
| 558 | char buf[UINTMAX_STRSIZE_BOUND];
|
|---|
| 559 |
|
|---|
| 560 | if (stat (name, &st))
|
|---|
| 561 | {
|
|---|
| 562 | error (0, errno, _("stat(%s) failed"), name);
|
|---|
| 563 | return;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | fmt = strdup (stat_format);
|
|---|
| 567 | for (p = strtok (fmt, ","); p; )
|
|---|
| 568 | {
|
|---|
| 569 | if (memcmp (p, "st_", 3) == 0)
|
|---|
| 570 | p += 3;
|
|---|
| 571 | if (strcmp (p, "name") == 0)
|
|---|
| 572 | printf ("%s", name);
|
|---|
| 573 | else if (strcmp (p, "dev") == 0)
|
|---|
| 574 | printf ("%lu", (unsigned long) st.st_dev);
|
|---|
| 575 | else if (strcmp (p, "ino") == 0)
|
|---|
| 576 | printf ("%lu", (unsigned long) st.st_ino);
|
|---|
| 577 | else if (strncmp (p, "mode", 4) == 0)
|
|---|
| 578 | {
|
|---|
| 579 | mode_t mask = ~0;
|
|---|
| 580 |
|
|---|
| 581 | if (ispunct (p[4]))
|
|---|
| 582 | {
|
|---|
| 583 | char *q;
|
|---|
| 584 |
|
|---|
| 585 | mask = strtoul (p + 5, &q, 8);
|
|---|
| 586 | if (*q)
|
|---|
| 587 | {
|
|---|
| 588 | printf ("\n");
|
|---|
| 589 | error (EXIT_FAILURE, 0, _("incorrect mask (near `%s')"), q);
|
|---|
| 590 | }
|
|---|
| 591 | }
|
|---|
| 592 | else if (p[4])
|
|---|
| 593 | {
|
|---|
| 594 | printf ("\n");
|
|---|
| 595 | error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
|
|---|
| 596 | }
|
|---|
| 597 | printf ("%0o", st.st_mode & mask);
|
|---|
| 598 | }
|
|---|
| 599 | else if (strcmp (p, "nlink") == 0)
|
|---|
| 600 | printf ("%lu", (unsigned long) st.st_nlink);
|
|---|
| 601 | else if (strcmp (p, "uid") == 0)
|
|---|
| 602 | printf ("%ld", (long unsigned) st.st_uid);
|
|---|
| 603 | else if (strcmp (p, "gid") == 0)
|
|---|
| 604 | printf ("%lu", (unsigned long) st.st_gid);
|
|---|
| 605 | else if (strcmp (p, "size") == 0)
|
|---|
| 606 | printf ("%s", umaxtostr (st.st_size, buf));
|
|---|
| 607 | else if (strcmp (p, "blksize") == 0)
|
|---|
| 608 | printf ("%s", umaxtostr (st.st_blksize, buf));
|
|---|
| 609 | else if (strcmp (p, "blocks") == 0)
|
|---|
| 610 | printf ("%s", umaxtostr (st.st_blocks, buf));
|
|---|
| 611 | else if (strcmp (p, "atime") == 0)
|
|---|
| 612 | printf ("%lu", (unsigned long) st.st_atime);
|
|---|
| 613 | else if (strcmp (p, "atimeH") == 0)
|
|---|
| 614 | print_time (st.st_atime);
|
|---|
| 615 | else if (strcmp (p, "mtime") == 0)
|
|---|
| 616 | printf ("%lu", (unsigned long) st.st_mtime);
|
|---|
| 617 | else if (strcmp (p, "mtimeH") == 0)
|
|---|
| 618 | print_time (st.st_mtime);
|
|---|
| 619 | else if (strcmp (p, "ctime") == 0)
|
|---|
| 620 | printf ("%lu", (unsigned long) st.st_ctime);
|
|---|
| 621 | else if (strcmp (p, "ctimeH") == 0)
|
|---|
| 622 | print_time (st.st_ctime);
|
|---|
| 623 | else if (strcmp (p, "sparse") == 0)
|
|---|
| 624 | printf ("%d", ST_IS_SPARSE (st));
|
|---|
| 625 | else
|
|---|
| 626 | {
|
|---|
| 627 | printf ("\n");
|
|---|
| 628 | error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
|
|---|
| 629 | }
|
|---|
| 630 | p = strtok (NULL, ",");
|
|---|
| 631 | if (p)
|
|---|
| 632 | printf (" ");
|
|---|
| 633 | }
|
|---|
| 634 | printf ("\n");
|
|---|
| 635 | free (fmt);
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | |
|---|
| 639 |
|
|---|
| 640 | /* Exec Mode */
|
|---|
| 641 |
|
|---|
| 642 | void
|
|---|
| 643 | exec_checkpoint (struct action *p)
|
|---|
| 644 | {
|
|---|
| 645 | if (verbose)
|
|---|
| 646 | printf ("processing checkpoint %lu\n", (unsigned long) p->checkpoint);
|
|---|
| 647 | switch (p->action)
|
|---|
| 648 | {
|
|---|
| 649 | case OPT_TOUCH:
|
|---|
| 650 | {
|
|---|
| 651 | struct timespec ts[2];
|
|---|
| 652 |
|
|---|
| 653 | ts[0] = ts[1] = p->ts;
|
|---|
| 654 | if (utimens (p->name, ts) != 0)
|
|---|
| 655 | {
|
|---|
| 656 | error (0, errno, _("cannot set time on `%s'"), p->name);
|
|---|
| 657 | break;
|
|---|
| 658 | }
|
|---|
| 659 | }
|
|---|
| 660 | break;
|
|---|
| 661 |
|
|---|
| 662 | case OPT_APPEND:
|
|---|
| 663 | {
|
|---|
| 664 | FILE *fp = fopen (p->name, "a");
|
|---|
| 665 | if (!fp)
|
|---|
| 666 | {
|
|---|
| 667 | error (0, errno, _("cannot open `%s'"), p->name);
|
|---|
| 668 | break;
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | fill (fp, p->size, p->pattern);
|
|---|
| 672 | fclose (fp);
|
|---|
| 673 | }
|
|---|
| 674 | break;
|
|---|
| 675 |
|
|---|
| 676 | case OPT_TRUNCATE:
|
|---|
| 677 | {
|
|---|
| 678 | int fd = open (p->name, O_RDWR);
|
|---|
| 679 | if (fd == -1)
|
|---|
| 680 | {
|
|---|
| 681 | error (0, errno, _("cannot open `%s'"), p->name);
|
|---|
| 682 | break;
|
|---|
| 683 | }
|
|---|
| 684 | ftruncate (fd, p->size);
|
|---|
| 685 | close (fd);
|
|---|
| 686 | }
|
|---|
| 687 | break;
|
|---|
| 688 |
|
|---|
| 689 | case OPT_EXEC:
|
|---|
| 690 | system (p->name);
|
|---|
| 691 | break;
|
|---|
| 692 |
|
|---|
| 693 | default:
|
|---|
| 694 | abort ();
|
|---|
| 695 | }
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 | void
|
|---|
| 699 | process_checkpoint (size_t n)
|
|---|
| 700 | {
|
|---|
| 701 | struct action *p, *prev = NULL;
|
|---|
| 702 |
|
|---|
| 703 | for (p = action_list; p; )
|
|---|
| 704 | {
|
|---|
| 705 | struct action *next = p->next;
|
|---|
| 706 |
|
|---|
| 707 | if (p->checkpoint <= n)
|
|---|
| 708 | {
|
|---|
| 709 | exec_checkpoint (p);
|
|---|
| 710 | /* Remove the item from the list */
|
|---|
| 711 | if (prev)
|
|---|
| 712 | prev->next = next;
|
|---|
| 713 | else
|
|---|
| 714 | action_list = next;
|
|---|
| 715 | free (p);
|
|---|
| 716 | }
|
|---|
| 717 | else
|
|---|
| 718 | prev = p;
|
|---|
| 719 |
|
|---|
| 720 | p = next;
|
|---|
| 721 | }
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | #define CHECKPOINT_TEXT "Write checkpoint"
|
|---|
| 725 |
|
|---|
| 726 | void
|
|---|
| 727 | exec_command (void)
|
|---|
| 728 | {
|
|---|
| 729 | int status;
|
|---|
| 730 | pid_t pid;
|
|---|
| 731 | int fd[2];
|
|---|
| 732 | char *p;
|
|---|
| 733 | FILE *fp;
|
|---|
| 734 | char buf[128];
|
|---|
| 735 |
|
|---|
| 736 | /* Insert --checkpoint option.
|
|---|
| 737 | FIXME: This assumes that exec_argv does not use traditional tar options
|
|---|
| 738 | (without dash) */
|
|---|
| 739 | exec_argc++;
|
|---|
| 740 | exec_argv = xrealloc (exec_argv, (exec_argc + 1) * sizeof (*exec_argv));
|
|---|
| 741 | memmove (exec_argv+2, exec_argv+1, (exec_argc - 1) * sizeof (*exec_argv));
|
|---|
| 742 | exec_argv[1] = "--checkpoint";
|
|---|
| 743 |
|
|---|
| 744 | #ifdef SIGCHLD
|
|---|
| 745 | /* System V fork+wait does not work if SIGCHLD is ignored. */
|
|---|
| 746 | signal (SIGCHLD, SIG_DFL);
|
|---|
| 747 | #endif
|
|---|
| 748 |
|
|---|
| 749 | pipe (fd);
|
|---|
| 750 |
|
|---|
| 751 | pid = fork ();
|
|---|
| 752 | if (pid == -1)
|
|---|
| 753 | error (EXIT_FAILURE, errno, "fork");
|
|---|
| 754 |
|
|---|
| 755 | if (pid == 0)
|
|---|
| 756 | {
|
|---|
| 757 | /* Child */
|
|---|
| 758 |
|
|---|
| 759 | /* Pipe stderr */
|
|---|
| 760 | if (fd[1] != 2)
|
|---|
| 761 | dup2 (fd[1], 2);
|
|---|
| 762 | close (fd[0]);
|
|---|
| 763 |
|
|---|
| 764 | /* Make sure POSIX locale is used */
|
|---|
| 765 | setenv ("LC_ALL", "POSIX", 1);
|
|---|
| 766 |
|
|---|
| 767 | execvp (exec_argv[0], exec_argv);
|
|---|
| 768 | error (EXIT_FAILURE, errno, "execvp");
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | /* Master */
|
|---|
| 772 | close (fd[1]);
|
|---|
| 773 | fp = fdopen (fd[0], "r");
|
|---|
| 774 | if (fp == NULL)
|
|---|
| 775 | error (EXIT_FAILURE, errno, "fdopen");
|
|---|
| 776 |
|
|---|
| 777 | while ((p = fgets (buf, sizeof buf, fp)))
|
|---|
| 778 | {
|
|---|
| 779 | while (*p && !isspace (*p) && *p != ':')
|
|---|
| 780 | p++;
|
|---|
| 781 |
|
|---|
| 782 | if (*p == ':')
|
|---|
| 783 | {
|
|---|
| 784 | for (p++; *p && isspace (*p); p++)
|
|---|
| 785 | ;
|
|---|
| 786 |
|
|---|
| 787 | if (*p
|
|---|
| 788 | && memcmp (p, CHECKPOINT_TEXT, sizeof CHECKPOINT_TEXT - 1) == 0)
|
|---|
| 789 | {
|
|---|
| 790 | char *end;
|
|---|
| 791 | size_t n = strtoul (p + sizeof CHECKPOINT_TEXT - 1, &end, 10);
|
|---|
| 792 | if (!(*end && !isspace (*end)))
|
|---|
| 793 | {
|
|---|
| 794 | process_checkpoint (n);
|
|---|
| 795 | continue;
|
|---|
| 796 | }
|
|---|
| 797 | }
|
|---|
| 798 | }
|
|---|
| 799 | fprintf (stderr, "%s", buf);
|
|---|
| 800 | }
|
|---|
| 801 |
|
|---|
| 802 | /* Collect exit status */
|
|---|
| 803 | waitpid (pid, &status, 0);
|
|---|
| 804 |
|
|---|
| 805 | if (verbose)
|
|---|
| 806 | {
|
|---|
| 807 | if (WIFEXITED (status))
|
|---|
| 808 | {
|
|---|
| 809 | if (WEXITSTATUS (status) == 0)
|
|---|
| 810 | printf (_("Command exited successfully\n"));
|
|---|
| 811 | else
|
|---|
| 812 | printf (_("Command failed with status %d\n"),
|
|---|
| 813 | WEXITSTATUS (status));
|
|---|
| 814 | }
|
|---|
| 815 | else if (WIFSIGNALED (status))
|
|---|
| 816 | printf (_("Command terminated on signal %d\n"), WTERMSIG (status));
|
|---|
| 817 | else if (WIFSTOPPED (status))
|
|---|
| 818 | printf (_("Command stopped on signal %d\n"), WSTOPSIG (status));
|
|---|
| 819 | #ifdef WCOREDUMP
|
|---|
| 820 | else if (WCOREDUMP (status))
|
|---|
| 821 | printf (_("Command dumped core\n"));
|
|---|
| 822 | #endif
|
|---|
| 823 | else
|
|---|
| 824 | printf(_("Command terminated\n"));
|
|---|
| 825 | }
|
|---|
| 826 |
|
|---|
| 827 | if (WIFEXITED (status))
|
|---|
| 828 | exit (WEXITSTATUS (status));
|
|---|
| 829 | exit (EXIT_FAILURE);
|
|---|
| 830 | }
|
|---|
| 831 |
|
|---|
| 832 | int
|
|---|
| 833 | main (int argc, char **argv)
|
|---|
| 834 | {
|
|---|
| 835 | int index;
|
|---|
| 836 |
|
|---|
| 837 | program_name = argv[0];
|
|---|
| 838 | setlocale (LC_ALL, "");
|
|---|
| 839 | bindtextdomain (PACKAGE, LOCALEDIR);
|
|---|
| 840 | textdomain (PACKAGE);
|
|---|
| 841 |
|
|---|
| 842 | get_date (&touch_time, "now", NULL);
|
|---|
| 843 |
|
|---|
| 844 | /* Decode command options. */
|
|---|
| 845 |
|
|---|
| 846 | if (argp_parse (&argp, argc, argv, 0, &index, NULL))
|
|---|
| 847 | exit (EXIT_FAILURE);
|
|---|
| 848 |
|
|---|
| 849 | argc -= index;
|
|---|
| 850 | argv += index;
|
|---|
| 851 |
|
|---|
| 852 | switch (mode)
|
|---|
| 853 | {
|
|---|
| 854 | case mode_stat:
|
|---|
| 855 | if (argc == 0)
|
|---|
| 856 | error (EXIT_FAILURE, 0, _("--stat requires file names"));
|
|---|
| 857 |
|
|---|
| 858 | while (argc--)
|
|---|
| 859 | print_stat (*argv++);
|
|---|
| 860 | break;
|
|---|
| 861 |
|
|---|
| 862 | case mode_sparse:
|
|---|
| 863 | generate_sparse_file (argc, argv);
|
|---|
| 864 | verify_file (file_name);
|
|---|
| 865 | break;
|
|---|
| 866 |
|
|---|
| 867 | case mode_generate:
|
|---|
| 868 | if (argc)
|
|---|
| 869 | error (EXIT_FAILURE, 0, _("too many arguments"));
|
|---|
| 870 | if (files_from)
|
|---|
| 871 | generate_files_from_list ();
|
|---|
| 872 | else
|
|---|
| 873 | {
|
|---|
| 874 | generate_simple_file (file_name);
|
|---|
| 875 | verify_file (file_name);
|
|---|
| 876 | }
|
|---|
| 877 | break;
|
|---|
| 878 |
|
|---|
| 879 | case mode_exec:
|
|---|
| 880 | exec_command ();
|
|---|
| 881 | break;
|
|---|
| 882 |
|
|---|
| 883 | default:
|
|---|
| 884 | /* Just in case */
|
|---|
| 885 | abort ();
|
|---|
| 886 | }
|
|---|
| 887 | exit (EXIT_SUCCESS);
|
|---|
| 888 | }
|
|---|