Commit 0849bf0821fc174621d6b91a3c0a5709639ddab4
1 parent
30503481
allow read only images
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@299 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
1 changed file
with
12 additions
and
3 deletions
block.c
| @@ -44,6 +44,7 @@ | @@ -44,6 +44,7 @@ | ||
| 44 | struct BlockDriverState { | 44 | struct BlockDriverState { |
| 45 | int fd; | 45 | int fd; |
| 46 | int64_t total_sectors; | 46 | int64_t total_sectors; |
| 47 | + int read_only; | ||
| 47 | }; | 48 | }; |
| 48 | 49 | ||
| 49 | BlockDriverState *bdrv_open(const char *filename) | 50 | BlockDriverState *bdrv_open(const char *filename) |
| @@ -55,11 +56,16 @@ BlockDriverState *bdrv_open(const char *filename) | @@ -55,11 +56,16 @@ BlockDriverState *bdrv_open(const char *filename) | ||
| 55 | bs = malloc(sizeof(BlockDriverState)); | 56 | bs = malloc(sizeof(BlockDriverState)); |
| 56 | if(!bs) | 57 | if(!bs) |
| 57 | return NULL; | 58 | return NULL; |
| 59 | + bs->read_only = 0; | ||
| 58 | fd = open(filename, O_RDWR); | 60 | fd = open(filename, O_RDWR); |
| 59 | if (fd < 0) { | 61 | if (fd < 0) { |
| 60 | - close(fd); | ||
| 61 | - free(bs); | ||
| 62 | - return NULL; | 62 | + fd = open(filename, O_RDONLY); |
| 63 | + if (fd < 0) { | ||
| 64 | + close(fd); | ||
| 65 | + free(bs); | ||
| 66 | + return NULL; | ||
| 67 | + } | ||
| 68 | + bs->read_only = 1; | ||
| 63 | } | 69 | } |
| 64 | size = lseek64(fd, 0, SEEK_END); | 70 | size = lseek64(fd, 0, SEEK_END); |
| 65 | bs->total_sectors = size / 512; | 71 | bs->total_sectors = size / 512; |
| @@ -93,6 +99,9 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num, | @@ -93,6 +99,9 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num, | ||
| 93 | { | 99 | { |
| 94 | int ret; | 100 | int ret; |
| 95 | 101 | ||
| 102 | + if (bs->read_only) | ||
| 103 | + return -1; | ||
| 104 | + | ||
| 96 | lseek64(bs->fd, sector_num * 512, SEEK_SET); | 105 | lseek64(bs->fd, sector_num * 512, SEEK_SET); |
| 97 | ret = write(bs->fd, buf, nb_sectors * 512); | 106 | ret = write(bs->fd, buf, nb_sectors * 512); |
| 98 | if (ret != nb_sectors * 512) | 107 | if (ret != nb_sectors * 512) |