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)
264 error (1, 0, _("requested file length %lu, actual %lu"),
265 (unsigned long)st.st_size, (unsigned long)file_length);
267 if (mode == mode_sparse && !ST_IS_SPARSE (st))
268 error (1, 0, _("created file is not sparse"));
279 enum pattern pattern;
283 static struct action *action_list;
286 reg_action (int action, char *arg)
288 struct action *act = xmalloc (sizeof (*act));
289 act->checkpoint = checkpoint;
290 act->action = action;
291 act->pattern = pattern;
292 act->ts = touch_time;
293 act->size = file_length;
295 act->next = action_list;
300 parse_opt (int key, char *arg, struct argp_state *state)
305 filename_terminator = 0;
313 file_length = get_size (arg, 1);
317 pattern = XARGMATCH ("--pattern", arg, pattern_args, pattern_types);
321 block_size = get_size (arg, 0);
336 argcv_get (arg, "", NULL, &exec_argc, &exec_argv);
344 seek_offset = get_size (arg, 0);
351 checkpoint = strtoul (arg, &p, 0);
353 argp_error (state, _("Error parsing number near `%s'"), p);
358 if (!get_date (&touch_time, arg, NULL))
359 argp_error (state, _("Unknown date format"));
366 reg_action (key, arg);
374 return ARGP_ERR_UNKNOWN;
379 static struct argp argp = {
391 fill (FILE *fp, off_t length, enum pattern pattern)
397 case DEFAULT_PATTERN:
398 for (i = 0; i < length; i++)
403 for (i = 0; i < length; i++)
409 /* Generate Mode: usual files */
411 generate_simple_file (char *filename)
417 fp = fopen (filename, seek_offset ? "rb+" : "wb");
419 error (EXIT_FAILURE, errno, _("cannot open `%s'"), filename);
424 if (fseeko (fp, seek_offset, 0))
425 error (EXIT_FAILURE, errno, "%s", _("cannot seek"));
427 fill (fp, file_length, pattern);
432 /* A simplified version of the same function from tar */
434 read_name_from_file (FILE *fp, struct obstack *stk)
439 for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
442 error (EXIT_FAILURE, 0, _("file name contains null character"));
443 obstack_1grow (stk, c);
447 obstack_1grow (stk, 0);
449 return (counter == 0 && c == EOF);
453 generate_files_from_list ()
455 FILE *fp = strcmp (files_from, "-") ? fopen (files_from, "rb") : stdin;
459 error (EXIT_FAILURE, errno, _("cannot open `%s'"), files_from);
462 while (!read_name_from_file (fp, &stk))
464 char *name = obstack_finish (&stk);
465 generate_simple_file (name);
467 obstack_free (&stk, name);
470 obstack_free (&stk, NULL);
474 /* Generate Mode: sparse files */
477 mkhole (int fd, off_t displ)
479 if (lseek (fd, displ, SEEK_CUR) == -1)
480 error (EXIT_FAILURE, errno, "lseek");
481 ftruncate (fd, lseek (fd, 0, SEEK_CUR));
485 mksparse (int fd, off_t displ, char *marks)
487 if (lseek (fd, displ, SEEK_CUR) == -1)
488 error (EXIT_FAILURE, errno, "lseek");
490 for (; *marks; marks++)
492 memset (buffer, *marks, block_size);
493 if (write (fd, buffer, block_size) != block_size)
494 error (EXIT_FAILURE, errno, "write");
499 generate_sparse_file (int argc, char **argv)
503 int flags = O_CREAT | O_RDWR | O_BINARY;
506 error (EXIT_FAILURE, 0,
507 _("cannot generate sparse files on standard output, use --file option"));
510 fd = open (file_name, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
512 error (EXIT_FAILURE, errno, _("cannot open `%s'"), file_name);
514 buffer = xmalloc (block_size);
518 for (i = 0; i < argc; i += 2)
520 off_t displ = get_size (argv[i], 1);
521 file_length += displ;
530 file_length += block_size * strlen (argv[i+1]);
531 mksparse (fd, displ, argv[i+1]);
542 print_time (time_t t)
544 char buf[20]; /* ccyy-mm-dd HH:MM:SS\0 */
545 strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", gmtime (&t));
550 print_stat (const char *name)
554 char buf[UINTMAX_STRSIZE_BOUND];
556 if (stat (name, &st))
558 error (0, errno, _("stat(%s) failed"), name);
562 fmt = strdup (stat_format);
563 for (p = strtok (fmt, ","); p; )
565 if (memcmp (p, "st_", 3) == 0)
567 if (strcmp (p, "name") == 0)
569 else if (strcmp (p, "dev") == 0)
570 printf ("%lu", (unsigned long) st.st_dev);
571 else if (strcmp (p, "ino") == 0)
572 printf ("%lu", (unsigned long) st.st_ino);
573 else if (strncmp (p, "mode", 4) == 0)
581 mask = strtoul (p + 5, &q, 8);
585 error (EXIT_FAILURE, 0, _("incorrect mask (near `%s')"), q);
591 error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
593 printf ("%0o", st.st_mode & mask);
595 else if (strcmp (p, "nlink") == 0)
596 printf ("%lu", (unsigned long) st.st_nlink);
597 else if (strcmp (p, "uid") == 0)
598 printf ("%ld", (long unsigned) st.st_uid);
599 else if (strcmp (p, "gid") == 0)
600 printf ("%lu", (unsigned long) st.st_gid);
601 else if (strcmp (p, "size") == 0)
602 printf ("%s", umaxtostr (st.st_size, buf));
603 else if (strcmp (p, "blksize") == 0)
604 printf ("%s", umaxtostr (st.st_blksize, buf));
605 else if (strcmp (p, "blocks") == 0)
606 printf ("%s", umaxtostr (st.st_blocks, buf));
607 else if (strcmp (p, "atime") == 0)
608 printf ("%lu", (unsigned long) st.st_atime);
609 else if (strcmp (p, "atimeH") == 0)
610 print_time (st.st_atime);
611 else if (strcmp (p, "mtime") == 0)
612 printf ("%lu", (unsigned long) st.st_mtime);
613 else if (strcmp (p, "mtimeH") == 0)
614 print_time (st.st_mtime);
615 else if (strcmp (p, "ctime") == 0)
616 printf ("%lu", (unsigned long) st.st_ctime);
617 else if (strcmp (p, "ctimeH") == 0)
618 print_time (st.st_ctime);
619 else if (strcmp (p, "sparse") == 0)
620 printf ("%d", ST_IS_SPARSE (st));
624 error (EXIT_FAILURE, 0, _("Unknown field `%s'"), p);
626 p = strtok (NULL, ",");
638 exec_checkpoint (struct action *p)
641 printf ("processing checkpoint %lu\n", (unsigned long) p->checkpoint);
646 struct timespec ts[2];
648 ts[0] = ts[1] = p->ts;
649 if (utimens (p->name, ts) != 0)
651 error (0, errno, _("cannot set time on `%s'"), p->name);
659 FILE *fp = fopen (p->name, "ab");
662 error (0, errno, _("cannot open `%s'"), p->name);
666 fill (fp, p->size, p->pattern);
673 int fd = open (p->name, O_RDWR | O_BINARY);
676 error (0, errno, _("cannot open `%s'"), p->name);
679 ftruncate (fd, p->size);
694 process_checkpoint (size_t n)
696 struct action *p, *prev = NULL;
698 for (p = action_list; p; )
700 struct action *next = p->next;
702 if (p->checkpoint <= n)
705 /* Remove the item from the list */
719 #define CHECKPOINT_TEXT "Write checkpoint"
731 /* Insert --checkpoint option.
732 FIXME: This assumes that exec_argv does not use traditional tar options
735 exec_argv = xrealloc (exec_argv, (exec_argc + 1) * sizeof (*exec_argv));
736 memmove (exec_argv+2, exec_argv+1, (exec_argc - 1) * sizeof (*exec_argv));
737 exec_argv[1] = "--checkpoint";
740 /* System V fork+wait does not work if SIGCHLD is ignored. */
741 signal (SIGCHLD, SIG_DFL);
748 error (EXIT_FAILURE, errno, "fork");
759 /* Make sure POSIX locale is used */
760 setenv ("LC_ALL", "POSIX", 1);
762 execvp (exec_argv[0], exec_argv);
763 error (EXIT_FAILURE, errno, "execvp");
768 fp = fdopen (fd[0], "rb");
770 error (EXIT_FAILURE, errno, "fdopen");
772 while ((p = fgets (buf, sizeof buf, fp)))
774 while (*p && !isspace (*p) && *p != ':')
779 for (p++; *p && isspace (*p); p++)
783 && memcmp (p, CHECKPOINT_TEXT, sizeof CHECKPOINT_TEXT - 1) == 0)
786 size_t n = strtoul (p + sizeof CHECKPOINT_TEXT - 1, &end, 10);
787 if (!(*end && !isspace (*end)))
789 process_checkpoint (n);
794 fprintf (stderr, "%s", buf);
797 /* Collect exit status */
798 waitpid (pid, &status, 0);
802 if (WIFEXITED (status))
804 if (WEXITSTATUS (status) == 0)
805 printf (_("Command exited successfully\n"));
807 printf (_("Command failed with status %d\n"),
808 WEXITSTATUS (status));
810 else if (WIFSIGNALED (status))
811 printf (_("Command terminated on signal %d\n"), WTERMSIG (status));
812 else if (WIFSTOPPED (status))
813 printf (_("Command stopped on signal %d\n"), WSTOPSIG (status));
815 else if (WCOREDUMP (status))
816 printf (_("Command dumped core\n"));
819 printf(_("Command terminated\n"));
822 if (WIFEXITED (status))
823 exit (WEXITSTATUS (status));
828 main (int argc, char **argv)
832 program_name = argv[0];
833 setlocale (LC_ALL, "");
834 bindtextdomain (PACKAGE, LOCALEDIR);
835 textdomain (PACKAGE);
837 get_date (&touch_time, "now", NULL);
839 /* Decode command options. */
841 if (argp_parse (&argp, argc, argv, 0, &index, NULL))
851 error (EXIT_FAILURE, 0, _("--stat requires file names"));
854 print_stat (*argv++);
858 generate_sparse_file (argc, argv);
859 verify_file (file_name);
864 error (EXIT_FAILURE, 0, _("too many arguments"));
866 generate_files_from_list ();
869 generate_simple_file (file_name);
870 verify_file (file_name);