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/getcpy.h"
10 #include "sbr/getfolder.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/folder_free.h"
13 #include "sbr/folder_addmsg.h"
14 #include "sbr/context_find.h"
16 #include "sbr/error.h"
21 #include "h/fmt_scan.h"
23 #include "h/mhparse.h"
26 #include "mhshowsbr.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_mktemp.h"
30 enum clobber_policy_t
{
31 NMH_CLOBBER_ALWAYS
= 0,
38 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
41 CT
*cts
; /* Top-level list of contents to store. */
42 char *cwd
; /* cached current directory */
43 int autosw
; /* -auto enabled */
44 int verbosw
; /* -verbose enabled */
45 int files_not_clobbered
; /* output flag indicating that store failed
46 in order to not clobber an existing file */
48 /* The following must never be touched by a caller: they are for
49 internal use by the mhstoresbr functions. */
50 char *dir
; /* directory in which to store contents */
51 enum clobber_policy_t clobber_policy
; /* -clobber selection */
54 static bool use_param_as_filename(const char *p
);
57 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
66 info
->files_not_clobbered
= 0;
68 info
->clobber_policy
= clobber_policy (csw
);
74 mhstoreinfo_free (mhstoreinfo_t info
)
82 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
84 return info
->files_not_clobbered
;
89 * Type for a compare function for qsort. This keeps
92 typedef int (*qsort_comp
) (const void *, const void *);
98 static void store_single_message (CT
, mhstoreinfo_t
);
99 static int store_switch (CT
, mhstoreinfo_t
);
100 static int store_generic (CT
, mhstoreinfo_t
);
101 static int store_application (CT
, mhstoreinfo_t
);
102 static int store_multi (CT
, mhstoreinfo_t
);
103 static int store_partial (CT
, mhstoreinfo_t
);
104 static int store_external (CT
, mhstoreinfo_t
);
105 static int ct_compar (CT
*, CT
*);
106 static int store_content (CT
, CT
, mhstoreinfo_t
);
107 static int output_content_file (CT
, int);
108 static int output_content_folder (char *, char *);
109 static int parse_format_string (CT
, char *, char *, int, char *);
110 static void get_storeproc (CT
);
111 static int copy_some_headers (FILE *, CT
);
112 static char *clobber_check (char *, mhstoreinfo_t
);
115 * Main entry point to store content
116 * from a collection of messages.
120 store_all_messages (mhstoreinfo_t info
)
126 * Check for the directory in which to
127 * store any contents.
129 if ((cp
= context_find (nmhstorage
)) && *cp
)
130 info
->dir
= mh_xstrdup(cp
);
132 info
->dir
= getcpy (info
->cwd
);
134 for (ctp
= info
->cts
; *ctp
; ctp
++) {
136 store_single_message (ct
, info
);
144 * Entry point to store the content
145 * in a (single) message
149 store_single_message (CT ct
, mhstoreinfo_t info
)
151 if (type_ok (ct
, 1)) {
153 store_switch (ct
, info
);
158 if (ct
->c_ceclosefnx
)
159 (*ct
->c_ceclosefnx
) (ct
);
165 * Switching routine to store different content types
169 store_switch (CT ct
, mhstoreinfo_t info
)
171 switch (ct
->c_type
) {
173 return store_multi (ct
, info
);
176 switch (ct
->c_subtype
) {
177 case MESSAGE_PARTIAL
:
178 return store_partial (ct
, info
);
180 case MESSAGE_EXTERNAL
:
181 return store_external (ct
, info
);
185 return store_generic (ct
, info
);
190 return store_application (ct
, info
);
196 return store_generic (ct
, info
);
199 return OK
; /* NOT REACHED */
204 * Generic routine to store a MIME content.
205 * (audio, video, image, text, message/rfc822)
209 store_generic (CT ct
, mhstoreinfo_t info
)
212 * Check if the content specifies a filename.
213 * Don't bother with this for type "message"
214 * (only "message/rfc822" will use store_generic).
216 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
219 return store_content (ct
, NULL
, info
);
224 * Store content of type "application"
228 store_application (CT ct
, mhstoreinfo_t info
)
230 CI ci
= &ct
->c_ctinfo
;
232 /* Check if the content specifies a filename */
237 * If storeproc is not defined, and the content is type
238 * "application/octet-stream", we also check for various
239 * attribute/value pairs which specify if this a tar file.
241 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
247 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
248 if (strcasecmp (cp
, "tar") == 0)
252 /* check for "conversions=compress" attribute */
253 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
254 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
255 if (strcasecmp (cp
, "compress") == 0 ||
256 strcasecmp (cp
, "x-compress") == 0) {
259 if (strcasecmp (cp
, "gzip") == 0 ||
260 strcasecmp (cp
, "x-gzip") == 0) {
266 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
267 : (gzP
? "%egzip -dc | tar tvf -"
268 : "%etar tvf -"), NULL
);
269 if (!ct
->c_storeproc
) {
271 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
272 : (gzP
? "| gzip -dc | tar xvpf -"
273 : "| tar xvpf -"), NULL
);
276 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
277 : (gzP
? "%m%P.tar.gz"
278 : "%m%P.tar"), NULL
);
284 return store_content (ct
, NULL
, info
);
289 * Store the content of a multipart message
293 store_multi (CT ct
, mhstoreinfo_t info
)
296 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
300 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
301 CT p
= part
->mp_part
;
303 if (part_ok (p
) && type_ok (p
, 1)) {
305 /* Support mhstore -outfile. The MIME parser doesn't
306 load c_storage, so we know that p->c_storage is
308 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
310 result
= store_switch (p
, info
);
312 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
322 * Reassemble and store the contents of a collection
323 * of messages of type "message/partial".
327 store_partial (CT ct
, mhstoreinfo_t info
)
332 struct partial
*pm
, *qm
;
334 qm
= (struct partial
*) ct
->c_ctparams
;
339 for (ctp
= info
->cts
; *ctp
; ctp
++) {
341 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
342 pm
= (struct partial
*) p
->c_ctparams
;
344 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
345 pm
->pm_marked
= pm
->pm_partno
;
357 inform("missing (at least) last part of multipart message");
361 base
= mh_xcalloc(i
+ 1, sizeof *base
);
363 for (ctp
= info
->cts
; *ctp
; ctp
++) {
365 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
366 pm
= (struct partial
*) p
->c_ctparams
;
374 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
377 for (ctq
= base
; *ctq
; ctq
++) {
379 pm
= (struct partial
*) p
->c_ctparams
;
380 if (pm
->pm_marked
== cur
) {
385 if (pm
->pm_marked
== cur
- 1) {
386 inform("duplicate part %d of %d part multipart message, continuing...",
392 inform("missing %spart %d of %d part multipart message",
393 cur
!= hi
? "(at least) " : "", cur
, hi
);
402 * Now cycle through the sorted list of messages of type
403 * "message/partial" and save/append them to a file.
408 if (store_content (ct
, NULL
, info
) == NOTOK
) {
414 for (; *ctq
; ctq
++) {
416 if (store_content (p
, ct
, info
) == NOTOK
)
426 * Store content from a message of type "message/external".
430 store_external (CT ct
, mhstoreinfo_t info
)
433 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
434 CT p
= e
->eb_content
;
440 * Check if the parameters for the external body
441 * specified a filename.
446 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
447 if (!ct
->c_storeproc
)
448 ct
->c_storeproc
= mh_xstrdup(cp
);
450 p
->c_storeproc
= mh_xstrdup(cp
);
455 * Since we will let the Content structure for the
456 * external body substitute for the current content,
457 * we temporarily change its partno (number inside
458 * multipart), so everything looks right.
460 p
->c_partno
= ct
->c_partno
;
462 /* we probably need to check if content is really there */
464 /* Support mhstore -outfile. The MIME parser doesn't load
465 c_storage, so we know that p->c_storage is NULL here. */
466 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
468 result
= store_switch (p
, info
);
476 * Compare the numbering from two different
477 * message/partials (needed for sorting).
481 ct_compar (CT
*a
, CT
*b
)
483 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
484 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
486 return am
->pm_marked
- bm
->pm_marked
;
491 * Store contents of a message or message part to
492 * a folder, a file, the standard output, or pass
493 * the contents to a command.
495 * If the current content to be saved is a followup part
496 * to a collection of messages of type "message/partial",
497 * then field "p" is a pointer to the Content structure
498 * to the first message/partial in the group.
502 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
504 bool appending
= false;
506 bool is_partial
= false;
507 bool first_partial
= false;
508 bool last_partial
= false;
509 char *cp
, buffer
[BUFSIZ
];
512 * Do special processing for messages of
513 * type "message/partial".
515 * We first check if this content is of type
516 * "message/partial". If it is, then we need to check
517 * whether it is the first and/or last in the group.
519 * Then if "p" is a valid pointer, it points to the Content
520 * structure of the first partial in the group. So we copy
521 * the file name and/or folder name from that message. In
522 * this case, we also note that we will be appending.
524 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
525 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
527 /* Yep, it's a message/partial */
530 /* But is it the first and/or last in the collection? */
531 if (pm
->pm_partno
== 1)
532 first_partial
= true;
533 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
537 * If "p" is a valid pointer, then it points to the
538 * Content structure for the first message in the group.
539 * So we just copy the filename or foldername information
540 * from the previous iteration of this function.
544 if (! ct
->c_storage
) {
545 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
547 /* record the folder name */
549 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
557 * Get storage formatting string.
559 * 1) If we have storeproc defined, then use that
560 * 2) Else check for a mhn-store-<type>/<subtype> entry
561 * 3) Else check for a mhn-store-<type> entry
562 * 4) Else if content is "message", use "+" (current folder)
563 * 5) Else use string "%m%P.%s".
565 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
566 CI ci
= &ct
->c_ctinfo
;
568 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
570 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
574 if (! ct
->c_storage
) {
576 * Check the beginning of storage formatting string
577 * to see if we are saving content to a folder.
579 if (*cp
== '+' || *cp
== '@') {
580 char *tmpfilenam
, *folder
;
582 /* Store content in temporary file for now */
583 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
584 die("unable to create temporary file in %s",
587 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
589 /* Get the folder name */
591 folder
= pluspath (cp
);
593 folder
= getfolder (1);
595 /* Check if folder exists */
596 create_folder(m_mailpath(folder
), 0, exit
);
598 /* Record the folder name */
599 ct
->c_folder
= mh_xstrdup(folder
);
608 * Parse and expand the storage formatting string
609 * in `cp' into `buffer'.
611 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
614 * If formatting begins with '|' or '!', then pass
615 * content to standard input of a command and return.
617 if (buffer
[0] == '|' || buffer
[0] == '!')
618 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
620 /* record the filename */
621 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
626 /* The output filename was explicitly specified, so use it. */
627 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
634 /* flush the output stream */
637 /* Now save or append the content to a file */
638 if (output_content_file (ct
, appending
) == NOTOK
)
642 * If necessary, link the file into a folder and remove
643 * the temporary file. If this message is a partial,
644 * then only do this if it is the last one in the group.
646 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
647 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
648 (void) m_unlink (ct
->c_storage
);
655 * Now print out the name/number of the message
656 * that we are storing.
660 fprintf (stderr
, "reassembling partials ");
662 fputs(ct
->c_file
, stderr
);
664 fprintf (stderr
, "%s,", ct
->c_file
);
666 fprintf (stderr
, "storing message %s", ct
->c_file
);
668 fprintf (stderr
, " part %s", ct
->c_partno
);
672 * Unless we are in the "middle" of group of message/partials,
673 * we now print the name of the file, folder, and/or message
674 * to which we are storing the content.
676 if (!is_partial
|| last_partial
) {
678 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
680 } else if (!strcmp(ct
->c_storage
, "-")) {
681 fprintf (stderr
, " to stdout\n");
683 int cwdlen
= strlen (info
->cwd
);
685 fprintf (stderr
, " as file %s\n",
686 !has_prefix(ct
->c_storage
, info
->cwd
)
687 || ct
->c_storage
[cwdlen
] != '/'
688 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
698 * Output content to a file
702 output_content_file (CT ct
, int appending
)
705 char *file
, buffer
[BUFSIZ
];
710 * If the pathname is absolute, make sure
711 * all the relevant directories exist.
713 if (strchr(ct
->c_storage
, '/')
714 && make_intermediates (ct
->c_storage
) == NOTOK
)
717 if (ct
->c_encoding
!= CE_7BIT
) {
720 if (!ct
->c_ceopenfnx
) {
721 inform("don't know how to decode part %s of message %s",
722 ct
->c_partno
, ct
->c_file
);
726 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
728 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
730 if (!strcmp (file
, ct
->c_storage
)) {
731 (*ct
->c_ceclosefnx
) (ct
);
736 * Send to standard output
738 if (!strcmp (ct
->c_storage
, "-")) {
741 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
742 advise ("stdout", "unable to dup");
744 (*ct
->c_ceclosefnx
) (ct
);
747 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
748 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
749 appending
? "a" : "w");
757 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
758 advise (ct
->c_storage
, "unable to fopen for %s",
759 appending
? "appending" : "writing");
765 * Filter the header fields of the initial enclosing
766 * message/partial into the file.
768 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
769 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
771 if (pm
->pm_partno
== 1)
772 copy_some_headers (fp
, ct
);
776 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
778 advise (file
, "error reading content from");
785 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
786 advise ("output_content_file", "fwrite");
793 (*ct
->c_ceclosefnx
) (ct
);
795 if (cc
!= NOTOK
&& fflush (fp
))
796 advise (ct
->c_storage
, "error writing to");
800 return cc
== NOTOK
? NOTOK
: OK
;
803 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
804 advise (ct
->c_file
, "unable to open for reading");
810 fseek (ct
->c_fp
, pos
, SEEK_SET
);
812 if (!strcmp (ct
->c_storage
, "-")) {
815 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
816 advise ("stdout", "unable to dup");
819 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
820 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
821 appending
? "a" : "w");
826 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
827 advise (ct
->c_storage
, "unable to fopen for %s",
828 appending
? "appending" : "writing");
834 * Copy a few of the header fields of the initial
835 * enclosing message/partial into the file.
838 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
839 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
841 if (pm
->pm_partno
== 1) {
842 copy_some_headers (fp
, ct
);
847 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
848 if ((pos
+= strlen (buffer
)) > last
) {
851 diff
= strlen (buffer
) - (pos
- last
);
856 * If this is the first content of a group of
857 * message/partial contents, then we only copy a few
858 * of the header fields of the enclosed message.
873 if (!uprf (buffer
, XXX_FIELD_PRF
)
874 && !uprf (buffer
, VRSN_FIELD
)
875 && !uprf (buffer
, "Subject:")
876 && !uprf (buffer
, "Encrypted:")
877 && !uprf (buffer
, "Message-ID:")) {
892 advise (ct
->c_storage
, "error writing to");
902 * Add a file to a folder.
904 * Return the new message number of the file
905 * when added to the folder. Return -1, if
910 output_content_folder (char *folder
, char *filename
)
915 /* Read the folder. */
916 if (!(mp
= folder_read(folder
, 0))) {
917 inform("unable to read folder %s", folder
);
920 /* Link file into folder */
921 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
923 /* free folder structure */
927 * Return msgnum. We are relying on the fact that
928 * msgnum will be -1, if folder_addmsg() had an error.
935 * Parse and expand the storage formatting string
936 * pointed to by "cp" into "buffer".
940 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
944 CI ci
= &ct
->c_ctinfo
;
947 * If storage string is "-", just copy it, and
948 * return (send content to standard output).
950 if (cp
[0] == '-' && cp
[1] == '\0') {
951 strncpy (buffer
, cp
, buflen
);
959 * If formatting string is a pathname that doesn't
960 * begin with '/', then preface the path with the
961 * appropriate directory.
963 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
964 if (!strcmp(dir
, "/"))
965 dir
= ""; /* Don't start with "//". */
966 snprintf (bp
, buflen
, "%s/", dir
);
974 /* We are processing a storage escape */
979 * Insert parameters from Content-Type.
980 * This is only valid for '|' commands.
982 if (buffer
[0] != '|' && buffer
[0] != '!') {
992 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
993 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
994 pm
->pm_name
, get_param_value(pm
, '?'));
1004 /* insert message number */
1005 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1009 /* insert part number with leading dot */
1011 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1015 /* insert part number without leading dot */
1017 strncpy (bp
, ct
->c_partno
, buflen
);
1021 /* insert content type */
1022 strncpy (bp
, ci
->ci_type
, buflen
);
1026 /* insert content subtype */
1027 strncpy (bp
, ci
->ci_subtype
, buflen
);
1031 /* insert the character % */
1041 /* Advance bp and decrement buflen */
1059 * Check if the content specifies a filename
1060 * in its MIME parameters.
1064 get_storeproc (CT ct
)
1070 * If the storeproc has already been defined,
1071 * we just return (for instance, if this content
1072 * is part of a "message/external".
1074 if (ct
->c_storeproc
)
1078 * If there's a Content-Disposition header and it has a filename,
1079 * use that (RFC-2183).
1082 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1083 use_param_as_filename(cp
)) {
1084 ct
->c_storeproc
= mh_xstrdup(cp
);
1092 * Check the attribute/value pairs, for the attribute "name".
1093 * If found, do a few sanity checks and copy the value into
1097 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1098 use_param_as_filename(cp
)) {
1099 ct
->c_storeproc
= mh_xstrdup(cp
);
1107 * Copy some of the header fields of the initial message/partial
1108 * message into the header of the reassembled message.
1112 copy_some_headers (FILE *out
, CT ct
)
1116 hp
= ct
->c_first_hf
; /* start at first header field */
1120 * A few of the header fields of the enclosing
1121 * messages are not copied.
1123 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1124 && strcasecmp (hp
->name
, VRSN_FIELD
)
1125 && strcasecmp (hp
->name
, "Subject")
1126 && strcasecmp (hp
->name
, "Encrypted")
1127 && strcasecmp (hp
->name
, "Message-ID"))
1128 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1129 hp
= hp
->next
; /* next header field */
1135 /******************************************************************************/
1136 /* -clobber support */
1138 static enum clobber_policy_t
1139 clobber_policy (const char *value
)
1141 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1142 return NMH_CLOBBER_ALWAYS
;
1144 if (! strcasecmp (value
, "auto")) {
1145 return NMH_CLOBBER_AUTO
;
1147 if (! strcasecmp (value
, "suffix")) {
1148 return NMH_CLOBBER_SUFFIX
;
1150 if (! strcasecmp (value
, "ask")) {
1151 return NMH_CLOBBER_ASK
;
1153 if (! strcasecmp (value
, "never")) {
1154 return NMH_CLOBBER_NEVER
;
1157 die("invalid argument, %s, to clobber", value
);
1162 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1164 const size_t max_versions
= 1000000;
1165 /* 8 = log max_versions + one for - or . + one for null terminator */
1166 const size_t buflen
= strlen (file
) + 8;
1167 char *buffer
= mh_xmalloc (buflen
);
1170 char *extension
= NULL
;
1171 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1172 ((extension
= strrchr (file
, '.')) != NULL
)) {
1173 *extension
++ = '\0';
1176 for (version
= 1; version
< max_versions
; ++version
) {
1179 switch (clobber_policy
) {
1180 case NMH_CLOBBER_AUTO
: {
1181 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1182 extension
== NULL
? "" : ".",
1183 extension
== NULL
? "" : extension
);
1187 case NMH_CLOBBER_SUFFIX
:
1188 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1192 /* Should never get here. */
1193 inform("will not overwrite %s, invalid clobber policy", buffer
);
1198 /* Actually (try to) create the file here to avoid a race
1199 condition on file naming + creation. This won't solve the
1200 problem with old NFS that doesn't support O_EXCL, though.
1201 Let the umask strip off permissions from 0666 as desired.
1202 That's what fopen () would do if it was creating the file. */
1203 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1204 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1205 S_IROTH
| S_IWOTH
)) >= 0) {
1213 if (version
>= max_versions
) {
1214 inform("will not overwrite %s, too many versions", buffer
);
1224 clobber_check (char *original_file
, mhstoreinfo_t info
)
1226 /* clobber policy return value
1227 * -------------- ------------
1228 * -always original_file
1229 * -auto original_file-<digits>.extension
1230 * -suffix original_file.<digits>
1231 * -ask original_file, 0, or another filename/path
1239 if (! strcmp (original_file
, "-")) {
1240 return original_file
;
1243 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1244 /* Save cwd for possible use in loop below. */
1247 cwd
= mh_xstrdup(original_file
);
1248 slash
= strrchr (cwd
, '/');
1253 /* original_file isn't a full path, which should only happen if
1263 file
= original_file
;
1264 check_again
= false;
1266 switch (info
->clobber_policy
) {
1267 case NMH_CLOBBER_ALWAYS
:
1270 case NMH_CLOBBER_SUFFIX
:
1271 case NMH_CLOBBER_AUTO
:
1272 if (stat (file
, &st
) == OK
) {
1273 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1275 ++info
->files_not_clobbered
;
1280 case NMH_CLOBBER_ASK
:
1281 if (stat (file
, &st
) == OK
) {
1282 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1283 static struct swit answer
[4] = {
1284 { "yes", 0, NMH_YES
},
1285 { "no", 0, NMH_NO
},
1286 { "rename", 0, NMH_RENAME
},
1290 if (isatty (fileno (stdin
))) {
1292 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1293 ans
= read_switch_multiword (prompt
, answer
);
1296 /* Overwrite, that's what nmh used to do. And warn. */
1297 inform("-clobber ask but no tty, so overwrite %s", file
);
1301 switch ((enum answers
) smatch (*ans
, answer
)) {
1307 ++info
->files_not_clobbered
;
1311 fputs("Enter filename or full path of the new file: ", stdout
);
1312 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1315 ++info
->files_not_clobbered
;
1317 trim_suffix_c(buf
, '\n');
1322 if (buf
[0] == '/') {
1323 /* Full path, use it. */
1324 file
= mh_xstrdup(buf
);
1326 /* Relative path. */
1327 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1337 case NMH_CLOBBER_NEVER
:
1338 if (stat (file
, &st
) == OK
) {
1339 /* Keep count of files that would have been clobbered,
1340 and return that as process exit status. */
1341 inform("will not overwrite %s with -clobber never", file
);
1344 ++info
->files_not_clobbered
;
1349 original_file
= file
;
1350 } while (check_again
);
1358 use_param_as_filename(const char *p
)
1360 /* Preserve result of original test that considered an empty string
1362 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1365 /* -clobber support */
1366 /******************************************************************************/