/*------------------------------------------------------------/\r
-/ Remove all contents of a directory\r
-/ This function works regardless of FF_FS_RPATH.\r
-/------------------------------------------------------------*/\r
+/ Delete a sub-directory even if it contains any file\r
+/-------------------------------------------------------------/\r
+/ The delete_node() function is for R0.12+.\r
+/ It works regardless of FF_FS_RPATH.\r
+*/\r
\r
\r
-FILINFO fno;\r
-\r
-FRESULT empty_directory (\r
- char* path /* Working buffer filled with start directory */\r
+FRESULT delete_node (\r
+ TCHAR* path, /* Path name buffer with the sub-directory to delete */\r
+ UINT sz_buff, /* Size of path name buffer (items) */\r
+ FILINFO* fno /* Name read buffer */\r
)\r
{\r
UINT i, j;\r
FRESULT fr;\r
DIR dir;\r
\r
- fr = f_opendir(&dir, path);\r
- if (fr == FR_OK) {\r
- for (i = 0; path[i]; i++) ;\r
- path[i++] = '/';\r
- for (;;) {\r
- fr = f_readdir(&dir, &fno);\r
- if (fr != FR_OK || !fno.fname[0]) break;\r
- if (_FS_RPATH && fno.fname[0] == '.') continue;\r
- j = 0;\r
- do\r
- path[i+j] = fno.fname[j];\r
- while (fno.fname[j++]);\r
- if (fno.fattrib & AM_DIR) {\r
- fr = empty_directory(path);\r
- if (fr != FR_OK) break;\r
+\r
+ fr = f_opendir(&dir, path); /* Open the sub-directory to make it empty */\r
+ if (fr != FR_OK) return fr;\r
+\r
+ for (i = 0; path[i]; i++) ; /* Get current path length */\r
+ path[i++] = _T('/');\r
+\r
+ for (;;) {\r
+ fr = f_readdir(&dir, fno); /* Get a directory item */\r
+ if (fr != FR_OK || !fno->fname[0]) break; /* End of directory? */\r
+ j = 0;\r
+ do { /* Make a path name */\r
+ if (i + j >= sz_buff) { /* Buffer over flow? */\r
+ fr = 100; break; /* Fails with 100 when buffer overflow */\r
}\r
+ path[i + j] = fno->fname[j];\r
+ } while (fno->fname[j++]);\r
+ if (fno->fattrib & AM_DIR) { /* Item is a sub-directory */\r
+ fr = delete_node(path, sz_buff, fno);\r
+ } else { /* Item is a file */\r
fr = f_unlink(path);\r
- if (fr != FR_OK) break;\r
}\r
- path[--i] = '\0';\r
- closedir(&dir);\r
+ if (fr != FR_OK) break;\r
}\r
\r
+ path[--i] = 0; /* Restore the path name */\r
+ f_closedir(&dir);\r
+\r
+ if (fr == FR_OK) fr = f_unlink(path); /* Delete the empty sub-directory */\r
return fr;\r
}\r
\r
\r
\r
-int main (void)\r
+\r
+int main (void) /* How to use */\r
{\r
FRESULT fr;\r
FATFS fs;\r
- char buff[256]; /* Working buffer */\r
+ TCHAR buff[256];\r
+ FILINFO fno;\r
\r
\r
+ f_mount(&fs, _T("5:"), 0);\r
\r
- f_mount(&fs, "", 0);\r
+ /* Directory to be deleted */\r
+ _tcscpy(buff, _T("5:dir"));\r
\r
- strcpy(buff, "/"); /* Directory to be emptied */\r
- fr = empty_directory(buff);\r
+ /* Delete the directory */\r
+ fr = delete_node(buff, sizeof buff / sizeof buff[0], &fno);\r
\r
+ /* Check the result */\r
if (fr) {\r
- printf("Function failed. (%u)\n", fr);\r
+ _tprintf(_T("Failed to delete the directory. (%u)\n"), fr);\r
return fr;\r
} else {\r
- printf("All contents in the %s are successfully removed.\n", buff);\r
+ _tprintf(_T("The directory and the contents have successfully been deleted.\n"), buff);\r
return 0;\r
}\r
}\r