mirror of
https://github.com/mauiaaron/apple2.git
synced 2025-01-10 23:29:43 +00:00
User lower-level I/O with temp failure retries
This commit is contained in:
parent
9eee9293bc
commit
57b7d930d6
15
src/disk.c
15
src/disk.c
@ -703,7 +703,7 @@ const char *c_eject_6(int drive) {
|
||||
const char *err = NULL;
|
||||
|
||||
// foo.dsk -> foo.dsk.gz
|
||||
err = def(disk6.disk[drive].file_name, is_nib(disk6.disk[drive].file_name) ? NIB_SIZE : DSK_SIZE);
|
||||
err = zlib_deflate(disk6.disk[drive].file_name, is_nib(disk6.disk[drive].file_name) ? NIB_SIZE : DSK_SIZE);
|
||||
if (err) {
|
||||
ERRLOG("OOPS: An error occurred when attempting to compress a disk image : %s", err);
|
||||
} else {
|
||||
@ -732,17 +732,10 @@ const char *c_new_diskette_6(int drive, const char * const raw_file_name, int fo
|
||||
/* uncompress the gziped disk */
|
||||
char *file_name = strdup(raw_file_name);
|
||||
if (is_gz(file_name)) {
|
||||
int rawcount = 0;
|
||||
const char *err = inf(file_name, &rawcount); // foo.dsk.gz -> foo.dsk
|
||||
if (!err) {
|
||||
int expected = is_nib(file_name) ? NIB_SIZE : DSK_SIZE;
|
||||
if (rawcount != expected) {
|
||||
err = "disk image is not expected size!";
|
||||
}
|
||||
}
|
||||
const char *err = zlib_inflate(file_name, is_nib(file_name) ? NIB_SIZE : DSK_SIZE); // foo.dsk.gz -> foo.dsk
|
||||
if (err) {
|
||||
ERRLOG("OOPS: An error occurred when attempting to inflate/load a disk image : %s", err);
|
||||
free(file_name);
|
||||
FREE(file_name);
|
||||
return err;
|
||||
}
|
||||
if (unlink(file_name)) { // temporarily remove .gz file
|
||||
@ -759,7 +752,7 @@ const char *c_new_diskette_6(int drive, const char * const raw_file_name, int fo
|
||||
if (is_po(file_name)) {
|
||||
disk6.disk[drive].skew_table = skew_table_6_po;
|
||||
}
|
||||
free(file_name);
|
||||
FREE(file_name);
|
||||
file_name = NULL;
|
||||
|
||||
if (disk6.disk[drive].fp) {
|
||||
|
@ -36,11 +36,12 @@
|
||||
#else
|
||||
#define CHUNK 16384
|
||||
#endif
|
||||
#define UNKNOWN_ERR 42
|
||||
|
||||
/* report a zlib or i/o error */
|
||||
static const char* const _gzerr(gzFile gzf)
|
||||
{
|
||||
static const char* const _gzerr(gzFile gzf) {
|
||||
if (gzf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
int err_num = 0;
|
||||
const char *reason = gzerror(gzf, &err_num);
|
||||
if (err_num == Z_ERRNO) {
|
||||
@ -55,10 +56,9 @@ static const char* const _gzerr(gzFile gzf)
|
||||
for processing, Z_VERSION_ERROR if the version of zlib.h and the version of
|
||||
the library linked do not match, or Z_ERRNO if there is an error reading or
|
||||
writing the files. */
|
||||
const char *def(const char* const src, const int expected_bytecount)
|
||||
{
|
||||
const char *zlib_deflate(const char* const src, const int expected_bytecount) {
|
||||
unsigned char buf[CHUNK];
|
||||
FILE *source = NULL;
|
||||
int fd = -1;
|
||||
gzFile gzdest = NULL;
|
||||
|
||||
if (src == NULL) {
|
||||
@ -66,15 +66,16 @@ const char *def(const char* const src, const int expected_bytecount)
|
||||
}
|
||||
|
||||
int bytecount = 0;
|
||||
int ret = UNKNOWN_ERR;
|
||||
char *err = NULL;
|
||||
char dst[PATH_MAX] = { 0 };
|
||||
do {
|
||||
source = fopen(src, "r");
|
||||
if (source == NULL) {
|
||||
TEMP_FAILURE_RETRY(fd = open(src, O_RDONLY));
|
||||
if (fd == -1) {
|
||||
ERRLOG("Cannot open file '%s' for reading", src);
|
||||
err = ZERR_DEFLATE_OPEN_SOURCE;
|
||||
break;
|
||||
}
|
||||
|
||||
char dst[PATH_MAX];
|
||||
snprintf(dst, PATH_MAX-1, "%s%s", src, ".gz");
|
||||
|
||||
gzdest = gzopen(dst, "wb");
|
||||
@ -84,8 +85,7 @@ const char *def(const char* const src, const int expected_bytecount)
|
||||
}
|
||||
|
||||
#if !defined(ANDROID)
|
||||
int err = gzbuffer(gzdest, CHUNK);
|
||||
if (err != Z_OK) {
|
||||
if (gzbuffer(gzdest, CHUNK) != Z_OK) {
|
||||
ERRLOG("Cannot set bufsize on gz output");
|
||||
break;
|
||||
}
|
||||
@ -93,57 +93,51 @@ const char *def(const char* const src, const int expected_bytecount)
|
||||
|
||||
// deflate ...
|
||||
do {
|
||||
size_t buflen = fread(buf, 1, CHUNK, source);
|
||||
ssize_t buflen = 0;
|
||||
TEMP_FAILURE_RETRY(buflen = read(fd, buf, CHUNK));
|
||||
|
||||
if (ferror(source)) {
|
||||
ERRLOG("OOPS fread ...");
|
||||
if (buflen < 0) {
|
||||
ERRLOG("OOPS read ...");
|
||||
err = ZERR_DEFLATE_READ_SOURCE;
|
||||
break;
|
||||
} else if (buflen == 0) {
|
||||
break; // DONE
|
||||
}
|
||||
|
||||
size_t written = gzwrite(gzdest, buf, buflen);
|
||||
if (written < buflen) {
|
||||
ERRLOG("OOPS gzwrite ...");
|
||||
break;
|
||||
}
|
||||
|
||||
if (buflen > 0) {
|
||||
unsigned int buflen_ = 0;
|
||||
if (buflen > UINT_MAX) {
|
||||
ERRLOG("OOPS buffer is huge!");
|
||||
break;
|
||||
} else {
|
||||
buflen_ = (unsigned int)buflen;
|
||||
}
|
||||
size_t written = gzwrite(gzdest, buf, buflen_);
|
||||
if (written < buflen) {
|
||||
ERRLOG("OOPS gzwrite ...");
|
||||
break;
|
||||
}
|
||||
bytecount += written;
|
||||
}
|
||||
|
||||
if (feof(source)) {
|
||||
if (bytecount != expected_bytecount) {
|
||||
ERRLOG("OOPS did not write expected_bytecount of %d ... apparently wrote %d", expected_bytecount, bytecount);
|
||||
ret = UNKNOWN_ERR;
|
||||
} else {
|
||||
ret = Z_OK;
|
||||
}
|
||||
break; // finished deflating
|
||||
}
|
||||
bytecount += written;
|
||||
} while (1);
|
||||
|
||||
} while (0);
|
||||
|
||||
const char *err = NULL;
|
||||
if (ret != Z_OK) {
|
||||
err = _gzerr(gzdest);
|
||||
if (bytecount != expected_bytecount) {
|
||||
ERRLOG("OOPS did not write expected_bytecount of %d ... apparently wrote %d", expected_bytecount, bytecount);
|
||||
if (gzdest) {
|
||||
err = (char *)_gzerr(gzdest);
|
||||
}
|
||||
if (!err) {
|
||||
err = ZERR_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// clean up
|
||||
|
||||
if (source) {
|
||||
fclose(source);
|
||||
if (fd) {
|
||||
TEMP_FAILURE_RETRY(close(fd));
|
||||
}
|
||||
|
||||
if (gzdest) {
|
||||
gzclose(gzdest);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
unlink(dst);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -153,20 +147,17 @@ const char *def(const char* const src, const int expected_bytecount)
|
||||
invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
|
||||
the version of the library linked do not match, or Z_ERRNO if there
|
||||
is an error reading or writing the files. */
|
||||
const char *inf(const char* const src, int *rawcount)
|
||||
{
|
||||
unsigned char buf[CHUNK];
|
||||
const char *zlib_inflate(const char* const src, const int expected_bytecount) {
|
||||
gzFile gzsource = NULL;
|
||||
FILE *dest = NULL;
|
||||
int fd = -1;
|
||||
|
||||
if (src == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (rawcount) {
|
||||
*rawcount = 0;
|
||||
}
|
||||
int ret = UNKNOWN_ERR;
|
||||
int bytecount = 0;
|
||||
char *err = NULL;
|
||||
char dst[PATH_MAX] = { 0 };
|
||||
do {
|
||||
gzsource = gzopen(src, "rb");
|
||||
if (gzsource == NULL) {
|
||||
@ -175,7 +166,6 @@ const char *inf(const char* const src, int *rawcount)
|
||||
}
|
||||
|
||||
size_t len = strlen(src);
|
||||
char dst[PATH_MAX];
|
||||
snprintf(dst, PATH_MAX-1, "%s", src);
|
||||
if (! ( (dst[len-3] == '.') && (dst[len-2] == 'g') && (dst[len-1] == 'z') ) ) {
|
||||
ERRLOG("Expected filename ending in .gz");
|
||||
@ -183,40 +173,55 @@ const char *inf(const char* const src, int *rawcount)
|
||||
}
|
||||
dst[len-3] = '\0';
|
||||
|
||||
dest = fopen(dst, "w+");
|
||||
if (dest == NULL) {
|
||||
TEMP_FAILURE_RETRY(fd = open(dst, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR));
|
||||
if (fd == -1) {
|
||||
err = ZERR_INFLATE_OPEN_DEST;
|
||||
ERRLOG("Cannot open file '%s' for writing", dst);
|
||||
break;
|
||||
}
|
||||
|
||||
// inflate ...
|
||||
do {
|
||||
unsigned char buf[CHUNK];
|
||||
int buflen = gzread(gzsource, buf, CHUNK-1); // hmmm ... valgrind complains of a buffer read overflow in zlib inffast()?
|
||||
if (buflen < 0) {
|
||||
ERRLOG("OOPS, gzip read ...");
|
||||
break;
|
||||
} else if (buflen == 0) {
|
||||
break; // DONE
|
||||
}
|
||||
|
||||
if (buflen == 0) {
|
||||
ret = Z_OK;
|
||||
break; // finished inflating
|
||||
}
|
||||
ssize_t idx = 0;
|
||||
size_t chunk = buflen;
|
||||
do {
|
||||
ssize_t outlen = 0;
|
||||
TEMP_FAILURE_RETRY(outlen = write(fd, buf+idx, chunk));
|
||||
if (outlen <= 0) {
|
||||
break;
|
||||
}
|
||||
idx += outlen;
|
||||
chunk -= outlen;
|
||||
} while (idx < buflen);
|
||||
|
||||
if ( (fwrite(buf, 1, buflen, dest) != buflen) || ferror(dest) ) {
|
||||
ERRLOG("OOPS fwrite ...");
|
||||
if (idx != buflen) {
|
||||
ERRLOG("OOPS, write");
|
||||
err = ZERR_INFLATE_WRITE_DEST;
|
||||
break;
|
||||
}
|
||||
if (rawcount) {
|
||||
*rawcount += buflen;
|
||||
}
|
||||
fflush(dest);
|
||||
|
||||
bytecount += buflen;
|
||||
} while (1);
|
||||
|
||||
} while (0);
|
||||
|
||||
const char *err = NULL;
|
||||
if (ret != Z_OK) {
|
||||
err = _gzerr(gzsource);
|
||||
if (bytecount != expected_bytecount) {
|
||||
ERRLOG("OOPS did not write expected_bytecount of %d ... apparently wrote %d", expected_bytecount, bytecount);
|
||||
if (gzsource) {
|
||||
err = (char *)_gzerr(gzsource);
|
||||
}
|
||||
if (!err) {
|
||||
err = ZERR_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// clean up
|
||||
@ -225,9 +230,13 @@ const char *inf(const char* const src, int *rawcount)
|
||||
gzclose(gzsource);
|
||||
}
|
||||
|
||||
if (dest) {
|
||||
fflush(dest);
|
||||
fclose(dest);
|
||||
if (fd) {
|
||||
TEMP_FAILURE_RETRY(fsync(fd));
|
||||
TEMP_FAILURE_RETRY(close(fd));
|
||||
}
|
||||
|
||||
if (err) {
|
||||
unlink(dst);
|
||||
}
|
||||
|
||||
return err;
|
||||
|
@ -3,7 +3,14 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
const char* inf(const char* const src, int *rawcount); // src : foo.dsk.gz --> foo.dsk
|
||||
const char* def(const char* const src, const int expected_bytes); // src : foo.dsk --> foo.dsk.gz
|
||||
// augmented error codes/strings (not actually from zlib routines)
|
||||
#define ZERR_UNKNOWN "Unknown zlib error"
|
||||
#define ZERR_INFLATE_OPEN_DEST "Error opening destination file for inflation"
|
||||
#define ZERR_INFLATE_WRITE_DEST "Error writing destination file for inflation"
|
||||
#define ZERR_DEFLATE_OPEN_SOURCE "Error opening source file for deflation"
|
||||
#define ZERR_DEFLATE_READ_SOURCE "Error reading source file for deflation"
|
||||
|
||||
const char* zlib_inflate(const char* const src, const int expected_bytes); // src : foo.dsk.gz --> foo.dsk
|
||||
const char* zlib_deflate(const char* const src, const int expected_bytes); // src : foo.dsk --> foo.dsk.gz
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user