]>
Commit | Line | Data |
---|---|---|
1e3baf05 DM |
1 | /* |
2 | Copyright (C) 2007-2009 Proxmox Server Solutions GmbH | |
3 | ||
4 | Copyright: vzdump is under GNU GPL, the GNU General Public License. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; version 2 dated June, 1991. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the | |
17 | Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
18 | MA 02110-1301, USA. | |
19 | ||
20 | Author: Dietmar Maurer <dietmar@proxmox.com> | |
21 | ||
22 | */ | |
23 | ||
24 | #define _GNU_SOURCE | |
25 | ||
26 | #include <sys/types.h> | |
27 | #include <sys/stat.h> | |
28 | #include <fcntl.h> | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <unistd.h> | |
32 | #include <errno.h> | |
33 | #include <string.h> | |
34 | #include <time.h> | |
35 | #include <stdint.h> | |
36 | #include <getopt.h> | |
37 | #include <signal.h> | |
38 | ||
39 | #include "utils.c" | |
40 | ||
41 | #define BLOCKSIZE 512*8 | |
42 | ||
43 | static char *outname; | |
44 | ||
45 | static void | |
46 | cleanup (void) | |
47 | { | |
48 | if (outname) | |
49 | unlink (outname); | |
50 | } | |
51 | ||
52 | void term_handler() | |
53 | { | |
54 | fprintf (stderr, "received signal - terminate process\n"); | |
55 | exit(-1); | |
56 | } | |
57 | ||
58 | size_t | |
59 | sparse_cp (int infd, int outfd) | |
60 | { | |
61 | size_t total = 0; | |
62 | size_t count; | |
63 | char buffer[BLOCKSIZE]; | |
64 | int last_write_made_hole = 0; | |
65 | ||
66 | while ((count = safe_read (infd, buffer, sizeof (buffer))) > 0) { | |
67 | if (block_is_zero (buffer, count)) { | |
68 | ||
69 | if (lseek (outfd, count, SEEK_CUR) < 0) { | |
70 | perror ("cannot lseek\n"); | |
71 | exit (-1); | |
72 | } | |
73 | last_write_made_hole = 1; | |
74 | } else { | |
75 | full_write (outfd, buffer, count); | |
76 | last_write_made_hole = 0; | |
77 | } | |
78 | total += count; | |
79 | } | |
80 | ||
81 | if (last_write_made_hole) { | |
82 | if (ftruncate (outfd, total) < 0) { | |
83 | perror ("cannot ftruncate\n"); | |
84 | exit (-1); | |
85 | } | |
86 | } | |
87 | ||
88 | return total; | |
89 | } | |
90 | ||
91 | int | |
92 | main (int argc, char **argv) | |
93 | { | |
94 | struct sigaction sa; | |
95 | ||
96 | if (argc != 2) { | |
97 | fprintf (stderr, "wrong number of arguments\n"); | |
98 | exit (-1); | |
99 | } | |
100 | ||
101 | time_t starttime = time(NULL); | |
102 | ||
103 | outname = argv[1]; | |
104 | ||
105 | int outfd; | |
106 | ||
107 | if ((outfd = open(outname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) { | |
108 | fprintf (stderr, "unable to open file '%s' - %s\n", | |
109 | outname, strerror (errno)); | |
110 | exit (-1); | |
111 | } | |
112 | atexit(cleanup); | |
113 | ||
114 | setsig(&sa, SIGINT, term_handler, SA_RESTART); | |
115 | setsig(&sa, SIGQUIT, term_handler, SA_RESTART); | |
116 | setsig(&sa, SIGTERM, term_handler, SA_RESTART); | |
117 | setsig(&sa, SIGPIPE, term_handler, SA_RESTART); | |
118 | ||
119 | size_t total = sparse_cp (0, outfd); | |
120 | ||
121 | close (outfd); | |
122 | ||
123 | time_t delay = time(NULL) - starttime; | |
124 | if (delay <= 0) delay = 1; | |
125 | ||
126 | fprintf (stderr, "%zu bytes copied, %d s, %.2f MiB/s\n", total, delay, | |
127 | (total/(1024*1024))/(float)delay); | |
128 | ||
129 | outname = NULL; | |
130 | ||
131 | exit (0); | |
132 | } |