wut/libraries/wutdevoptab/devoptab_fs_stat.c

51 lines
1.2 KiB
C
Raw Normal View History

#include "devoptab_fs.h"
2018-05-27 12:11:45 +01:00
int
__wut_fs_stat(struct _reent *r,
const char *path,
2018-05-27 12:11:45 +01:00
struct stat *st)
{
int fd;
FSStatus status;
FSCmdBlock cmd;
2018-05-27 12:11:45 +01:00
if (!path || !st) {
r->_errno = EINVAL;
2018-05-27 12:11:45 +01:00
return -1;
}
char *fixedPath = __wut_fs_fixpath(r, path);
if (!fixedPath) {
return -1;
}
2018-05-27 12:11:45 +01:00
FSInitCmdBlock(&cmd);
2018-05-27 12:11:45 +01:00
// First try open as file
status = FSOpenFile(__wut_devoptab_fs_client, &cmd, fixedPath, "r",
(FSFileHandle*)&fd, -1);
if (status >= 0) {
2018-05-27 12:11:45 +01:00
__wut_fs_file_t tmpfd = { .fd = fd };
status = __wut_fs_fstat(r, &tmpfd, st);
FSCloseFile(__wut_devoptab_fs_client, &cmd, fd, -1);
free(fixedPath);
return status;
2018-05-27 12:11:45 +01:00
}
// File failed, so lets try open as directory
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;
2018-05-27 12:11:45 +01:00
}
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;
2018-05-27 12:11:45 +01:00
}