Commit 0849bf0821fc174621d6b91a3c0a5709639ddab4

Authored by bellard
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
... ... @@ -44,6 +44,7 @@
44 44 struct BlockDriverState {
45 45 int fd;
46 46 int64_t total_sectors;
  47 + int read_only;
47 48 };
48 49  
49 50 BlockDriverState *bdrv_open(const char *filename)
... ... @@ -55,11 +56,16 @@ BlockDriverState *bdrv_open(const char *filename)
55 56 bs = malloc(sizeof(BlockDriverState));
56 57 if(!bs)
57 58 return NULL;
  59 + bs->read_only = 0;
58 60 fd = open(filename, O_RDWR);
59 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 70 size = lseek64(fd, 0, SEEK_END);
65 71 bs->total_sectors = size / 512;
... ... @@ -93,6 +99,9 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num,
93 99 {
94 100 int ret;
95 101  
  102 + if (bs->read_only)
  103 + return -1;
  104 +
96 105 lseek64(bs->fd, sector_num * 512, SEEK_SET);
97 106 ret = write(bs->fd, buf, nb_sectors * 512);
98 107 if (ret != nb_sectors * 512)
... ...