Created
February 11, 2020 17:21
-
-
Save averne/12206644a9914d6ee0c3b5300f9224ce to your computer and use it in GitHub Desktop.
Listing dir contents with raw fs calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <switch.h> | |
int main(int argc, char **argv) { | |
consoleInit(NULL); | |
FsFileSystem sd_fs; | |
Result rc = fsOpenSdCardFileSystem(&sd_fs); | |
if (R_FAILED(rc)) | |
printf("Failed to open sd filesystem: %#x", rc); | |
if (R_SUCCEEDED(rc)) { | |
s64 free = 0; | |
rc = fsFsGetFreeSpace(&sd_fs, "/", &free); | |
if (R_SUCCEEDED(rc)) | |
printf("Free space: %ld\n", free); | |
else | |
printf("Failed to get free space info: %#x\n", rc); | |
} | |
if (R_SUCCEEDED(rc)) { | |
s64 total = 0; | |
rc = fsFsGetTotalSpace(&sd_fs, "/", &total); | |
if (R_SUCCEEDED(rc)) | |
printf("Total space: %ld\n", total); | |
else | |
printf("Failed to get total space info: %#x\n", rc); | |
} | |
FsDir dir; | |
if (R_SUCCEEDED(rc)) { | |
rc = fsFsOpenDirectory(&sd_fs, "/", FsDirOpenMode_ReadDirs | FsDirOpenMode_ReadFiles, &dir); | |
if (R_FAILED(rc)) | |
printf("Failed to open directory: %#x\n", rc); | |
} | |
s64 count; | |
if (R_SUCCEEDED(rc)) { | |
rc = fsDirGetEntryCount(&dir, &count); | |
if (R_SUCCEEDED(rc)) | |
printf("Entry count: %ld\n", count); | |
else | |
printf("Failed to get entry count: %#x\n", rc); | |
} | |
FsDirectoryEntry entries[count]; | |
if (R_SUCCEEDED(rc)) { | |
s64 total_entries; | |
rc = fsDirRead(&dir, &total_entries, count, entries); | |
if (R_SUCCEEDED(rc)) | |
for (s64 i = 0; i < total_entries; ++i) | |
printf("%s: %d, %#lx\n", entries[i].name, entries[i].type, entries[i].file_size); | |
else | |
printf("Failed to read dir: %#x\n", rc); | |
} | |
fsDirClose(&dir); | |
fsFsClose(&sd_fs); | |
while (appletMainLoop()) { | |
hidScanInput(); | |
if (hidKeysDown(CONTROLLER_P1_AUTO) & KEY_PLUS) | |
break; | |
consoleUpdate(NULL); | |
} | |
consoleExit(NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment