24 |
#include "vl.h" |
#include "vl.h" |
25 |
#include "block_int.h" |
#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 |
|
|
35 |
static BlockDriverState *bdrv_first; |
static BlockDriverState *bdrv_first; |
36 |
static BlockDriver *first_drv; |
static BlockDriver *first_drv; |
37 |
|
|
96 |
} |
} |
97 |
#endif |
#endif |
98 |
|
|
99 |
|
/* XXX: force raw format if block or character device ? It would |
100 |
|
simplify the BSD case */ |
101 |
static BlockDriver *find_image_format(const char *filename) |
static BlockDriver *find_image_format(const char *filename) |
102 |
{ |
{ |
103 |
int fd, ret, score, score_max; |
int fd, ret, score, score_max; |
104 |
BlockDriver *drv1, *drv; |
BlockDriver *drv1, *drv; |
105 |
uint8_t buf[1024]; |
uint8_t *buf; |
106 |
|
size_t bufsize = 1024; |
107 |
|
|
108 |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
109 |
if (fd < 0) |
if (fd < 0) |
110 |
return NULL; |
return NULL; |
111 |
ret = read(fd, buf, sizeof(buf)); |
#ifdef DIOCGSECTORSIZE |
112 |
|
{ |
113 |
|
unsigned int sectorsize = 512; |
114 |
|
if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && |
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); |
123 |
if (ret < 0) { |
if (ret < 0) { |
124 |
close(fd); |
close(fd); |
125 |
|
free(buf); |
126 |
return NULL; |
return NULL; |
127 |
} |
} |
128 |
close(fd); |
close(fd); |
136 |
drv = drv1; |
drv = drv1; |
137 |
} |
} |
138 |
} |
} |
139 |
|
free(buf); |
140 |
return drv; |
return drv; |
141 |
} |
} |
142 |
|
|
556 |
return -1; |
return -1; |
557 |
bs->read_only = 1; |
bs->read_only = 1; |
558 |
} |
} |
559 |
size = lseek(fd, 0, SEEK_END); |
#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 |
|
} |
572 |
#ifdef _WIN32 |
#ifdef _WIN32 |
573 |
/* On Windows hosts it can happen that we're unable to get file size |
/* 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). */ |
for CD-ROM raw device (it's inherent limitation of the CDFS driver). */ |