44 |
struct BlockDriverState { |
struct BlockDriverState { |
45 |
int fd; |
int fd; |
46 |
int64_t total_sectors; |
int64_t total_sectors; |
47 |
|
int read_only; |
48 |
}; |
}; |
49 |
|
|
50 |
BlockDriverState *bdrv_open(const char *filename) |
BlockDriverState *bdrv_open(const char *filename) |
56 |
bs = malloc(sizeof(BlockDriverState)); |
bs = malloc(sizeof(BlockDriverState)); |
57 |
if(!bs) |
if(!bs) |
58 |
return NULL; |
return NULL; |
59 |
|
bs->read_only = 0; |
60 |
fd = open(filename, O_RDWR); |
fd = open(filename, O_RDWR); |
61 |
if (fd < 0) { |
if (fd < 0) { |
62 |
close(fd); |
fd = open(filename, O_RDONLY); |
63 |
free(bs); |
if (fd < 0) { |
64 |
return NULL; |
close(fd); |
65 |
|
free(bs); |
66 |
|
return NULL; |
67 |
|
} |
68 |
|
bs->read_only = 1; |
69 |
} |
} |
70 |
size = lseek64(fd, 0, SEEK_END); |
size = lseek64(fd, 0, SEEK_END); |
71 |
bs->total_sectors = size / 512; |
bs->total_sectors = size / 512; |
99 |
{ |
{ |
100 |
int ret; |
int ret; |
101 |
|
|
102 |
|
if (bs->read_only) |
103 |
|
return -1; |
104 |
|
|
105 |
lseek64(bs->fd, sector_num * 512, SEEK_SET); |
lseek64(bs->fd, sector_num * 512, SEEK_SET); |
106 |
ret = write(bs->fd, buf, nb_sectors * 512); |
ret = write(bs->fd, buf, nb_sectors * 512); |
107 |
if (ret != nb_sectors * 512) |
if (ret != nb_sectors * 512) |