From: Sergey Poznyakoff Date: Sat, 3 Jan 2004 22:27:36 +0000 (+0000) Subject: New file. X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=1665d38fb74067f82e3de2ec11020e84f3a0ad39;p=chaz%2Ftar New file. --- diff --git a/tests/mksparse.c b/tests/mksparse.c new file mode 100644 index 0000000..3a712f7 --- /dev/null +++ b/tests/mksparse.c @@ -0,0 +1,159 @@ +/* This file is part of GNU tar test suite + + Copyright (C) 2004 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifdef HAVE_CONFIG_H +# include +#endif +#include +#include +#include +#include +#include +#include + +char *progname; +char *buffer; +size_t buffer_size; + +void +die (char *fmt, ...) +{ + va_list ap; + + fprintf (stderr, "%s: ", progname); + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + fprintf (stderr, "\n"); +} + +void +mkhole (int fd, off_t displ) +{ + if (displ = lseek (fd, displ, SEEK_CUR) == -1) + { + perror ("lseek"); + exit (1); + } + ftruncate (fd, lseek (fd, 0, SEEK_CUR)); +} + +void +mksparse (int fd, off_t displ, char *marks) +{ + int i; + + for (; *marks; marks++) + { + memset (buffer, *marks, buffer_size); + if (write(fd, buffer, buffer_size) != buffer_size) + { + perror ("write"); + exit (1); + } + + if (lseek (fd, displ, SEEK_CUR) == -1) + { + perror ("lseek"); + exit (1); + } + } +} + +void +usage () +{ + printf ("Usage: mksparse filename blocksize disp letters [disp letters...] [disp]\n"); + exit (1); +} + +int +xlat_suffix (off_t *vp, char *p) +{ + if (p[1]) + return 1; + switch (p[0]) + { + case 'g': + case 'G': + *vp *= 1024; + + case 'm': + case 'M': + *vp *= 1024; + + case 'k': + case 'K': + *vp *= 1024; + break; + + default: + return 1; + } + return 0; +} + +int +main (int argc, char **argv) +{ + int i; + int fd; + char *p; + off_t n; + + progname = strrchr (argv[0], '/'); + if (progname) + progname++; + else + progname = argv[0]; + + if (argc < 4) + usage (); + + fd = open (argv[1], O_CREAT|O_TRUNC|O_RDWR, 0644); + if (fd < 0) + die ("cannot open %s", argv[1]); + + n = strtoul (argv[2], &p, 0); + if (n <= 0 || (*p && xlat_suffix (&n, p))) + die ("Invalid buffer size: %s", argv[2]); + buffer_size = n; + buffer = malloc (buffer_size); + if (!buffer) + die ("Not enough memory"); + + for (i = 3; i < argc; i += 2) + { + off_t displ; + + displ = strtoul (argv[i], &p, 0); + if (displ < 0 || (*p && xlat_suffix (&displ, p))) + die ("Invalid displacement: %s", argv[i]); + + if (i == argc-1) + { + mkhole (fd, displ); + break; + } + else + mksparse (fd, displ, argv[i+1]); + } + + close(fd); + return 0; +}