Make the devoptab code more consistent.
This commit is contained in:
parent
63119deb63
commit
069e28acec
libraries/wutdevoptab
devoptab_fs_chdir.cdevoptab_fs_chmod.cdevoptab_fs_close.cdevoptab_fs_dirclose.cdevoptab_fs_dirnext.cdevoptab_fs_diropen.cdevoptab_fs_dirreset.cdevoptab_fs_fchmod.cdevoptab_fs_fstat.cdevoptab_fs_fsync.cdevoptab_fs_mkdir.cdevoptab_fs_open.cdevoptab_fs_read.cdevoptab_fs_rename.cdevoptab_fs_rmdir.cdevoptab_fs_seek.cdevoptab_fs_stat.cdevoptab_fs_statvfs.cdevoptab_fs_truncate.cdevoptab_fs_unlink.cdevoptab_fs_utils.cdevoptab_fs_write.c
@ -2,32 +2,28 @@
|
||||
|
||||
int
|
||||
__wut_fs_chdir(struct _reent *r,
|
||||
const char *name)
|
||||
const char *path)
|
||||
{
|
||||
FSStatus rc;
|
||||
|
||||
if (name == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *path = __wut_fs_fixpath(r, name);
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
|
||||
if (!path) {
|
||||
r->_errno = ENOMEM;
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSChangeDir(__wut_devoptab_fs_client, &fsCmd, path, -1);
|
||||
free(path);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
char *fixedPath = __wut_fs_fixpath(r, path);
|
||||
if (!fixedPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
FSInitCmdBlock(&cmd);
|
||||
status = FSChangeDir(__wut_devoptab_fs_client, &cmd, fixedPath, -1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,25 +5,27 @@ __wut_fs_chmod(struct _reent *r,
|
||||
const char *path,
|
||||
mode_t mode)
|
||||
{
|
||||
FSStatus rc;
|
||||
char *path_fix = __wut_fs_fixpath(r, path);
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
|
||||
if (!path_fix) {
|
||||
r->_errno = ENOMEM;
|
||||
if (!path) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSChangeMode(__wut_devoptab_fs_client, &fsCmd, path_fix, (FSMode)mode, -1);
|
||||
free(path_fix);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
char *fixedPath = __wut_fs_fixpath(r, path);
|
||||
if (!fixedPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
FSInitCmdBlock(&cmd);
|
||||
status = FSChangeMode(__wut_devoptab_fs_client, &cmd, fixedPath,
|
||||
(FSMode)mode, -1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,19 +4,22 @@ int
|
||||
__wut_fs_close(struct _reent *r,
|
||||
void *fd)
|
||||
{
|
||||
FSStatus rc;
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSCloseFile(__wut_devoptab_fs_client, &fsCmd, file->fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
if (!fd) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
FSInitCmdBlock(&cmd);
|
||||
file = (__wut_fs_file_t *)fd;
|
||||
status = FSCloseFile(__wut_devoptab_fs_client, &cmd, file->fd, -1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,19 +4,22 @@ int
|
||||
__wut_fs_dirclose(struct _reent *r,
|
||||
DIR_ITER *dirState)
|
||||
{
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_dir_t *dir;
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
__wut_fs_dir_t *dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
rc = FSCloseDir(__wut_devoptab_fs_client, &fsCmd, dir->fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
if (!dirState) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
FSInitCmdBlock(&cmd);
|
||||
dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
status = FSCloseDir(__wut_devoptab_fs_client, &cmd, dir->fd, -1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,44 +6,40 @@ __wut_fs_dirnext(struct _reent *r,
|
||||
char *filename,
|
||||
struct stat *filestat)
|
||||
{
|
||||
FSStatus rc;
|
||||
__wut_fs_dir_t *dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_dir_t *dir;
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
// Fetch the next dir
|
||||
memset(&dir->entry_data, 0, sizeof(dir->entry_data));
|
||||
rc = FSReadDir(__wut_devoptab_fs_client, &fsCmd, dir->fd, &dir->entry_data, -1);
|
||||
|
||||
if (rc < 0) {
|
||||
// There are no more entries; ENOENT signals end-of-directory
|
||||
r->_errno = ENOENT;
|
||||
if (!dirState || !filename || !filestat) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (rc >= 0) {
|
||||
memset(filestat, 0, sizeof(struct stat));
|
||||
|
||||
// Fill in the stat info
|
||||
filestat->st_ino = 0;
|
||||
|
||||
if (dir->entry_data.info.flags & FS_STAT_DIRECTORY) {
|
||||
filestat->st_mode = S_IFDIR;
|
||||
} else {
|
||||
filestat->st_mode = S_IFREG;
|
||||
}
|
||||
|
||||
filestat->st_uid = dir->entry_data.info.owner;
|
||||
filestat->st_gid = dir->entry_data.info.group;
|
||||
filestat->st_size = dir->entry_data.info.size;
|
||||
|
||||
memset(filename, 0, NAME_MAX);
|
||||
strcpy(filename, dir->entry_data.name);
|
||||
return 0;
|
||||
FSInitCmdBlock(&cmd);
|
||||
dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
memset(&dir->entry_data, 0, sizeof(dir->entry_data));
|
||||
status = FSReadDir(__wut_devoptab_fs_client, &cmd, dir->fd, &dir->entry_data,
|
||||
-1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
// Fill in the stat info
|
||||
memset(filestat, 0, sizeof(struct stat));
|
||||
filestat->st_ino = 0;
|
||||
|
||||
if (dir->entry_data.info.flags & FS_STAT_DIRECTORY) {
|
||||
filestat->st_mode = S_IFDIR;
|
||||
} else {
|
||||
filestat->st_mode = S_IFREG;
|
||||
}
|
||||
|
||||
filestat->st_uid = dir->entry_data.info.owner;
|
||||
filestat->st_gid = dir->entry_data.info.group;
|
||||
filestat->st_size = dir->entry_data.info.size;
|
||||
|
||||
memset(filename, 0, NAME_MAX);
|
||||
strcpy(filename, dir->entry_data.name);
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,35 +6,31 @@ __wut_fs_diropen(struct _reent *r,
|
||||
const char *path)
|
||||
{
|
||||
FSDirectoryHandle fd;
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_dir_t *dir;
|
||||
|
||||
if (path == NULL) {
|
||||
if (!dirState || !path) {
|
||||
r->_errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *path_fixed = __wut_fs_fixpath(r,path);
|
||||
|
||||
if (!path_fixed) {
|
||||
r->_errno = ENOMEM;
|
||||
char *fixedPath = __wut_fs_fixpath(r, path);
|
||||
if (!fixedPath) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
__wut_fs_dir_t *dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
rc = FSOpenDir(__wut_devoptab_fs_client, &fsCmd, path_fixed, &fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
dir->magic = FS_DIRITER_MAGIC;
|
||||
dir->fd = fd;
|
||||
memset(&dir->entry_data, 0, sizeof(dir->entry_data));
|
||||
free(path_fixed);
|
||||
return dirState;
|
||||
FSInitCmdBlock(&cmd);
|
||||
dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
status = FSOpenDir(__wut_devoptab_fs_client, &cmd, fixedPath, &fd, -1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
free(path_fixed);
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return NULL;
|
||||
dir->magic = FS_DIRITER_MAGIC;
|
||||
dir->fd = fd;
|
||||
memset(&dir->entry_data, 0, sizeof(dir->entry_data));
|
||||
return dirState;
|
||||
}
|
||||
|
@ -4,20 +4,22 @@ int
|
||||
__wut_fs_dirreset(struct _reent *r,
|
||||
DIR_ITER *dirState)
|
||||
{
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_dir_t *dir;
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
__wut_fs_dir_t *dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
rc = FSRewindDir(__wut_devoptab_fs_client, &fsCmd, dir->fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
if (!dirState) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
}
|
||||
FSInitCmdBlock(&cmd);
|
||||
dir = (__wut_fs_dir_t *)(dirState->dirStruct);
|
||||
status = FSRewindDir(__wut_devoptab_fs_client, &cmd, dir->fd, -1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ __wut_fs_fchmod(struct _reent *r,
|
||||
void *fd,
|
||||
mode_t mode)
|
||||
{
|
||||
//TODO: FSChangeMode and FSStatFile?
|
||||
// TODO: FSChangeMode and FSStatFile?
|
||||
r->_errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
@ -5,26 +5,30 @@ __wut_fs_fstat(struct _reent *r,
|
||||
void *fd,
|
||||
struct stat *st)
|
||||
{
|
||||
FSStatus rc;
|
||||
FSStat fsstat;
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
||||
FSStatus status;
|
||||
FSStat fsStat;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSGetStatFile(__wut_devoptab_fs_client, &fsCmd, file->fd, &fsstat, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
memset(st, 0, sizeof(struct stat));
|
||||
st->st_size = fsstat.size;
|
||||
st->st_uid = fsstat.owner;
|
||||
st->st_gid = fsstat.group;
|
||||
st->st_nlink = 1;
|
||||
st->st_mode = fsstat.mode;
|
||||
return 0;
|
||||
if (!fd || !st) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
FSInitCmdBlock(&cmd);
|
||||
file = (__wut_fs_file_t *)fd;
|
||||
status = FSGetStatFile(__wut_devoptab_fs_client, &cmd, file->fd, &fsStat,
|
||||
-1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(st, 0, sizeof(struct stat));
|
||||
st->st_size = fsStat.size;
|
||||
st->st_uid = fsStat.owner;
|
||||
st->st_gid = fsStat.group;
|
||||
st->st_nlink = 1;
|
||||
st->st_mode = fsStat.mode;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,19 +4,22 @@ int
|
||||
__wut_fs_fsync(struct _reent *r,
|
||||
void *fd)
|
||||
{
|
||||
FSStatus rc;
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSFlushFile(__wut_devoptab_fs_client, &fsCmd, file->fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
if (!fd) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
FSInitCmdBlock(&cmd);
|
||||
file = (__wut_fs_file_t *)fd;
|
||||
status = FSFlushFile(__wut_devoptab_fs_client, &cmd, file->fd, -1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,34 +5,28 @@ __wut_fs_mkdir(struct _reent *r,
|
||||
const char *path,
|
||||
int mode)
|
||||
{
|
||||
FSError rc;
|
||||
FSError status;
|
||||
FSCmdBlock cmd;
|
||||
char *fixedPath;
|
||||
|
||||
if (path == NULL) {
|
||||
if (!path) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *path_fix = __wut_fs_fixpath(r, path);
|
||||
|
||||
if (!path_fix) {
|
||||
r->_errno = ENOMEM;
|
||||
fixedPath = __wut_fs_fixpath(r, path);
|
||||
if (!fixedPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
// TODO: Use mode to set directory attributes.
|
||||
rc = FSMakeDir(__wut_devoptab_fs_client, &fsCmd, path_fix, -1);
|
||||
free(path_fix);
|
||||
|
||||
if (rc == FS_ERROR_ALREADY_EXISTS) {
|
||||
r->_errno = EEXIST;
|
||||
FSInitCmdBlock(&cmd);
|
||||
status = FSMakeDir(__wut_devoptab_fs_client, &cmd, fixedPath, -1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
} else if(rc >= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -8,57 +8,52 @@ __wut_fs_open(struct _reent *r,
|
||||
int mode)
|
||||
{
|
||||
FSFileHandle fd;
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
const char *fsMode;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
if (path == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *path_fixed = __wut_fs_fixpath(r,path);
|
||||
if (!path_fixed) {
|
||||
r->_errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get pointer to our data
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fileStruct;
|
||||
const char *fs_mode;
|
||||
|
||||
// Map flags to open modes
|
||||
if (flags == 0) {
|
||||
fs_mode = "r";
|
||||
} else if (flags == 2) {
|
||||
fs_mode = "r+";
|
||||
} else if (flags == 0x601) {
|
||||
fs_mode = "w";
|
||||
} else if(flags == 0x602) {
|
||||
fs_mode = "w+";
|
||||
} else if(flags == 0x209) {
|
||||
fs_mode = "a";
|
||||
} else if(flags == 0x20A) {
|
||||
fs_mode = "a+";
|
||||
} else {
|
||||
free(path_fixed);
|
||||
if (!fileStruct || !path) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
// Open the file
|
||||
rc = FSOpenFile(__wut_devoptab_fs_client, &fsCmd, path_fixed, fs_mode, &fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
file->fd = fd;
|
||||
file->flags = (flags & (O_ACCMODE|O_APPEND|O_SYNC));
|
||||
FSGetPosFile(__wut_devoptab_fs_client, &fsCmd, fd, &file->offset, -1);
|
||||
free(path_fixed);
|
||||
return 0;
|
||||
// Map flags to open modes
|
||||
if (flags == 0) {
|
||||
fsMode = "r";
|
||||
} else if (flags == 2) {
|
||||
fsMode = "r+";
|
||||
} else if (flags == 0x601) {
|
||||
fsMode = "w";
|
||||
} else if(flags == 0x602) {
|
||||
fsMode = "w+";
|
||||
} else if(flags == 0x209) {
|
||||
fsMode = "a";
|
||||
} else if(flags == 0x20A) {
|
||||
fsMode = "a+";
|
||||
} else {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(path_fixed);
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
char *fixedPath = __wut_fs_fixpath(r,path);
|
||||
if (!fixedPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Open the file
|
||||
FSInitCmdBlock(&cmd);
|
||||
status = FSOpenFile(__wut_devoptab_fs_client, &cmd, fixedPath, fsMode, &fd,
|
||||
-1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
file = (__wut_fs_file_t *)fileStruct;
|
||||
file->fd = fd;
|
||||
file->flags = (flags & (O_ACCMODE|O_APPEND|O_SYNC));
|
||||
FSGetPosFile(__wut_devoptab_fs_client, &cmd, fd, &file->offset, -1);
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,9 +6,20 @@ __wut_fs_read(struct _reent *r,
|
||||
char *ptr,
|
||||
size_t len)
|
||||
{
|
||||
FSStatus rc;
|
||||
uint32_t bytes, bytesRead = 0;
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
uint8_t *alignedReadBuffer;
|
||||
uint32_t bytes, bytesRead;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
if (!fd || !ptr) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
FSInitCmdBlock(&cmd);
|
||||
file = (__wut_fs_file_t *)fd;
|
||||
bytesRead = 0;
|
||||
|
||||
// Check that the file was opened with read access
|
||||
if ((file->flags & O_ACCMODE) == O_WRONLY) {
|
||||
@ -16,55 +27,44 @@ __wut_fs_read(struct _reent *r,
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
FSStat fsstat;
|
||||
rc = FSGetStatFile(__wut_devoptab_fs_client, &fsCmd, file->fd, &fsstat, -1);
|
||||
|
||||
if(rc < 0) {
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Copy to internal buffer and read in chunks.
|
||||
uint8_t *tmp_buffer = memalign(0x40, 8192);
|
||||
|
||||
while(len > 0) {
|
||||
size_t toRead = len;
|
||||
|
||||
if (toRead > 8192) {
|
||||
toRead = 8192;
|
||||
}
|
||||
// Copy to internal buffer due to alignment requirement and read in chunks.
|
||||
alignedReadBuffer = memalign(0x40, 8192);
|
||||
while (len > 0) {
|
||||
size_t toRead = len > 8192 ? 8192 : len;
|
||||
|
||||
// Write the data
|
||||
rc = FSReadFile(__wut_devoptab_fs_client, &fsCmd, tmp_buffer, 1, toRead, file->fd, 0, -1);
|
||||
|
||||
if(rc <= 0)
|
||||
{
|
||||
free(tmp_buffer);
|
||||
|
||||
// Return partial transfer
|
||||
if (bytesRead > 0) {
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
} else {
|
||||
bytes = rc;
|
||||
status = FSReadFile(__wut_devoptab_fs_client, &cmd, alignedReadBuffer, 1,
|
||||
toRead, file->fd, 0, -1);
|
||||
if (status <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Copy to internal buffer
|
||||
memcpy(ptr, tmp_buffer, bytes);
|
||||
bytes = (uint32_t)status;
|
||||
memcpy(ptr, alignedReadBuffer, bytes);
|
||||
|
||||
file->offset += bytes;
|
||||
bytesRead += bytes;
|
||||
ptr += bytes;
|
||||
len -= bytes;
|
||||
|
||||
if (bytes < toRead) {
|
||||
// If we did not read the full requested toRead bytes then we reached
|
||||
// the end of the file.
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(alignedReadBuffer);
|
||||
|
||||
// Return partial read
|
||||
if (bytesRead > 0) {
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
free(tmp_buffer);
|
||||
return bytesRead;
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,42 +5,36 @@ __wut_fs_rename(struct _reent *r,
|
||||
const char *oldName,
|
||||
const char *newName)
|
||||
{
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
char *fixedOldPath, *fixedNewPath;
|
||||
|
||||
if (oldName == NULL) {
|
||||
if (!oldName || !newName) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (newName == NULL) {
|
||||
fixedOldPath = __wut_fs_fixpath(r, oldName);
|
||||
if (!fixedOldPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *path_old = __wut_fs_fixpath(r, oldName);
|
||||
|
||||
if (!path_old) {
|
||||
r->_errno = ENOMEM;
|
||||
fixedNewPath = __wut_fs_fixpath(r, newName);
|
||||
if (!fixedNewPath) {
|
||||
free(fixedOldPath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *path_new = __wut_fs_fixpath(r, newName);
|
||||
FSInitCmdBlock(&cmd);
|
||||
status = FSRename(__wut_devoptab_fs_client, &cmd, fixedOldPath, fixedNewPath,
|
||||
-1);
|
||||
free(fixedOldPath);
|
||||
free(fixedNewPath);
|
||||
|
||||
if (!path_new) {
|
||||
r->_errno = ENOMEM;
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSRename(__wut_devoptab_fs_client, &fsCmd, path_old, path_new, -1);
|
||||
free(path_old);
|
||||
free(path_new);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,30 +4,26 @@ int
|
||||
__wut_fs_rmdir(struct _reent *r,
|
||||
const char *name)
|
||||
{
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
|
||||
if (name == NULL) {
|
||||
if (!name) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *path_fix = __wut_fs_fixpath(r, name);
|
||||
|
||||
if (!path_fix) {
|
||||
r->_errno = ENOMEM;
|
||||
char *fixedPath = __wut_fs_fixpath(r, name);
|
||||
if (!fixedPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSRemove(__wut_devoptab_fs_client, &fsCmd, path_fix, -1);
|
||||
free(path_fix);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
FSInitCmdBlock(&cmd);
|
||||
status = FSRemove(__wut_devoptab_fs_client, &cmd, fixedPath, -1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,19 +6,23 @@ __wut_fs_seek(struct _reent *r,
|
||||
off_t pos,
|
||||
int whence)
|
||||
{
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
FSStat fsStat;
|
||||
uint64_t offset;
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
if (!fd) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
FSStat fsstat;
|
||||
rc = FSGetStatFile(__wut_devoptab_fs_client, &fsCmd, file->fd, &fsstat, -1);
|
||||
|
||||
if (rc < 0) {
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
FSInitCmdBlock(&cmd);
|
||||
file = (__wut_fs_file_t *)fd;
|
||||
status = FSGetStatFile(__wut_devoptab_fs_client, &cmd, file->fd, &fsStat,
|
||||
-1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -36,7 +40,7 @@ __wut_fs_seek(struct _reent *r,
|
||||
|
||||
// Set position relative to the end of the file
|
||||
case SEEK_END:
|
||||
offset = fsstat.size;
|
||||
offset = fsStat.size;
|
||||
break;
|
||||
|
||||
// An invalid option was provided
|
||||
@ -54,10 +58,11 @@ __wut_fs_seek(struct _reent *r,
|
||||
|
||||
// Update the current offset
|
||||
file->offset = offset + pos;
|
||||
FSStatus result = FSSetPosFile(__wut_devoptab_fs_client, &fsCmd, file->fd, file->offset, -1);
|
||||
|
||||
if (result < 0) {
|
||||
return result;
|
||||
status = FSSetPosFile(__wut_devoptab_fs_client, &cmd, file->fd, file->offset,
|
||||
-1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return file->offset;
|
||||
|
@ -2,41 +2,49 @@
|
||||
|
||||
int
|
||||
__wut_fs_stat(struct _reent *r,
|
||||
const char *file,
|
||||
const char *path,
|
||||
struct stat *st)
|
||||
{
|
||||
int fd;
|
||||
FSStatus rc;
|
||||
int fd;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
|
||||
if (file == NULL) {
|
||||
if (!path || !st) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
char *fixedPath = __wut_fs_fixpath(r, path);
|
||||
if (!fixedPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
FSInitCmdBlock(&cmd);
|
||||
|
||||
// First try open as file
|
||||
rc = FSOpenFile(__wut_devoptab_fs_client, &fsCmd, file, "r", (FSFileHandle*)&fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
status = FSOpenFile(__wut_devoptab_fs_client, &cmd, fixedPath, "r",
|
||||
(FSFileHandle*)&fd, -1);
|
||||
if (status >= 0) {
|
||||
__wut_fs_file_t tmpfd = { .fd = fd };
|
||||
rc = __wut_fs_fstat(r, &tmpfd, st);
|
||||
FSCloseFile(__wut_devoptab_fs_client, &fsCmd, fd, -1);
|
||||
return rc;
|
||||
status = __wut_fs_fstat(r, &tmpfd, st);
|
||||
FSCloseFile(__wut_devoptab_fs_client, &cmd, fd, -1);
|
||||
free(fixedPath);
|
||||
return status;
|
||||
}
|
||||
|
||||
// File failed, so lets try open as directory
|
||||
rc = FSOpenDir(__wut_devoptab_fs_client, &fsCmd, file, (FSDirectoryHandle*)&fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
memset(st, 0, sizeof(struct stat));
|
||||
st->st_nlink = 1;
|
||||
st->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
|
||||
FSCloseDir(__wut_devoptab_fs_client, &fsCmd, fd, -1);
|
||||
return 0;
|
||||
status = FSOpenDir(__wut_devoptab_fs_client, &cmd, fixedPath,
|
||||
(FSDirectoryHandle*)&fd, -1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
memset(st, 0, sizeof(struct stat));
|
||||
st->st_nlink = 1;
|
||||
st->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
|
||||
FSCloseDir(__wut_devoptab_fs_client, &cmd, fd, -1);
|
||||
free(fixedPath);
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,17 +5,7 @@ __wut_fs_statvfs(struct _reent *r,
|
||||
const char *path,
|
||||
struct statvfs *buf)
|
||||
{
|
||||
FSStatus rc;
|
||||
char *path_fix = __wut_fs_fixpath(r, path);
|
||||
|
||||
if (!path_fix) {
|
||||
r->_errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
//TODO: FSGetFileSystemInfo
|
||||
|
||||
free(path_fix);
|
||||
r->_errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
@ -5,32 +5,30 @@ __wut_fs_ftruncate(struct _reent *r,
|
||||
void *fd,
|
||||
off_t len)
|
||||
{
|
||||
FSStatus rc;
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
// Make sure length is non-negative
|
||||
if (len < 0) {
|
||||
if (!fd || len < 0) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
// Set the new file size
|
||||
rc = FSSetPosFile(__wut_devoptab_fs_client, &fsCmd, file->fd, len, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
FSInitCmdBlock(&cmd);
|
||||
file = (__wut_fs_file_t *)fd;
|
||||
status = FSSetPosFile(__wut_devoptab_fs_client, &cmd, file->fd, len, -1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = FSTruncateFile(__wut_devoptab_fs_client, &fsCmd, file->fd, -1);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
status = FSTruncateFile(__wut_devoptab_fs_client, &cmd, file->fd, -1);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,30 +4,27 @@ int
|
||||
__wut_fs_unlink(struct _reent *r,
|
||||
const char *name)
|
||||
{
|
||||
FSStatus rc;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
char *fixedPath;
|
||||
|
||||
if (name == NULL) {
|
||||
if (!name) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *path_fix = __wut_fs_fixpath(r, name);
|
||||
|
||||
if (!path_fix) {
|
||||
r->_errno = ENOMEM;
|
||||
fixedPath = __wut_fs_fixpath(r, name);
|
||||
if (!fixedPath) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
|
||||
rc = FSRemove(__wut_devoptab_fs_client, &fsCmd, path_fix, -1);
|
||||
free(path_fix);
|
||||
|
||||
if (rc >= 0) {
|
||||
return 0;
|
||||
FSInitCmdBlock(&cmd);
|
||||
status = FSRemove(__wut_devoptab_fs_client, &cmd, fixedPath, -1);
|
||||
free(fixedPath);
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,8 +4,15 @@ char *
|
||||
__wut_fs_fixpath(struct _reent *r,
|
||||
const char *path)
|
||||
{
|
||||
char *p = strchr(path, ':') + 1;
|
||||
char *p;
|
||||
char *fixedPath;
|
||||
|
||||
if (!path) {
|
||||
r->_errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = strchr(path, ':') + 1;
|
||||
if (!strchr(path, ':')) {
|
||||
p = (char*)path;
|
||||
}
|
||||
@ -15,21 +22,23 @@ __wut_fs_fixpath(struct _reent *r,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *__fixedpath = memalign(0x40, PATH_MAX + 1);
|
||||
|
||||
if (__fixedpath == NULL) {
|
||||
fixedPath = memalign(0x40, PATH_MAX + 1);
|
||||
if (!fixedPath) {
|
||||
r->_errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// cwd is handled by coreinit, so just strip the 'device:' if it exists
|
||||
strcpy(__fixedpath, p);
|
||||
return __fixedpath;
|
||||
strcpy(fixedPath, p);
|
||||
return fixedPath;
|
||||
}
|
||||
|
||||
int
|
||||
__wut_fs_translate_error(FSStatus error)
|
||||
{
|
||||
switch (error) {
|
||||
case FS_STATUS_END:
|
||||
return ENOENT;
|
||||
case FS_STATUS_CANCELLED:
|
||||
return EINVAL;
|
||||
case FS_STATUS_EXISTS:
|
||||
@ -44,4 +53,3 @@ __wut_fs_translate_error(FSStatus error)
|
||||
return (int)error;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,9 +6,20 @@ __wut_fs_write(struct _reent *r,
|
||||
const char *ptr,
|
||||
size_t len)
|
||||
{
|
||||
FSStatus rc;
|
||||
uint32_t bytes, bytesWritten = 0;
|
||||
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
||||
FSStatus status;
|
||||
FSCmdBlock cmd;
|
||||
uint8_t *alignedWriteBuffer;
|
||||
uint32_t bytes, bytesWritten;
|
||||
__wut_fs_file_t *file;
|
||||
|
||||
if (!fd || !ptr) {
|
||||
r->_errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
FSInitCmdBlock(&cmd);
|
||||
file = (__wut_fs_file_t *)fd;
|
||||
bytesWritten = 0;
|
||||
|
||||
// Check that the file was opened with write access
|
||||
if ((file->flags & O_ACCMODE) == O_RDONLY) {
|
||||
@ -16,46 +27,42 @@ __wut_fs_write(struct _reent *r,
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Copy to internal buffer and write in chunks.
|
||||
uint8_t *tmp_buffer = memalign(0x40, 8192);
|
||||
|
||||
while(len > 0) {
|
||||
size_t toWrite = len;
|
||||
|
||||
if (toWrite > 8192) {
|
||||
toWrite = 8192;
|
||||
}
|
||||
// Copy to internal buffer due to alignment requirement and write in chunks.
|
||||
alignedWriteBuffer = memalign(0x40, 8192);
|
||||
while (len > 0) {
|
||||
size_t toWrite = len > 8192 ? 8192 : len;
|
||||
|
||||
// Copy to internal buffer
|
||||
memcpy(tmp_buffer, ptr, toWrite);
|
||||
|
||||
// Set up command block
|
||||
FSCmdBlock fsCmd;
|
||||
FSInitCmdBlock(&fsCmd);
|
||||
memcpy(alignedWriteBuffer, ptr, toWrite);
|
||||
|
||||
// Write the data
|
||||
rc = FSWriteFile(__wut_devoptab_fs_client, &fsCmd, tmp_buffer, 1, toWrite, file->fd, 0, -1);
|
||||
|
||||
if (rc < 0) {
|
||||
free(tmp_buffer);
|
||||
|
||||
// Return partial transfer
|
||||
if (bytesWritten > 0) {
|
||||
return bytesWritten;
|
||||
}
|
||||
|
||||
r->_errno = __wut_fs_translate_error(rc);
|
||||
return -1;
|
||||
} else {
|
||||
bytes = rc;
|
||||
status = FSWriteFile(__wut_devoptab_fs_client, &cmd, alignedWriteBuffer,
|
||||
1, toWrite, file->fd, 0, -1);
|
||||
if (status <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
bytes = (uint32_t)status;
|
||||
file->offset += bytes;
|
||||
bytesWritten += bytes;
|
||||
ptr += bytes;
|
||||
len -= bytes;
|
||||
|
||||
if (bytes < toWrite) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(alignedWriteBuffer);
|
||||
|
||||
// Return partial write
|
||||
if (bytesWritten > 0) {
|
||||
return bytesWritten;
|
||||
}
|
||||
|
||||
free(tmp_buffer);
|
||||
return bytesWritten;
|
||||
if (status < 0) {
|
||||
r->_errno = __wut_fs_translate_error(status);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user