| 305 | } |
| 306 | |
| 307 | static BlockDevice *block_device_init(const char *filename, |
| 308 | BlockDeviceModeEnum mode) |
| 309 | { |
| 310 | BlockDevice *bs; |
| 311 | BlockDeviceFile *bf; |
| 312 | int64_t file_size; |
| 313 | FILE *f; |
| 314 | const char *mode_str; |
| 315 | |
| 316 | if (mode == BF_MODE_RW) { |
| 317 | mode_str = "r+b"; |
| 318 | } else { |
| 319 | mode_str = "rb"; |
| 320 | } |
| 321 | |
| 322 | f = fopen(filename, mode_str); |
| 323 | if (!f) { |
| 324 | perror(filename); |
| 325 | exit(1); |
| 326 | } |
| 327 | fseek(f, 0, SEEK_END); |
| 328 | file_size = ftello(f); |
| 329 | |
| 330 | bs = mallocz(sizeof(*bs)); |
| 331 | bf = mallocz(sizeof(*bf)); |
| 332 | |
| 333 | bf->mode = mode; |
| 334 | bf->nb_sectors = file_size / 512; |
| 335 | bf->f = f; |
| 336 | |
| 337 | if (mode == BF_MODE_SNAPSHOT) { |
| 338 | bf->sector_table = mallocz(sizeof(bf->sector_table[0]) * |
| 339 | bf->nb_sectors); |
| 340 | } |
| 341 | |
| 342 | bs->opaque = bf; |
| 343 | bs->get_sector_count = bf_get_sector_count; |
| 344 | bs->read_async = bf_read_async; |
| 345 | bs->write_async = bf_write_async; |
| 346 | return bs; |
| 347 | } |
| 348 | |
| 349 | #ifndef _WIN32 |
| 350 | |