1 /* mhstoresbr.c -- routines to save/store the contents of MIME messages
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.
9 #include "sbr/read_switch_multiword.h"
10 #include "sbr/concat.h"
11 #include "sbr/smatch.h"
12 #include "sbr/r1bindex.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/folder_addmsg.h"
19 #include "sbr/context_find.h"
21 #include "sbr/error.h"
25 #include "h/fmt_scan.h"
27 #include "h/mhparse.h"
30 #include "mhshowsbr.h"
31 #include "sbr/m_maildir.h"
32 #include "sbr/m_mktemp.h"
34 enum clobber_policy_t
{
35 NMH_CLOBBER_ALWAYS
= 0,
42 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
45 CT
*cts
; /* Top-level list of contents to store. */
46 char *cwd
; /* cached current directory */
47 int autosw
; /* -auto enabled */
48 int verbosw
; /* -verbose enabled */
49 int files_not_clobbered
; /* output flag indicating that store failed
50 in order to not clobber an existing file */
52 /* The following must never be touched by a caller: they are for
53 internal use by the mhstoresbr functions. */
54 char *dir
; /* directory in which to store contents */
55 enum clobber_policy_t clobber_policy
; /* -clobber selection */
58 static bool use_param_as_filename(const char *p
);
61 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
70 info
->files_not_clobbered
= 0;
72 info
->clobber_policy
= clobber_policy (csw
);
78 mhstoreinfo_free (mhstoreinfo_t info
)
86 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
88 return info
->files_not_clobbered
;
93 * Type for a compare function for qsort. This keeps
96 typedef int (*qsort_comp
) (const void *, const void *);
102 static void store_single_message (CT
, mhstoreinfo_t
);
103 static int store_switch (CT
, mhstoreinfo_t
);
104 static int store_generic (CT
, mhstoreinfo_t
);
105 static int store_application (CT
, mhstoreinfo_t
);
106 static int store_multi (CT
, mhstoreinfo_t
);
107 static int store_partial (CT
, mhstoreinfo_t
);
108 static int store_external (CT
, mhstoreinfo_t
);
109 static int ct_compar (CT
*, CT
*);
110 static int store_content (CT
, CT
, mhstoreinfo_t
);
111 static int output_content_file (CT
, int);
112 static int output_content_folder (char *, char *);
113 static int parse_format_string (CT
, char *, char *, int, char *);
114 static void get_storeproc (CT
);
115 static int copy_some_headers (FILE *, CT
);
116 static char *clobber_check (char *, mhstoreinfo_t
);
119 * Main entry point to store content
120 * from a collection of messages.
124 store_all_messages (mhstoreinfo_t info
)
130 * Check for the directory in which to
131 * store any contents.
133 if ((cp
= context_find (nmhstorage
)) && *cp
)
134 info
->dir
= mh_xstrdup(cp
);
136 info
->dir
= getcpy (info
->cwd
);
138 for (ctp
= info
->cts
; *ctp
; ctp
++) {
140 store_single_message (ct
, info
);
148 * Entry point to store the content
149 * in a (single) message
153 store_single_message (CT ct
, mhstoreinfo_t info
)
155 if (type_ok (ct
, 1)) {
157 store_switch (ct
, info
);
162 if (ct
->c_ceclosefnx
)
163 (*ct
->c_ceclosefnx
) (ct
);
169 * Switching routine to store different content types
173 store_switch (CT ct
, mhstoreinfo_t info
)
175 switch (ct
->c_type
) {
177 return store_multi (ct
, info
);
180 switch (ct
->c_subtype
) {
181 case MESSAGE_PARTIAL
:
182 return store_partial (ct
, info
);
184 case MESSAGE_EXTERNAL
:
185 return store_external (ct
, info
);
189 return store_generic (ct
, info
);
194 return store_application (ct
, info
);
200 return store_generic (ct
, info
);
203 return OK
; /* NOT REACHED */
208 * Generic routine to store a MIME content.
209 * (audio, video, image, text, message/rfc822)
213 store_generic (CT ct
, mhstoreinfo_t info
)
216 * Check if the content specifies a filename.
217 * Don't bother with this for type "message"
218 * (only "message/rfc822" will use store_generic).
220 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
223 return store_content (ct
, NULL
, info
);
228 * Store content of type "application"
232 store_application (CT ct
, mhstoreinfo_t info
)
234 CI ci
= &ct
->c_ctinfo
;
236 /* Check if the content specifies a filename */
241 * If storeproc is not defined, and the content is type
242 * "application/octet-stream", we also check for various
243 * attribute/value pairs which specify if this a tar file.
245 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
251 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
252 if (strcasecmp (cp
, "tar") == 0)
256 /* check for "conversions=compress" attribute */
257 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
258 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
259 if (strcasecmp (cp
, "compress") == 0 ||
260 strcasecmp (cp
, "x-compress") == 0) {
263 if (strcasecmp (cp
, "gzip") == 0 ||
264 strcasecmp (cp
, "x-gzip") == 0) {
270 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
271 : (gzP
? "%egzip -dc | tar tvf -"
272 : "%etar tvf -"), NULL
);
273 if (!ct
->c_storeproc
) {
275 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
276 : (gzP
? "| gzip -dc | tar xvpf -"
277 : "| tar xvpf -"), NULL
);
280 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
281 : (gzP
? "%m%P.tar.gz"
282 : "%m%P.tar"), NULL
);
288 return store_content (ct
, NULL
, info
);
293 * Store the content of a multipart message
297 store_multi (CT ct
, mhstoreinfo_t info
)
300 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
304 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
305 CT p
= part
->mp_part
;
307 if (part_ok (p
) && type_ok (p
, 1)) {
309 /* Support mhstore -outfile. The MIME parser doesn't
310 load c_storage, so we know that p->c_storage is
312 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
314 result
= store_switch (p
, info
);
316 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
326 * Reassemble and store the contents of a collection
327 * of messages of type "message/partial".
331 store_partial (CT ct
, mhstoreinfo_t info
)
336 struct partial
*pm
, *qm
;
338 qm
= (struct partial
*) ct
->c_ctparams
;
343 for (ctp
= info
->cts
; *ctp
; ctp
++) {
345 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
346 pm
= (struct partial
*) p
->c_ctparams
;
348 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
349 pm
->pm_marked
= pm
->pm_partno
;
361 inform("missing (at least) last part of multipart message");
365 base
= mh_xcalloc(i
+ 1, sizeof *base
);
367 for (ctp
= info
->cts
; *ctp
; ctp
++) {
369 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
370 pm
= (struct partial
*) p
->c_ctparams
;
378 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
381 for (ctq
= base
; *ctq
; ctq
++) {
383 pm
= (struct partial
*) p
->c_ctparams
;
384 if (pm
->pm_marked
== cur
) {
389 if (pm
->pm_marked
== cur
- 1) {
390 inform("duplicate part %d of %d part multipart message, continuing...",
396 inform("missing %spart %d of %d part multipart message",
397 cur
!= hi
? "(at least) " : "", cur
, hi
);
406 * Now cycle through the sorted list of messages of type
407 * "message/partial" and save/append them to a file.
412 if (store_content (ct
, NULL
, info
) == NOTOK
) {
418 for (; *ctq
; ctq
++) {
420 if (store_content (p
, ct
, info
) == NOTOK
)
430 * Store content from a message of type "message/external".
434 store_external (CT ct
, mhstoreinfo_t info
)
437 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
438 CT p
= e
->eb_content
;
444 * Check if the parameters for the external body
445 * specified a filename.
450 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
451 if (!ct
->c_storeproc
)
452 ct
->c_storeproc
= mh_xstrdup(cp
);
454 p
->c_storeproc
= mh_xstrdup(cp
);
459 * Since we will let the Content structure for the
460 * external body substitute for the current content,
461 * we temporarily change its partno (number inside
462 * multipart), so everything looks right.
464 p
->c_partno
= ct
->c_partno
;
466 /* we probably need to check if content is really there */
468 /* Support mhstore -outfile. The MIME parser doesn't load
469 c_storage, so we know that p->c_storage is NULL here. */
470 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
472 result
= store_switch (p
, info
);
480 * Compare the numbering from two different
481 * message/partials (needed for sorting).
485 ct_compar (CT
*a
, CT
*b
)
487 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
488 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
490 return am
->pm_marked
- bm
->pm_marked
;
495 * Store contents of a message or message part to
496 * a folder, a file, the standard output, or pass
497 * the contents to a command.
499 * If the current content to be saved is a followup part
500 * to a collection of messages of type "message/partial",
501 * then field "p" is a pointer to the Content structure
502 * to the first message/partial in the group.
506 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
508 bool appending
= false;
510 bool is_partial
= false;
511 bool first_partial
= false;
512 bool last_partial
= false;
513 char *cp
, buffer
[BUFSIZ
];
516 * Do special processing for messages of
517 * type "message/partial".
519 * We first check if this content is of type
520 * "message/partial". If it is, then we need to check
521 * whether it is the first and/or last in the group.
523 * Then if "p" is a valid pointer, it points to the Content
524 * structure of the first partial in the group. So we copy
525 * the file name and/or folder name from that message. In
526 * this case, we also note that we will be appending.
528 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
529 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
531 /* Yep, it's a message/partial */
534 /* But is it the first and/or last in the collection? */
535 if (pm
->pm_partno
== 1)
536 first_partial
= true;
537 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
541 * If "p" is a valid pointer, then it points to the
542 * Content structure for the first message in the group.
543 * So we just copy the filename or foldername information
544 * from the previous iteration of this function.
548 if (! ct
->c_storage
) {
549 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
551 /* record the folder name */
553 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
561 * Get storage formatting string.
563 * 1) If we have storeproc defined, then use that
564 * 2) Else check for a mhn-store-<type>/<subtype> entry
565 * 3) Else check for a mhn-store-<type> entry
566 * 4) Else if content is "message", use "+" (current folder)
567 * 5) Else use string "%m%P.%s".
569 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
570 CI ci
= &ct
->c_ctinfo
;
572 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
574 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
578 if (! ct
->c_storage
) {
580 * Check the beginning of storage formatting string
581 * to see if we are saving content to a folder.
583 if (*cp
== '+' || *cp
== '@') {
584 char *tmpfilenam
, *folder
;
586 /* Store content in temporary file for now */
587 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
588 die("unable to create temporary file in %s",
591 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
593 /* Get the folder name */
595 folder
= pluspath (cp
);
597 folder
= getfolder (1);
599 /* Check if folder exists */
600 create_folder(m_mailpath(folder
), 0, exit
);
602 /* Record the folder name */
603 ct
->c_folder
= mh_xstrdup(folder
);
612 * Parse and expand the storage formatting string
613 * in `cp' into `buffer'.
615 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
618 * If formatting begins with '|' or '!', then pass
619 * content to standard input of a command and return.
621 if (buffer
[0] == '|' || buffer
[0] == '!')
622 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
624 /* record the filename */
625 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
630 /* The output filename was explicitly specified, so use it. */
631 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
638 /* flush the output stream */
641 /* Now save or append the content to a file */
642 if (output_content_file (ct
, appending
) == NOTOK
)
646 * If necessary, link the file into a folder and remove
647 * the temporary file. If this message is a partial,
648 * then only do this if it is the last one in the group.
650 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
651 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
652 (void) m_unlink (ct
->c_storage
);
659 * Now print out the name/number of the message
660 * that we are storing.
664 fprintf (stderr
, "reassembling partials ");
666 fputs(ct
->c_file
, stderr
);
668 fprintf (stderr
, "%s,", ct
->c_file
);
670 fprintf (stderr
, "storing message %s", ct
->c_file
);
672 fprintf (stderr
, " part %s", ct
->c_partno
);
676 * Unless we are in the "middle" of group of message/partials,
677 * we now print the name of the file, folder, and/or message
678 * to which we are storing the content.
680 if (!is_partial
|| last_partial
) {
682 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
684 } else if (!strcmp(ct
->c_storage
, "-")) {
685 fprintf (stderr
, " to stdout\n");
687 int cwdlen
= strlen (info
->cwd
);
689 fprintf (stderr
, " as file %s\n",
690 !has_prefix(ct
->c_storage
, info
->cwd
)
691 || ct
->c_storage
[cwdlen
] != '/'
692 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
702 * Output content to a file
706 output_content_file (CT ct
, int appending
)
709 char *file
, buffer
[BUFSIZ
];
714 * If the pathname is absolute, make sure
715 * all the relevant directories exist.
717 if (strchr(ct
->c_storage
, '/')
718 && make_intermediates (ct
->c_storage
) == NOTOK
)
721 if (ct
->c_encoding
!= CE_7BIT
) {
724 if (!ct
->c_ceopenfnx
) {
725 inform("don't know how to decode part %s of message %s",
726 ct
->c_partno
, ct
->c_file
);
730 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
732 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
734 if (!strcmp (file
, ct
->c_storage
)) {
735 (*ct
->c_ceclosefnx
) (ct
);
740 * Send to standard output
742 if (!strcmp (ct
->c_storage
, "-")) {
745 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
746 advise ("stdout", "unable to dup");
748 (*ct
->c_ceclosefnx
) (ct
);
751 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
752 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
753 appending
? "a" : "w");
761 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
762 advise (ct
->c_storage
, "unable to fopen for %s",
763 appending
? "appending" : "writing");
769 * Filter the header fields of the initial enclosing
770 * message/partial into the file.
772 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
773 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
775 if (pm
->pm_partno
== 1)
776 copy_some_headers (fp
, ct
);
780 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
782 advise (file
, "error reading content from");
789 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
790 advise ("output_content_file", "fwrite");
797 (*ct
->c_ceclosefnx
) (ct
);
799 if (cc
!= NOTOK
&& fflush (fp
))
800 advise (ct
->c_storage
, "error writing to");
804 return cc
== NOTOK
? NOTOK
: OK
;
807 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
808 advise (ct
->c_file
, "unable to open for reading");
814 fseek (ct
->c_fp
, pos
, SEEK_SET
);
816 if (!strcmp (ct
->c_storage
, "-")) {
819 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
820 advise ("stdout", "unable to dup");
823 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
824 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
825 appending
? "a" : "w");
830 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
831 advise (ct
->c_storage
, "unable to fopen for %s",
832 appending
? "appending" : "writing");
838 * Copy a few of the header fields of the initial
839 * enclosing message/partial into the file.
842 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
843 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
845 if (pm
->pm_partno
== 1) {
846 copy_some_headers (fp
, ct
);
851 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
852 if ((pos
+= strlen (buffer
)) > last
) {
855 diff
= strlen (buffer
) - (pos
- last
);
860 * If this is the first content of a group of
861 * message/partial contents, then we only copy a few
862 * of the header fields of the enclosed message.
877 if (!uprf (buffer
, XXX_FIELD_PRF
)
878 && !uprf (buffer
, VRSN_FIELD
)
879 && !uprf (buffer
, "Subject:")
880 && !uprf (buffer
, "Encrypted:")
881 && !uprf (buffer
, "Message-ID:")) {
896 advise (ct
->c_storage
, "error writing to");
906 * Add a file to a folder.
908 * Return the new message number of the file
909 * when added to the folder. Return -1, if
914 output_content_folder (char *folder
, char *filename
)
919 /* Read the folder. */
920 if (!(mp
= folder_read(folder
, 0))) {
921 inform("unable to read folder %s", folder
);
924 /* Link file into folder */
925 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
927 /* free folder structure */
931 * Return msgnum. We are relying on the fact that
932 * msgnum will be -1, if folder_addmsg() had an error.
939 * Parse and expand the storage formatting string
940 * pointed to by "cp" into "buffer".
944 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
948 CI ci
= &ct
->c_ctinfo
;
951 * If storage string is "-", just copy it, and
952 * return (send content to standard output).
954 if (cp
[0] == '-' && cp
[1] == '\0') {
955 strncpy (buffer
, cp
, buflen
);
963 * If formatting string is a pathname that doesn't
964 * begin with '/', then preface the path with the
965 * appropriate directory.
967 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
968 if (!strcmp(dir
, "/"))
969 dir
= ""; /* Don't start with "//". */
970 snprintf (bp
, buflen
, "%s/", dir
);
978 /* We are processing a storage escape */
983 * Insert parameters from Content-Type.
984 * This is only valid for '|' commands.
986 if (buffer
[0] != '|' && buffer
[0] != '!') {
996 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
997 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
998 pm
->pm_name
, get_param_value(pm
, '?'));
1008 /* insert message number */
1009 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1013 /* insert part number with leading dot */
1015 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1019 /* insert part number without leading dot */
1021 strncpy (bp
, ct
->c_partno
, buflen
);
1025 /* insert content type */
1026 strncpy (bp
, ci
->ci_type
, buflen
);
1030 /* insert content subtype */
1031 strncpy (bp
, ci
->ci_subtype
, buflen
);
1035 /* insert the character % */
1045 /* Advance bp and decrement buflen */
1063 * Check if the content specifies a filename
1064 * in its MIME parameters.
1068 get_storeproc (CT ct
)
1074 * If the storeproc has already been defined,
1075 * we just return (for instance, if this content
1076 * is part of a "message/external".
1078 if (ct
->c_storeproc
)
1082 * If there's a Content-Disposition header and it has a filename,
1083 * use that (RFC-2183).
1086 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1087 use_param_as_filename(cp
)) {
1088 ct
->c_storeproc
= mh_xstrdup(cp
);
1096 * Check the attribute/value pairs, for the attribute "name".
1097 * If found, do a few sanity checks and copy the value into
1101 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1102 use_param_as_filename(cp
)) {
1103 ct
->c_storeproc
= mh_xstrdup(cp
);
1111 * Copy some of the header fields of the initial message/partial
1112 * message into the header of the reassembled message.
1116 copy_some_headers (FILE *out
, CT ct
)
1120 hp
= ct
->c_first_hf
; /* start at first header field */
1124 * A few of the header fields of the enclosing
1125 * messages are not copied.
1127 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1128 && strcasecmp (hp
->name
, VRSN_FIELD
)
1129 && strcasecmp (hp
->name
, "Subject")
1130 && strcasecmp (hp
->name
, "Encrypted")
1131 && strcasecmp (hp
->name
, "Message-ID"))
1132 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1133 hp
= hp
->next
; /* next header field */
1139 /******************************************************************************/
1140 /* -clobber support */
1142 static enum clobber_policy_t
1143 clobber_policy (const char *value
)
1145 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1146 return NMH_CLOBBER_ALWAYS
;
1148 if (! strcasecmp (value
, "auto")) {
1149 return NMH_CLOBBER_AUTO
;
1151 if (! strcasecmp (value
, "suffix")) {
1152 return NMH_CLOBBER_SUFFIX
;
1154 if (! strcasecmp (value
, "ask")) {
1155 return NMH_CLOBBER_ASK
;
1157 if (! strcasecmp (value
, "never")) {
1158 return NMH_CLOBBER_NEVER
;
1161 die("invalid argument, %s, to clobber", value
);
1166 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1168 const size_t max_versions
= 1000000;
1169 /* 8 = log max_versions + one for - or . + one for null terminator */
1170 const size_t buflen
= strlen (file
) + 8;
1171 char *buffer
= mh_xmalloc (buflen
);
1174 char *extension
= NULL
;
1175 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1176 ((extension
= strrchr (file
, '.')) != NULL
)) {
1177 *extension
++ = '\0';
1180 for (version
= 1; version
< max_versions
; ++version
) {
1183 switch (clobber_policy
) {
1184 case NMH_CLOBBER_AUTO
: {
1185 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1186 extension
== NULL
? "" : ".",
1187 extension
== NULL
? "" : extension
);
1191 case NMH_CLOBBER_SUFFIX
:
1192 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1196 /* Should never get here. */
1197 inform("will not overwrite %s, invalid clobber policy", buffer
);
1202 /* Actually (try to) create the file here to avoid a race
1203 condition on file naming + creation. This won't solve the
1204 problem with old NFS that doesn't support O_EXCL, though.
1205 Let the umask strip off permissions from 0666 as desired.
1206 That's what fopen () would do if it was creating the file. */
1207 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1208 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1209 S_IROTH
| S_IWOTH
)) >= 0) {
1217 if (version
>= max_versions
) {
1218 inform("will not overwrite %s, too many versions", buffer
);
1228 clobber_check (char *original_file
, mhstoreinfo_t info
)
1230 /* clobber policy return value
1231 * -------------- ------------
1232 * -always original_file
1233 * -auto original_file-<digits>.extension
1234 * -suffix original_file.<digits>
1235 * -ask original_file, 0, or another filename/path
1243 if (! strcmp (original_file
, "-")) {
1244 return original_file
;
1247 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1248 /* Save cwd for possible use in loop below. */
1251 cwd
= mh_xstrdup(original_file
);
1252 slash
= strrchr (cwd
, '/');
1257 /* original_file isn't a full path, which should only happen if
1267 file
= original_file
;
1268 check_again
= false;
1270 switch (info
->clobber_policy
) {
1271 case NMH_CLOBBER_ALWAYS
:
1274 case NMH_CLOBBER_SUFFIX
:
1275 case NMH_CLOBBER_AUTO
:
1276 if (stat (file
, &st
) == OK
) {
1277 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1279 ++info
->files_not_clobbered
;
1284 case NMH_CLOBBER_ASK
:
1285 if (stat (file
, &st
) == OK
) {
1286 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1287 static struct swit answer
[4] = {
1288 { "yes", 0, NMH_YES
},
1289 { "no", 0, NMH_NO
},
1290 { "rename", 0, NMH_RENAME
},
1294 if (isatty (fileno (stdin
))) {
1296 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1297 ans
= read_switch_multiword (prompt
, answer
);
1300 /* Overwrite, that's what nmh used to do. And warn. */
1301 inform("-clobber ask but no tty, so overwrite %s", file
);
1305 switch ((enum answers
) smatch (*ans
, answer
)) {
1311 ++info
->files_not_clobbered
;
1315 fputs("Enter filename or full path of the new file: ", stdout
);
1316 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1319 ++info
->files_not_clobbered
;
1321 trim_suffix_c(buf
, '\n');
1326 if (buf
[0] == '/') {
1327 /* Full path, use it. */
1328 file
= mh_xstrdup(buf
);
1330 /* Relative path. */
1331 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1341 case NMH_CLOBBER_NEVER
:
1342 if (stat (file
, &st
) == OK
) {
1343 /* Keep count of files that would have been clobbered,
1344 and return that as process exit status. */
1345 inform("will not overwrite %s with -clobber never", file
);
1348 ++info
->files_not_clobbered
;
1353 original_file
= file
;
1354 } while (check_again
);
1362 use_param_as_filename(const char *p
)
1364 /* Preserve result of original test that considered an empty string
1366 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1369 /* -clobber support */
1370 /******************************************************************************/