mirror of
https://github.com/Polprzewodnikowy/N64FlashcartMenu.git
synced 2024-11-22 02:29:19 +01:00
Added more fs utils
This commit is contained in:
parent
638aafcb74
commit
0fd8494007
@ -1,3 +1,4 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
@ -7,39 +8,50 @@
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
char *strip_sd_prefix (char *path) {
|
||||
const char *prefix = "sd:/";
|
||||
|
||||
char *found = strstr(path, prefix);
|
||||
if (found) {
|
||||
return found + strlen(prefix);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
bool file_exists (char *path) {
|
||||
FRESULT fr;
|
||||
FILINFO fno;
|
||||
|
||||
fr = f_stat(path, &fno);
|
||||
fr = f_stat(strip_sd_prefix(path), &fno);
|
||||
|
||||
return (fr == FR_OK);
|
||||
return ((fr == FR_OK) && (!(fno.fattrib & AM_DIR)));
|
||||
}
|
||||
|
||||
bool directory_exists (char *path) {
|
||||
FRESULT fr;
|
||||
FILINFO fno;
|
||||
|
||||
fr = f_stat(path, &fno);
|
||||
|
||||
return ((fr == FR_OK) && (fno.fattrib & AM_DIR));
|
||||
}
|
||||
|
||||
size_t file_get_size (char *path) {
|
||||
FILINFO fno;
|
||||
|
||||
if (f_stat(path, &fno) != FR_OK) {
|
||||
if (f_stat(strip_sd_prefix(path), &fno) != FR_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (size_t) (fno.fsize);
|
||||
}
|
||||
|
||||
bool file_delete (char *path) {
|
||||
if (file_exists(path)) {
|
||||
return (f_unlink(strip_sd_prefix(path)) != FR_OK);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool file_allocate (char *path, size_t size) {
|
||||
FIL fil;
|
||||
bool error = false;
|
||||
|
||||
if (f_open(&fil, path, FA_WRITE | FA_CREATE_NEW) != FR_OK) {
|
||||
if (f_open(&fil, strip_sd_prefix(path), FA_WRITE | FA_CREATE_NEW) != FR_OK) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -67,7 +79,7 @@ bool file_get_sectors (char *path, uint32_t *sectors, size_t entries) {
|
||||
sectors[i] = 0;
|
||||
}
|
||||
|
||||
if (f_open(&fil, path, FA_READ) != FR_OK) {
|
||||
if (f_open(&fil, strip_sd_prefix(path), FA_READ) != FR_OK) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -117,3 +129,55 @@ bool file_has_extensions (char *path, const char *extensions[]) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool directory_exists (char *path) {
|
||||
FRESULT fr;
|
||||
FILINFO fno;
|
||||
|
||||
fr = f_stat(strip_sd_prefix(path), &fno);
|
||||
|
||||
return ((fr == FR_OK) && (fno.fattrib & AM_DIR));
|
||||
}
|
||||
|
||||
bool directory_delete (char *path) {
|
||||
if (directory_exists(path)) {
|
||||
return (f_unlink(strip_sd_prefix(path)) != FR_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool directory_create (char *path) {
|
||||
bool error = false;
|
||||
|
||||
if (directory_exists(path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char *directory = strdup(strip_sd_prefix(path));
|
||||
char *separator = directory;
|
||||
|
||||
do {
|
||||
separator = strchr(separator, '/');
|
||||
|
||||
if (separator != NULL) {
|
||||
*separator++ = '\0';
|
||||
}
|
||||
|
||||
if ((strlen(directory) > 0) && (!directory_exists(directory))) {
|
||||
if (f_mkdir(directory) != FR_OK) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (separator != NULL) {
|
||||
*(separator - 1) = '/';
|
||||
}
|
||||
} while (separator != NULL);
|
||||
|
||||
free(directory);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
@ -10,12 +10,18 @@
|
||||
#define FS_SECTOR_SIZE (512)
|
||||
|
||||
|
||||
char *strip_sd_prefix (char *path);
|
||||
|
||||
bool file_exists (char *path);
|
||||
bool directory_exists (char *path);
|
||||
size_t file_get_size (char *path);
|
||||
bool file_delete (char *path);
|
||||
bool file_allocate (char *path, size_t size);
|
||||
bool file_get_sectors (char *path, uint32_t *sectors, size_t entries);
|
||||
bool file_has_extensions (char *path, const char *extensions[]);
|
||||
|
||||
bool directory_exists (char *path);
|
||||
bool directory_delete (char *path);
|
||||
bool directory_create (char *path);
|
||||
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user