Revision 67b915a5 block.c
b/block.c | ||
---|---|---|
21 | 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22 | 22 |
* THE SOFTWARE. |
23 | 23 |
*/ |
24 |
#include <stdlib.h> |
|
25 |
#include <stdio.h> |
|
26 |
#include <stdarg.h> |
|
27 |
#include <string.h> |
|
28 |
#include <getopt.h> |
|
29 |
#include <inttypes.h> |
|
30 |
#include <unistd.h> |
|
31 |
#include <sys/mman.h> |
|
32 |
#include <fcntl.h> |
|
33 |
#include <signal.h> |
|
34 |
#include <time.h> |
|
35 |
#include <sys/time.h> |
|
36 |
#include <malloc.h> |
|
37 |
#include <termios.h> |
|
38 |
#include <sys/poll.h> |
|
39 |
#include <errno.h> |
|
40 |
#include <sys/wait.h> |
|
41 |
#include <netinet/in.h> |
|
42 |
|
|
43 | 24 |
#include "vl.h" |
44 | 25 |
|
45 |
#define NO_THUNK_TYPE_SIZE |
|
46 |
#include "thunk.h" |
|
26 |
#ifndef _WIN32 |
|
27 |
#include <sys/mman.h> |
|
28 |
#endif |
|
47 | 29 |
|
48 | 30 |
#include "cow.h" |
49 | 31 |
|
... | ... | |
97 | 79 |
|
98 | 80 |
int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
99 | 81 |
{ |
100 |
int fd, cow_fd;
|
|
82 |
int fd; |
|
101 | 83 |
int64_t size; |
102 |
char template[] = "/tmp/vl.XXXXXX"; |
|
103 | 84 |
struct cow_header_v2 cow_header; |
85 |
#ifndef _WIN32 |
|
86 |
char template[] = "/tmp/vl.XXXXXX"; |
|
87 |
int cow_fd; |
|
104 | 88 |
struct stat st; |
89 |
#endif |
|
105 | 90 |
|
106 | 91 |
bs->read_only = 0; |
107 | 92 |
bs->fd = -1; |
... | ... | |
110 | 95 |
strcpy(bs->filename, filename); |
111 | 96 |
|
112 | 97 |
/* open standard HD image */ |
98 |
#ifdef _WIN32 |
|
99 |
fd = open(filename, O_RDWR | O_BINARY); |
|
100 |
#else |
|
113 | 101 |
fd = open(filename, O_RDWR | O_LARGEFILE); |
102 |
#endif |
|
114 | 103 |
if (fd < 0) { |
115 | 104 |
/* read only image on disk */ |
105 |
#ifdef _WIN32 |
|
106 |
fd = open(filename, O_RDONLY | O_BINARY); |
|
107 |
#else |
|
116 | 108 |
fd = open(filename, O_RDONLY | O_LARGEFILE); |
109 |
#endif |
|
117 | 110 |
if (fd < 0) { |
118 | 111 |
perror(filename); |
119 | 112 |
goto fail; |
... | ... | |
128 | 121 |
fprintf(stderr, "%s: could not read header\n", filename); |
129 | 122 |
goto fail; |
130 | 123 |
} |
131 |
if (cow_header.magic == htonl(COW_MAGIC) && |
|
132 |
cow_header.version == htonl(COW_VERSION)) { |
|
124 |
#ifndef _WIN32 |
|
125 |
if (be32_to_cpu(cow_header.magic) == COW_MAGIC && |
|
126 |
be32_to_cpu(cow_header.version) == COW_VERSION) { |
|
133 | 127 |
/* cow image found */ |
134 | 128 |
size = cow_header.size; |
135 | 129 |
#ifndef WORDS_BIGENDIAN |
... | ... | |
144 | 138 |
fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file); |
145 | 139 |
goto fail; |
146 | 140 |
} |
147 |
if (st.st_mtime != htonl(cow_header.mtime)) {
|
|
141 |
if (st.st_mtime != be32_to_cpu(cow_header.mtime)) {
|
|
148 | 142 |
fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file); |
149 | 143 |
goto fail; |
150 | 144 |
} |
... | ... | |
164 | 158 |
bs->cow_bitmap = bs->cow_bitmap_addr + sizeof(cow_header); |
165 | 159 |
bs->cow_sectors_offset = (bs->cow_bitmap_size + 511) & ~511; |
166 | 160 |
snapshot = 0; |
167 |
} else { |
|
161 |
} else |
|
162 |
#endif |
|
163 |
{ |
|
168 | 164 |
/* standard raw image */ |
169 | 165 |
size = lseek64(fd, 0, SEEK_END); |
170 | 166 |
bs->total_sectors = size / 512; |
171 | 167 |
bs->fd = fd; |
172 | 168 |
} |
173 | 169 |
|
170 |
#ifndef _WIN32 |
|
174 | 171 |
if (snapshot) { |
175 | 172 |
/* create a temporary COW file */ |
176 | 173 |
cow_fd = mkstemp(template); |
... | ... | |
190 | 187 |
bs->cow_bitmap = bs->cow_bitmap_addr; |
191 | 188 |
bs->cow_sectors_offset = 0; |
192 | 189 |
} |
190 |
#endif |
|
193 | 191 |
|
194 | 192 |
bs->inserted = 1; |
195 | 193 |
|
... | ... | |
206 | 204 |
void bdrv_close(BlockDriverState *bs) |
207 | 205 |
{ |
208 | 206 |
if (bs->inserted) { |
207 |
#ifndef _WIN32 |
|
209 | 208 |
/* we unmap the mapping so that it is written to the COW file */ |
210 | 209 |
if (bs->cow_bitmap_addr) |
211 | 210 |
munmap(bs->cow_bitmap_addr, bs->cow_bitmap_size); |
211 |
#endif |
|
212 | 212 |
if (bs->cow_fd >= 0) |
213 | 213 |
close(bs->cow_fd); |
214 | 214 |
if (bs->fd >= 0) |
Also available in: Unified diff