diff --git a/src/utils/fs.c b/src/utils/fs.c index 987aed79..e37ef5a4 100644 --- a/src/utils/fs.c +++ b/src/utils/fs.c @@ -1,3 +1,4 @@ +#include #include #include @@ -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; +} diff --git a/src/utils/fs.h b/src/utils/fs.h index 1f7108e3..2f70df12 100644 --- a/src/utils/fs.h +++ b/src/utils/fs.h @@ -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