aboutsummaryrefslogtreecommitdiff
path: root/block.c
diff options
context:
space:
mode:
authorbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>2003-06-30 23:17:31 +0000
committerbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>2003-06-30 23:17:31 +0000
commit0849bf0821fc174621d6b91a3c0a5709639ddab4 (patch)
tree29862cb199f0ce3a0f821f7ec6b6dc56ca634bb2 /block.c
parent305034817d3ee36bd40895b72234e80a32d06f16 (diff)
downloadqemu-0849bf0821fc174621d6b91a3c0a5709639ddab4.zip
qemu-0849bf0821fc174621d6b91a3c0a5709639ddab4.tar.gz
qemu-0849bf0821fc174621d6b91a3c0a5709639ddab4.tar.bz2
allow read only images
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@299 c046a42c-6fe2-441c-8c8c-71466251a162
Diffstat (limited to 'block.c')
-rw-r--r--block.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/block.c b/block.c
index 1ac8210..c687ff7 100644
--- a/block.c
+++ b/block.c
@@ -44,6 +44,7 @@
struct BlockDriverState {
int fd;
int64_t total_sectors;
+ int read_only;
};
BlockDriverState *bdrv_open(const char *filename)
@@ -55,11 +56,16 @@ BlockDriverState *bdrv_open(const char *filename)
bs = malloc(sizeof(BlockDriverState));
if(!bs)
return NULL;
+ bs->read_only = 0;
fd = open(filename, O_RDWR);
if (fd < 0) {
- close(fd);
- free(bs);
- return NULL;
+ fd = open(filename, O_RDONLY);
+ if (fd < 0) {
+ close(fd);
+ free(bs);
+ return NULL;
+ }
+ bs->read_only = 1;
}
size = lseek64(fd, 0, SEEK_END);
bs->total_sectors = size / 512;
@@ -93,6 +99,9 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num,
{
int ret;
+ if (bs->read_only)
+ return -1;
+
lseek64(bs->fd, sector_num * 512, SEEK_SET);
ret = write(bs->fd, buf, nb_sectors * 512);
if (ret != nb_sectors * 512)