1 | /*
|
---|
2 | * mdel.c
|
---|
3 | * Delete an MSDOS file
|
---|
4 | *
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "sysincludes.h"
|
---|
8 | #include "msdos.h"
|
---|
9 | #include "mtools.h"
|
---|
10 | #include "stream.h"
|
---|
11 | #include "mainloop.h"
|
---|
12 | #include "fs.h"
|
---|
13 | #include "file.h"
|
---|
14 |
|
---|
15 | typedef struct Arg_t {
|
---|
16 | int deltype;
|
---|
17 | int verbose;
|
---|
18 | } Arg_t;
|
---|
19 |
|
---|
20 | static int del_entry(direntry_t *entry, MainParam_t *mp)
|
---|
21 | {
|
---|
22 | Arg_t *arg=(Arg_t *) mp->arg;
|
---|
23 | direntry_t longNameEntry;
|
---|
24 | int i;
|
---|
25 |
|
---|
26 | if(got_signal)
|
---|
27 | return ERROR_ONE;
|
---|
28 |
|
---|
29 | if(entry->entry == -3) {
|
---|
30 | fprintf(stderr, "Cannot remove root directory\n");
|
---|
31 | return ERROR_ONE;
|
---|
32 | }
|
---|
33 |
|
---|
34 | if (arg->verbose) {
|
---|
35 | fprintf(stderr,"Removing ");
|
---|
36 | fprintPwd(stdout, entry,0);
|
---|
37 | putchar('\n');
|
---|
38 | }
|
---|
39 |
|
---|
40 | if ((entry->dir.attr & (ATTR_READONLY | ATTR_SYSTEM)) &&
|
---|
41 | (ask_confirmation("%s: \"%s\" is read only, erase anyway (y/n) ? ",
|
---|
42 | progname, entry->name)))
|
---|
43 | return ERROR_ONE;
|
---|
44 | if (fatFreeWithDirentry(entry))
|
---|
45 | return ERROR_ONE;
|
---|
46 |
|
---|
47 | initializeDirentry(&longNameEntry, entry->Dir);
|
---|
48 | for(i=entry->beginSlot; i< entry->endSlot; i++) {
|
---|
49 | int error;
|
---|
50 | longNameEntry.entry=i;
|
---|
51 | dir_read(&longNameEntry, &error);
|
---|
52 | if(error)
|
---|
53 | break;
|
---|
54 | longNameEntry.dir.name[0] = (char) DELMARK;
|
---|
55 | dir_write(&longNameEntry);
|
---|
56 | }
|
---|
57 |
|
---|
58 | entry->dir.name[0] = (char) DELMARK;
|
---|
59 | dir_write(entry);
|
---|
60 | return GOT_ONE;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int del_file(direntry_t *entry, MainParam_t *mp)
|
---|
64 | {
|
---|
65 | char shortname[13];
|
---|
66 | direntry_t subEntry;
|
---|
67 | Stream_t *SubDir;
|
---|
68 | Arg_t *arg = (Arg_t *) mp->arg;
|
---|
69 | MainParam_t sonmp;
|
---|
70 | int ret;
|
---|
71 | int r;
|
---|
72 |
|
---|
73 | sonmp = *mp;
|
---|
74 | sonmp.arg = mp->arg;
|
---|
75 |
|
---|
76 | r = 0;
|
---|
77 | if (IS_DIR(entry)){
|
---|
78 | /* a directory */
|
---|
79 | SubDir = OpenFileByDirentry(entry);
|
---|
80 | initializeDirentry(&subEntry, SubDir);
|
---|
81 | ret = 0;
|
---|
82 | while((r=vfat_lookup(&subEntry, "*", 1,
|
---|
83 | ACCEPT_DIR | ACCEPT_PLAIN,
|
---|
84 | shortname, NULL)) == 0 ){
|
---|
85 | if(shortname[0] != DELMARK &&
|
---|
86 | shortname[0] &&
|
---|
87 | shortname[0] != '.' ){
|
---|
88 | if(arg->deltype != 2){
|
---|
89 | fprintf(stderr,
|
---|
90 | "Directory ");
|
---|
91 | fprintPwd(stderr, entry,0);
|
---|
92 | fprintf(stderr," non empty\n");
|
---|
93 | ret = ERROR_ONE;
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | if(got_signal) {
|
---|
97 | ret = ERROR_ONE;
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | ret = del_file(&subEntry, &sonmp);
|
---|
101 | if( ret & ERROR_ONE)
|
---|
102 | break;
|
---|
103 | ret = 0;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | FREE(&SubDir);
|
---|
107 | if (r == -2)
|
---|
108 | return ERROR_ONE;
|
---|
109 | if(ret)
|
---|
110 | return ret;
|
---|
111 | }
|
---|
112 | return del_entry(entry, mp);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | static void usage(void)
|
---|
117 | {
|
---|
118 | fprintf(stderr,
|
---|
119 | "Mtools version %s, dated %s\n", mversion, mdate);
|
---|
120 | fprintf(stderr,
|
---|
121 | "Usage: %s [-v] msdosfile [msdosfiles...]\n"
|
---|
122 | "\t-v Verbose\n",
|
---|
123 | progname);
|
---|
124 | exit(1);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void mdel(int argc, char **argv, int deltype)
|
---|
128 | {
|
---|
129 | Arg_t arg;
|
---|
130 | MainParam_t mp;
|
---|
131 | int c,i;
|
---|
132 |
|
---|
133 | arg.verbose = 0;
|
---|
134 | while ((c = getopt(argc, argv, "v")) != EOF) {
|
---|
135 | switch (c) {
|
---|
136 | case 'v':
|
---|
137 | arg.verbose = 1;
|
---|
138 | break;
|
---|
139 | default:
|
---|
140 | usage();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | if(argc == optind)
|
---|
145 | usage();
|
---|
146 |
|
---|
147 | init_mp(&mp);
|
---|
148 | mp.callback = del_file;
|
---|
149 | mp.arg = (void *) &arg;
|
---|
150 | mp.openflags = O_RDWR;
|
---|
151 | arg.deltype = deltype;
|
---|
152 | switch(deltype){
|
---|
153 | case 0:
|
---|
154 | mp.lookupflags = ACCEPT_PLAIN; /* mdel */
|
---|
155 | break;
|
---|
156 | case 1:
|
---|
157 | mp.lookupflags = ACCEPT_DIR; /* mrd */
|
---|
158 | break;
|
---|
159 | case 2:
|
---|
160 | mp.lookupflags = ACCEPT_DIR | ACCEPT_PLAIN; /* mdeltree */
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | mp.lookupflags |= NO_DOTS;
|
---|
164 | for(i=optind;i<argc;i++) {
|
---|
165 | int b,l;
|
---|
166 | b = skip_drive(argv[i]) - argv[i];
|
---|
167 | l = strlen(argv[i]+b);
|
---|
168 | if(l > 1 && argv[i][b+l-1] == '/')
|
---|
169 | argv[i][b+l-1] = '\0';
|
---|
170 | }
|
---|
171 |
|
---|
172 | exit(main_loop(&mp, argv + optind, argc - optind));
|
---|
173 | }
|
---|