From 17147ce662b69f4f5056d928de7fe3fd6962abef Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Fri, 28 Jun 2019 20:17:37 -0700 Subject: [PATCH] LOG strerror(errno) for functions that may fail --- Android/jni/jnihooks.c | 12 ++++---- Apple2Mac/Apple2Mac/disksViewController.m | 3 ++ .../Classes/OSX/EmulatorDiskController.m | 14 ++++++++-- src/disk.c | 16 +++++------ src/interface.c | 6 ++++ src/json_parse.c | 3 ++ src/misc.c | 14 +++++----- src/prefs.c | 8 ++---- src/zlib-helpers.c | 28 +++++++++++-------- 9 files changed, 63 insertions(+), 41 deletions(-) diff --git a/Android/jni/jnihooks.c b/Android/jni/jnihooks.c index 083980df..76c1ec75 100644 --- a/Android/jni/jnihooks.c +++ b/Android/jni/jnihooks.c @@ -388,12 +388,12 @@ jstring Java_org_deadc0de_apple2ix_Apple2DisksMenu_nativeChooseDisk(JNIEnv *env, if (!json_mapParseLongValue(jsonData, "fd", &fd, 10)) { TEMP_FAILURE_RETRY(fd = open(path, readOnly ? O_RDONLY : O_RDWR)); if (fd == -1) { - LOG("OOPS could not open disk path : %s", path); + LOG("OOPS could not open disk path : %s (%s)", path, strerror(errno)); } } else { - fd = dup(fd); + TEMP_FAILURE_RETRY(fd = dup(fd)); if (fd == -1) { - LOG("OOPS could not dup file descriptor!"); + LOG("OOPS could not dup file descriptor! (%s)", strerror(errno)); } } @@ -463,12 +463,12 @@ static int _openFdFromJson(OUTPARM int *fdOut, JSON_ref jsonData, const char * c TEMP_FAILURE_RETRY(fd = open(path, flags, mode)); } if (fd == -1) { - LOG("OOPS could not open state file path %s", path); + LOG("OOPS could not open state file path %s (%s)", path, strerror(errno)); } } else { - fd = dup(fd); + TEMP_FAILURE_RETRY(fd = dup(fd)); if (fd == -1) { - LOG("OOPS could not dup file descriptor!"); + LOG("OOPS could not dup file descriptor! (%s)", strerror(errno)); } } } while (0); diff --git a/Apple2Mac/Apple2Mac/disksViewController.m b/Apple2Mac/Apple2Mac/disksViewController.m index 66ea6cd8..73cef1dd 100644 --- a/Apple2Mac/Apple2Mac/disksViewController.m +++ b/Apple2Mac/Apple2Mac/disksViewController.m @@ -76,6 +76,9 @@ const char *path = [[self.path stringByAppendingPathComponent:[self._disks objectAtIndex:row]] UTF8String]; int fd = -1; TEMP_FAILURE_RETRY(fd = open(path, ro ? O_RDONLY : O_RDWR)); + if (fd == -1) { + LOG("OOPS, open failed for path %s (%s)", path, strerror(errno)); + } const char *errMsg = disk6_insert(fd, drive, path, ro); (void)errMsg; if (fd >= 0) { diff --git a/Apple2Mac/Classes/OSX/EmulatorDiskController.m b/Apple2Mac/Classes/OSX/EmulatorDiskController.m index 006e03ef..cbd649d8 100644 --- a/Apple2Mac/Classes/OSX/EmulatorDiskController.m +++ b/Apple2Mac/Classes/OSX/EmulatorDiskController.m @@ -88,6 +88,9 @@ static void prefsChangeCallback(const char *domain) const char *path = [startupDiskA UTF8String]; int fdA = -1; TEMP_FAILURE_RETRY(fdA = open(path, readOnlyA ? O_RDONLY : O_RDWR)); + if (fdA == -1) { + LOG("OOPS, open failed for path %s (%s)", path, strerror(errno)); + } const char *err = disk6_insert(fdA, 0, path, readOnlyA); if (fdA >= 0) { TEMP_FAILURE_RETRY(close(fdA)); @@ -115,6 +118,9 @@ static void prefsChangeCallback(const char *domain) const char *path = [startupDiskB UTF8String]; int fdB = -1; TEMP_FAILURE_RETRY(fdB = open(path, readOnlyB ? O_RDONLY : O_RDWR)); + if (fdB == -1) { + LOG("OOPS, open failed for path %s (%s)", path, strerror(errno)); + } const char *err = disk6_insert(fdB, 1, path, readOnlyB); if (fdB >= 0) { TEMP_FAILURE_RETRY(close(fdB)); @@ -190,8 +196,12 @@ static void prefsChangeCallback(const char *domain) disk6_eject(drive); int fd = -1; - TEMP_FAILURE_RETRY(fd = open([path UTF8String], readOnly ? O_RDONLY : O_RDWR)); - const char *errMsg = disk6_insert(fd, drive, [path UTF8String], readOnly); + const char *cPath = [path UTF8String]; + TEMP_FAILURE_RETRY(fd = open(cPath, readOnly ? O_RDONLY : O_RDWR)); + if (fd == -1) { + LOG("OOPS, open failed for path %s (%s)", cPath, strerror(errno)); + } + const char *errMsg = disk6_insert(fd, drive, cPath, readOnly); if (fd >= 0) { TEMP_FAILURE_RETRY(close(fd)); } diff --git a/src/disk.c b/src/disk.c index b754f77c..c44843e8 100644 --- a/src/disk.c +++ b/src/disk.c @@ -819,32 +819,32 @@ const char *disk6_eject(int drive) { TEMP_FAILURE_RETRY(ret = msync(disk6.disk[drive].raw_image_data, disk6.disk[drive].whole_len, MS_SYNC)); if (ret) { - LOG("Error syncing file %s", disk6.disk[drive].file_name); + LOG("Error syncing file %s (%s)", disk6.disk[drive].file_name, strerror(errno)); } } } TEMP_FAILURE_RETRY(ret = munmap(disk6.disk[drive].raw_image_data, disk6.disk[drive].whole_len)); if (ret) { - LOG("Error munmap()ping file %s", disk6.disk[drive].file_name); + LOG("Error munmap()ping file %s (%s)", disk6.disk[drive].file_name, strerror(errno)); } } if (compressed_size > 0) { TEMP_FAILURE_RETRY(ret = ftruncate(disk6.disk[drive].fd, compressed_size)); if (ret == -1) { - LOG("OOPS, cannot truncate file descriptor!"); + LOG("OOPS, cannot truncate file descriptor! (%s)", strerror(errno)); } } TEMP_FAILURE_RETRY(ret = fsync(disk6.disk[drive].fd)); if (ret) { - LOG("Error fsync()ing file %s", disk6.disk[drive].file_name); + LOG("Error fsync()ing file %s (%s)", disk6.disk[drive].file_name, strerror(errno)); } TEMP_FAILURE_RETRY(ret = close(disk6.disk[drive].fd)); if (ret) { - LOG("Error close()ing file %s", disk6.disk[drive].file_name); + LOG("Error close()ing file %s (%s)", disk6.disk[drive].file_name, strerror(errno)); } } @@ -913,7 +913,7 @@ const char *disk6_insert(int fd, int drive, const char * const file_name, int re // disk images inserted read/write are mmap'd/inflated in place ... TEMP_FAILURE_RETRY(fd = dup(fd)); if (fd == -1) { - LOG("OOPS, could not dup() file descriptor %d", fd); + LOG("OOPS, could not dup() file descriptor %d (%s)", fd, strerror(errno)); err = ERR_CANNOT_DUP; break; } @@ -928,7 +928,7 @@ const char *disk6_insert(int fd, int drive, const char * const file_name, int re TEMP_FAILURE_RETRY( (long)(disk6.disk[drive].raw_image_data = mmap(NULL, disk6.disk[drive].whole_len, (readonly ? PROT_READ : PROT_READ|PROT_WRITE), MAP_SHARED|MAP_FILE, disk6.disk[drive].fd, /*offset:*/0)) ); if (disk6.disk[drive].raw_image_data == MAP_FAILED) { - LOG("OOPS, could not mmap file %s", disk6.disk[drive].file_name); + LOG("OOPS, could not mmap file %s (%s)", disk6.disk[drive].file_name, strerror(errno)); err = ERR_MMAP_FAILED; break; } @@ -1007,7 +1007,7 @@ void disk6_flush(int drive) { int ret = -1; TEMP_FAILURE_RETRY(ret = msync(disk6.disk[drive].raw_image_data, disk6.disk[drive].whole_len, MS_SYNC)); if (ret) { - LOG("Error syncing file %s", disk6.disk[drive].file_name); + LOG("Error syncing file %s (%s)", disk6.disk[drive].file_name, strerror(errno)); } } diff --git a/src/interface.c b/src/interface.c index b5f9b609..dbc50ac6 100644 --- a/src/interface.c +++ b/src/interface.c @@ -552,6 +552,9 @@ void c_interface_select_diskette( int drive ) { int fd = -1; TEMP_FAILURE_RETRY(fd = open(temp, O_RDWR)); + if (fd == -1) { + LOG("OOPS, could not open disk path %s (%s)", temp, strerror(errno)); + } const char *err_str = disk6_insert(fd, drive, temp, /*readonly:*/0); if (fd > 0) { TEMP_FAILURE_RETRY(close(fd)); @@ -627,6 +630,9 @@ void c_interface_select_diskette( int drive ) int fd = -1; TEMP_FAILURE_RETRY(fd = open(temp, O_RDWR)); + if (fd == -1) { + LOG("OOPS, could not open disk path %s (%s)", temp, strerror(errno)); + } const char *err_str = disk6_insert(fd, drive, temp, /*readonly:*/(toupper(ch) != 'W')); if (fd > 0) { TEMP_FAILURE_RETRY(close(fd)); diff --git a/src/json_parse.c b/src/json_parse.c index b0a43de2..25dcb8cf 100644 --- a/src/json_parse.c +++ b/src/json_parse.c @@ -47,6 +47,9 @@ static bool _json_write(const char *jsonString, size_t buflen, int fd) { ssize_t outlen = 0; TEMP_FAILURE_RETRY(outlen = write(fd, jsonString+idx, chunk)); if (outlen <= 0) { + if (outlen < 0) { + LOG("OOPS write failed : %s", strerror(errno)); + } break; } idx += outlen; diff --git a/src/misc.c b/src/misc.c index e4529294..dec87796 100644 --- a/src/misc.c +++ b/src/misc.c @@ -109,7 +109,7 @@ static bool _save_state(int fd, const uint8_t * outbuf, ssize_t outmax) { ssize_t outlen = 0; do { if (TEMP_FAILURE_RETRY(outlen = write(fd, outbuf, outmax)) == -1) { - LOG("OOPS, error writing emulator save-state file"); + LOG("OOPS, error writing emulator save-state file (%s)", strerror(errno)); break; } outbuf += outlen; @@ -126,13 +126,13 @@ static bool _load_state(int fd, uint8_t * inbuf, ssize_t inmax) { struct stat stat_buf; if (UNLIKELY(fstat(fd, &stat_buf) < 0)) { - LOG("OOPS, could not stat FD"); + LOG("OOPS, could not stat FD (%s)", strerror(errno)); return false; } off_t fileSiz = stat_buf.st_size; off_t filePos = lseek(fd, 0, SEEK_CUR); if (UNLIKELY(filePos < 0)) { - LOG("OOPS, could not lseek FD"); + LOG("OOPS, could not lseek FD (%s)", strerror(errno)); return false; } @@ -143,7 +143,7 @@ static bool _load_state(int fd, uint8_t * inbuf, ssize_t inmax) { do { if (TEMP_FAILURE_RETRY(inlen = read(fd, inbuf, inmax)) == -1) { - LOG("error reading emulator save-state file"); + LOG("error reading emulator save-state file (%s)", strerror(errno)); break; } if (inlen == 0) { @@ -283,12 +283,12 @@ bool emulator_loadState(int fd, int fdA, int fdB) { struct stat stat_buf; if (fstat(fd, &stat_buf) < 0) { - LOG("OOPS, could not stat FD"); + LOG("OOPS, could not stat FD (%s)", strerror(errno)); } off_t fileSiz = stat_buf.st_size; off_t filePos = lseek(fd, 0, SEEK_CUR); if (filePos < 0) { - LOG("OOPS, could not lseek FD"); + LOG("OOPS, could not lseek FD (%s)", strerror(errno)); } if (UNLIKELY(filePos != fileSiz)) { @@ -334,7 +334,7 @@ bool emulator_stateExtractDiskPaths(int fd, JSON_ref json) { // Ensure that we leave the file descriptor ready for a call to emulator_loadState() off_t ret = lseek(fd, 0, SEEK_SET); if (ret != 0) { - LOG("OOPS : state file lseek() failed!"); + LOG("OOPS : state file lseek() failed! (%s)", strerror(errno)); } } diff --git a/src/prefs.c b/src/prefs.c index 95e87eda..f42dd7d6 100644 --- a/src/prefs.c +++ b/src/prefs.c @@ -108,11 +108,6 @@ bool prefs_save(void) { assert(((JSON_s *)jsonPrefs)->jsonString && "string should be valid"); -#define PREFS_ERRPRINT() \ - LOG( \ - "Cannot open the .apple2.json preferences file for writing.\n" \ - "Make sure it has R/W permission in your home directory.") - #define ERROR_SUBMENU_H 8 #define ERROR_SUBMENU_W 40 #if defined(INTERFACE_CLASSIC) && !TESTING @@ -131,7 +126,8 @@ bool prefs_save(void) { TEMP_FAILURE_RETRY(fd = open(prefsFile, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)); if (fd == -1) { - PREFS_ERRPRINT(); + LOG("Cannot open the .apple2.json preferences file for writing.\n" + "Make sure it has R/W permission in your home directory. (%s)", strerror(errno)); #if defined(INTERFACE_CLASSIC) && !TESTING c_interface_print_submenu_centered(submenu[0], ERROR_SUBMENU_W, ERROR_SUBMENU_H); while ((ch = c_mygetch(1)) == -1) { diff --git a/src/zlib-helpers.c b/src/zlib-helpers.c index 923c5160..c5de78ac 100644 --- a/src/zlib-helpers.c +++ b/src/zlib-helpers.c @@ -73,10 +73,10 @@ static ssize_t _read_data(int fd_own, uint8_t *buf, const off_t expected_bytesco TEMP_FAILURE_RETRY(bytesread = read(fd_own, buf+bytescount, len)); if (bytesread <= 0) { if (--maxtries == 0) { - LOG("OOPS, giving up on read() ..."); + LOG("OOPS, giving up on read() ... (%s)", strerror(errno)); break; } - LOG("OOPS, read() ..."); + LOG("OOPS, read() ... (%s)", strerror(errno)); usleep(100); continue; } @@ -103,10 +103,10 @@ static ssize_t _write_data(int fd_own, uint8_t *buf, const unsigned int expected if (byteswritten <= 0) { if (--maxtries == 0) { - LOG("OOPS, giving up on write() ..."); + LOG("OOPS, giving up on write() ... (%s)", strerror(errno)); break; } - LOG("OOPS, write ..."); + LOG("OOPS, write ... (%s)", strerror(errno)); usleep(100); continue; } @@ -136,7 +136,7 @@ static a2gzip_t _check_gzip_magick(int fd_own, const unsigned int expected_bytes bytescount = lseek(fd_own, 0L, SEEK_END); if (bytescount == -1) { - LOG("OOPS, cannot seek to end of file descriptor!"); + LOG("OOPS, cannot seek to end of file descriptor! (%s)", strerror(errno)); break; } @@ -189,7 +189,7 @@ const char *zlib_inflate_to_buffer(int fd, const unsigned int expected_bytescoun } if (lseek(fd_own, 0L, SEEK_SET) == -1) { - LOG("OOPS, cannot seek to start of file descriptor!"); + LOG("OOPS, cannot seek to start of file descriptor! (%s)", strerror(errno)); break; } @@ -213,7 +213,7 @@ const char *zlib_inflate_to_buffer(int fd, const unsigned int expected_bytescoun LOG("OOPS, did not gzread() expected_bytescount of %u ... apparently read %zd ... checking file length heuristic ...", expected_bytescount, bytescount); if (lseek(fd_own, 0L, SEEK_SET) == -1) { - LOG("OOPS, cannot seek to start of file descriptor!"); + LOG("OOPS, cannot seek to start of file descriptor! (%s)", strerror(errno)); break; } @@ -295,7 +295,7 @@ const char *zlib_inflate_inplace(int fd, const unsigned int expected_bytescount, } if (lseek(fd_own, 0L, SEEK_SET) == -1) { - LOG("OOPS, cannot seek to start of file descriptor!"); + LOG("OOPS, cannot seek to start of file descriptor! (%s)", strerror(errno)); break; } @@ -312,7 +312,7 @@ const char *zlib_inflate_inplace(int fd, const unsigned int expected_bytescount, bytescount = lseek(fd_own, 0L, SEEK_END); if (bytescount == -1) { - LOG("OOPS, cannot seek to end of file descriptor!"); + LOG("OOPS, cannot seek to end of file descriptor! (%s)", strerror(errno)); break; } @@ -332,7 +332,7 @@ const char *zlib_inflate_inplace(int fd, const unsigned int expected_bytescount, // inplace write file if (lseek(fd_own, 0L, SEEK_SET) == -1) { - LOG("OOPS, cannot seek to start of file descriptor!"); + LOG("OOPS, cannot seek to start of file descriptor! (%s)", strerror(errno)); break; } @@ -441,12 +441,16 @@ const char *zlib_deflate_buffer(const uint8_t *src, const unsigned int src_bytes { off_t compressed_size = lseek(fd_own, 0L, SEEK_CUR); - assert(compressed_size > 0 && compressed_size < expected_bytescount); + if (compressed_size == -1) { + LOG("OOPS, cannot seek to current position! (%s)", strerror(errno)); + break; + } + assert(/*compressed_size > 0 && */compressed_size < expected_bytescount); expected_bytescount = compressed_size; } if (lseek(fd_own, 0L, SEEK_SET) == -1) { - LOG("OOPS, cannot seek to start of file descriptor!"); + LOG("OOPS, cannot seek to start of file descriptor! (%s)", strerror(errno)); break; }