Added more fs utils

This commit is contained in:
Mateusz Faderewski 2023-07-28 21:22:40 +02:00
parent 638aafcb74
commit 0fd8494007
2 changed files with 84 additions and 14 deletions

View File

@ -1,3 +1,4 @@
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
@ -7,39 +8,50 @@
#include "utils.h" #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) { bool file_exists (char *path) {
FRESULT fr; FRESULT fr;
FILINFO fno; 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) { size_t file_get_size (char *path) {
FILINFO fno; FILINFO fno;
if (f_stat(path, &fno) != FR_OK) { if (f_stat(strip_sd_prefix(path), &fno) != FR_OK) {
return 0; return 0;
} }
return (size_t) (fno.fsize); 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) { bool file_allocate (char *path, size_t size) {
FIL fil; FIL fil;
bool error = false; 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; return true;
} }
@ -67,7 +79,7 @@ bool file_get_sectors (char *path, uint32_t *sectors, size_t entries) {
sectors[i] = 0; 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; return true;
} }
@ -117,3 +129,55 @@ bool file_has_extensions (char *path, const char *extensions[]) {
return false; 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;
}

View File

@ -10,12 +10,18 @@
#define FS_SECTOR_SIZE (512) #define FS_SECTOR_SIZE (512)
char *strip_sd_prefix (char *path);
bool file_exists (char *path); bool file_exists (char *path);
bool directory_exists (char *path);
size_t file_get_size (char *path); size_t file_get_size (char *path);
bool file_delete (char *path);
bool file_allocate (char *path, size_t size); bool file_allocate (char *path, size_t size);
bool file_get_sectors (char *path, uint32_t *sectors, size_t entries); bool file_get_sectors (char *path, uint32_t *sectors, size_t entries);
bool file_has_extensions (char *path, const char *extensions[]); 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 #endif