comparison rvinterf/etmsync/cleandir.c @ 594:2c75cf810146

fc-fsio: cleandir command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 04 Feb 2020 05:09:13 +0000
parents
children e6fe9d25377a
comparison
equal deleted inserted replaced
593:f315cdb1555f 594:2c75cf810146
1 /*
2 * The implementation of cleandir and rm-subtree commands lives here.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include "etm.h"
11 #include "ffs.h"
12 #include "tmffs2.h"
13 #include "limits.h"
14 #include "ffslimits.h"
15 #include "localtypes.h"
16 #include "localstruct.h"
17 #include "exitcodes.h"
18
19 extern char *pathname_for_ffs_child();
20
21 cleandir_level(ffspath_dir)
22 char *ffspath_dir;
23 {
24 u_char rdstate[4];
25 char rdbuf[MAX_FN_COMPONENT+1], ffspath_child[MAX_FULL_PATHNAME+1];
26 char *childp;
27 int nument, i, rc;
28 struct stat_info stat;
29
30 rc = do_opendir(ffspath_dir, rdstate, &nument);
31 if (rc)
32 return(rc);
33 if (!nument)
34 return(0);
35 childp = pathname_for_ffs_child(ffspath_dir, ffspath_child);
36 if (!childp) {
37 printf("error: non-empty dir at the limit of pathname depth\n");
38 return(ERROR_TARGET);
39 }
40 for (i = 0; i < nument; i++) {
41 rc = do_readdir(rdstate, rdbuf, MAX_FN_COMPONENT+1);
42 if (rc)
43 return(rc);
44 if (index(rdbuf, '/')) {
45 printf("error: readdir result contains a slash\n");
46 return(ERROR_TARGET);
47 }
48 strcpy(childp, rdbuf);
49 rc = do_xlstat(ffspath_child, &stat);
50 if (rc) {
51 printf("xlstat failed on %s\n", ffspath_child);
52 return(rc);
53 }
54 if (stat.type == OT_DIR) {
55 rc = cleandir_level(ffspath_child);
56 if (rc)
57 return(rc);
58 }
59 rc = do_ffs_remove(ffspath_child, 0);
60 if (rc)
61 return(rc);
62 }
63 return(0);
64 }
65
66 cmd_cleandir(argc, argv)
67 char **argv;
68 {
69 int rc;
70
71 rc = validate_ffs_pathname(argv[1]);
72 if (rc < 0)
73 return(ERROR_USAGE); /* err msg already printed */
74 if (rc == 0) {
75 fprintf(stderr, "error: cleandir / is not allowed\n");
76 return(ERROR_USAGE);
77 }
78 return cleandir_level(argv[1]);
79 }