-
Notifications
You must be signed in to change notification settings - Fork 1
/
remove.c
62 lines (55 loc) · 1.6 KB
/
remove.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <fatio.h>
#include <wchar.h>
#include "fatfs/ff.h"
static
FRESULT remove_files(const wchar_t* path)
{
FRESULT res;
DIR dir;
static FILINFO fno;
wchar_t new_path[256];
res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
if (fno.fattrib & AM_DIR) /* It is a directory */
{
swprintf(new_path, sizeof(new_path) / sizeof(new_path[0]), L"%s\\%s", path, fno.fname);
res = remove_files(new_path); /* Enter the directory */
if (res != FR_OK) break;
f_unlink(new_path);
}
else /* It is a file. */
{
swprintf(new_path, sizeof(new_path) / sizeof(new_path[0]), L"%s\\%s", path, fno.fname);
f_unlink(new_path);
}
}
f_closedir(&dir);
f_unlink(path);
}
return res;
}
bool
fatio_remove(const wchar_t* path)
{
FRESULT rc;
DIR dir;
rc = f_opendir(&dir, path);
if (rc == FR_OK)
{
rc = remove_files(path);
if (rc == FR_OK)
return true;
}
else
{
rc = f_unlink(path);
if (rc == FR_OK || rc == FR_EXIST)
return true;
}
wprintf(L"remove %s failed %d\n", path, rc);
return false;
}