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, 2009 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 */
102 char *checkpoint_option;
104 /* Time for --touch option */
105 struct timespec touch_time;
110 const char *argp_program_version = "genfile (" PACKAGE ") " VERSION;
111 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
112 static char doc[] = N_("genfile manipulates data files for GNU paxutils test suite.\n"
115 #define OPT_CHECKPOINT 256
116 #define OPT_TOUCH 257
117 #define OPT_APPEND 258
118 #define OPT_TRUNCATE 259
121 #define OPT_VERBOSE 262
123 #define OPT_UNLINK 264
125 static struct argp_option options[] = {
128 N_("File creation options:"), GRP},
129 {"length", 'l', N_("SIZE"), 0,
130 N_("Create file of the given SIZE"), GRP+1 },
131 {"file", 'f', N_("NAME"), 0,
132 N_("Write to file NAME, instead of standard output"), GRP+1},
133 {"files-from", 'T', N_("FILE"), 0,
134 N_("Read file names from FILE"), GRP+1},
135 {"null", '0', NULL, 0,
136 N_("-T reads null-terminated names"), GRP+1},
137 {"pattern", 'p', N_("PATTERN"), 0,
138 N_("Fill the file with the given PATTERN. PATTERN is 'default' or 'zeros'"),
140 {"block-size", 'b', N_("SIZE"), 0,
141 N_("Size of a block for sparse file"), GRP+1},
142 {"sparse", 's', NULL, 0,
143 N_("Generate sparse file. Rest of the command line gives the file map."),
145 {"seek", OPT_SEEK, N_("OFFSET"), 0,
146 N_("Seek to the given offset before writing data"),
152 N_("File statistics options:"), GRP},
154 {"stat", 'S', N_("FORMAT"), OPTION_ARG_OPTIONAL,
155 N_("Print contents of struct stat for each given file. Default FORMAT is: ")
162 N_("Synchronous execution options:"), GRP},
164 {"run", 'r', N_("OPTION"), OPTION_ARG_OPTIONAL,
165 N_("Execute ARGS. Useful with --checkpoint and one of --cut, --append, --touch, --unlink"),
167 {"checkpoint", OPT_CHECKPOINT, N_("NUMBER"), 0,
168 N_("Perform given action (see below) upon reaching checkpoint NUMBER"),
170 {"date", OPT_DATE, N_("STRING"), 0,
171 N_("Set date for next --touch option"),
173 {"verbose", OPT_VERBOSE, NULL, 0,
174 N_("Display executed checkpoints and exit status of COMMAND"),
179 N_("Synchronous execution actions. These are executed when checkpoint number given by --checkpoint option is reached."), GRP},
181 {"cut", OPT_TRUNCATE, N_("FILE"), 0,
182 N_("Truncate FILE to the size specified by previous --length option (or 0, if it is not given)"),
184 {"truncate", 0, NULL, OPTION_ALIAS, NULL, GRP+1 },
185 {"append", OPT_APPEND, N_("FILE"), 0,
186 N_("Append SIZE bytes to FILE. SIZE is given by previous --length option."),
188 {"touch", OPT_TOUCH, N_("FILE"), 0,
189 N_("Update the access and modification times of FILE"),
191 {"exec", OPT_EXEC, N_("COMMAND"), 0,
192 N_("Execute COMMAND"),
194 {"unlink", OPT_UNLINK, N_("FILE"), 0,
201 static char const * const pattern_args[] = { "default", "zeros", 0 };
202 static enum pattern const pattern_types[] = {DEFAULT_PATTERN, ZEROS_PATTERN};
205 xlat_suffix (off_t *vp, const char *p)
233 get_size (const char *str, int allow_zero)
238 for (p = str; *p; p++)
240 int digit = *p - '0';
242 if (9 < (unsigned) digit)
244 if (xlat_suffix (&v, p))
245 error (EXIT_FAILURE, 0, _("Invalid size: %s"), str);
249 else if (x / 10 != v)
250 error (EXIT_FAILURE, 0, _("Number out of allowed range: %s"), str);
253 error (EXIT_FAILURE, 0, _("Negative size: %s"), str);
259 verify_file (char *file_name)
265 if (stat (file_name, &st))
266 error (0, errno, _("stat(%s) failed"), file_name);
268 if (st.st_size != file_length + seek_offset)
269 error (1, 0, _("requested file length %lu, actual %lu"),
270 (unsigned long)st.st_size, (unsigned long)file_length);
272 if (mode == mode_sparse && !ST_IS_SPARSE (st))
273 error (1, 0, _("created file is not sparse"));
284 enum pattern pattern;
288 static struct action *action_list;
291 reg_action (int action, char *arg)
293 struct action *act = xmalloc (sizeof (*act));
294 act->checkpoint = checkpoint;
295 act->action = action;
296 act->pattern = pattern;
297 act->ts = touch_time;
298 act->size = file_length;
300 act->next = action_list;
305 parse_opt (int key, char *arg, struct argp_state *state)
310 filename_terminator = 0;
318 file_length = get_size (arg, 1);
322 pattern = XARGMATCH ("--pattern", arg, pattern_args, pattern_types);
326 block_size = get_size (arg, 0);
343 argcv_get (arg, "", NULL, &exec_argc, &exec_argv);
344 checkpoint_option = "--checkpoint";
353 seek_offset = get_size (arg, 0);
360 checkpoint = strtoul (arg, &p, 0);
362 argp_error (state, _("Error parsing number near `%s'"), p);
367 if (!get_date (&touch_time, arg, NULL))
368 argp_error (state, _("Unknown date format"));
376 reg_action (key, arg);
384 return ARGP_ERR_UNKNOWN;
389 static struct argp argp = {
401 fill (FILE *fp, off_t length, enum pattern pattern)
407 case DEFAULT_PATTERN:
408 for (i = 0; i < length; i++)
413 for (i = 0; i < length; i++)
419 /* Generate Mode: usual files */
421 generate_simple_file (char *filename)
427 fp = fopen (filename, seek_offset ? "rb+" : "wb");
429 error (EXIT_FAILURE, errno, _("cannot open `%s'"), filename);
434 if (fseeko (fp, seek_offset, 0))
435 error (EXIT_FAILURE, errno, "%s", _("cannot seek"));
437 fill (fp, file_length, pattern);
442 /* A simplified version of the same function from tar */
444 read_name_from_file (FILE *fp, struct obstack *stk)
449 for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
452 error (EXIT_FAILURE, 0, _("file name contains null character"));
453 obstack_1grow (stk, c);
457 obstack_1grow (stk, 0);
459 return (counter == 0 && c == EOF);
463 generate_files_from_list ()
465 FILE *fp = strcmp (files_from, "-") ? fopen (files_from, "rb") : stdin;
469 error (EXIT_FAILURE, errno, _("cannot open `%s'"), files_from);
472 while (!read_name_from_file (fp, &stk))
474 char *name = obstack_finish (&stk);
475 generate_simple_file (name);
477 obstack_free (&stk, name);
480 obstack_free (&stk, NULL);
484 /* Generate Mode: sparse files */
487 mkhole (int fd, off_t displ)
489 if (lseek (fd, displ, SEEK_CUR) == -1)
490 error (EXIT_FAILURE, errno, "lseek");
491 ftruncate (fd, lseek (fd, 0, SEEK_CUR));
495 mksparse (int fd, off_t displ, char *marks)
497 if (lseek (fd, displ, SEEK_CUR) == -1)
498 error (EXIT_FAILURE, errno, "lseek");
500 for (; *marks; marks++)
502 memset (buffer, *marks, block_size);
503 if (write (fd, buffer, block_size) != block_size)
504 error (EXIT_FAILURE, errno, "write");
509 generate_sparse_file (int argc, char **argv)
513 int flags = O_CREAT | O_RDWR | O_BINARY;
516 error (EXIT_FAILURE, 0,
517 _("cannot generate sparse files on standard output, use --file option"));
520 fd = open (file_name, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
522 error (EXIT_FAILURE, errno, _("cannot open `%s'"), file_name);
524 buffer = xmalloc (block_size);
528 for (i = 0; i < argc; i += 2)
530 off_t displ = get_size (argv[i], 1);
531 file_length += displ;
540 file_length += block_size * strlen (argv[i+1]);
541 mksparse (fd, displ, argv[i+1]);
552 print_time (time_t t)
554 char buf[20]; /* ccyy-mm-dd HH:MM:SS\0 */
555 strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", gmtime (&t));
560 print_stat (const char *name)
564 char buf[UINTMAX_STRSIZE_BOUND];
566 if (stat (name, &st))
568 error (0, errno, _("stat(%s) failed"), name);
572 fmt = strdup (stat_format);
573 for (p = strtok (fmt, ","); p; )
575 if (memcmp (p, "st_", 3) == 0)
577 if (strcmp (p, "name") == 0)
579 else if (strcmp (p, "dev") == 0)
580 printf ("%lu", (unsigned long) st.st_dev);
581 else if (strcmp (p, "ino") == 0)
582 printf ("%lu", (unsigned long) st.st_ino);
583 else if (strncmp (p, "mode", 4) == 0)
587 if (ispunct ((unsigned char) p[4]))
591 mask = strtoul (p + 5, &q, 8);
595 error (EXIT_FAILURE, 0, _("incorrect mask (near `%s')"), q);
601 error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
603 printf ("%0o", st.st_mode & mask);
605 else if (strcmp (p, "nlink") == 0)
606 printf ("%lu", (unsigned long) st.st_nlink);
607 else if (strcmp (p, "uid") == 0)
608 printf ("%ld", (long unsigned) st.st_uid);
609 else if (strcmp (p, "gid") == 0)
610 printf ("%lu", (unsigned long) st.st_gid);
611 else if (strcmp (p, "size") == 0)
612 printf ("%s", umaxtostr (st.st_size, buf));
613 else if (strcmp (p, "blksize") == 0)
614 printf ("%s", umaxtostr (st.st_blksize, buf));
615 else if (strcmp (p, "blocks") == 0)
616 printf ("%s", umaxtostr (st.st_blocks, buf));
617 else if (strcmp (p, "atime") == 0)
618 printf ("%lu", (unsigned long) st.st_atime);
619 else if (strcmp (p, "atimeH") == 0)
620 print_time (st.st_atime);
621 else if (strcmp (p, "mtime") == 0)
622 printf ("%lu", (unsigned long) st.st_mtime);
623 else if (strcmp (p, "mtimeH") == 0)
624 print_time (st.st_mtime);
625 else if (strcmp (p, "ctime") == 0)
626 printf ("%lu", (unsigned long) st.st_ctime);
627 else if (strcmp (p, "ctimeH") == 0)
628 print_time (st.st_ctime);
629 else if (strcmp (p, "sparse") == 0)
630 printf ("%d", ST_IS_SPARSE (st));
634 error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
636 p = strtok (NULL, ",");
648 exec_checkpoint (struct action *p)
651 printf ("processing checkpoint %lu\n", (unsigned long) p->checkpoint);
656 struct timespec ts[2];
658 ts[0] = ts[1] = p->ts;
659 if (utimens (p->name, ts) != 0)
661 error (0, errno, _("cannot set time on `%s'"), p->name);
669 FILE *fp = fopen (p->name, "ab");
672 error (0, errno, _("cannot open `%s'"), p->name);
676 fill (fp, p->size, p->pattern);
683 int fd = open (p->name, O_RDWR | O_BINARY);
686 error (0, errno, _("cannot open `%s'"), p->name);
689 ftruncate (fd, p->size);
699 if (unlink (p->name))
700 error (0, errno, _("cannot unlink `%s'"), p->name);
709 process_checkpoint (size_t n)
711 struct action *p, *prev = NULL;
713 for (p = action_list; p; )
715 struct action *next = p->next;
717 if (p->checkpoint <= n)
720 /* Remove the item from the list */
734 #define CHECKPOINT_TEXT "Write checkpoint"
746 /* Insert --checkpoint option.
747 FIXME: This assumes that exec_argv does not use traditional tar options
749 FIXME: There is no way to set checkpoint argument (granularity).
751 if (checkpoint_option)
754 exec_argv = xrealloc (exec_argv, (exec_argc + 1) * sizeof (*exec_argv));
755 memmove (exec_argv+2, exec_argv+1,
756 (exec_argc - 1) * sizeof (*exec_argv));
757 exec_argv[1] = checkpoint_option;
761 /* System V fork+wait does not work if SIGCHLD is ignored. */
762 signal (SIGCHLD, SIG_DFL);
769 error (EXIT_FAILURE, errno, "fork");
780 /* Make sure POSIX locale is used */
781 setenv ("LC_ALL", "POSIX", 1);
783 execvp (exec_argv[0], exec_argv);
784 error (EXIT_FAILURE, errno, "execvp %s", exec_argv[0]);
789 fp = fdopen (fd[0], "rb");
791 error (EXIT_FAILURE, errno, "fdopen");
793 while ((p = fgets (buf, sizeof buf, fp)))
795 while (*p && !isspace ((unsigned char) *p) && *p != ':')
800 for (p++; *p && isspace ((unsigned char) *p); p++)
804 && memcmp (p, CHECKPOINT_TEXT, sizeof CHECKPOINT_TEXT - 1) == 0)
807 size_t n = strtoul (p + sizeof CHECKPOINT_TEXT - 1, &end, 10);
808 if (!(*end && !isspace ((unsigned char) *end)))
810 process_checkpoint (n);
815 fprintf (stderr, "%s", buf);
818 /* Collect exit status */
819 waitpid (pid, &status, 0);
823 if (WIFEXITED (status))
825 if (WEXITSTATUS (status) == 0)
826 printf (_("Command exited successfully\n"));
828 printf (_("Command failed with status %d\n"),
829 WEXITSTATUS (status));
831 else if (WIFSIGNALED (status))
832 printf (_("Command terminated on signal %d\n"), WTERMSIG (status));
833 else if (WIFSTOPPED (status))
834 printf (_("Command stopped on signal %d\n"), WSTOPSIG (status));
836 else if (WCOREDUMP (status))
837 printf (_("Command dumped core\n"));
840 printf(_("Command terminated\n"));
843 if (WIFEXITED (status))
844 exit (WEXITSTATUS (status));
849 main (int argc, char **argv)
853 program_name = argv[0];
854 setlocale (LC_ALL, "");
855 bindtextdomain (PACKAGE, LOCALEDIR);
856 textdomain (PACKAGE);
858 get_date (&touch_time, "now", NULL);
860 /* Decode command options. */
862 if (argp_parse (&argp, argc, argv, 0, &index, NULL))
872 error (EXIT_FAILURE, 0, _("--stat requires file names"));
875 print_stat (*argv++);
879 generate_sparse_file (argc, argv);
880 verify_file (file_name);
885 error (EXIT_FAILURE, 0, _("too many arguments"));
887 generate_files_from_list ();
890 generate_simple_file (file_name);
891 verify_file (file_name);
896 if (!checkpoint_option)
902 error (EXIT_FAILURE, 0, _("too many arguments"));