allow read only images
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@299 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
parent
305034817d
commit
0849bf0821
15
block.c
15
block.c
|
@ -44,6 +44,7 @@
|
||||||
struct BlockDriverState {
|
struct BlockDriverState {
|
||||||
int fd;
|
int fd;
|
||||||
int64_t total_sectors;
|
int64_t total_sectors;
|
||||||
|
int read_only;
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDriverState *bdrv_open(const char *filename)
|
BlockDriverState *bdrv_open(const char *filename)
|
||||||
|
@ -55,11 +56,16 @@ BlockDriverState *bdrv_open(const char *filename)
|
||||||
bs = malloc(sizeof(BlockDriverState));
|
bs = malloc(sizeof(BlockDriverState));
|
||||||
if(!bs)
|
if(!bs)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
bs->read_only = 0;
|
||||||
fd = open(filename, O_RDWR);
|
fd = open(filename, O_RDWR);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
close(fd);
|
fd = open(filename, O_RDONLY);
|
||||||
free(bs);
|
if (fd < 0) {
|
||||||
return NULL;
|
close(fd);
|
||||||
|
free(bs);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
bs->read_only = 1;
|
||||||
}
|
}
|
||||||
size = lseek64(fd, 0, SEEK_END);
|
size = lseek64(fd, 0, SEEK_END);
|
||||||
bs->total_sectors = size / 512;
|
bs->total_sectors = size / 512;
|
||||||
|
@ -93,6 +99,9 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
if (bs->read_only)
|
||||||
|
return -1;
|
||||||
|
|
||||||
lseek64(bs->fd, sector_num * 512, SEEK_SET);
|
lseek64(bs->fd, sector_num * 512, SEEK_SET);
|
||||||
ret = write(bs->fd, buf, nb_sectors * 512);
|
ret = write(bs->fd, buf, nb_sectors * 512);
|
||||||
if (ret != nb_sectors * 512)
|
if (ret != nb_sectors * 512)
|
||||||
|
|
Loading…
Reference in New Issue