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