wut/libraries/wutdevoptab/devoptab_fs_stat.c

43 lines
1013 B
C
Raw Normal View History

2018-05-27 13:11:45 +02:00
#include "devoptab_fs.h"
int
__wut_fs_stat(struct _reent *r,
const char *file,
struct stat *st)
{
int fd;
FSStatus rc;
if (file == NULL) {
return -1;
}
// Set up command block
FSCmdBlock fsCmd;
FSInitCmdBlock(&fsCmd);
// First try open as file
rc = FSOpenFile(__wut_devoptab_fs_client, &fsCmd, file, "r", (FSFileHandle*)&fd, -1);
if (rc >= 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;
}
// 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;
}
r->_errno = __wut_fs_translate_error(rc);
return -1;
}