]>
Commit | Line | Data |
---|---|---|
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 <signal.h> | |
35 | ||
36 | /* Set a signal handler */ | |
37 | static void | |
38 | setsig (struct sigaction *sa, int sig, void (*fun)(int), int flags) | |
39 | { | |
40 | sa->sa_handler = fun; | |
41 | sa->sa_flags = flags; | |
42 | sigemptyset(&sa->sa_mask); | |
43 | sigaction(sig, sa, NULL); | |
44 | } | |
45 | ||
46 | int | |
47 | block_is_zero (char const *buffer, size_t size) | |
48 | { | |
49 | while (size--) | |
50 | if (*buffer++) | |
51 | return 0; | |
52 | ||
53 | return 1; | |
54 | } | |
55 | ||
56 | ssize_t | |
57 | safe_read(int fd, char *buf, size_t count) | |
58 | { | |
59 | ssize_t n; | |
60 | ||
61 | do { | |
62 | n = read(fd, buf, count); | |
63 | } while (n < 0 && errno == EINTR); | |
64 | ||
65 | return n; | |
66 | } | |
67 | ||
68 | int | |
69 | full_read(int fd, char *buf, size_t len) | |
70 | { | |
71 | ssize_t n; | |
72 | size_t total; | |
73 | ||
74 | total = 0; | |
75 | ||
76 | while (len > 0) { | |
77 | n = safe_read(fd, buf, len); | |
78 | ||
79 | if (n == 0) | |
80 | return total; | |
81 | ||
82 | if (n < 0) | |
83 | break; | |
84 | ||
85 | buf += n; | |
86 | total += n; | |
87 | len -= n; | |
88 | } | |
89 | ||
90 | if (len) { | |
91 | fprintf (stderr, "ERROR: incomplete read detected\n"); | |
92 | exit (-1); | |
93 | } | |
94 | ||
95 | return total; | |
96 | } | |
97 | ||
98 | ssize_t | |
99 | safe_write(int fd, char *buf, size_t count) | |
100 | { | |
101 | ssize_t n; | |
102 | ||
103 | do { | |
104 | n = write(fd, buf, count); | |
105 | } while (n < 0 && errno == EINTR); | |
106 | ||
107 | return n; | |
108 | } | |
109 | ||
110 | int | |
111 | full_write(int fd, char *buf, size_t len) | |
112 | { | |
113 | ssize_t n; | |
114 | size_t total; | |
115 | ||
116 | total = 0; | |
117 | ||
118 | while (len > 0) { | |
119 | n = safe_write(fd, buf, len); | |
120 | ||
121 | if (n < 0) | |
122 | break; | |
123 | ||
124 | buf += n; | |
125 | total += n; | |
126 | len -= n; | |
127 | } | |
128 | ||
129 | if (len) { | |
130 | fprintf (stderr, "ERROR: incomplete write detected\n"); | |
131 | exit (-1); | |
132 | } | |
133 | ||
134 | return total; | |
135 | } |