wut/libraries/wutdevoptab_sd/devoptab_sd_stat.c

43 lines
1013 B
C
Raw Normal View History

2018-06-13 14:36:15 +02:00
#include "devoptab_sd.h"
2018-05-27 13:11:45 +02:00
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
2018-06-13 14:36:15 +02:00
rc = FSOpenFile(__wut_devoptab_sd_client, &fsCmd, file, "r", (FSFileHandle*)&fd, -1);
2018-05-27 13:11:45 +02:00
if (rc >= 0) {
__wut_fs_file_t tmpfd = { .fd = fd };
rc = __wut_fs_fstat(r, &tmpfd, st);
2018-06-13 14:36:15 +02:00
FSCloseFile(__wut_devoptab_sd_client, &fsCmd, fd, -1);
2018-05-27 13:11:45 +02:00
return rc;
}
// File failed, so lets try open as directory
2018-06-13 14:36:15 +02:00
rc = FSOpenDir(__wut_devoptab_sd_client, &fsCmd, file, (FSDirectoryHandle*)&fd, -1);
2018-05-27 13:11:45 +02:00
if (rc >= 0) {
memset(st, 0, sizeof(struct stat));
st->st_nlink = 1;
st->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
2018-06-13 14:36:15 +02:00
FSCloseDir(__wut_devoptab_sd_client, &fsCmd, fd, -1);
2018-05-27 13:11:45 +02:00
return 0;
}
r->_errno = __wut_fs_translate_error(rc);
return -1;
}