]> diplodocus.org Git - nmh/blob - uip/rmf.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / rmf.c
1 /* rmf.c -- remove a folder
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10 #include "sbr/m_maildir.h"
11 #include "sbr/m_mktemp.h"
12
13 #define RMF_SWITCHES \
14 X("interactive", 0, INTRSW) \
15 X("nointeractive", 0, NINTRSW) \
16 X("version", 0, VERSIONSW) \
17 X("help", 0, HELPSW) \
18
19 #define X(sw, minchars, id) id,
20 DEFINE_SWITCH_ENUM(RMF);
21 #undef X
22
23 #define X(sw, minchars, id) { sw, minchars, id },
24 DEFINE_SWITCH_ARRAY(RMF, switches);
25 #undef X
26
27 /*
28 * static prototypes
29 */
30 static int rmf(char *);
31 static void rma (char *);
32
33
34 int
35 main (int argc, char **argv)
36 {
37 int defolder = 0, interactive = -1;
38 char *cp, *folder = NULL, newfolder[BUFSIZ];
39 char buf[BUFSIZ], **argp, **arguments;
40 char *fp;
41
42 if (nmh_init(argv[0], 1)) { return 1; }
43
44 arguments = getarguments (invo_name, argc, argv, 1);
45 argp = arguments;
46
47 while ((cp = *argp++)) {
48 if (*cp == '-') {
49 switch (smatch (++cp, switches)) {
50 case AMBIGSW:
51 ambigsw (cp, switches);
52 done (1);
53 case UNKWNSW:
54 adios (NULL, "-%s unknown", cp);
55
56 case HELPSW:
57 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
58 invo_name);
59 print_help (buf, switches, 1);
60 done (0);
61 case VERSIONSW:
62 print_version(invo_name);
63 done (0);
64
65 case INTRSW:
66 interactive = 1;
67 continue;
68 case NINTRSW:
69 interactive = 0;
70 continue;
71 }
72 }
73 if (*cp == '+' || *cp == '@') {
74 if (folder)
75 adios (NULL, "only one folder at a time!");
76 folder = pluspath (cp);
77 } else {
78 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
79 }
80 }
81
82 if (!context_find ("path"))
83 free (path ("./", TFOLDER));
84 if (!folder) {
85 folder = getfolder (1);
86 defolder++;
87 }
88 fp = m_mailpath(folder);
89 if (!strcmp(fp, pwd()))
90 adios (NULL, "sorry, you can't remove the current working directory");
91 free(fp);
92
93 if (interactive == -1)
94 interactive = defolder;
95
96 if (strchr (folder, '/') && (*folder != '/') && (*folder != '.')) {
97 for (cp = stpcpy(newfolder, folder); cp > newfolder && *cp != '/'; cp--)
98 continue;
99 if (cp > newfolder)
100 *cp = '\0';
101 else
102 strncpy (newfolder, getfolder(0), sizeof(newfolder));
103 } else {
104 strncpy (newfolder, getfolder(0), sizeof(newfolder));
105 }
106
107 if (interactive) {
108 cp = concat ("Remove folder \"", folder, "\"? ", NULL);
109 if (!read_yes_or_no_if_tty (cp))
110 done (0);
111 free (cp);
112 }
113
114 if (rmf (folder) == OK) {
115 char *cfolder = context_find(pfolder);
116 if (cfolder && strcmp (cfolder, newfolder)) {
117 printf ("[+%s now current]\n", newfolder);
118 context_replace (pfolder, newfolder); /* update current folder */
119 }
120 }
121 context_save (); /* save the context file */
122 done (0);
123 return 1;
124 }
125
126 static int
127 rmf (char *folder)
128 {
129 int i, others;
130 char *fp;
131 char *maildir;
132 char cur[BUFSIZ];
133 struct dirent *dp;
134 DIR *dd;
135
136 switch (i = chdir (maildir = m_maildir (folder))) {
137 case OK:
138 if (access (".", W_OK) != NOTOK && access ("..", W_OK) != NOTOK)
139 break;
140 /* FALLTHRU */
141
142 case NOTOK:
143 fp = m_mailpath(folder);
144 snprintf (cur, sizeof(cur), "atr-%s-%s", current, fp);
145 free(fp);
146 if (!context_del (cur)) {
147 printf ("[+%s de-referenced]\n", folder);
148 return OK;
149 }
150 inform("you have no profile entry for the %s folder +%s",
151 i == NOTOK ? "unreadable" : "read-only", folder);
152 return NOTOK;
153 }
154
155 if ((dd = opendir (".")) == NULL)
156 adios (NULL, "unable to read folder +%s", folder);
157 others = 0;
158
159 /*
160 * Run the external delete hook program.
161 */
162
163 (void)ext_hook("del-hook", maildir, NULL);
164
165 while ((dp = readdir (dd))) {
166 switch (dp->d_name[0]) {
167 case '.':
168 if (strcmp (dp->d_name, ".") == 0
169 || strcmp (dp->d_name, "..") == 0)
170 continue;
171 break;
172
173 case ',':
174 break;
175
176 default:
177 if (m_atoi (dp->d_name))
178 break;
179 if (strcmp (dp->d_name, LINK) == 0
180 || has_prefix(dp->d_name, BACKUP_PREFIX))
181 break;
182
183 inform("file \"%s/%s\" not deleted, continuing...",
184 folder, dp->d_name);
185 others++;
186 continue;
187 }
188 if (m_unlink (dp->d_name) == NOTOK) {
189 admonish (dp->d_name, "unable to unlink %s:", folder);
190 others++;
191 }
192 }
193
194 closedir (dd);
195
196 /*
197 * Remove any relevant private sequences
198 * or attributes from context file.
199 */
200 rma (folder);
201
202 if (chdir ("..") < 0) {
203 advise ("..", "chdir");
204 }
205 if (others == 0 && remdir (maildir))
206 return OK;
207
208 inform("folder +%s not removed", folder);
209 return NOTOK;
210 }
211
212
213 /*
214 * Remove all the (private) sequence information for
215 * this folder from the profile/context list.
216 */
217
218 static void
219 rma (char *folder)
220 {
221 int alen, j, plen;
222 char *cp;
223 struct node *np, *pp;
224
225 alen = LEN("atr-");
226 plen = strlen (cp = m_mailpath (folder)) + 1;
227
228 /*
229 * Search context list for keys that look like
230 * "atr-something-folderpath", and remove them.
231 */
232 for (np = m_defs, pp = NULL; np; np = np->n_next) {
233 if (ssequal ("atr-", np->n_name)
234 && (j = strlen (np->n_name) - plen) > alen
235 && *(np->n_name + j) == '-'
236 && strcmp (cp, np->n_name + j + 1) == 0) {
237 if (!np->n_context)
238 inform("bug: context_del(key=\"%s\"), continuing...",
239 np->n_name);
240 if (pp) {
241 pp->n_next = np->n_next;
242 np = pp;
243 } else {
244 m_defs = np->n_next;
245 }
246 ctxflags |= CTXMOD;
247 } else {
248 pp = np;
249 }
250 }
251
252 free(cp);
253 }