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