Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Jul 30, 2022
1 parent 2d65308 commit 516d3e0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
7 changes: 0 additions & 7 deletions libraries/wutdevoptab/MutexWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
32 changes: 22 additions & 10 deletions libraries/wutdevoptab/devoptab_fsa_open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion libraries/wutdevoptab/devoptab_fsa_rmdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 516d3e0

Please sign in to comment.