1 #include <stdio.h>
2 #include <dirent.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <aos/cli.h>
8 #include <stdbool.h>
9 #include <fcntl.h>
10 #include <aos/vfs.h>
11 #include <path_helper.h>
12 
13 extern int32_t vfs_get_node_name(const char *path, char names[][64], uint32_t* size);
14 
15 #define KB (1024ULL)
df_do_dir(const char * dir)16 static void df_do_dir(const char *dir)
17 {
18     struct aos_statfs sfs;
19     unsigned long long total, used, free;
20     char abspath[256] = {0}, *dir1;
21 
22     dir1 = get_realpath(dir, abspath, sizeof(abspath));
23     if (!dir1) {
24         aos_cli_printf("Failed to get real path!\r\n");
25         return;
26     }
27 
28     if (access(dir1, F_OK) != 0) {
29         aos_cli_printf("Failed to access path:%s\r\n", dir1);
30         return;
31     }
32 
33     if (aos_statfs(dir1, &sfs) < 0) {
34         aos_cli_printf("statfs %s failed\n", dir);
35         return;
36     }
37 
38     total = ((unsigned long long)sfs.f_bsize * (unsigned long long)sfs.f_blocks) >> 10;
39     if (total == 0) {
40         aos_cli_printf("total size error!\r\n");
41         return;
42     }
43     free = ((unsigned long long)sfs.f_bsize * (unsigned long long)sfs.f_bavail) >> 10;
44     used = total - free;
45 
46     if (!strcmp(dir, "/")) {
47         aos_cli_printf("%10llu%10llu%10llu%6llu%%    %s\n", total, used, free,
48                 used * 100 / total, dir);
49     } else {
50         aos_cli_printf("%10llu%10llu%10llu     0%%    %s\n", total, used, free, dir);
51     }
52 }
53 
print_help()54 static void print_help()
55 {
56     aos_cli_printf(
57         "usage:\r\n"
58         "  df <dir>\r\n"
59         "eg:\r\n"
60         "  1.show /etc/config disk space info:\r\n"
61         "    df /etc/cofig\r\n");
62 }
63 
df_main(int argc,char ** argv)64 static int df_main(int argc, char **argv)
65 {
66     int i;
67     char node_names[8][64];
68     uint32_t count = 0;
69     uint32_t index;
70 
71 
72     if (argc >= 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
73         print_help();
74         return 0;
75     }
76 
77     aos_cli_printf("%10s%10s%10s%7s    %s\n", "Total", "Used", "Free", "Use%", "Mount");
78 
79     if (argc <= 1) {
80         vfs_get_node_name("/", node_names, &count);
81         for (index = 0; index < count; index++) {
82             df_do_dir(node_names[index]);
83         }
84 
85         return 0;
86     }
87 
88     for (i = 1; i < argc; i++)
89         df_do_dir(argv[i]);
90     return 0;
91 }
92 ALIOS_CLI_CMD_REGISTER(df_main, df, show fs usage info);
93