block.c (c747cd1fa26d1ed0cc009302fb9c75a8f03651e2) block.c (7674e7bf08c0be8e6872f8602e2d875b3efb6903)
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 10 unchanged lines hidden (view full) ---

19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25#include "block_int.h"
26
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 10 unchanged lines hidden (view full) ---

19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25#include "block_int.h"
26
27#ifdef _BSD
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/ioctl.h>
31#include <sys/queue.h>
32#include <sys/disk.h>
33#endif
34
27static BlockDriverState *bdrv_first;
28static BlockDriver *first_drv;
29
30void bdrv_register(BlockDriver *bdrv)
31{
32 bdrv->next = first_drv;
33 first_drv = bdrv;
34}

--- 48 unchanged lines hidden (view full) ---

83 int fd;
84 /* XXX: race condition possible */
85 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
86 fd = mkstemp(filename);
87 close(fd);
88}
89#endif
90
35static BlockDriverState *bdrv_first;
36static BlockDriver *first_drv;
37
38void bdrv_register(BlockDriver *bdrv)
39{
40 bdrv->next = first_drv;
41 first_drv = bdrv;
42}

--- 48 unchanged lines hidden (view full) ---

91 int fd;
92 /* XXX: race condition possible */
93 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
94 fd = mkstemp(filename);
95 close(fd);
96}
97#endif
98
99/* XXX: force raw format if block or character device ? It would
100 simplify the BSD case */
91static BlockDriver *find_image_format(const char *filename)
92{
93 int fd, ret, score, score_max;
94 BlockDriver *drv1, *drv;
101static BlockDriver *find_image_format(const char *filename)
102{
103 int fd, ret, score, score_max;
104 BlockDriver *drv1, *drv;
95 uint8_t buf[1024];
105 uint8_t *buf;
106 size_t bufsize = 1024;
96
97 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
98 if (fd < 0)
99 return NULL;
107
108 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
109 if (fd < 0)
110 return NULL;
100 ret = read(fd, buf, sizeof(buf));
111#ifdef DIOCGSECTORSIZE
112 {
113 unsigned int sectorsize = 512;
114 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
115 sectorsize > bufsize)
116 bufsize = sectorsize;
117 }
118#endif
119 buf = malloc(bufsize);
120 if (!buf)
121 return NULL;
122 ret = read(fd, buf, bufsize);
101 if (ret < 0) {
102 close(fd);
123 if (ret < 0) {
124 close(fd);
125 free(buf);
103 return NULL;
104 }
105 close(fd);
106
107 drv = NULL;
108 score_max = 0;
109 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
110 score = drv1->bdrv_probe(buf, ret, filename);
111 if (score > score_max) {
112 score_max = score;
113 drv = drv1;
114 }
115 }
126 return NULL;
127 }
128 close(fd);
129
130 drv = NULL;
131 score_max = 0;
132 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
133 score = drv1->bdrv_probe(buf, ret, filename);
134 if (score > score_max) {
135 score_max = score;
136 drv = drv1;
137 }
138 }
139 free(buf);
116 return drv;
117}
118
119int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
120{
121 return bdrv_open2(bs, filename, snapshot, NULL);
122}
123

--- 403 unchanged lines hidden (view full) ---

527
528 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
529 if (fd < 0) {
530 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
531 if (fd < 0)
532 return -1;
533 bs->read_only = 1;
534 }
140 return drv;
141}
142
143int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
144{
145 return bdrv_open2(bs, filename, snapshot, NULL);
146}
147

--- 403 unchanged lines hidden (view full) ---

551
552 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
553 if (fd < 0) {
554 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
555 if (fd < 0)
556 return -1;
557 bs->read_only = 1;
558 }
535 size = lseek(fd, 0, SEEK_END);
559#ifdef _BSD
560 {
561 struct stat sb;
562 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
563#ifdef DIOCGMEDIASIZE
564 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
565#endif
566 size = lseek(fd, 0LL, SEEK_END);
567 } else
568#endif
569 {
570 size = lseek(fd, 0, SEEK_END);
571 }
536#ifdef _WIN32
537 /* On Windows hosts it can happen that we're unable to get file size
538 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
539 if (size == -1)
540 size = LONG_LONG_MAX;
541#endif
542 bs->total_sectors = size / 512;
543 s->fd = fd;

--- 75 unchanged lines hidden ---
572#ifdef _WIN32
573 /* On Windows hosts it can happen that we're unable to get file size
574 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
575 if (size == -1)
576 size = LONG_LONG_MAX;
577#endif
578 bs->total_sectors = size / 512;
579 s->fd = fd;

--- 75 unchanged lines hidden ---