]>
git.proxmox.com Git - qemu-server.git/blob - sparsecp.c
2 Copyright (C) 2007-2009 Proxmox Server Solutions GmbH
4 Copyright: vzdump is under GNU GPL, the GNU General Public License.
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.
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.
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,
20 Author: Dietmar Maurer <dietmar@proxmox.com>
26 #include <sys/types.h>
41 #define BLOCKSIZE 512*8
54 fprintf (stderr
, "received signal - terminate process\n");
59 sparse_cp (int infd
, int outfd
)
63 char buffer
[BLOCKSIZE
];
64 int last_write_made_hole
= 0;
66 while ((count
= safe_read (infd
, buffer
, sizeof (buffer
))) > 0) {
67 if (block_is_zero (buffer
, count
)) {
69 if (lseek (outfd
, count
, SEEK_CUR
) < 0) {
70 perror ("cannot lseek\n");
73 last_write_made_hole
= 1;
75 full_write (outfd
, buffer
, count
);
76 last_write_made_hole
= 0;
81 if (last_write_made_hole
) {
82 if (ftruncate (outfd
, total
) < 0) {
83 perror ("cannot ftruncate\n");
92 main (int argc
, char **argv
)
97 fprintf (stderr
, "wrong number of arguments\n");
101 time_t starttime
= time(NULL
);
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
));
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
);
119 size_t total
= sparse_cp (0, outfd
);
123 time_t delay
= time(NULL
) - starttime
;
124 if (delay
<= 0) delay
= 1;
126 fprintf (stderr
, "%zu bytes copied, %zd s, %.2f MiB/s\n", total
, delay
,
127 (total
/(1024*1024))/(float)delay
);