Skip to content

Commit

Permalink
Autodetect image format
Browse files Browse the repository at this point in the history
The basics are in there, but it's a bit rough around the edges
  • Loading branch information
nepx committed Jun 10, 2020
1 parent fbfa3a9 commit 8ac6c89
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/drive.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ void drive_state(struct drive_info* info, char* filename);

#ifdef EMSCRIPTEN
int drive_internal_init(struct drive_info* info, char* filename, void* info_dat, int);
#else
int drive_autodetect_type(char* path);
#endif

int drive_init(struct drive_info* info, char* path);
Expand Down
30 changes: 30 additions & 0 deletions src/drive.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef EMSCRIPTEN
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <zlib.h>
Expand Down Expand Up @@ -254,6 +255,9 @@ static int drive_internal_read_check(struct drive_internal_info* this, void* buf
if (end == 0)
end = BLOCK_SIZE;
}
#if 0 && defined(EMSCRIPTEN)
blockInformation->data = (void*)1;
#endif
len = end - begin;
//printf("BlockInformation: %p Data: %p cfp=%d\n", blockInformation, blockInformation->data, currentFilePosition / 512);
if (blockInformation->data) {
Expand Down Expand Up @@ -916,4 +920,30 @@ void drive_destroy_simple(struct drive_info* info)
free(simple_info->blocks);
free(simple_info);
}

#ifndef EMSCRIPTEN
// Autodetect drive type
int drive_autodetect_type(char* path)
{
struct stat statbuf;

// Check for URL
if (strstr(path, "http://") != NULL || strstr(path, "https://") != NULL)
return 2;

int fd = open(path, O_RDONLY);
if (fd < 0)
return -1;
if(fstat(fd, &statbuf)){
close(fd);
return -1;
}
close(fd);
if(S_ISDIR(statbuf.st_mode))
return 0; // Chunked file
else
return 1; // Raw image file

}
#endif
#endif
18 changes: 17 additions & 1 deletion src/ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ static const struct ini_enum driver_types[] = {
{ "raw", 1 },
{ "chunked", 0 },
{ "normal", 0 },
{ "network", 2 },
{ "net", 2 },
{ NULL, 0 }
};

Expand All @@ -278,8 +280,22 @@ static int parse_disk(struct drive_info* drv, struct ini_section* s, int id)

// Determine the media type
drv->type = get_field_enum(s, "type", drive_types, DRIVE_TYPE_DISK);
int driver = get_field_enum(s, "driver", driver_types, 0), inserted = get_field_int(s, "inserted", 0), wb = get_field_int(s, "writeback", 0);
int driver = get_field_enum(s, "driver", driver_types, -1), inserted = get_field_int(s, "inserted", 0), wb = get_field_int(s, "writeback", 0);
char* path = get_field_string(s, "file");

if(driver < 0 && inserted){
#ifndef EMSCRIPTEN
// Try auto-detecting driver type if not specified.
driver = drive_autodetect_type(path);
if(driver < 0)
FATAL("INI", "Unable to determine driver to use for ata%d-%s!\n", id >> 1, id & 1 ? "slave" : "master");

#else
// The wrapper code already knows what driver we have. It knows best.
driver = 0;
#endif
}

if(driver == 0 && wb)
printf("WARNING: Disk %d uses async (chunked) driver but writeback is not supported!!\n", id);
drv->modify_backing_file = wb;
Expand Down

0 comments on commit 8ac6c89

Please sign in to comment.