diff --git a/libraries/wutdevoptab/MutexWrapper.h b/libraries/wutdevoptab/MutexWrapper.h index 1758a150..f2f63733 100644 --- a/libraries/wutdevoptab/MutexWrapper.h +++ b/libraries/wutdevoptab/MutexWrapper.h @@ -8,21 +8,14 @@ class MutexWrapper { MutexWrapper() = default; void init(const char *name) { - if (name) { - OSReport("Init mutex %s\n", name); - } else { - OSReport("Init mutex null"); - } OSInitMutexEx(&mutex, name); } void lock() { - OSReport("lock mutex %08X %08X %s\n", &mutex, mutex.tag, mutex.name); OSLockMutex(&mutex); } void unlock() { - OSReport("unlock mutex %08X %08X %s\n", &mutex, mutex.tag, mutex.name); OSUnlockMutex(&mutex); OSMemoryBarrier(); } diff --git a/libraries/wutdevoptab/devoptab_fsa_open.cpp b/libraries/wutdevoptab/devoptab_fsa_open.cpp index 06eb82df..c20f17bd 100644 --- a/libraries/wutdevoptab/devoptab_fsa_open.cpp +++ b/libraries/wutdevoptab/devoptab_fsa_open.cpp @@ -23,6 +23,7 @@ int __wut_fsa_open(struct _reent *r, } bool createFileManually = false; + bool checkFileExists = false; // Map flags to open modes int commonFlagMask = O_CREAT | O_TRUNC | O_APPEND; if (((flags & O_ACCMODE) == O_RDONLY) && !(flags & commonFlagMask)) { @@ -43,6 +44,14 @@ int __wut_fsa_open(struct _reent *r, // It's not possible to open a file with write only mode which doesn't truncate the file // Technically we could read from the file, but our read implementation is blocking this. fsMode = "r+"; + } else if (((flags & O_ACCMODE) == O_WRONLY) && ((flags & commonFlagMask) == (O_APPEND))) { + // Cafe OS doesn't have a matching mode for this, so we have to check if the file exists. + checkFileExists = true; + fsMode = "a"; + } else if (((flags & O_ACCMODE) == O_WRONLY) && ((flags & commonFlagMask) == (O_TRUNC))) { + // Cafe OS doesn't have a matching mode for this, so we have to check if the file exists. + checkFileExists = true; + fsMode = "w"; } else { r->_errno = EINVAL; return -1; @@ -69,16 +78,25 @@ int __wut_fsa_open(struct _reent *r, deviceData = (__wut_fsa_device_t *) r->deviceData; // Check if we need to create the file manually - if (createFileManually) { + if (checkFileExists || createFileManually || (flags & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { FSStat stat; status = FSAGetStat(deviceData->clientHandle, file->name, &stat); if (status == FS_ERROR_NOT_FOUND) { - status = FSAOpenFileEx(deviceData->clientHandle, file->name, "w", translatedMode, openFlags, preAllocSize, &fd); - if (status == FS_ERROR_OK) { - FSACloseFile(deviceData->clientHandle, fd); + if (createFileManually) { + status = FSAOpenFileEx(deviceData->clientHandle, file->name, "w", translatedMode, openFlags, preAllocSize, &fd); + if (status == FS_ERROR_OK) { + FSACloseFile(deviceData->clientHandle, fd); + fd = -1; + } + } else if(checkFileExists) { fd = -1; } } + // If O_CREAT and O_EXCL are set, open() shall fail if the file exists. + if (status == FS_ERROR_OK && (flags & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { + r->_errno = EEXIST; + return -1; + } if (status < 0) { r->_errno = __wut_fsa_translate_error(status); return -1; @@ -94,12 +112,6 @@ int __wut_fsa_open(struct _reent *r, return -1; } - // If O_CREAT and O_EXCL are set, open() shall fail if the file exists. - if (flags & (O_EXCL | O_CREAT)) { - FSACloseFile(deviceData->clientHandle, fd); - r->_errno = EEXIST; - return -1; - } file->fd = fd; file->flags = (flags & (O_ACCMODE | O_APPEND | O_SYNC)); diff --git a/libraries/wutdevoptab/devoptab_fsa_rmdir.cpp b/libraries/wutdevoptab/devoptab_fsa_rmdir.cpp index 1f6d951c..192bd6c5 100644 --- a/libraries/wutdevoptab/devoptab_fsa_rmdir.cpp +++ b/libraries/wutdevoptab/devoptab_fsa_rmdir.cpp @@ -16,11 +16,13 @@ int __wut_fsa_rmdir(struct _reent *r, return -1; } + deviceData = (__wut_fsa_device_t*) r->deviceData; + status = FSARemove(deviceData->clientHandle, fixedPath); if (status < 0) { OSReport("FSARemove(0x%08X, %s) failed: %s\n", deviceData->clientHandle, fixedPath, FSAGetStatusStr(status)); free(fixedPath); - r->_errno = status == FS_STATUS_EXISTS ? ENOTEMPTY : __wut_fsa_translate_error(status); + r->_errno = status == FS_ERROR_ALREADY_EXISTS ? ENOTEMPTY : __wut_fsa_translate_error(status); return -1; }