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/getfolder.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/folder_free.h"
12 #include "sbr/folder_addmsg.h"
13 #include "sbr/context_find.h"
15 #include "sbr/error.h"
20 #include "h/fmt_scan.h"
22 #include "h/mhparse.h"
25 #include "mhshowsbr.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/m_mktemp.h"
29 enum clobber_policy_t
{
30 NMH_CLOBBER_ALWAYS
= 0,
37 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
40 CT
*cts
; /* Top-level list of contents to store. */
41 char *cwd
; /* cached current directory */
42 int autosw
; /* -auto enabled */
43 int verbosw
; /* -verbose enabled */
44 int files_not_clobbered
; /* output flag indicating that store failed
45 in order to not clobber an existing file */
47 /* The following must never be touched by a caller: they are for
48 internal use by the mhstoresbr functions. */
49 char *dir
; /* directory in which to store contents */
50 enum clobber_policy_t clobber_policy
; /* -clobber selection */
53 static bool use_param_as_filename(const char *p
);
56 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
65 info
->files_not_clobbered
= 0;
67 info
->clobber_policy
= clobber_policy (csw
);
73 mhstoreinfo_free (mhstoreinfo_t info
)
81 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
83 return info
->files_not_clobbered
;
88 * Type for a compare function for qsort. This keeps
91 typedef int (*qsort_comp
) (const void *, const void *);
97 static void store_single_message (CT
, mhstoreinfo_t
);
98 static int store_switch (CT
, mhstoreinfo_t
);
99 static int store_generic (CT
, mhstoreinfo_t
);
100 static int store_application (CT
, mhstoreinfo_t
);
101 static int store_multi (CT
, mhstoreinfo_t
);
102 static int store_partial (CT
, mhstoreinfo_t
);
103 static int store_external (CT
, mhstoreinfo_t
);
104 static int ct_compar (CT
*, CT
*);
105 static int store_content (CT
, CT
, mhstoreinfo_t
);
106 static int output_content_file (CT
, int);
107 static int output_content_folder (char *, char *);
108 static int parse_format_string (CT
, char *, char *, int, char *);
109 static void get_storeproc (CT
);
110 static int copy_some_headers (FILE *, CT
);
111 static char *clobber_check (char *, mhstoreinfo_t
);
114 * Main entry point to store content
115 * from a collection of messages.
119 store_all_messages (mhstoreinfo_t info
)
125 * Check for the directory in which to
126 * store any contents.
128 if ((cp
= context_find (nmhstorage
)) && *cp
)
129 info
->dir
= mh_xstrdup(cp
);
131 info
->dir
= getcpy (info
->cwd
);
133 for (ctp
= info
->cts
; *ctp
; ctp
++) {
135 store_single_message (ct
, info
);
143 * Entry point to store the content
144 * in a (single) message
148 store_single_message (CT ct
, mhstoreinfo_t info
)
150 if (type_ok (ct
, 1)) {
152 store_switch (ct
, info
);
157 if (ct
->c_ceclosefnx
)
158 (*ct
->c_ceclosefnx
) (ct
);
164 * Switching routine to store different content types
168 store_switch (CT ct
, mhstoreinfo_t info
)
170 switch (ct
->c_type
) {
172 return store_multi (ct
, info
);
175 switch (ct
->c_subtype
) {
176 case MESSAGE_PARTIAL
:
177 return store_partial (ct
, info
);
179 case MESSAGE_EXTERNAL
:
180 return store_external (ct
, info
);
184 return store_generic (ct
, info
);
189 return store_application (ct
, info
);
195 return store_generic (ct
, info
);
198 return OK
; /* NOT REACHED */
203 * Generic routine to store a MIME content.
204 * (audio, video, image, text, message/rfc822)
208 store_generic (CT ct
, mhstoreinfo_t info
)
211 * Check if the content specifies a filename.
212 * Don't bother with this for type "message"
213 * (only "message/rfc822" will use store_generic).
215 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
218 return store_content (ct
, NULL
, info
);
223 * Store content of type "application"
227 store_application (CT ct
, mhstoreinfo_t info
)
229 CI ci
= &ct
->c_ctinfo
;
231 /* Check if the content specifies a filename */
236 * If storeproc is not defined, and the content is type
237 * "application/octet-stream", we also check for various
238 * attribute/value pairs which specify if this a tar file.
240 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
246 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
247 if (strcasecmp (cp
, "tar") == 0)
251 /* check for "conversions=compress" attribute */
252 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
253 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
254 if (strcasecmp (cp
, "compress") == 0 ||
255 strcasecmp (cp
, "x-compress") == 0) {
258 if (strcasecmp (cp
, "gzip") == 0 ||
259 strcasecmp (cp
, "x-gzip") == 0) {
265 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
266 : (gzP
? "%egzip -dc | tar tvf -"
267 : "%etar tvf -"), NULL
);
268 if (!ct
->c_storeproc
) {
270 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
271 : (gzP
? "| gzip -dc | tar xvpf -"
272 : "| tar xvpf -"), NULL
);
275 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
276 : (gzP
? "%m%P.tar.gz"
277 : "%m%P.tar"), NULL
);
283 return store_content (ct
, NULL
, info
);
288 * Store the content of a multipart message
292 store_multi (CT ct
, mhstoreinfo_t info
)
295 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
299 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
300 CT p
= part
->mp_part
;
302 if (part_ok (p
) && type_ok (p
, 1)) {
304 /* Support mhstore -outfile. The MIME parser doesn't
305 load c_storage, so we know that p->c_storage is
307 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
309 result
= store_switch (p
, info
);
311 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
321 * Reassemble and store the contents of a collection
322 * of messages of type "message/partial".
326 store_partial (CT ct
, mhstoreinfo_t info
)
331 struct partial
*pm
, *qm
;
333 qm
= (struct partial
*) ct
->c_ctparams
;
338 for (ctp
= info
->cts
; *ctp
; ctp
++) {
340 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
341 pm
= (struct partial
*) p
->c_ctparams
;
343 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
344 pm
->pm_marked
= pm
->pm_partno
;
356 inform("missing (at least) last part of multipart message");
360 base
= mh_xcalloc(i
+ 1, sizeof *base
);
362 for (ctp
= info
->cts
; *ctp
; ctp
++) {
364 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
365 pm
= (struct partial
*) p
->c_ctparams
;
373 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
376 for (ctq
= base
; *ctq
; ctq
++) {
378 pm
= (struct partial
*) p
->c_ctparams
;
379 if (pm
->pm_marked
== cur
) {
384 if (pm
->pm_marked
== cur
- 1) {
385 inform("duplicate part %d of %d part multipart message, continuing...",
391 inform("missing %spart %d of %d part multipart message",
392 cur
!= hi
? "(at least) " : "", cur
, hi
);
401 * Now cycle through the sorted list of messages of type
402 * "message/partial" and save/append them to a file.
407 if (store_content (ct
, NULL
, info
) == NOTOK
) {
413 for (; *ctq
; ctq
++) {
415 if (store_content (p
, ct
, info
) == NOTOK
)
425 * Store content from a message of type "message/external".
429 store_external (CT ct
, mhstoreinfo_t info
)
432 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
433 CT p
= e
->eb_content
;
439 * Check if the parameters for the external body
440 * specified a filename.
445 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
446 if (!ct
->c_storeproc
)
447 ct
->c_storeproc
= mh_xstrdup(cp
);
449 p
->c_storeproc
= mh_xstrdup(cp
);
454 * Since we will let the Content structure for the
455 * external body substitute for the current content,
456 * we temporarily change its partno (number inside
457 * multipart), so everything looks right.
459 p
->c_partno
= ct
->c_partno
;
461 /* we probably need to check if content is really there */
463 /* Support mhstore -outfile. The MIME parser doesn't load
464 c_storage, so we know that p->c_storage is NULL here. */
465 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
467 result
= store_switch (p
, info
);
475 * Compare the numbering from two different
476 * message/partials (needed for sorting).
480 ct_compar (CT
*a
, CT
*b
)
482 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
483 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
485 return am
->pm_marked
- bm
->pm_marked
;
490 * Store contents of a message or message part to
491 * a folder, a file, the standard output, or pass
492 * the contents to a command.
494 * If the current content to be saved is a followup part
495 * to a collection of messages of type "message/partial",
496 * then field "p" is a pointer to the Content structure
497 * to the first message/partial in the group.
501 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
503 bool appending
= false;
505 bool is_partial
= false;
506 bool first_partial
= false;
507 bool last_partial
= false;
508 char *cp
, buffer
[BUFSIZ
];
511 * Do special processing for messages of
512 * type "message/partial".
514 * We first check if this content is of type
515 * "message/partial". If it is, then we need to check
516 * whether it is the first and/or last in the group.
518 * Then if "p" is a valid pointer, it points to the Content
519 * structure of the first partial in the group. So we copy
520 * the file name and/or folder name from that message. In
521 * this case, we also note that we will be appending.
523 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
524 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
526 /* Yep, it's a message/partial */
529 /* But is it the first and/or last in the collection? */
530 if (pm
->pm_partno
== 1)
531 first_partial
= true;
532 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
536 * If "p" is a valid pointer, then it points to the
537 * Content structure for the first message in the group.
538 * So we just copy the filename or foldername information
539 * from the previous iteration of this function.
543 if (! ct
->c_storage
) {
544 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
546 /* record the folder name */
548 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
556 * Get storage formatting string.
558 * 1) If we have storeproc defined, then use that
559 * 2) Else check for a mhn-store-<type>/<subtype> entry
560 * 3) Else check for a mhn-store-<type> entry
561 * 4) Else if content is "message", use "+" (current folder)
562 * 5) Else use string "%m%P.%s".
564 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
565 CI ci
= &ct
->c_ctinfo
;
567 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
569 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
573 if (! ct
->c_storage
) {
575 * Check the beginning of storage formatting string
576 * to see if we are saving content to a folder.
578 if (*cp
== '+' || *cp
== '@') {
579 char *tmpfilenam
, *folder
;
581 /* Store content in temporary file for now */
582 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
583 die("unable to create temporary file in %s",
586 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
588 /* Get the folder name */
590 folder
= pluspath (cp
);
592 folder
= getfolder (1);
594 /* Check if folder exists */
595 create_folder(m_mailpath(folder
), 0, exit
);
597 /* Record the folder name */
598 ct
->c_folder
= mh_xstrdup(folder
);
607 * Parse and expand the storage formatting string
608 * in `cp' into `buffer'.
610 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
613 * If formatting begins with '|' or '!', then pass
614 * content to standard input of a command and return.
616 if (buffer
[0] == '|' || buffer
[0] == '!')
617 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
619 /* record the filename */
620 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
625 /* The output filename was explicitly specified, so use it. */
626 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
633 /* flush the output stream */
636 /* Now save or append the content to a file */
637 if (output_content_file (ct
, appending
) == NOTOK
)
641 * If necessary, link the file into a folder and remove
642 * the temporary file. If this message is a partial,
643 * then only do this if it is the last one in the group.
645 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
646 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
647 (void) m_unlink (ct
->c_storage
);
654 * Now print out the name/number of the message
655 * that we are storing.
659 fprintf (stderr
, "reassembling partials ");
661 fputs(ct
->c_file
, stderr
);
663 fprintf (stderr
, "%s,", ct
->c_file
);
665 fprintf (stderr
, "storing message %s", ct
->c_file
);
667 fprintf (stderr
, " part %s", ct
->c_partno
);
671 * Unless we are in the "middle" of group of message/partials,
672 * we now print the name of the file, folder, and/or message
673 * to which we are storing the content.
675 if (!is_partial
|| last_partial
) {
677 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
679 } else if (!strcmp(ct
->c_storage
, "-")) {
680 fprintf (stderr
, " to stdout\n");
682 int cwdlen
= strlen (info
->cwd
);
684 fprintf (stderr
, " as file %s\n",
685 !has_prefix(ct
->c_storage
, info
->cwd
)
686 || ct
->c_storage
[cwdlen
] != '/'
687 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
697 * Output content to a file
701 output_content_file (CT ct
, int appending
)
704 char *file
, buffer
[BUFSIZ
];
709 * If the pathname is absolute, make sure
710 * all the relevant directories exist.
712 if (strchr(ct
->c_storage
, '/')
713 && make_intermediates (ct
->c_storage
) == NOTOK
)
716 if (ct
->c_encoding
!= CE_7BIT
) {
719 if (!ct
->c_ceopenfnx
) {
720 inform("don't know how to decode part %s of message %s",
721 ct
->c_partno
, ct
->c_file
);
725 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
727 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
729 if (!strcmp (file
, ct
->c_storage
)) {
730 (*ct
->c_ceclosefnx
) (ct
);
735 * Send to standard output
737 if (!strcmp (ct
->c_storage
, "-")) {
740 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
741 advise ("stdout", "unable to dup");
743 (*ct
->c_ceclosefnx
) (ct
);
746 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
747 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
748 appending
? "a" : "w");
756 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
757 advise (ct
->c_storage
, "unable to fopen for %s",
758 appending
? "appending" : "writing");
764 * Filter the header fields of the initial enclosing
765 * message/partial into the file.
767 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
768 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
770 if (pm
->pm_partno
== 1)
771 copy_some_headers (fp
, ct
);
775 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
777 advise (file
, "error reading content from");
784 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
785 advise ("output_content_file", "fwrite");
792 (*ct
->c_ceclosefnx
) (ct
);
794 if (cc
!= NOTOK
&& fflush (fp
))
795 advise (ct
->c_storage
, "error writing to");
799 return cc
== NOTOK
? NOTOK
: OK
;
802 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
803 advise (ct
->c_file
, "unable to open for reading");
809 fseek (ct
->c_fp
, pos
, SEEK_SET
);
811 if (!strcmp (ct
->c_storage
, "-")) {
814 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
815 advise ("stdout", "unable to dup");
818 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
819 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
820 appending
? "a" : "w");
825 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
826 advise (ct
->c_storage
, "unable to fopen for %s",
827 appending
? "appending" : "writing");
833 * Copy a few of the header fields of the initial
834 * enclosing message/partial into the file.
837 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
838 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
840 if (pm
->pm_partno
== 1) {
841 copy_some_headers (fp
, ct
);
846 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
847 if ((pos
+= strlen (buffer
)) > last
) {
850 diff
= strlen (buffer
) - (pos
- last
);
855 * If this is the first content of a group of
856 * message/partial contents, then we only copy a few
857 * of the header fields of the enclosed message.
872 if (!uprf (buffer
, XXX_FIELD_PRF
)
873 && !uprf (buffer
, VRSN_FIELD
)
874 && !uprf (buffer
, "Subject:")
875 && !uprf (buffer
, "Encrypted:")
876 && !uprf (buffer
, "Message-ID:")) {
891 advise (ct
->c_storage
, "error writing to");
901 * Add a file to a folder.
903 * Return the new message number of the file
904 * when added to the folder. Return -1, if
909 output_content_folder (char *folder
, char *filename
)
914 /* Read the folder. */
915 if (!(mp
= folder_read(folder
, 0))) {
916 inform("unable to read folder %s", folder
);
919 /* Link file into folder */
920 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
922 /* free folder structure */
926 * Return msgnum. We are relying on the fact that
927 * msgnum will be -1, if folder_addmsg() had an error.
934 * Parse and expand the storage formatting string
935 * pointed to by "cp" into "buffer".
939 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
943 CI ci
= &ct
->c_ctinfo
;
946 * If storage string is "-", just copy it, and
947 * return (send content to standard output).
949 if (cp
[0] == '-' && cp
[1] == '\0') {
950 strncpy (buffer
, cp
, buflen
);
958 * If formatting string is a pathname that doesn't
959 * begin with '/', then preface the path with the
960 * appropriate directory.
962 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
963 if (!strcmp(dir
, "/"))
964 dir
= ""; /* Don't start with "//". */
965 snprintf (bp
, buflen
, "%s/", dir
);
973 /* We are processing a storage escape */
978 * Insert parameters from Content-Type.
979 * This is only valid for '|' commands.
981 if (buffer
[0] != '|' && buffer
[0] != '!') {
991 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
992 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
993 pm
->pm_name
, get_param_value(pm
, '?'));
1003 /* insert message number */
1004 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1008 /* insert part number with leading dot */
1010 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1014 /* insert part number without leading dot */
1016 strncpy (bp
, ct
->c_partno
, buflen
);
1020 /* insert content type */
1021 strncpy (bp
, ci
->ci_type
, buflen
);
1025 /* insert content subtype */
1026 strncpy (bp
, ci
->ci_subtype
, buflen
);
1030 /* insert the character % */
1040 /* Advance bp and decrement buflen */
1058 * Check if the content specifies a filename
1059 * in its MIME parameters.
1063 get_storeproc (CT ct
)
1069 * If the storeproc has already been defined,
1070 * we just return (for instance, if this content
1071 * is part of a "message/external".
1073 if (ct
->c_storeproc
)
1077 * If there's a Content-Disposition header and it has a filename,
1078 * use that (RFC-2183).
1081 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1082 use_param_as_filename(cp
)) {
1083 ct
->c_storeproc
= mh_xstrdup(cp
);
1091 * Check the attribute/value pairs, for the attribute "name".
1092 * If found, do a few sanity checks and copy the value into
1096 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1097 use_param_as_filename(cp
)) {
1098 ct
->c_storeproc
= mh_xstrdup(cp
);
1106 * Copy some of the header fields of the initial message/partial
1107 * message into the header of the reassembled message.
1111 copy_some_headers (FILE *out
, CT ct
)
1115 hp
= ct
->c_first_hf
; /* start at first header field */
1119 * A few of the header fields of the enclosing
1120 * messages are not copied.
1122 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1123 && strcasecmp (hp
->name
, VRSN_FIELD
)
1124 && strcasecmp (hp
->name
, "Subject")
1125 && strcasecmp (hp
->name
, "Encrypted")
1126 && strcasecmp (hp
->name
, "Message-ID"))
1127 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1128 hp
= hp
->next
; /* next header field */
1134 /******************************************************************************/
1135 /* -clobber support */
1137 static enum clobber_policy_t
1138 clobber_policy (const char *value
)
1140 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1141 return NMH_CLOBBER_ALWAYS
;
1143 if (! strcasecmp (value
, "auto")) {
1144 return NMH_CLOBBER_AUTO
;
1146 if (! strcasecmp (value
, "suffix")) {
1147 return NMH_CLOBBER_SUFFIX
;
1149 if (! strcasecmp (value
, "ask")) {
1150 return NMH_CLOBBER_ASK
;
1152 if (! strcasecmp (value
, "never")) {
1153 return NMH_CLOBBER_NEVER
;
1156 die("invalid argument, %s, to clobber", value
);
1161 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1163 const size_t max_versions
= 1000000;
1164 /* 8 = log max_versions + one for - or . + one for null terminator */
1165 const size_t buflen
= strlen (file
) + 8;
1166 char *buffer
= mh_xmalloc (buflen
);
1169 char *extension
= NULL
;
1170 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1171 ((extension
= strrchr (file
, '.')) != NULL
)) {
1172 *extension
++ = '\0';
1175 for (version
= 1; version
< max_versions
; ++version
) {
1178 switch (clobber_policy
) {
1179 case NMH_CLOBBER_AUTO
: {
1180 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1181 extension
== NULL
? "" : ".",
1182 extension
== NULL
? "" : extension
);
1186 case NMH_CLOBBER_SUFFIX
:
1187 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1191 /* Should never get here. */
1192 inform("will not overwrite %s, invalid clobber policy", buffer
);
1197 /* Actually (try to) create the file here to avoid a race
1198 condition on file naming + creation. This won't solve the
1199 problem with old NFS that doesn't support O_EXCL, though.
1200 Let the umask strip off permissions from 0666 as desired.
1201 That's what fopen () would do if it was creating the file. */
1202 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1203 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1204 S_IROTH
| S_IWOTH
)) >= 0) {
1212 if (version
>= max_versions
) {
1213 inform("will not overwrite %s, too many versions", buffer
);
1223 clobber_check (char *original_file
, mhstoreinfo_t info
)
1225 /* clobber policy return value
1226 * -------------- ------------
1227 * -always original_file
1228 * -auto original_file-<digits>.extension
1229 * -suffix original_file.<digits>
1230 * -ask original_file, 0, or another filename/path
1238 if (! strcmp (original_file
, "-")) {
1239 return original_file
;
1242 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1243 /* Save cwd for possible use in loop below. */
1246 cwd
= mh_xstrdup(original_file
);
1247 slash
= strrchr (cwd
, '/');
1252 /* original_file isn't a full path, which should only happen if
1262 file
= original_file
;
1263 check_again
= false;
1265 switch (info
->clobber_policy
) {
1266 case NMH_CLOBBER_ALWAYS
:
1269 case NMH_CLOBBER_SUFFIX
:
1270 case NMH_CLOBBER_AUTO
:
1271 if (stat (file
, &st
) == OK
) {
1272 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1274 ++info
->files_not_clobbered
;
1279 case NMH_CLOBBER_ASK
:
1280 if (stat (file
, &st
) == OK
) {
1281 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1282 static struct swit answer
[4] = {
1283 { "yes", 0, NMH_YES
},
1284 { "no", 0, NMH_NO
},
1285 { "rename", 0, NMH_RENAME
},
1289 if (isatty (fileno (stdin
))) {
1291 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1292 ans
= read_switch_multiword (prompt
, answer
);
1295 /* Overwrite, that's what nmh used to do. And warn. */
1296 inform("-clobber ask but no tty, so overwrite %s", file
);
1300 switch ((enum answers
) smatch (*ans
, answer
)) {
1306 ++info
->files_not_clobbered
;
1310 fputs("Enter filename or full path of the new file: ", stdout
);
1311 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1314 ++info
->files_not_clobbered
;
1316 trim_suffix_c(buf
, '\n');
1321 if (buf
[0] == '/') {
1322 /* Full path, use it. */
1323 file
= mh_xstrdup(buf
);
1325 /* Relative path. */
1326 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1336 case NMH_CLOBBER_NEVER
:
1337 if (stat (file
, &st
) == OK
) {
1338 /* Keep count of files that would have been clobbered,
1339 and return that as process exit status. */
1340 inform("will not overwrite %s with -clobber never", file
);
1343 ++info
->files_not_clobbered
;
1348 original_file
= file
;
1349 } while (check_again
);
1357 use_param_as_filename(const char *p
)
1359 /* Preserve result of original test that considered an empty string
1361 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1364 /* -clobber support */
1365 /******************************************************************************/