1 /* Generate a file containing some preset patterns.
2 Print statistics for existing files.
4 Copyright (C) 1995, 1996, 1997, 2001, 2003, 2004, 2005, 2006, 2007,
5 2008 Free Software Foundation, Inc.
7 François Pinard <pinard@iro.umontreal.ca>, 1995.
8 Sergey Poznyakoff <gray@mirddin.farlep.net>, 2004, 2005, 2006, 2007, 2008.
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 3, or (at your option)
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.
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.
36 #define obstack_chunk_alloc malloc
37 #define obstack_chunk_free free
41 # define EXIT_SUCCESS 0
44 # define EXIT_FAILURE 1
47 #if ! defined SIGCHLD && defined SIGCLD
48 # define SIGCHLD SIGCLD
57 /* The name this program was run with. */
58 const char *program_name;
60 /* Name of file to generate */
61 static char *file_name;
63 /* Name of the file-list file: */
64 static char *files_from;
65 static char filename_terminator = '\n';
67 /* Length of file to generate. */
68 static off_t file_length = 0;
69 static off_t seek_offset = 0;
71 /* Pattern to generate. */
72 static enum pattern pattern = DEFAULT_PATTERN;
74 /* Next checkpoint number */
85 enum genfile_mode mode = mode_generate;
87 #define DEFAULT_STAT_FORMAT \
88 "name,dev,ino,mode,nlink,uid,gid,size,blksize,blocks,atime,mtime,ctime"
90 /* Format for --stat option */
91 static char *stat_format = DEFAULT_STAT_FORMAT;
93 /* Size of a block for sparse file */
94 size_t block_size = 512;
96 /* Block buffer for sparse file */
99 /* Number of arguments and argument vector for mode == mode_exec */
103 /* Time for --touch option */
104 struct timespec touch_time;
109 const char *argp_program_version = "genfile (" PACKAGE ") " VERSION;
110 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
111 static char doc[] = N_("genfile manipulates data files for GNU paxutils test suite.\n"
114 #define OPT_CHECKPOINT 256
115 #define OPT_TOUCH 257
116 #define OPT_APPEND 258
117 #define OPT_TRUNCATE 259
120 #define OPT_VERBOSE 262
123 static struct argp_option options[] = {
126 N_("File creation options:"), GRP},
127 {"length", 'l', N_("SIZE"), 0,
128 N_("Create file of the given SIZE"), GRP+1 },
129 {"file", 'f', N_("NAME"), 0,
130 N_("Write to file NAME, instead of standard output"), GRP+1},
131 {"files-from", 'T', N_("FILE"), 0,
132 N_("Read file names from FILE"), GRP+1},
133 {"null", '0', NULL, 0,
134 N_("-T reads null-terminated names"), GRP+1},
135 {"pattern", 'p', N_("PATTERN"), 0,
136 N_("Fill the file with the given PATTERN. PATTERN is 'default' or 'zeros'"),
138 {"block-size", 'b', N_("SIZE"), 0,
139 N_("Size of a block for sparse file"), GRP+1},
140 {"sparse", 's', NULL, 0,
141 N_("Generate sparse file. Rest of the command line gives the file map."),
143 {"seek", OPT_SEEK, N_("OFFSET"), 0,
144 N_("Seek to the given offset before writing data"),
150 N_("File statistics options:"), GRP},
152 {"stat", 'S', N_("FORMAT"), OPTION_ARG_OPTIONAL,
153 N_("Print contents of struct stat for each given file. Default FORMAT is: ")
160 N_("Synchronous execution options:"), GRP},
162 {"run", 'r', N_("COMMAND"), 0,
163 N_("Execute given COMMAND. Useful with --checkpoint and one of --cut, --append, --touch"),
165 {"checkpoint", OPT_CHECKPOINT, N_("NUMBER"), 0,
166 N_("Perform given action (see below) upon reaching checkpoint NUMBER"),
168 {"date", OPT_DATE, N_("STRING"), 0,
169 N_("Set date for next --touch option"),
171 {"verbose", OPT_VERBOSE, NULL, 0,
172 N_("Display executed checkpoints and exit status of COMMAND"),
177 N_("Synchronous execution actions. These are executed when checkpoint number given by --checkpoint option is reached."), GRP},
179 {"cut", OPT_TRUNCATE, N_("FILE"), 0,
180 N_("Truncate FILE to the size specified by previous --length option (or 0, if it is not given)"),
182 {"truncate", 0, NULL, OPTION_ALIAS, NULL, GRP+1 },
183 {"append", OPT_APPEND, N_("FILE"), 0,
184 N_("Append SIZE bytes to FILE. SIZE is given by previous --length option."),
186 {"touch", OPT_TOUCH, N_("FILE"), 0,
187 N_("Update the access and modification times of FILE"),
189 {"exec", OPT_EXEC, N_("COMMAND"), 0,
190 N_("Execute COMMAND"),
196 static char const * const pattern_args[] = { "default", "zeros", 0 };
197 static enum pattern const pattern_types[] = {DEFAULT_PATTERN, ZEROS_PATTERN};
200 xlat_suffix (off_t *vp, const char *p)
228 get_size (const char *str, int allow_zero)
233 for (p = str; *p; p++)
235 int digit = *p - '0';
237 if (9 < (unsigned) digit)
239 if (xlat_suffix (&v, p))
240 error (EXIT_FAILURE, 0, _("Invalid size: %s"), str);
244 else if (x / 10 != v)
245 error (EXIT_FAILURE, 0, _("Number out of allowed range: %s"), str);
248 error (EXIT_FAILURE, 0, _("Negative size: %s"), str);
254 verify_file (char *file_name)
260 if (stat (file_name, &st))
261 error (0, errno, _("stat(%s) failed"), file_name);
263 if (st.st_size != file_length + seek_offset)
265 printf ("%lu %lu\n", (unsigned long)st.st_size , (unsigned long)file_length);
269 if (mode == mode_sparse && !ST_IS_SPARSE (st))
281 enum pattern pattern;
285 static struct action *action_list;
288 reg_action (int action, char *arg)
290 struct action *act = xmalloc (sizeof (*act));
291 act->checkpoint = checkpoint;
292 act->action = action;
293 act->pattern = pattern;
294 act->ts = touch_time;
295 act->size = file_length;
297 act->next = action_list;
302 parse_opt (int key, char *arg, struct argp_state *state)
307 filename_terminator = 0;
315 file_length = get_size (arg, 1);
319 pattern = XARGMATCH ("--pattern", arg, pattern_args, pattern_types);
323 block_size = get_size (arg, 0);
338 argcv_get (arg, "", NULL, &exec_argc, &exec_argv);
346 seek_offset = get_size (arg, 0);
353 checkpoint = strtoul (arg, &p, 0);
355 argp_error (state, _("Error parsing number near `%s'"), p);
360 if (!get_date (&touch_time, arg, NULL))
361 argp_error (state, _("Unknown date format"));
368 reg_action (key, arg);
376 return ARGP_ERR_UNKNOWN;
381 static struct argp argp = {
393 fill (FILE *fp, off_t length, enum pattern pattern)
399 case DEFAULT_PATTERN:
400 for (i = 0; i < length; i++)
405 for (i = 0; i < length; i++)
411 /* Generate Mode: usual files */
413 generate_simple_file (char *filename)
419 fp = fopen (filename, seek_offset ? "rb+" : "wb");
421 error (EXIT_FAILURE, errno, _("cannot open `%s'"), filename);
426 if (fseeko (fp, seek_offset, 0))
427 error (EXIT_FAILURE, errno, "%s", _("cannot seek"));
429 fill (fp, file_length, pattern);
434 /* A simplified version of the same function from tar */
436 read_name_from_file (FILE *fp, struct obstack *stk)
441 for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
444 error (EXIT_FAILURE, 0, _("file name contains null character"));
445 obstack_1grow (stk, c);
449 obstack_1grow (stk, 0);
451 return (counter == 0 && c == EOF);
455 generate_files_from_list ()
457 FILE *fp = strcmp (files_from, "-") ? fopen (files_from, "rb") : stdin;
461 error (EXIT_FAILURE, errno, _("cannot open `%s'"), files_from);
464 while (!read_name_from_file (fp, &stk))
466 char *name = obstack_finish (&stk);
467 generate_simple_file (name);
469 obstack_free (&stk, name);
472 obstack_free (&stk, NULL);
476 /* Generate Mode: sparse files */
479 mkhole (int fd, off_t displ)
481 if (lseek (fd, displ, SEEK_CUR) == -1)
482 error (EXIT_FAILURE, errno, "lseek");
483 ftruncate (fd, lseek (fd, 0, SEEK_CUR));
487 mksparse (int fd, off_t displ, char *marks)
489 if (lseek (fd, displ, SEEK_CUR) == -1)
490 error (EXIT_FAILURE, errno, "lseek");
492 for (; *marks; marks++)
494 memset (buffer, *marks, block_size);
495 if (write (fd, buffer, block_size) != block_size)
496 error (EXIT_FAILURE, errno, "write");
501 generate_sparse_file (int argc, char **argv)
505 int flags = O_CREAT | O_RDWR | O_BINARY;
508 error (EXIT_FAILURE, 0,
509 _("cannot generate sparse files on standard output, use --file option"));
512 fd = open (file_name, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
514 error (EXIT_FAILURE, errno, _("cannot open `%s'"), file_name);
516 buffer = xmalloc (block_size);
520 for (i = 0; i < argc; i += 2)
522 off_t displ = get_size (argv[i], 1);
523 file_length += displ;
532 file_length += block_size * strlen (argv[i+1]);
533 mksparse (fd, displ, argv[i+1]);
544 print_time (time_t t)
546 char buf[20]; /* ccyy-mm-dd HH:MM:SS\0 */
547 strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", gmtime (&t));
552 print_stat (const char *name)
556 char buf[UINTMAX_STRSIZE_BOUND];
558 if (stat (name, &st))
560 error (0, errno, _("stat(%s) failed"), name);
564 fmt = strdup (stat_format);
565 for (p = strtok (fmt, ","); p; )
567 if (memcmp (p, "st_", 3) == 0)
569 if (strcmp (p, "name") == 0)
571 else if (strcmp (p, "dev") == 0)
572 printf ("%lu", (unsigned long) st.st_dev);
573 else if (strcmp (p, "ino") == 0)
574 printf ("%lu", (unsigned long) st.st_ino);
575 else if (strncmp (p, "mode", 4) == 0)
583 mask = strtoul (p + 5, &q, 8);
587 error (EXIT_FAILURE, 0, _("incorrect mask (near `%s')"), q);
593 error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
595 printf ("%0o", st.st_mode & mask);
597 else if (strcmp (p, "nlink") == 0)
598 printf ("%lu", (unsigned long) st.st_nlink);
599 else if (strcmp (p, "uid") == 0)
600 printf ("%ld", (long unsigned) st.st_uid);
601 else if (strcmp (p, "gid") == 0)
602 printf ("%lu", (unsigned long) st.st_gid);
603 else if (strcmp (p, "size") == 0)
604 printf ("%s", umaxtostr (st.st_size, buf));
605 else if (strcmp (p, "blksize") == 0)
606 printf ("%s", umaxtostr (st.st_blksize, buf));
607 else if (strcmp (p, "blocks") == 0)
608 printf ("%s", umaxtostr (st.st_blocks, buf));
609 else if (strcmp (p, "atime") == 0)
610 printf ("%lu", (unsigned long) st.st_atime);
611 else if (strcmp (p, "atimeH") == 0)
612 print_time (st.st_atime);
613 else if (strcmp (p, "mtime") == 0)
614 printf ("%lu", (unsigned long) st.st_mtime);
615 else if (strcmp (p, "mtimeH") == 0)
616 print_time (st.st_mtime);
617 else if (strcmp (p, "ctime") == 0)
618 printf ("%lu", (unsigned long) st.st_ctime);
619 else if (strcmp (p, "ctimeH") == 0)
620 print_time (st.st_ctime);
621 else if (strcmp (p, "sparse") == 0)
622 printf ("%d", ST_IS_SPARSE (st));
626 error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
628 p = strtok (NULL, ",");
640 exec_checkpoint (struct action *p)
643 printf ("processing checkpoint %lu\n", (unsigned long) p->checkpoint);
648 struct timespec ts[2];
650 ts[0] = ts[1] = p->ts;
651 if (utimens (p->name, ts) != 0)
653 error (0, errno, _("cannot set time on `%s'"), p->name);
661 FILE *fp = fopen (p->name, "ab");
664 error (0, errno, _("cannot open `%s'"), p->name);
668 fill (fp, p->size, p->pattern);
675 int fd = open (p->name, O_RDWR | O_BINARY);
678 error (0, errno, _("cannot open `%s'"), p->name);
681 ftruncate (fd, p->size);
696 process_checkpoint (size_t n)
698 struct action *p, *prev = NULL;
700 for (p = action_list; p; )
702 struct action *next = p->next;
704 if (p->checkpoint <= n)
707 /* Remove the item from the list */
721 #define CHECKPOINT_TEXT "Write checkpoint"
733 /* Insert --checkpoint option.
734 FIXME: This assumes that exec_argv does not use traditional tar options
737 exec_argv = xrealloc (exec_argv, (exec_argc + 1) * sizeof (*exec_argv));
738 memmove (exec_argv+2, exec_argv+1, (exec_argc - 1) * sizeof (*exec_argv));
739 exec_argv[1] = "--checkpoint";
742 /* System V fork+wait does not work if SIGCHLD is ignored. */
743 signal (SIGCHLD, SIG_DFL);
750 error (EXIT_FAILURE, errno, "fork");
761 /* Make sure POSIX locale is used */
762 setenv ("LC_ALL", "POSIX", 1);
764 execvp (exec_argv[0], exec_argv);
765 error (EXIT_FAILURE, errno, "execvp");
770 fp = fdopen (fd[0], "rb");
772 error (EXIT_FAILURE, errno, "fdopen");
774 while ((p = fgets (buf, sizeof buf, fp)))
776 while (*p && !isspace (*p) && *p != ':')
781 for (p++; *p && isspace (*p); p++)
785 && memcmp (p, CHECKPOINT_TEXT, sizeof CHECKPOINT_TEXT - 1) == 0)
788 size_t n = strtoul (p + sizeof CHECKPOINT_TEXT - 1, &end, 10);
789 if (!(*end && !isspace (*end)))
791 process_checkpoint (n);
796 fprintf (stderr, "%s", buf);
799 /* Collect exit status */
800 waitpid (pid, &status, 0);
804 if (WIFEXITED (status))
806 if (WEXITSTATUS (status) == 0)
807 printf (_("Command exited successfully\n"));
809 printf (_("Command failed with status %d\n"),
810 WEXITSTATUS (status));
812 else if (WIFSIGNALED (status))
813 printf (_("Command terminated on signal %d\n"), WTERMSIG (status));
814 else if (WIFSTOPPED (status))
815 printf (_("Command stopped on signal %d\n"), WSTOPSIG (status));
817 else if (WCOREDUMP (status))
818 printf (_("Command dumped core\n"));
821 printf(_("Command terminated\n"));
824 if (WIFEXITED (status))
825 exit (WEXITSTATUS (status));
830 main (int argc, char **argv)
834 program_name = argv[0];
835 setlocale (LC_ALL, "");
836 bindtextdomain (PACKAGE, LOCALEDIR);
837 textdomain (PACKAGE);
839 get_date (&touch_time, "now", NULL);
841 /* Decode command options. */
843 if (argp_parse (&argp, argc, argv, 0, &index, NULL))
853 error (EXIT_FAILURE, 0, _("--stat requires file names"));
856 print_stat (*argv++);
860 generate_sparse_file (argc, argv);
861 verify_file (file_name);
866 error (EXIT_FAILURE, 0, _("too many arguments"));
868 generate_files_from_list ();
871 generate_simple_file (file_name);
872 verify_file (file_name);