]> diplodocus.org Git - nmh/blob - uip/annosbr.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / annosbr.c
1 /* annosbr.c -- prepend annotation to messages
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/tws.h>
10 #include <h/utils.h>
11 #include "sbr/lock_file.h"
12 #include "sbr/m_mktemp.h"
13 #include <fcntl.h>
14 #include <utime.h>
15
16
17 /*
18 * static prototypes
19 */
20 static int annosbr (int, char *, char *, char *, bool, bool, int, bool);
21
22 /*
23 * This "local" global and the annopreserve() function are a hack that allows additional
24 * functionality to be added to anno without piling on yet another annotate() argument.
25 */
26
27 static int preserve_actime_and_modtime = 0; /* set to preserve access and modification times on annotated message */
28
29 int
30 annotate (char *file, char *comp, char *text, bool inplace, bool datesw, int delete, bool append)
31 {
32 int i, fd;
33 struct utimbuf b;
34 struct stat s;
35 int failed_to_lock = 0;
36
37 /* open and lock the file to be annotated */
38 if ((fd = lkopendata (file, O_RDWR, 0, &failed_to_lock)) == NOTOK) {
39 switch (errno) {
40 case ENOENT:
41 break;
42
43 default:
44 if (failed_to_lock) {
45 admonish (file, "unable to lock");
46 } else {
47 admonish (file, "unable to open");
48 }
49 break;
50 }
51 return 1;
52 }
53
54 if (stat(file, &s) == -1) {
55 inform("can't get access and modification times for %s", file);
56 preserve_actime_and_modtime = 0;
57 }
58
59 b.actime = s.st_atime;
60 b.modtime = s.st_mtime;
61
62 i = annosbr (fd, file, comp, text, inplace, datesw, delete, append);
63
64 if (preserve_actime_and_modtime && utime(file, &b) == -1)
65 inform("can't set access and modification times for %s", file);
66
67 lkclosedata (fd, file);
68 return i;
69 }
70
71 /*
72 * Produce a listing of all header fields (annotations) whose field name matches
73 * comp. Number the listing if number is set. Treat the field bodies as path
74 * names and just output the last component unless text is non-NULL. We don't
75 * care what text is set to.
76 */
77
78 void
79 annolist(char *file, char *comp, char *text, int number)
80 {
81 int c; /* current character */
82 int count; /* header field (annotation) counter */
83 char *cp; /* miscellaneous character pointer */
84 char *field; /* buffer for header field */
85 int field_size; /* size of field buffer */
86 FILE *fp; /* file pointer made from locked file descriptor */
87 int length; /* length of field name */
88 int n; /* number of bytes written */
89 char *sp; /* another miscellaneous character pointer */
90
91 if ((fp = fopen(file, "r")) == NULL)
92 adios(file, "unable to open");
93
94 /*
95 * Allocate a buffer to hold the header components as they're read in.
96 * This buffer might need to be quite large, so we grow it as needed.
97 */
98
99 field = (char *)mh_xmalloc(field_size = 256);
100
101 /*
102 * Get the length of the field name since we use it often.
103 */
104
105 length = strlen(comp);
106
107 count = 0;
108
109 do {
110 /*
111 * Get a line from the input file, growing the field buffer as needed. We do this
112 * so that we can fit an entire line in the buffer making it easy to do a string
113 * comparison on both the field name and the field body which might be a long path
114 * name.
115 */
116
117 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
118 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
119 (void)ungetc(c, fp);
120 break;
121 }
122
123 if (++n >= field_size - 1) {
124 field = (char *) mh_xrealloc((void *)field, field_size += 256);
125
126 cp = field + n - 1;
127 }
128 }
129
130 /*
131 * NUL-terminate the field..
132 */
133
134 *cp = '\0';
135
136 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
137 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
138 ;
139
140 if (number)
141 (void)printf("%d\t", ++count);
142
143 if (text == NULL && (sp = strrchr(cp, '/')))
144 cp = sp + 1;
145
146 puts(cp);
147 }
148
149 } while (*field != '\0' && *field != '-');
150
151 /*
152 * Clean up.
153 */
154
155 free(field);
156
157 (void)fclose(fp);
158 }
159
160 /*
161 * Set the preserve-times flag. This hack eliminates the need for an additional argument to annotate().
162 */
163
164 void
165 annopreserve(int preserve)
166 {
167 preserve_actime_and_modtime = preserve;
168 }
169
170 static int
171 annosbr (int fd, char *file, char *comp, char *text, bool inplace, bool datesw, int delete, bool append)
172 {
173 int mode, tmpfd;
174 char *cp, *sp;
175 char buffer[BUFSIZ], tmpfil[BUFSIZ];
176 struct stat st;
177 FILE *tmp;
178 int c; /* current character */
179 int count; /* header field (annotation) counter */
180 char *field = NULL; /* buffer for header field */
181 int field_size = 0; /* size of field buffer */
182 FILE *fp = NULL; /* file pointer made from locked file descriptor */
183 int length; /* length of field name */
184 int n; /* number of bytes written */
185
186 mode = fstat (fd, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
187
188 if ((cp = m_mktemp2(file, "annotate", NULL, &tmp)) == NULL) {
189 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
190 }
191 strncpy (tmpfil, cp, sizeof(tmpfil));
192 chmod (tmpfil, mode);
193
194 /*
195 * We're going to need to copy some of the message file to the temporary
196 * file while examining the contents. Convert the message file descriptor
197 * to a file pointer since it's a lot easier and more efficient to use
198 * stdio for this. Also allocate a buffer to hold the header components
199 * as they're read in. This buffer is grown as needed later.
200 */
201
202 if (delete >= -1 || append) {
203 if ((fp = fdopen(fd, "r")) == NULL)
204 adios(NULL, "unable to fdopen file.");
205
206 field = (char *)mh_xmalloc(field_size = 256);
207 }
208
209 /*
210 * We're trying to delete a header field (annotation )if the delete flag is
211 * not -2 or less. A value greater than zero means that we're deleting the
212 * nth header field that matches the field (component) name. A value of
213 * zero means that we're deleting the first field in which both the field
214 * name matches the component name and the field body matches the text.
215 * The text is matched in its entirety if it begins with a slash; otherwise
216 * the text is matched against whatever portion of the field body follows
217 * the last slash. This allows matching of both absolute and relative path
218 * names. This is because this functionality was added to support attachments.
219 * It might be worth having a separate flag to indicate path name matching to
220 * make it more general. A value of -1 means to delete all matching fields.
221 */
222
223 if (delete >= -1) {
224 /*
225 * Get the length of the field name since we use it often.
226 */
227
228 length = strlen(comp);
229
230 /*
231 * Initialize the field counter. This is only used if we're deleting by
232 * number.
233 */
234
235 count = 0;
236
237 /*
238 * Copy lines from the input file to the temporary file until we either find the one
239 * that we're looking for (which we don't copy) or we reach the end of the headers.
240 * Both a blank line and a line beginning with a - terminate the headers so that we
241 * can handle both drafts and RFC-2822 format messages.
242 */
243
244 do {
245 /*
246 * Get a line from the input file, growing the field buffer as needed. We do this
247 * so that we can fit an entire line in the buffer making it easy to do a string
248 * comparison on both the field name and the field body which might be a long path
249 * name.
250 */
251
252 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
253 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
254 (void)ungetc(c, fp);
255 c = '\n';
256 break;
257 }
258
259 if (++n >= field_size - 1) {
260 field = (char *) mh_xrealloc((void *)field, field_size *= 2);
261
262 cp = field + n - 1;
263 }
264 }
265
266 /*
267 * NUL-terminate the field..
268 */
269
270 *cp = '\0';
271
272 /*
273 * Check for a match on the field name. We delete the line by not copying it to the
274 * temporary file if
275 *
276 * o The delete flag is 0, meaning that we're going to delete the first matching
277 * field, and the text is NULL meaning that we don't care about the field body.
278 *
279 * o The delete flag is 0, meaning that we're going to delete the first matching
280 * field, and the text begins with a / meaning that we're looking for a full
281 * path name, and the text matches the field body.
282 *
283 * o The delete flag is 0, meaning that we're going to delete the first matching
284 * field, the text does not begin with a / meaning that we're looking for the
285 * last path name component, and the last path name component matches the text.
286 *
287 * o The delete flag is positive meaning that we're going to delete the nth field
288 * with a matching field name, and this is the nth matching field name.
289 *
290 * o The delete flag is -1 meaning that we're going to delete all fields with a
291 * matching field name.
292 */
293
294 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
295 if (delete == 0) {
296 if (text == NULL)
297 break;
298
299 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
300 ;
301
302 if (*text == '/') {
303 if (strcmp(cp, text) == 0)
304 break;
305 }
306 else {
307 if ((sp = strrchr(cp, '/')) != NULL)
308 cp = sp + 1;
309
310 if (strcmp(cp, text) == 0)
311 break;
312 }
313 }
314
315 else if (delete == -1)
316 continue;
317
318 else if (++count == delete)
319 break;
320 }
321
322 /*
323 * This line wasn't a match so copy it to the temporary file.
324 */
325
326 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
327 adios(NULL, "unable to write temporary file.");
328
329 } while (*field != '\0' && *field != '-');
330
331 /*
332 * Get rid of the field buffer because we're done with it.
333 */
334
335 free((void *)field);
336 }
337
338 else {
339 /*
340 * Find the end of the headers before adding the annotations if we're
341 * appending instead of the default prepending. A special check for
342 * no headers is needed if appending.
343 */
344
345 if (append) {
346 /*
347 * Copy lines from the input file to the temporary file until we
348 * reach the end of the headers.
349 */
350
351 if ((c = getc(fp)) == '\n')
352 rewind(fp);
353
354 else {
355 (void)putc(c, tmp);
356
357 while ((c = getc(fp)) != EOF) {
358 (void)putc(c, tmp);
359
360 if (c == '\n') {
361 (void)ungetc(c = getc(fp), fp);
362
363 if (c == '\n' || c == '-')
364 break;
365 }
366 }
367 }
368 }
369
370 if (datesw)
371 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
372 if ((cp = text)) {
373 do {
374 while (*cp == ' ' || *cp == '\t')
375 cp++;
376 sp = cp;
377 while (*cp && *cp++ != '\n')
378 continue;
379 if (cp - sp)
380 fprintf (tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
381 } while (*cp);
382 if (cp[-1] != '\n' && cp != text)
383 putc ('\n', tmp);
384 }
385 }
386
387 fflush (tmp);
388
389 /*
390 * We've been messing with the input file position. Move the input file
391 * descriptor to the current place in the file because the stock data
392 * copying routine uses the descriptor, not the pointer.
393 */
394
395 if (fp && lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
396 adios(NULL, "can't seek.");
397
398 cpydata (fd, fileno (tmp), file, tmpfil);
399 fclose (tmp);
400
401 if (inplace) {
402 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
403 adios (tmpfil, "unable to open for re-reading");
404
405 lseek(fd, 0, SEEK_SET);
406
407 /*
408 * We're making the file shorter if we're deleting a header field
409 * so the file has to be truncated or it will contain garbage.
410 */
411
412 if (delete >= -1 && ftruncate(fd, 0) == -1)
413 adios(tmpfil, "unable to truncate.");
414
415 cpydata (tmpfd, fd, tmpfil, file);
416 close (tmpfd);
417 (void) m_unlink (tmpfil);
418 } else {
419 strncpy (buffer, m_backup (file), sizeof(buffer));
420 if (rename (file, buffer) == NOTOK) {
421 switch (errno) {
422 case ENOENT: /* unlinked early - no annotations */
423 (void) m_unlink (tmpfil);
424 break;
425
426 default:
427 admonish (buffer, "unable to rename %s to", file);
428 break;
429 }
430 return 1;
431 }
432 if (rename (tmpfil, file) == NOTOK) {
433 admonish (file, "unable to rename %s to", tmpfil);
434 return 1;
435 }
436 }
437
438 /*
439 * Close the delete file so that we don't run out of file pointers if
440 * we're doing piles of files. Note that this will make the close() in
441 * lkclose() fail, but that failure is ignored so it's not a problem.
442 */
443
444 if (fp)
445 (void)fclose(fp);
446
447 return 0;
448 }