2 * mhstoresbr.c -- routines to save/store the contents of MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
15 #include <h/mhparse.h>
18 enum clobber_policy_t
{
19 NMH_CLOBBER_ALWAYS
= 0,
26 static enum clobber_policy_t
clobber_policy (const char *);
29 CT
*cts
; /* Top-level list of contents to store. */
30 char *cwd
; /* cached current directory */
31 int autosw
; /* -auto enabled */
32 int verbosw
; /* -verbose enabled */
33 int files_not_clobbered
; /* output flag indicating that store failed
34 in order to not clobber an existing file */
36 /* The following must never be touched by a caller: they are for
37 internal use by the mhstoresbr functions. */
38 char *dir
; /* directory in which to store contents */
39 enum clobber_policy_t clobber_policy
; /* -clobber selection */
42 static bool use_param_as_filename(const char *p
);
45 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
) {
53 info
->files_not_clobbered
= 0;
55 info
->clobber_policy
= clobber_policy (csw
);
61 mhstoreinfo_free (mhstoreinfo_t info
) {
68 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
) {
69 return info
->files_not_clobbered
;
74 * Type for a compare function for qsort. This keeps
77 typedef int (*qsort_comp
) (const void *, const void *);
82 int type_ok (CT
, int);
83 void flush_errors (void);
88 static void store_single_message (CT
, mhstoreinfo_t
);
89 static int store_switch (CT
, mhstoreinfo_t
);
90 static int store_generic (CT
, mhstoreinfo_t
);
91 static int store_application (CT
, mhstoreinfo_t
);
92 static int store_multi (CT
, mhstoreinfo_t
);
93 static int store_partial (CT
, mhstoreinfo_t
);
94 static int store_external (CT
, mhstoreinfo_t
);
95 static int ct_compar (CT
*, CT
*);
96 static int store_content (CT
, CT
, mhstoreinfo_t
);
97 static int output_content_file (CT
, int);
98 static int output_content_folder (char *, char *);
99 static int parse_format_string (CT
, char *, char *, int, char *);
100 static void get_storeproc (CT
);
101 static int copy_some_headers (FILE *, CT
);
102 static char *clobber_check (char *, mhstoreinfo_t
);
105 * Main entry point to store content
106 * from a collection of messages.
110 store_all_messages (mhstoreinfo_t info
)
116 * Check for the directory in which to
117 * store any contents.
119 if ((cp
= context_find (nmhstorage
)) && *cp
)
120 info
->dir
= mh_xstrdup(cp
);
122 info
->dir
= getcpy (info
->cwd
);
124 for (ctp
= info
->cts
; *ctp
; ctp
++) {
126 store_single_message (ct
, info
);
134 * Entry point to store the content
135 * in a (single) message
139 store_single_message (CT ct
, mhstoreinfo_t info
)
141 if (type_ok (ct
, 1)) {
143 store_switch (ct
, info
);
148 if (ct
->c_ceclosefnx
)
149 (*ct
->c_ceclosefnx
) (ct
);
155 * Switching routine to store different content types
159 store_switch (CT ct
, mhstoreinfo_t info
)
161 switch (ct
->c_type
) {
163 return store_multi (ct
, info
);
166 switch (ct
->c_subtype
) {
167 case MESSAGE_PARTIAL
:
168 return store_partial (ct
, info
);
170 case MESSAGE_EXTERNAL
:
171 return store_external (ct
, info
);
175 return store_generic (ct
, info
);
180 return store_application (ct
, info
);
186 return store_generic (ct
, info
);
189 return OK
; /* NOT REACHED */
194 * Generic routine to store a MIME content.
195 * (audio, video, image, text, message/rfc822)
199 store_generic (CT ct
, mhstoreinfo_t info
)
202 * Check if the content specifies a filename.
203 * Don't bother with this for type "message"
204 * (only "message/rfc822" will use store_generic).
206 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
209 return store_content (ct
, NULL
, info
);
214 * Store content of type "application"
218 store_application (CT ct
, mhstoreinfo_t info
)
220 CI ci
= &ct
->c_ctinfo
;
222 /* Check if the content specifies a filename */
227 * If storeproc is not defined, and the content is type
228 * "application/octet-stream", we also check for various
229 * attribute/value pairs which specify if this a tar file.
231 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
232 int tarP
= 0, zP
= 0, gzP
= 0;
235 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
236 if (strcasecmp (cp
, "tar") == 0)
240 /* check for "conversions=compress" attribute */
241 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
242 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
243 if (strcasecmp (cp
, "compress") == 0 ||
244 strcasecmp (cp
, "x-compress") == 0) {
247 if (strcasecmp (cp
, "gzip") == 0 ||
248 strcasecmp (cp
, "x-gzip") == 0) {
254 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
255 : (gzP
? "%egzip -dc | tar tvf -"
256 : "%etar tvf -"), NULL
);
257 if (!ct
->c_storeproc
) {
259 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
260 : (gzP
? "| gzip -dc | tar xvpf -"
261 : "| tar xvpf -"), NULL
);
264 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
265 : (gzP
? "%m%P.tar.gz"
266 : "%m%P.tar"), NULL
);
272 return store_content (ct
, NULL
, info
);
277 * Store the content of a multipart message
281 store_multi (CT ct
, mhstoreinfo_t info
)
284 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
288 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
289 CT p
= part
->mp_part
;
291 if (part_ok (p
) && type_ok (p
, 1)) {
293 /* Support mhstore -outfile. The MIME parser doesn't
294 load c_storage, so we know that p->c_storage is
296 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
298 result
= store_switch (p
, info
);
300 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
310 * Reassemble and store the contents of a collection
311 * of messages of type "message/partial".
315 store_partial (CT ct
, mhstoreinfo_t info
)
320 struct partial
*pm
, *qm
;
322 qm
= (struct partial
*) ct
->c_ctparams
;
327 for (ctp
= info
->cts
; *ctp
; ctp
++) {
329 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
330 pm
= (struct partial
*) p
->c_ctparams
;
332 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
333 pm
->pm_marked
= pm
->pm_partno
;
345 inform("missing (at least) last part of multipart message");
349 base
= mh_xcalloc(i
+ 1, sizeof *base
);
351 for (ctp
= info
->cts
; *ctp
; ctp
++) {
353 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
354 pm
= (struct partial
*) p
->c_ctparams
;
362 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
365 for (ctq
= base
; *ctq
; ctq
++) {
367 pm
= (struct partial
*) p
->c_ctparams
;
368 if (pm
->pm_marked
!= cur
) {
369 if (pm
->pm_marked
== cur
- 1) {
370 inform("duplicate part %d of %d part multipart message, continuing...",
376 inform("missing %spart %d of %d part multipart message",
377 cur
!= hi
? "(at least) " : "", cur
, hi
);
389 * Now cycle through the sorted list of messages of type
390 * "message/partial" and save/append them to a file.
395 if (store_content (ct
, NULL
, info
) == NOTOK
) {
401 for (; *ctq
; ctq
++) {
403 if (store_content (p
, ct
, info
) == NOTOK
)
413 * Store content from a message of type "message/external".
417 store_external (CT ct
, mhstoreinfo_t info
)
420 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
421 CT p
= e
->eb_content
;
427 * Check if the parameters for the external body
428 * specified a filename.
433 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
434 if (!ct
->c_storeproc
)
435 ct
->c_storeproc
= mh_xstrdup(cp
);
437 p
->c_storeproc
= mh_xstrdup(cp
);
442 * Since we will let the Content structure for the
443 * external body substitute for the current content,
444 * we temporarily change its partno (number inside
445 * multipart), so everything looks right.
447 p
->c_partno
= ct
->c_partno
;
449 /* we probably need to check if content is really there */
451 /* Support mhstore -outfile. The MIME parser doesn't load
452 c_storage, so we know that p->c_storage is NULL here. */
453 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
455 result
= store_switch (p
, info
);
463 * Compare the numbering from two different
464 * message/partials (needed for sorting).
468 ct_compar (CT
*a
, CT
*b
)
470 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
471 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
473 return (am
->pm_marked
- bm
->pm_marked
);
478 * Store contents of a message or message part to
479 * a folder, a file, the standard output, or pass
480 * the contents to a command.
482 * If the current content to be saved is a followup part
483 * to a collection of messages of type "message/partial",
484 * then field "p" is a pointer to the Content structure
485 * to the first message/partial in the group.
489 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
491 int appending
= 0, msgnum
= 0;
492 int is_partial
= 0, first_partial
= 0;
493 int last_partial
= 0;
494 char *cp
, buffer
[BUFSIZ
];
497 * Do special processing for messages of
498 * type "message/partial".
500 * We first check if this content is of type
501 * "message/partial". If it is, then we need to check
502 * whether it is the first and/or last in the group.
504 * Then if "p" is a valid pointer, it points to the Content
505 * structure of the first partial in the group. So we copy
506 * the file name and/or folder name from that message. In
507 * this case, we also note that we will be appending.
509 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
510 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
512 /* Yep, it's a message/partial */
515 /* But is it the first and/or last in the collection? */
516 if (pm
->pm_partno
== 1)
518 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
522 * If "p" is a valid pointer, then it points to the
523 * Content structure for the first message in the group.
524 * So we just copy the filename or foldername information
525 * from the previous iteration of this function.
529 if (! ct
->c_storage
) {
530 ct
->c_storage
= add (p
->c_storage
, NULL
);
532 /* record the folder name */
534 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
542 * Get storage formatting string.
544 * 1) If we have storeproc defined, then use that
545 * 2) Else check for a mhn-store-<type>/<subtype> entry
546 * 3) Else check for a mhn-store-<type> entry
547 * 4) Else if content is "message", use "+" (current folder)
548 * 5) Else use string "%m%P.%s".
550 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
551 CI ci
= &ct
->c_ctinfo
;
553 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
555 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
559 if (! ct
->c_storage
) {
561 * Check the beginning of storage formatting string
562 * to see if we are saving content to a folder.
564 if (*cp
== '+' || *cp
== '@') {
565 char *tmpfilenam
, *folder
;
567 /* Store content in temporary file for now */
568 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
569 adios(NULL
, "unable to create temporary file in %s",
572 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
574 /* Get the folder name */
576 folder
= pluspath (cp
);
578 folder
= getfolder (1);
580 /* Check if folder exists */
581 create_folder(m_mailpath(folder
), 0, exit
);
583 /* Record the folder name */
584 ct
->c_folder
= mh_xstrdup(folder
);
593 * Parse and expand the storage formatting string
594 * in `cp' into `buffer'.
596 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
599 * If formatting begins with '|' or '!', then pass
600 * content to standard input of a command and return.
602 if (buffer
[0] == '|' || buffer
[0] == '!')
603 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
605 /* record the filename */
606 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
611 /* The output filename was explicitly specified, so use it. */
612 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
619 /* flush the output stream */
622 /* Now save or append the content to a file */
623 if (output_content_file (ct
, appending
) == NOTOK
)
627 * If necessary, link the file into a folder and remove
628 * the temporary file. If this message is a partial,
629 * then only do this if it is the last one in the group.
631 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
632 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
633 (void) m_unlink (ct
->c_storage
);
640 * Now print out the name/number of the message
641 * that we are storing.
645 fprintf (stderr
, "reassembling partials ");
647 fputs(ct
->c_file
, stderr
);
649 fprintf (stderr
, "%s,", ct
->c_file
);
651 fprintf (stderr
, "storing message %s", ct
->c_file
);
653 fprintf (stderr
, " part %s", ct
->c_partno
);
657 * Unless we are in the "middle" of group of message/partials,
658 * we now print the name of the file, folder, and/or message
659 * to which we are storing the content.
661 if (!is_partial
|| last_partial
) {
663 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
665 } else if (!strcmp(ct
->c_storage
, "-")) {
666 fprintf (stderr
, " to stdout\n");
668 int cwdlen
= strlen (info
->cwd
);
670 fprintf (stderr
, " as file %s\n",
671 !has_prefix(ct
->c_storage
, info
->cwd
)
672 || ct
->c_storage
[cwdlen
] != '/'
673 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
683 * Output content to a file
687 output_content_file (CT ct
, int appending
)
690 char *file
, buffer
[BUFSIZ
];
695 * If the pathname is absolute, make sure
696 * all the relevant directories exist.
698 if (strchr(ct
->c_storage
, '/')
699 && make_intermediates (ct
->c_storage
) == NOTOK
)
702 if (ct
->c_encoding
!= CE_7BIT
) {
705 if (!ct
->c_ceopenfnx
) {
706 inform("don't know how to decode part %s of message %s",
707 ct
->c_partno
, ct
->c_file
);
711 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
713 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
715 if (!strcmp (file
, ct
->c_storage
)) {
716 (*ct
->c_ceclosefnx
) (ct
);
721 * Send to standard output
723 if (!strcmp (ct
->c_storage
, "-")) {
726 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
727 advise ("stdout", "unable to dup");
729 (*ct
->c_ceclosefnx
) (ct
);
732 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
733 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
734 appending
? "a" : "w");
742 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
743 advise (ct
->c_storage
, "unable to fopen for %s",
744 appending
? "appending" : "writing");
750 * Filter the header fields of the initial enclosing
751 * message/partial into the file.
753 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
754 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
756 if (pm
->pm_partno
== 1)
757 copy_some_headers (fp
, ct
);
761 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
763 advise (file
, "error reading content from");
770 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
771 advise ("output_content_file", "fwrite");
778 (*ct
->c_ceclosefnx
) (ct
);
780 if (cc
!= NOTOK
&& fflush (fp
))
781 advise (ct
->c_storage
, "error writing to");
785 return (cc
!= NOTOK
? OK
: NOTOK
);
788 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
789 advise (ct
->c_file
, "unable to open for reading");
795 fseek (ct
->c_fp
, pos
, SEEK_SET
);
797 if (!strcmp (ct
->c_storage
, "-")) {
800 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
801 advise ("stdout", "unable to dup");
804 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
805 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
806 appending
? "a" : "w");
811 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
812 advise (ct
->c_storage
, "unable to fopen for %s",
813 appending
? "appending" : "writing");
819 * Copy a few of the header fields of the initial
820 * enclosing message/partial into the file.
823 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
824 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
826 if (pm
->pm_partno
== 1) {
827 copy_some_headers (fp
, ct
);
832 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
833 if ((pos
+= strlen (buffer
)) > last
) {
836 diff
= strlen (buffer
) - (pos
- last
);
841 * If this is the first content of a group of
842 * message/partial contents, then we only copy a few
843 * of the header fields of the enclosed message.
858 if (!uprf (buffer
, XXX_FIELD_PRF
)
859 && !uprf (buffer
, VRSN_FIELD
)
860 && !uprf (buffer
, "Subject:")
861 && !uprf (buffer
, "Encrypted:")
862 && !uprf (buffer
, "Message-ID:")) {
877 advise (ct
->c_storage
, "error writing to");
887 * Add a file to a folder.
889 * Return the new message number of the file
890 * when added to the folder. Return -1, if
895 output_content_folder (char *folder
, char *filename
)
900 /* Read the folder. */
901 if ((mp
= folder_read (folder
, 0))) {
902 /* Link file into folder */
903 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, NULL
);
905 inform("unable to read folder %s", folder
);
909 /* free folder structure */
913 * Return msgnum. We are relying on the fact that
914 * msgnum will be -1, if folder_addmsg() had an error.
921 * Parse and expand the storage formatting string
922 * pointed to by "cp" into "buffer".
926 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
930 CI ci
= &ct
->c_ctinfo
;
933 * If storage string is "-", just copy it, and
934 * return (send content to standard output).
936 if (cp
[0] == '-' && cp
[1] == '\0') {
937 strncpy (buffer
, cp
, buflen
);
945 * If formatting string is a pathname that doesn't
946 * begin with '/', then preface the path with the
947 * appropriate directory.
949 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
950 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
958 /* We are processing a storage escape */
963 * Insert parameters from Content-Type.
964 * This is only valid for '|' commands.
966 if (buffer
[0] != '|' && buffer
[0] != '!') {
975 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
976 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
977 pm
->pm_name
, get_param_value(pm
, '?'));
987 /* insert message number */
988 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
992 /* insert part number with leading dot */
994 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
998 /* insert part number withouth leading dot */
1000 strncpy (bp
, ct
->c_partno
, buflen
);
1004 /* insert content type */
1005 strncpy (bp
, ci
->ci_type
, buflen
);
1009 /* insert content subtype */
1010 strncpy (bp
, ci
->ci_subtype
, buflen
);
1014 /* insert the character % */
1024 /* Advance bp and decrement buflen */
1042 * Check if the content specifies a filename
1043 * in its MIME parameters.
1047 get_storeproc (CT ct
)
1053 * If the storeproc has already been defined,
1054 * we just return (for instance, if this content
1055 * is part of a "message/external".
1057 if (ct
->c_storeproc
)
1061 * If there's a Content-Disposition header and it has a filename,
1062 * use that (RFC-2183).
1065 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1066 use_param_as_filename(cp
)) {
1067 ct
->c_storeproc
= mh_xstrdup(cp
);
1075 * Check the attribute/value pairs, for the attribute "name".
1076 * If found, do a few sanity checks and copy the value into
1080 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1081 use_param_as_filename(cp
)) {
1082 ct
->c_storeproc
= mh_xstrdup(cp
);
1090 * Copy some of the header fields of the initial message/partial
1091 * message into the header of the reassembled message.
1095 copy_some_headers (FILE *out
, CT ct
)
1099 hp
= ct
->c_first_hf
; /* start at first header field */
1103 * A few of the header fields of the enclosing
1104 * messages are not copied.
1106 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1107 && strcasecmp (hp
->name
, VRSN_FIELD
)
1108 && strcasecmp (hp
->name
, "Subject")
1109 && strcasecmp (hp
->name
, "Encrypted")
1110 && strcasecmp (hp
->name
, "Message-ID"))
1111 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1112 hp
= hp
->next
; /* next header field */
1118 /******************************************************************************/
1119 /* -clobber support */
1122 enum clobber_policy_t
1123 clobber_policy (const char *value
) {
1124 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1125 return NMH_CLOBBER_ALWAYS
;
1127 if (! strcasecmp (value
, "auto")) {
1128 return NMH_CLOBBER_AUTO
;
1130 if (! strcasecmp (value
, "suffix")) {
1131 return NMH_CLOBBER_SUFFIX
;
1133 if (! strcasecmp (value
, "ask")) {
1134 return NMH_CLOBBER_ASK
;
1136 if (! strcasecmp (value
, "never")) {
1137 return NMH_CLOBBER_NEVER
;
1140 adios (NULL
, "invalid argument, %s, to clobber", value
);
1145 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1146 const size_t max_versions
= 1000000;
1147 /* 8 = log max_versions + one for - or . + one for null terminator */
1148 const size_t buflen
= strlen (file
) + 8;
1149 char *buffer
= mh_xmalloc (buflen
);
1152 char *extension
= NULL
;
1153 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1154 ((extension
= strrchr (file
, '.')) != NULL
)) {
1155 *extension
++ = '\0';
1158 for (version
= 1; version
< max_versions
; ++version
) {
1161 switch (clobber_policy
) {
1162 case NMH_CLOBBER_AUTO
: {
1163 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1164 extension
== NULL
? "" : ".",
1165 extension
== NULL
? "" : extension
);
1169 case NMH_CLOBBER_SUFFIX
:
1170 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1174 /* Should never get here. */
1175 inform("will not overwrite %s, invalid clobber policy", buffer
);
1180 /* Actually (try to) create the file here to avoid a race
1181 condition on file naming + creation. This won't solve the
1182 problem with old NFS that doesn't support O_EXCL, though.
1183 Let the umask strip off permissions from 0666 as desired.
1184 That's what fopen () would do if it was creating the file. */
1185 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1186 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1187 S_IROTH
| S_IWOTH
)) >= 0) {
1195 if (version
>= max_versions
) {
1196 inform("will not overwrite %s, too many versions", buffer
);
1206 clobber_check (char *original_file
, mhstoreinfo_t info
) {
1207 /* clobber policy return value
1208 * -------------- ------------
1209 * -always original_file
1210 * -auto original_file-<digits>.extension
1211 * -suffix original_file.<digits>
1212 * -ask original_file, 0, or another filename/path
1220 if (! strcmp (original_file
, "-")) {
1221 return original_file
;
1224 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1225 /* Save cwd for possible use in loop below. */
1228 cwd
= mh_xstrdup(original_file
);
1229 slash
= strrchr (cwd
, '/');
1234 /* original_file isn't a full path, which should only happen if
1244 file
= original_file
;
1247 switch (info
->clobber_policy
) {
1248 case NMH_CLOBBER_ALWAYS
:
1251 case NMH_CLOBBER_SUFFIX
:
1252 case NMH_CLOBBER_AUTO
:
1253 if (stat (file
, &st
) == OK
) {
1254 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1256 ++info
->files_not_clobbered
;
1261 case NMH_CLOBBER_ASK
:
1262 if (stat (file
, &st
) == OK
) {
1263 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1264 static struct swit answer
[4] = {
1265 { "yes", 0, NMH_YES
},
1266 { "no", 0, NMH_NO
},
1267 { "rename", 0, NMH_RENAME
},
1271 if (isatty (fileno (stdin
))) {
1273 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1274 ans
= read_switch_multiword (prompt
, answer
);
1277 /* Overwrite, that's what nmh used to do. And warn. */
1278 inform("-clobber ask but no tty, so overwrite %s", file
);
1282 switch ((enum answers
) smatch (*ans
, answer
)) {
1288 ++info
->files_not_clobbered
;
1292 printf ("Enter filename or full path of the new file: ");
1293 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1296 ++info
->files_not_clobbered
;
1298 trim_suffix_c(buf
, '\n');
1303 if (buf
[0] == '/') {
1304 /* Full path, use it. */
1305 file
= mh_xstrdup(buf
);
1307 /* Relative path. */
1308 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1318 case NMH_CLOBBER_NEVER
:
1319 if (stat (file
, &st
) == OK
) {
1320 /* Keep count of files that would have been clobbered,
1321 and return that as process exit status. */
1322 inform("will not overwrite %s with -clobber never", file
);
1325 ++info
->files_not_clobbered
;
1330 original_file
= file
;
1331 } while (check_again
);
1338 static bool use_param_as_filename(const char *p
)
1340 /* Preserve result of original test that considered an empty string
1342 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1345 /* -clobber support */
1346 /******************************************************************************/