Skip to content

Commit

Permalink
- Implement statFile for future use.
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Dec 13, 2024
1 parent e1d2a59 commit bf06fb8
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 4 deletions.
1 change: 1 addition & 0 deletions cube/swiss/source/devices/aram/deviceHandler-ARAM.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ DEVICEHANDLER_INTERFACE __device_aram = {
.init = deviceHandler_ARAM_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down
2 changes: 2 additions & 0 deletions cube/swiss/source/devices/deviceHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ typedef device_info* (* _fn_info)(file_handle*);
typedef s32 (* _fn_init)(file_handle*);
typedef s32 (* _fn_makeDir)(file_handle*);
typedef s32 (* _fn_readDir)(file_handle*, file_handle**, u32);
typedef s32 (* _fn_statFile)(file_handle*);
typedef s64 (* _fn_seekFile)(file_handle*, s64, u32);
typedef s32 (* _fn_readFile)(file_handle*, void*, u32);
typedef s32 (* _fn_writeFile)(file_handle*, const void*, u32);
Expand Down Expand Up @@ -194,6 +195,7 @@ struct DEVICEHANDLER_STRUCT {
_fn_init init;
_fn_makeDir makeDir;
_fn_readDir readDir;
_fn_statFile statFile;
_fn_seekFile seekFile;
_fn_readFile readFile;
_fn_writeFile writeFile;
Expand Down
19 changes: 19 additions & 0 deletions cube/swiss/source/devices/fat/deviceHandler-FAT.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ s32 deviceHandler_FAT_readDir(file_handle* ffile, file_handle** dir, u32 type) {
}
memset(&(*dir)[i], 0, sizeof(file_handle));
if(concat_path((*dir)[i].name, ffile->name, entry.fname) < PATHNAME_MAX && entry.fsize <= UINT32_MAX) {
(*dir)[i].fileBase = entry.fclust;
(*dir)[i].size = entry.fsize;
(*dir)[i].fileType = (entry.fattrib & AM_DIR) ? IS_DIR : IS_FILE;
(*dir)[i].fileAttrib = entry.fattrib;
Expand All @@ -144,6 +145,18 @@ s32 deviceHandler_FAT_readDir(file_handle* ffile, file_handle** dir, u32 type) {
return i;
}

s32 deviceHandler_FAT_statFile(file_handle* file) {
FILINFO entry;
int ret = f_stat(file->name, &entry);
if(ret == FR_OK) {
file->fileBase = entry.fclust;
file->size = entry.fsize;
file->fileType = (entry.fattrib & AM_DIR) ? IS_DIR : IS_FILE;
file->fileAttrib = entry.fattrib;
}
return ret;
}

s64 deviceHandler_FAT_seekFile(file_handle* file, s64 where, u32 type){
if(type == DEVICE_HANDLER_SEEK_SET) file->offset = where;
else if(type == DEVICE_HANDLER_SEEK_CUR) file->offset = file->offset + where;
Expand Down Expand Up @@ -564,6 +577,7 @@ DEVICEHANDLER_INTERFACE __device_sd_a = {
.init = deviceHandler_FAT_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down Expand Up @@ -593,6 +607,7 @@ DEVICEHANDLER_INTERFACE __device_sd_b = {
.init = deviceHandler_FAT_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down Expand Up @@ -622,6 +637,7 @@ DEVICEHANDLER_INTERFACE __device_ata_a = {
.init = deviceHandler_FAT_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down Expand Up @@ -650,6 +666,7 @@ DEVICEHANDLER_INTERFACE __device_ata_b = {
.init = deviceHandler_FAT_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down Expand Up @@ -678,6 +695,7 @@ DEVICEHANDLER_INTERFACE __device_sd_c = {
.init = deviceHandler_FAT_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down Expand Up @@ -707,6 +725,7 @@ DEVICEHANDLER_INTERFACE __device_ata_c = {
.init = deviceHandler_FAT_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down
1 change: 1 addition & 0 deletions cube/swiss/source/devices/fat/deviceHandler-FAT.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern DEVICEHANDLER_INTERFACE __device_ata_c;
extern device_info* deviceHandler_FAT_info(file_handle* file);
extern s32 deviceHandler_FAT_makeDir(file_handle* dir);
extern s32 deviceHandler_FAT_readDir(file_handle* ffile, file_handle** dir, u32 type);
extern s32 deviceHandler_FAT_statFile(file_handle* file);
extern s64 deviceHandler_FAT_seekFile(file_handle* file, s64 where, u32 type);
extern s32 deviceHandler_FAT_readFile(file_handle* file, void* buffer, u32 length);
extern s32 deviceHandler_FAT_writeFile(file_handle* file, const void* buffer, u32 length);
Expand Down
11 changes: 11 additions & 0 deletions cube/swiss/source/devices/fsp/deviceHandler-FSP.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ s32 deviceHandler_FSP_readDir(file_handle* ffile, file_handle** dir, u32 type) {
return i;
}

s32 deviceHandler_FSP_statFile(file_handle* file) {
struct stat fstat;
int ret = fsp_stat(fsp_session, getDevicePath(file->name), &fstat);
if(ret == 0) {
file->size = fstat.st_size;
file->fileType = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
}
return ret;
}

s64 deviceHandler_FSP_seekFile(file_handle* file, s64 where, u32 type) {
if(type == DEVICE_HANDLER_SEEK_SET) file->offset = where;
else if(type == DEVICE_HANDLER_SEEK_CUR) file->offset = file->offset + where;
Expand Down Expand Up @@ -335,6 +345,7 @@ DEVICEHANDLER_INTERFACE __device_fsp = {
.init = deviceHandler_FSP_init,
.makeDir = deviceHandler_FSP_makeDir,
.readDir = deviceHandler_FSP_readDir,
.statFile = deviceHandler_FSP_statFile,
.seekFile = deviceHandler_FSP_seekFile,
.readFile = deviceHandler_FSP_readFile,
.writeFile = deviceHandler_FSP_writeFile,
Expand Down
13 changes: 13 additions & 0 deletions cube/swiss/source/devices/ftp/deviceHandler-FTP.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ s32 deviceHandler_FTP_readDir(file_handle* ffile, file_handle** dir, u32 type){
&& !stat((*dir)[i].name, &fstat)
#endif
&& fstat.st_size <= UINT32_MAX) {
(*dir)[i].fileBase = fstat.st_ino;
(*dir)[i].size = fstat.st_size;
(*dir)[i].fileType = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
++i;
Expand All @@ -137,6 +138,17 @@ s32 deviceHandler_FTP_readDir(file_handle* ffile, file_handle** dir, u32 type){
return i;
}

s32 deviceHandler_FTP_statFile(file_handle* file) {
struct stat fstat;
int ret = stat(file->name, &fstat);
if(ret == 0) {
file->fileBase = fstat.st_ino;
file->size = fstat.st_size;
file->fileType = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
}
return ret;
}

s64 deviceHandler_FTP_seekFile(file_handle* file, s64 where, u32 type){
if(type == DEVICE_HANDLER_SEEK_SET) file->offset = where;
else if(type == DEVICE_HANDLER_SEEK_CUR) file->offset = file->offset + where;
Expand Down Expand Up @@ -281,6 +293,7 @@ DEVICEHANDLER_INTERFACE __device_ftp = {
.init = deviceHandler_FTP_init,
.makeDir = deviceHandler_FTP_makeDir,
.readDir = deviceHandler_FTP_readDir,
.statFile = deviceHandler_FTP_statFile,
.seekFile = deviceHandler_FTP_seekFile,
.readFile = deviceHandler_FTP_readFile,
.writeFile = deviceHandler_FTP_writeFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ DEVICEHANDLER_INTERFACE __device_gcloader = {
.init = deviceHandler_GCLoader_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_GCLoader_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down
13 changes: 13 additions & 0 deletions cube/swiss/source/devices/smb/deviceHandler-SMB.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ s32 deviceHandler_SMB_readDir(file_handle* ffile, file_handle** dir, u32 type){
&& !stat((*dir)[i].name, &fstat)
#endif
&& fstat.st_size <= UINT32_MAX) {
(*dir)[i].fileBase = fstat.st_ino;
(*dir)[i].size = fstat.st_size;
(*dir)[i].fileType = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
++i;
Expand All @@ -141,6 +142,17 @@ s32 deviceHandler_SMB_readDir(file_handle* ffile, file_handle** dir, u32 type){
return i;
}

s32 deviceHandler_SMB_statFile(file_handle* file) {
struct stat fstat;
int ret = stat(file->name, &fstat);
if(ret == 0) {
file->fileBase = fstat.st_ino;
file->size = fstat.st_size;
file->fileType = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
}
return ret;
}

s64 deviceHandler_SMB_seekFile(file_handle* file, s64 where, u32 type){
if(type == DEVICE_HANDLER_SEEK_SET) file->offset = where;
else if(type == DEVICE_HANDLER_SEEK_CUR) file->offset = file->offset + where;
Expand Down Expand Up @@ -286,6 +298,7 @@ DEVICEHANDLER_INTERFACE __device_smb = {
.init = deviceHandler_SMB_init,
.makeDir = deviceHandler_SMB_makeDir,
.readDir = deviceHandler_SMB_readDir,
.statFile = deviceHandler_SMB_statFile,
.seekFile = deviceHandler_SMB_seekFile,
.readFile = deviceHandler_SMB_readFile,
.writeFile = deviceHandler_SMB_writeFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ DEVICEHANDLER_INTERFACE __device_wkf = {
.init = deviceHandler_WKF_init,
.makeDir = deviceHandler_FAT_makeDir,
.readDir = deviceHandler_FAT_readDir,
.statFile = deviceHandler_FAT_statFile,
.seekFile = deviceHandler_FAT_seekFile,
.readFile = deviceHandler_FAT_readFile,
.writeFile = deviceHandler_FAT_writeFile,
Expand Down
20 changes: 16 additions & 4 deletions cube/swiss/source/fatfs/ff.c
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,7 @@ static void get_fileinfo (
fno->fname[di] = 0; /* Terminate the name */
fno->altname[0] = 0; /* exFAT does not support SFN */

fno->fclust = ld_dword(fs->dirbuf + XDIR_FstClus); /* Start cluster */
fno->fattrib = fs->dirbuf[XDIR_Attr] & AM_MASKX; /* Attribute */
fno->fsize = (fno->fattrib & AM_DIR) ? 0 : ld_qword(fs->dirbuf + XDIR_FileSize); /* Size */
fno->ftime = ld_word(fs->dirbuf + XDIR_ModTime + 0); /* Time */
Expand Down Expand Up @@ -2771,6 +2772,7 @@ static void get_fileinfo (
fno->fname[di] = 0; /* Terminate the SFN */
#endif

fno->fclust = ld_clust(fs, dp->dir); /* Start cluster */
fno->fattrib = dp->dir[DIR_Attr] & AM_MASK; /* Attribute */
fno->fsize = ld_dword(dp->dir + DIR_FileSize); /* Size */
fno->ftime = ld_word(dp->dir + DIR_ModTime + 0); /* Time */
Expand Down Expand Up @@ -4860,26 +4862,36 @@ FRESULT f_stat (
)
{
FRESULT res;
FATFS *fs;
DIRF dj;
DEF_NAMBUF


/* Get logical drive */
res = mount_volume(&path, &dj.obj.fs, 0);
res = mount_volume(&path, &fs, 0);
if (res == FR_OK) {
INIT_NAMBUF(dj.obj.fs);
dj.obj.fs = fs;
INIT_NAMBUF(fs);
res = follow_path(&dj, path); /* Follow the file path */
if (res == FR_OK) { /* Follow completed */
if (dj.fn[NSFLAG] & NS_NONAME) { /* It is origin directory */
res = FR_INVALID_NAME;
fno->fsize = 0;
fno->fclust = (fs->fs_type >= FS_FAT32) ? fs->dirbase : 0;
fno->fdate = 0;
fno->ftime = 0;
fno->fattrib = AM_DIR;
fno->fname[0] = 0;
#if FF_USE_LFN
fno->altname[0] = 0;
#endif
} else { /* Found an object */
if (fno) get_fileinfo(&dj, fno);
}
}
FREE_NAMBUF();
}

LEAVE_FF(dj.obj.fs, res);
LEAVE_FF(fs, res);
}


Expand Down
1 change: 1 addition & 0 deletions cube/swiss/source/fatfs/ff.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ typedef struct {

typedef struct {
FSIZE_t fsize; /* File size */
DWORD fclust; /* File cluster */
WORD fdate; /* Modified date */
WORD ftime; /* Modified time */
BYTE fattrib; /* File attribute */
Expand Down

0 comments on commit bf06fb8

Please sign in to comment.