1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include "amp_platform.h"
6 #include "aos_system.h"
7 #include "aos_fs.h"
8 #include "aos/vfs.h"
9 #ifndef AOS_BOARD_HAAS700
10 #include "vfs_types.h"
11 #else
12 #include "fs/vfs_types.h"
13 #endif
14
aos_fs_init(void)15 int aos_fs_init(void)
16 {
17 return 0;
18 }
19
aos_rmdir_r(const char * path)20 int aos_rmdir_r(const char *path)
21 {
22 struct aos_stat s;
23 int ret = -1;
24 char *dir, *p;
25 int path_len;
26
27 aos_dir_t *pdir = NULL;
28 aos_dirent_t *entry = NULL;
29
30 if (!path)
31 return -EINVAL;
32
33 path_len = strlen(path) + 1;
34 dir = aos_malloc(path_len);
35 if (dir == NULL) {
36 return -1;
37 }
38 memcpy(dir, path, path_len);
39
40 p = dir + strlen(dir) - 1;
41 while ((*p == '/') && (p > dir)) {
42 *p = '\0';
43 p--;
44 }
45
46 if (aos_stat(dir, &s) || !S_ISDIR(s.st_mode)) {
47 aos_printf("%s is neither existed nor a directory\n", dir);
48 goto out;
49 }
50
51 pdir = aos_opendir(dir);
52 if (!pdir) {
53 aos_printf("opendir %s failed - %s\n", dir, strerror(errno));
54 goto out;
55 }
56
57 ret = 0;
58 while ((ret == 0) && (entry = aos_readdir(pdir))) {
59 char fpath[128];
60
61 snprintf(fpath, 128, "%s/%s", dir, entry->d_name);
62
63 ret = aos_stat(fpath, &s);
64 if (ret) {
65 aos_printf("stat %s failed\n", fpath);
66 break;
67 }
68
69 if (!strcmp(entry->d_name, "."))
70 continue;
71 if (!strcmp(entry->d_name, ".."))
72 continue;
73
74 if (S_ISDIR(s.st_mode))
75 ret = aos_rmdir_r(fpath);
76 else
77 ret = aos_unlink(fpath);
78 }
79
80 aos_closedir(pdir);
81 if (ret == 0) {
82 ret = aos_rmdir(dir);
83 if (ret)
84 aos_printf("rmdir %s failed\n", dir);
85 }
86 out:
87 aos_free(dir);
88 return ret;
89 }
90
aos_fs_type(uint mode)91 int aos_fs_type(uint mode)
92 {
93 if (mode & S_IFDIR) {
94 return 0;
95 } else if (mode & S_IFREG) {
96 return 1;
97 }
98 return -1;
99 }