2018-07-19 08:42:12 +01:00
|
|
|
#include "devoptab_fs.h"
|
2018-05-27 12:11:45 +01:00
|
|
|
|
|
|
|
ssize_t
|
|
|
|
__wut_fs_read(struct _reent *r,
|
|
|
|
void *fd,
|
|
|
|
char *ptr,
|
|
|
|
size_t len)
|
|
|
|
{
|
2018-10-06 10:41:24 +01:00
|
|
|
FSStatus status;
|
|
|
|
FSCmdBlock cmd;
|
|
|
|
uint8_t *alignedReadBuffer;
|
|
|
|
uint32_t bytes, bytesRead;
|
|
|
|
__wut_fs_file_t *file;
|
2018-05-27 12:11:45 +01:00
|
|
|
|
2018-10-06 10:41:24 +01:00
|
|
|
if (!fd || !ptr) {
|
|
|
|
r->_errno = EINVAL;
|
2018-05-27 12:11:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-06 10:41:24 +01:00
|
|
|
FSInitCmdBlock(&cmd);
|
|
|
|
file = (__wut_fs_file_t *)fd;
|
|
|
|
bytesRead = 0;
|
2018-05-27 12:11:45 +01:00
|
|
|
|
2018-10-06 10:41:24 +01:00
|
|
|
// Check that the file was opened with read access
|
|
|
|
if ((file->flags & O_ACCMODE) == O_WRONLY) {
|
|
|
|
r->_errno = EBADF;
|
2018-05-27 12:11:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-06 10:41:24 +01:00
|
|
|
// 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;
|
2018-05-27 12:11:45 +01:00
|
|
|
|
|
|
|
// Write the data
|
2018-10-06 10:41:24 +01:00
|
|
|
status = FSReadFile(__wut_devoptab_fs_client, &cmd, alignedReadBuffer, 1,
|
|
|
|
toRead, file->fd, 0, -1);
|
|
|
|
if (status <= 0) {
|
|
|
|
break;
|
2018-05-27 12:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy to internal buffer
|
2018-10-06 10:41:24 +01:00
|
|
|
bytes = (uint32_t)status;
|
|
|
|
memcpy(ptr, alignedReadBuffer, bytes);
|
2018-05-27 12:11:45 +01:00
|
|
|
|
|
|
|
file->offset += bytes;
|
|
|
|
bytesRead += bytes;
|
|
|
|
ptr += bytes;
|
|
|
|
len -= bytes;
|
2018-10-06 10:41:24 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
r->_errno = __wut_fs_translate_error(status);
|
|
|
|
return -1;
|
2018-05-27 12:11:45 +01:00
|
|
|
}
|
|
|
|
|
2018-10-06 10:41:24 +01:00
|
|
|
return 0;
|
2018-05-27 12:11:45 +01:00
|
|
|
}
|