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/r1bindex.h"
11 #include "sbr/getcpy.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/folder_free.h"
15 #include "sbr/folder_addmsg.h"
16 #include "sbr/context_find.h"
18 #include "sbr/error.h"
23 #include "h/fmt_scan.h"
25 #include "h/mhparse.h"
28 #include "mhshowsbr.h"
29 #include "sbr/m_maildir.h"
30 #include "sbr/m_mktemp.h"
32 enum clobber_policy_t
{
33 NMH_CLOBBER_ALWAYS
= 0,
40 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
43 CT
*cts
; /* Top-level list of contents to store. */
44 char *cwd
; /* cached current directory */
45 int autosw
; /* -auto enabled */
46 int verbosw
; /* -verbose enabled */
47 int files_not_clobbered
; /* output flag indicating that store failed
48 in order to not clobber an existing file */
50 /* The following must never be touched by a caller: they are for
51 internal use by the mhstoresbr functions. */
52 char *dir
; /* directory in which to store contents */
53 enum clobber_policy_t clobber_policy
; /* -clobber selection */
56 static bool use_param_as_filename(const char *p
);
59 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
68 info
->files_not_clobbered
= 0;
70 info
->clobber_policy
= clobber_policy (csw
);
76 mhstoreinfo_free (mhstoreinfo_t info
)
84 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
86 return info
->files_not_clobbered
;
91 * Type for a compare function for qsort. This keeps
94 typedef int (*qsort_comp
) (const void *, const void *);
100 static void store_single_message (CT
, mhstoreinfo_t
);
101 static int store_switch (CT
, mhstoreinfo_t
);
102 static int store_generic (CT
, mhstoreinfo_t
);
103 static int store_application (CT
, mhstoreinfo_t
);
104 static int store_multi (CT
, mhstoreinfo_t
);
105 static int store_partial (CT
, mhstoreinfo_t
);
106 static int store_external (CT
, mhstoreinfo_t
);
107 static int ct_compar (CT
*, CT
*);
108 static int store_content (CT
, CT
, mhstoreinfo_t
);
109 static int output_content_file (CT
, int);
110 static int output_content_folder (char *, char *);
111 static int parse_format_string (CT
, char *, char *, int, char *);
112 static void get_storeproc (CT
);
113 static int copy_some_headers (FILE *, CT
);
114 static char *clobber_check (char *, mhstoreinfo_t
);
117 * Main entry point to store content
118 * from a collection of messages.
122 store_all_messages (mhstoreinfo_t info
)
128 * Check for the directory in which to
129 * store any contents.
131 if ((cp
= context_find (nmhstorage
)) && *cp
)
132 info
->dir
= mh_xstrdup(cp
);
134 info
->dir
= getcpy (info
->cwd
);
136 for (ctp
= info
->cts
; *ctp
; ctp
++) {
138 store_single_message (ct
, info
);
146 * Entry point to store the content
147 * in a (single) message
151 store_single_message (CT ct
, mhstoreinfo_t info
)
153 if (type_ok (ct
, 1)) {
155 store_switch (ct
, info
);
160 if (ct
->c_ceclosefnx
)
161 (*ct
->c_ceclosefnx
) (ct
);
167 * Switching routine to store different content types
171 store_switch (CT ct
, mhstoreinfo_t info
)
173 switch (ct
->c_type
) {
175 return store_multi (ct
, info
);
178 switch (ct
->c_subtype
) {
179 case MESSAGE_PARTIAL
:
180 return store_partial (ct
, info
);
182 case MESSAGE_EXTERNAL
:
183 return store_external (ct
, info
);
187 return store_generic (ct
, info
);
192 return store_application (ct
, info
);
198 return store_generic (ct
, info
);
201 return OK
; /* NOT REACHED */
206 * Generic routine to store a MIME content.
207 * (audio, video, image, text, message/rfc822)
211 store_generic (CT ct
, mhstoreinfo_t info
)
214 * Check if the content specifies a filename.
215 * Don't bother with this for type "message"
216 * (only "message/rfc822" will use store_generic).
218 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
221 return store_content (ct
, NULL
, info
);
226 * Store content of type "application"
230 store_application (CT ct
, mhstoreinfo_t info
)
232 CI ci
= &ct
->c_ctinfo
;
234 /* Check if the content specifies a filename */
239 * If storeproc is not defined, and the content is type
240 * "application/octet-stream", we also check for various
241 * attribute/value pairs which specify if this a tar file.
243 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
249 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
250 if (strcasecmp (cp
, "tar") == 0)
254 /* check for "conversions=compress" attribute */
255 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
256 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
257 if (strcasecmp (cp
, "compress") == 0 ||
258 strcasecmp (cp
, "x-compress") == 0) {
261 if (strcasecmp (cp
, "gzip") == 0 ||
262 strcasecmp (cp
, "x-gzip") == 0) {
268 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
269 : (gzP
? "%egzip -dc | tar tvf -"
270 : "%etar tvf -"), NULL
);
271 if (!ct
->c_storeproc
) {
273 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
274 : (gzP
? "| gzip -dc | tar xvpf -"
275 : "| tar xvpf -"), NULL
);
278 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
279 : (gzP
? "%m%P.tar.gz"
280 : "%m%P.tar"), NULL
);
286 return store_content (ct
, NULL
, info
);
291 * Store the content of a multipart message
295 store_multi (CT ct
, mhstoreinfo_t info
)
298 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
302 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
303 CT p
= part
->mp_part
;
305 if (part_ok (p
) && type_ok (p
, 1)) {
307 /* Support mhstore -outfile. The MIME parser doesn't
308 load c_storage, so we know that p->c_storage is
310 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
312 result
= store_switch (p
, info
);
314 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
324 * Reassemble and store the contents of a collection
325 * of messages of type "message/partial".
329 store_partial (CT ct
, mhstoreinfo_t info
)
334 struct partial
*pm
, *qm
;
336 qm
= (struct partial
*) ct
->c_ctparams
;
341 for (ctp
= info
->cts
; *ctp
; ctp
++) {
343 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
344 pm
= (struct partial
*) p
->c_ctparams
;
346 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
347 pm
->pm_marked
= pm
->pm_partno
;
359 inform("missing (at least) last part of multipart message");
363 base
= mh_xcalloc(i
+ 1, sizeof *base
);
365 for (ctp
= info
->cts
; *ctp
; ctp
++) {
367 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
368 pm
= (struct partial
*) p
->c_ctparams
;
376 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
379 for (ctq
= base
; *ctq
; ctq
++) {
381 pm
= (struct partial
*) p
->c_ctparams
;
382 if (pm
->pm_marked
== cur
) {
387 if (pm
->pm_marked
== cur
- 1) {
388 inform("duplicate part %d of %d part multipart message, continuing...",
394 inform("missing %spart %d of %d part multipart message",
395 cur
!= hi
? "(at least) " : "", cur
, hi
);
404 * Now cycle through the sorted list of messages of type
405 * "message/partial" and save/append them to a file.
410 if (store_content (ct
, NULL
, info
) == NOTOK
) {
416 for (; *ctq
; ctq
++) {
418 if (store_content (p
, ct
, info
) == NOTOK
)
428 * Store content from a message of type "message/external".
432 store_external (CT ct
, mhstoreinfo_t info
)
435 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
436 CT p
= e
->eb_content
;
442 * Check if the parameters for the external body
443 * specified a filename.
448 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
449 if (!ct
->c_storeproc
)
450 ct
->c_storeproc
= mh_xstrdup(cp
);
452 p
->c_storeproc
= mh_xstrdup(cp
);
457 * Since we will let the Content structure for the
458 * external body substitute for the current content,
459 * we temporarily change its partno (number inside
460 * multipart), so everything looks right.
462 p
->c_partno
= ct
->c_partno
;
464 /* we probably need to check if content is really there */
466 /* Support mhstore -outfile. The MIME parser doesn't load
467 c_storage, so we know that p->c_storage is NULL here. */
468 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
470 result
= store_switch (p
, info
);
478 * Compare the numbering from two different
479 * message/partials (needed for sorting).
483 ct_compar (CT
*a
, CT
*b
)
485 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
486 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
488 return am
->pm_marked
- bm
->pm_marked
;
493 * Store contents of a message or message part to
494 * a folder, a file, the standard output, or pass
495 * the contents to a command.
497 * If the current content to be saved is a followup part
498 * to a collection of messages of type "message/partial",
499 * then field "p" is a pointer to the Content structure
500 * to the first message/partial in the group.
504 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
506 bool appending
= false;
508 bool is_partial
= false;
509 bool first_partial
= false;
510 bool last_partial
= false;
511 char *cp
, buffer
[BUFSIZ
];
514 * Do special processing for messages of
515 * type "message/partial".
517 * We first check if this content is of type
518 * "message/partial". If it is, then we need to check
519 * whether it is the first and/or last in the group.
521 * Then if "p" is a valid pointer, it points to the Content
522 * structure of the first partial in the group. So we copy
523 * the file name and/or folder name from that message. In
524 * this case, we also note that we will be appending.
526 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
527 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
529 /* Yep, it's a message/partial */
532 /* But is it the first and/or last in the collection? */
533 if (pm
->pm_partno
== 1)
534 first_partial
= true;
535 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
539 * If "p" is a valid pointer, then it points to the
540 * Content structure for the first message in the group.
541 * So we just copy the filename or foldername information
542 * from the previous iteration of this function.
546 if (! ct
->c_storage
) {
547 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
549 /* record the folder name */
551 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
559 * Get storage formatting string.
561 * 1) If we have storeproc defined, then use that
562 * 2) Else check for a mhn-store-<type>/<subtype> entry
563 * 3) Else check for a mhn-store-<type> entry
564 * 4) Else if content is "message", use "+" (current folder)
565 * 5) Else use string "%m%P.%s".
567 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
568 CI ci
= &ct
->c_ctinfo
;
570 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
572 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
576 if (! ct
->c_storage
) {
578 * Check the beginning of storage formatting string
579 * to see if we are saving content to a folder.
581 if (*cp
== '+' || *cp
== '@') {
582 char *tmpfilenam
, *folder
;
584 /* Store content in temporary file for now */
585 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
586 die("unable to create temporary file in %s",
589 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
591 /* Get the folder name */
593 folder
= pluspath (cp
);
595 folder
= getfolder (1);
597 /* Check if folder exists */
598 create_folder(m_mailpath(folder
), 0, exit
);
600 /* Record the folder name */
601 ct
->c_folder
= mh_xstrdup(folder
);
610 * Parse and expand the storage formatting string
611 * in `cp' into `buffer'.
613 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
616 * If formatting begins with '|' or '!', then pass
617 * content to standard input of a command and return.
619 if (buffer
[0] == '|' || buffer
[0] == '!')
620 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
622 /* record the filename */
623 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
628 /* The output filename was explicitly specified, so use it. */
629 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
636 /* flush the output stream */
639 /* Now save or append the content to a file */
640 if (output_content_file (ct
, appending
) == NOTOK
)
644 * If necessary, link the file into a folder and remove
645 * the temporary file. If this message is a partial,
646 * then only do this if it is the last one in the group.
648 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
649 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
650 (void) m_unlink (ct
->c_storage
);
657 * Now print out the name/number of the message
658 * that we are storing.
662 fprintf (stderr
, "reassembling partials ");
664 fputs(ct
->c_file
, stderr
);
666 fprintf (stderr
, "%s,", ct
->c_file
);
668 fprintf (stderr
, "storing message %s", ct
->c_file
);
670 fprintf (stderr
, " part %s", ct
->c_partno
);
674 * Unless we are in the "middle" of group of message/partials,
675 * we now print the name of the file, folder, and/or message
676 * to which we are storing the content.
678 if (!is_partial
|| last_partial
) {
680 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
682 } else if (!strcmp(ct
->c_storage
, "-")) {
683 fprintf (stderr
, " to stdout\n");
685 int cwdlen
= strlen (info
->cwd
);
687 fprintf (stderr
, " as file %s\n",
688 !has_prefix(ct
->c_storage
, info
->cwd
)
689 || ct
->c_storage
[cwdlen
] != '/'
690 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
700 * Output content to a file
704 output_content_file (CT ct
, int appending
)
707 char *file
, buffer
[BUFSIZ
];
712 * If the pathname is absolute, make sure
713 * all the relevant directories exist.
715 if (strchr(ct
->c_storage
, '/')
716 && make_intermediates (ct
->c_storage
) == NOTOK
)
719 if (ct
->c_encoding
!= CE_7BIT
) {
722 if (!ct
->c_ceopenfnx
) {
723 inform("don't know how to decode part %s of message %s",
724 ct
->c_partno
, ct
->c_file
);
728 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
730 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
732 if (!strcmp (file
, ct
->c_storage
)) {
733 (*ct
->c_ceclosefnx
) (ct
);
738 * Send to standard output
740 if (!strcmp (ct
->c_storage
, "-")) {
743 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
744 advise ("stdout", "unable to dup");
746 (*ct
->c_ceclosefnx
) (ct
);
749 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
750 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
751 appending
? "a" : "w");
759 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
760 advise (ct
->c_storage
, "unable to fopen for %s",
761 appending
? "appending" : "writing");
767 * Filter the header fields of the initial enclosing
768 * message/partial into the file.
770 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
771 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
773 if (pm
->pm_partno
== 1)
774 copy_some_headers (fp
, ct
);
778 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
780 advise (file
, "error reading content from");
787 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
788 advise ("output_content_file", "fwrite");
795 (*ct
->c_ceclosefnx
) (ct
);
797 if (cc
!= NOTOK
&& fflush (fp
))
798 advise (ct
->c_storage
, "error writing to");
802 return cc
== NOTOK
? NOTOK
: OK
;
805 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
806 advise (ct
->c_file
, "unable to open for reading");
812 fseek (ct
->c_fp
, pos
, SEEK_SET
);
814 if (!strcmp (ct
->c_storage
, "-")) {
817 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
818 advise ("stdout", "unable to dup");
821 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
822 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
823 appending
? "a" : "w");
828 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
829 advise (ct
->c_storage
, "unable to fopen for %s",
830 appending
? "appending" : "writing");
836 * Copy a few of the header fields of the initial
837 * enclosing message/partial into the file.
840 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
841 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
843 if (pm
->pm_partno
== 1) {
844 copy_some_headers (fp
, ct
);
849 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
850 if ((pos
+= strlen (buffer
)) > last
) {
853 diff
= strlen (buffer
) - (pos
- last
);
858 * If this is the first content of a group of
859 * message/partial contents, then we only copy a few
860 * of the header fields of the enclosed message.
875 if (!uprf (buffer
, XXX_FIELD_PRF
)
876 && !uprf (buffer
, VRSN_FIELD
)
877 && !uprf (buffer
, "Subject:")
878 && !uprf (buffer
, "Encrypted:")
879 && !uprf (buffer
, "Message-ID:")) {
894 advise (ct
->c_storage
, "error writing to");
904 * Add a file to a folder.
906 * Return the new message number of the file
907 * when added to the folder. Return -1, if
912 output_content_folder (char *folder
, char *filename
)
917 /* Read the folder. */
918 if (!(mp
= folder_read(folder
, 0))) {
919 inform("unable to read folder %s", folder
);
922 /* Link file into folder */
923 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
925 /* free folder structure */
929 * Return msgnum. We are relying on the fact that
930 * msgnum will be -1, if folder_addmsg() had an error.
937 * Parse and expand the storage formatting string
938 * pointed to by "cp" into "buffer".
942 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
946 CI ci
= &ct
->c_ctinfo
;
949 * If storage string is "-", just copy it, and
950 * return (send content to standard output).
952 if (cp
[0] == '-' && cp
[1] == '\0') {
953 strncpy (buffer
, cp
, buflen
);
961 * If formatting string is a pathname that doesn't
962 * begin with '/', then preface the path with the
963 * appropriate directory.
965 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
966 if (!strcmp(dir
, "/"))
967 dir
= ""; /* Don't start with "//". */
968 snprintf (bp
, buflen
, "%s/", dir
);
976 /* We are processing a storage escape */
981 * Insert parameters from Content-Type.
982 * This is only valid for '|' commands.
984 if (buffer
[0] != '|' && buffer
[0] != '!') {
994 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
995 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
996 pm
->pm_name
, get_param_value(pm
, '?'));
1006 /* insert message number */
1007 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1011 /* insert part number with leading dot */
1013 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1017 /* insert part number without leading dot */
1019 strncpy (bp
, ct
->c_partno
, buflen
);
1023 /* insert content type */
1024 strncpy (bp
, ci
->ci_type
, buflen
);
1028 /* insert content subtype */
1029 strncpy (bp
, ci
->ci_subtype
, buflen
);
1033 /* insert the character % */
1043 /* Advance bp and decrement buflen */
1061 * Check if the content specifies a filename
1062 * in its MIME parameters.
1066 get_storeproc (CT ct
)
1072 * If the storeproc has already been defined,
1073 * we just return (for instance, if this content
1074 * is part of a "message/external".
1076 if (ct
->c_storeproc
)
1080 * If there's a Content-Disposition header and it has a filename,
1081 * use that (RFC-2183).
1084 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1085 use_param_as_filename(cp
)) {
1086 ct
->c_storeproc
= mh_xstrdup(cp
);
1094 * Check the attribute/value pairs, for the attribute "name".
1095 * If found, do a few sanity checks and copy the value into
1099 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1100 use_param_as_filename(cp
)) {
1101 ct
->c_storeproc
= mh_xstrdup(cp
);
1109 * Copy some of the header fields of the initial message/partial
1110 * message into the header of the reassembled message.
1114 copy_some_headers (FILE *out
, CT ct
)
1118 hp
= ct
->c_first_hf
; /* start at first header field */
1122 * A few of the header fields of the enclosing
1123 * messages are not copied.
1125 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1126 && strcasecmp (hp
->name
, VRSN_FIELD
)
1127 && strcasecmp (hp
->name
, "Subject")
1128 && strcasecmp (hp
->name
, "Encrypted")
1129 && strcasecmp (hp
->name
, "Message-ID"))
1130 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1131 hp
= hp
->next
; /* next header field */
1137 /******************************************************************************/
1138 /* -clobber support */
1140 static enum clobber_policy_t
1141 clobber_policy (const char *value
)
1143 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1144 return NMH_CLOBBER_ALWAYS
;
1146 if (! strcasecmp (value
, "auto")) {
1147 return NMH_CLOBBER_AUTO
;
1149 if (! strcasecmp (value
, "suffix")) {
1150 return NMH_CLOBBER_SUFFIX
;
1152 if (! strcasecmp (value
, "ask")) {
1153 return NMH_CLOBBER_ASK
;
1155 if (! strcasecmp (value
, "never")) {
1156 return NMH_CLOBBER_NEVER
;
1159 die("invalid argument, %s, to clobber", value
);
1164 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1166 const size_t max_versions
= 1000000;
1167 /* 8 = log max_versions + one for - or . + one for null terminator */
1168 const size_t buflen
= strlen (file
) + 8;
1169 char *buffer
= mh_xmalloc (buflen
);
1172 char *extension
= NULL
;
1173 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1174 ((extension
= strrchr (file
, '.')) != NULL
)) {
1175 *extension
++ = '\0';
1178 for (version
= 1; version
< max_versions
; ++version
) {
1181 switch (clobber_policy
) {
1182 case NMH_CLOBBER_AUTO
: {
1183 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1184 extension
== NULL
? "" : ".",
1185 extension
== NULL
? "" : extension
);
1189 case NMH_CLOBBER_SUFFIX
:
1190 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1194 /* Should never get here. */
1195 inform("will not overwrite %s, invalid clobber policy", buffer
);
1200 /* Actually (try to) create the file here to avoid a race
1201 condition on file naming + creation. This won't solve the
1202 problem with old NFS that doesn't support O_EXCL, though.
1203 Let the umask strip off permissions from 0666 as desired.
1204 That's what fopen () would do if it was creating the file. */
1205 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1206 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1207 S_IROTH
| S_IWOTH
)) >= 0) {
1215 if (version
>= max_versions
) {
1216 inform("will not overwrite %s, too many versions", buffer
);
1226 clobber_check (char *original_file
, mhstoreinfo_t info
)
1228 /* clobber policy return value
1229 * -------------- ------------
1230 * -always original_file
1231 * -auto original_file-<digits>.extension
1232 * -suffix original_file.<digits>
1233 * -ask original_file, 0, or another filename/path
1241 if (! strcmp (original_file
, "-")) {
1242 return original_file
;
1245 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1246 /* Save cwd for possible use in loop below. */
1249 cwd
= mh_xstrdup(original_file
);
1250 slash
= strrchr (cwd
, '/');
1255 /* original_file isn't a full path, which should only happen if
1265 file
= original_file
;
1266 check_again
= false;
1268 switch (info
->clobber_policy
) {
1269 case NMH_CLOBBER_ALWAYS
:
1272 case NMH_CLOBBER_SUFFIX
:
1273 case NMH_CLOBBER_AUTO
:
1274 if (stat (file
, &st
) == OK
) {
1275 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1277 ++info
->files_not_clobbered
;
1282 case NMH_CLOBBER_ASK
:
1283 if (stat (file
, &st
) == OK
) {
1284 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1285 static struct swit answer
[4] = {
1286 { "yes", 0, NMH_YES
},
1287 { "no", 0, NMH_NO
},
1288 { "rename", 0, NMH_RENAME
},
1292 if (isatty (fileno (stdin
))) {
1294 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1295 ans
= read_switch_multiword (prompt
, answer
);
1298 /* Overwrite, that's what nmh used to do. And warn. */
1299 inform("-clobber ask but no tty, so overwrite %s", file
);
1303 switch ((enum answers
) smatch (*ans
, answer
)) {
1309 ++info
->files_not_clobbered
;
1313 fputs("Enter filename or full path of the new file: ", stdout
);
1314 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1317 ++info
->files_not_clobbered
;
1319 trim_suffix_c(buf
, '\n');
1324 if (buf
[0] == '/') {
1325 /* Full path, use it. */
1326 file
= mh_xstrdup(buf
);
1328 /* Relative path. */
1329 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1339 case NMH_CLOBBER_NEVER
:
1340 if (stat (file
, &st
) == OK
) {
1341 /* Keep count of files that would have been clobbered,
1342 and return that as process exit status. */
1343 inform("will not overwrite %s with -clobber never", file
);
1346 ++info
->files_not_clobbered
;
1351 original_file
= file
;
1352 } while (check_again
);
1360 use_param_as_filename(const char *p
)
1362 /* Preserve result of original test that considered an empty string
1364 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1367 /* -clobber support */
1368 /******************************************************************************/