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/folder_read.h"
10 #include "sbr/folder_free.h"
11 #include "sbr/folder_addmsg.h"
12 #include "sbr/context_find.h"
14 #include "sbr/error.h"
19 #include "h/fmt_scan.h"
21 #include "h/mhparse.h"
24 #include "mhshowsbr.h"
25 #include "sbr/m_maildir.h"
26 #include "sbr/m_mktemp.h"
28 enum clobber_policy_t
{
29 NMH_CLOBBER_ALWAYS
= 0,
36 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
39 CT
*cts
; /* Top-level list of contents to store. */
40 char *cwd
; /* cached current directory */
41 int autosw
; /* -auto enabled */
42 int verbosw
; /* -verbose enabled */
43 int files_not_clobbered
; /* output flag indicating that store failed
44 in order to not clobber an existing file */
46 /* The following must never be touched by a caller: they are for
47 internal use by the mhstoresbr functions. */
48 char *dir
; /* directory in which to store contents */
49 enum clobber_policy_t clobber_policy
; /* -clobber selection */
52 static bool use_param_as_filename(const char *p
);
55 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
64 info
->files_not_clobbered
= 0;
66 info
->clobber_policy
= clobber_policy (csw
);
72 mhstoreinfo_free (mhstoreinfo_t info
)
80 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
82 return info
->files_not_clobbered
;
87 * Type for a compare function for qsort. This keeps
90 typedef int (*qsort_comp
) (const void *, const void *);
96 static void store_single_message (CT
, mhstoreinfo_t
);
97 static int store_switch (CT
, mhstoreinfo_t
);
98 static int store_generic (CT
, mhstoreinfo_t
);
99 static int store_application (CT
, mhstoreinfo_t
);
100 static int store_multi (CT
, mhstoreinfo_t
);
101 static int store_partial (CT
, mhstoreinfo_t
);
102 static int store_external (CT
, mhstoreinfo_t
);
103 static int ct_compar (CT
*, CT
*);
104 static int store_content (CT
, CT
, mhstoreinfo_t
);
105 static int output_content_file (CT
, int);
106 static int output_content_folder (char *, char *);
107 static int parse_format_string (CT
, char *, char *, int, char *);
108 static void get_storeproc (CT
);
109 static int copy_some_headers (FILE *, CT
);
110 static char *clobber_check (char *, mhstoreinfo_t
);
113 * Main entry point to store content
114 * from a collection of messages.
118 store_all_messages (mhstoreinfo_t info
)
124 * Check for the directory in which to
125 * store any contents.
127 if ((cp
= context_find (nmhstorage
)) && *cp
)
128 info
->dir
= mh_xstrdup(cp
);
130 info
->dir
= getcpy (info
->cwd
);
132 for (ctp
= info
->cts
; *ctp
; ctp
++) {
134 store_single_message (ct
, info
);
142 * Entry point to store the content
143 * in a (single) message
147 store_single_message (CT ct
, mhstoreinfo_t info
)
149 if (type_ok (ct
, 1)) {
151 store_switch (ct
, info
);
156 if (ct
->c_ceclosefnx
)
157 (*ct
->c_ceclosefnx
) (ct
);
163 * Switching routine to store different content types
167 store_switch (CT ct
, mhstoreinfo_t info
)
169 switch (ct
->c_type
) {
171 return store_multi (ct
, info
);
174 switch (ct
->c_subtype
) {
175 case MESSAGE_PARTIAL
:
176 return store_partial (ct
, info
);
178 case MESSAGE_EXTERNAL
:
179 return store_external (ct
, info
);
183 return store_generic (ct
, info
);
188 return store_application (ct
, info
);
194 return store_generic (ct
, info
);
197 return OK
; /* NOT REACHED */
202 * Generic routine to store a MIME content.
203 * (audio, video, image, text, message/rfc822)
207 store_generic (CT ct
, mhstoreinfo_t info
)
210 * Check if the content specifies a filename.
211 * Don't bother with this for type "message"
212 * (only "message/rfc822" will use store_generic).
214 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
217 return store_content (ct
, NULL
, info
);
222 * Store content of type "application"
226 store_application (CT ct
, mhstoreinfo_t info
)
228 CI ci
= &ct
->c_ctinfo
;
230 /* Check if the content specifies a filename */
235 * If storeproc is not defined, and the content is type
236 * "application/octet-stream", we also check for various
237 * attribute/value pairs which specify if this a tar file.
239 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
245 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
246 if (strcasecmp (cp
, "tar") == 0)
250 /* check for "conversions=compress" attribute */
251 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
252 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
253 if (strcasecmp (cp
, "compress") == 0 ||
254 strcasecmp (cp
, "x-compress") == 0) {
257 if (strcasecmp (cp
, "gzip") == 0 ||
258 strcasecmp (cp
, "x-gzip") == 0) {
264 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
265 : (gzP
? "%egzip -dc | tar tvf -"
266 : "%etar tvf -"), NULL
);
267 if (!ct
->c_storeproc
) {
269 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
270 : (gzP
? "| gzip -dc | tar xvpf -"
271 : "| tar xvpf -"), NULL
);
274 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
275 : (gzP
? "%m%P.tar.gz"
276 : "%m%P.tar"), NULL
);
282 return store_content (ct
, NULL
, info
);
287 * Store the content of a multipart message
291 store_multi (CT ct
, mhstoreinfo_t info
)
294 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
298 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
299 CT p
= part
->mp_part
;
301 if (part_ok (p
) && type_ok (p
, 1)) {
303 /* Support mhstore -outfile. The MIME parser doesn't
304 load c_storage, so we know that p->c_storage is
306 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
308 result
= store_switch (p
, info
);
310 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
320 * Reassemble and store the contents of a collection
321 * of messages of type "message/partial".
325 store_partial (CT ct
, mhstoreinfo_t info
)
330 struct partial
*pm
, *qm
;
332 qm
= (struct partial
*) ct
->c_ctparams
;
337 for (ctp
= info
->cts
; *ctp
; ctp
++) {
339 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
340 pm
= (struct partial
*) p
->c_ctparams
;
342 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
343 pm
->pm_marked
= pm
->pm_partno
;
355 inform("missing (at least) last part of multipart message");
359 base
= mh_xcalloc(i
+ 1, sizeof *base
);
361 for (ctp
= info
->cts
; *ctp
; ctp
++) {
363 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
364 pm
= (struct partial
*) p
->c_ctparams
;
372 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
375 for (ctq
= base
; *ctq
; ctq
++) {
377 pm
= (struct partial
*) p
->c_ctparams
;
378 if (pm
->pm_marked
== cur
) {
383 if (pm
->pm_marked
== cur
- 1) {
384 inform("duplicate part %d of %d part multipart message, continuing...",
390 inform("missing %spart %d of %d part multipart message",
391 cur
!= hi
? "(at least) " : "", cur
, hi
);
400 * Now cycle through the sorted list of messages of type
401 * "message/partial" and save/append them to a file.
406 if (store_content (ct
, NULL
, info
) == NOTOK
) {
412 for (; *ctq
; ctq
++) {
414 if (store_content (p
, ct
, info
) == NOTOK
)
424 * Store content from a message of type "message/external".
428 store_external (CT ct
, mhstoreinfo_t info
)
431 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
432 CT p
= e
->eb_content
;
438 * Check if the parameters for the external body
439 * specified a filename.
444 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
445 if (!ct
->c_storeproc
)
446 ct
->c_storeproc
= mh_xstrdup(cp
);
448 p
->c_storeproc
= mh_xstrdup(cp
);
453 * Since we will let the Content structure for the
454 * external body substitute for the current content,
455 * we temporarily change its partno (number inside
456 * multipart), so everything looks right.
458 p
->c_partno
= ct
->c_partno
;
460 /* we probably need to check if content is really there */
462 /* Support mhstore -outfile. The MIME parser doesn't load
463 c_storage, so we know that p->c_storage is NULL here. */
464 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
466 result
= store_switch (p
, info
);
474 * Compare the numbering from two different
475 * message/partials (needed for sorting).
479 ct_compar (CT
*a
, CT
*b
)
481 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
482 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
484 return am
->pm_marked
- bm
->pm_marked
;
489 * Store contents of a message or message part to
490 * a folder, a file, the standard output, or pass
491 * the contents to a command.
493 * If the current content to be saved is a followup part
494 * to a collection of messages of type "message/partial",
495 * then field "p" is a pointer to the Content structure
496 * to the first message/partial in the group.
500 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
502 bool appending
= false;
504 bool is_partial
= false;
505 bool first_partial
= false;
506 bool last_partial
= false;
507 char *cp
, buffer
[BUFSIZ
];
510 * Do special processing for messages of
511 * type "message/partial".
513 * We first check if this content is of type
514 * "message/partial". If it is, then we need to check
515 * whether it is the first and/or last in the group.
517 * Then if "p" is a valid pointer, it points to the Content
518 * structure of the first partial in the group. So we copy
519 * the file name and/or folder name from that message. In
520 * this case, we also note that we will be appending.
522 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
523 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
525 /* Yep, it's a message/partial */
528 /* But is it the first and/or last in the collection? */
529 if (pm
->pm_partno
== 1)
530 first_partial
= true;
531 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
535 * If "p" is a valid pointer, then it points to the
536 * Content structure for the first message in the group.
537 * So we just copy the filename or foldername information
538 * from the previous iteration of this function.
542 if (! ct
->c_storage
) {
543 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
545 /* record the folder name */
547 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
555 * Get storage formatting string.
557 * 1) If we have storeproc defined, then use that
558 * 2) Else check for a mhn-store-<type>/<subtype> entry
559 * 3) Else check for a mhn-store-<type> entry
560 * 4) Else if content is "message", use "+" (current folder)
561 * 5) Else use string "%m%P.%s".
563 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
564 CI ci
= &ct
->c_ctinfo
;
566 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
568 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
572 if (! ct
->c_storage
) {
574 * Check the beginning of storage formatting string
575 * to see if we are saving content to a folder.
577 if (*cp
== '+' || *cp
== '@') {
578 char *tmpfilenam
, *folder
;
580 /* Store content in temporary file for now */
581 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
582 die("unable to create temporary file in %s",
585 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
587 /* Get the folder name */
589 folder
= pluspath (cp
);
591 folder
= getfolder (1);
593 /* Check if folder exists */
594 create_folder(m_mailpath(folder
), 0, exit
);
596 /* Record the folder name */
597 ct
->c_folder
= mh_xstrdup(folder
);
606 * Parse and expand the storage formatting string
607 * in `cp' into `buffer'.
609 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
612 * If formatting begins with '|' or '!', then pass
613 * content to standard input of a command and return.
615 if (buffer
[0] == '|' || buffer
[0] == '!')
616 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
618 /* record the filename */
619 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
624 /* The output filename was explicitly specified, so use it. */
625 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
632 /* flush the output stream */
635 /* Now save or append the content to a file */
636 if (output_content_file (ct
, appending
) == NOTOK
)
640 * If necessary, link the file into a folder and remove
641 * the temporary file. If this message is a partial,
642 * then only do this if it is the last one in the group.
644 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
645 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
646 (void) m_unlink (ct
->c_storage
);
653 * Now print out the name/number of the message
654 * that we are storing.
658 fprintf (stderr
, "reassembling partials ");
660 fputs(ct
->c_file
, stderr
);
662 fprintf (stderr
, "%s,", ct
->c_file
);
664 fprintf (stderr
, "storing message %s", ct
->c_file
);
666 fprintf (stderr
, " part %s", ct
->c_partno
);
670 * Unless we are in the "middle" of group of message/partials,
671 * we now print the name of the file, folder, and/or message
672 * to which we are storing the content.
674 if (!is_partial
|| last_partial
) {
676 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
678 } else if (!strcmp(ct
->c_storage
, "-")) {
679 fprintf (stderr
, " to stdout\n");
681 int cwdlen
= strlen (info
->cwd
);
683 fprintf (stderr
, " as file %s\n",
684 !has_prefix(ct
->c_storage
, info
->cwd
)
685 || ct
->c_storage
[cwdlen
] != '/'
686 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
696 * Output content to a file
700 output_content_file (CT ct
, int appending
)
703 char *file
, buffer
[BUFSIZ
];
708 * If the pathname is absolute, make sure
709 * all the relevant directories exist.
711 if (strchr(ct
->c_storage
, '/')
712 && make_intermediates (ct
->c_storage
) == NOTOK
)
715 if (ct
->c_encoding
!= CE_7BIT
) {
718 if (!ct
->c_ceopenfnx
) {
719 inform("don't know how to decode part %s of message %s",
720 ct
->c_partno
, ct
->c_file
);
724 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
726 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
728 if (!strcmp (file
, ct
->c_storage
)) {
729 (*ct
->c_ceclosefnx
) (ct
);
734 * Send to standard output
736 if (!strcmp (ct
->c_storage
, "-")) {
739 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
740 advise ("stdout", "unable to dup");
742 (*ct
->c_ceclosefnx
) (ct
);
745 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
746 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
747 appending
? "a" : "w");
755 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
756 advise (ct
->c_storage
, "unable to fopen for %s",
757 appending
? "appending" : "writing");
763 * Filter the header fields of the initial enclosing
764 * message/partial into the file.
766 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
767 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
769 if (pm
->pm_partno
== 1)
770 copy_some_headers (fp
, ct
);
774 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
776 advise (file
, "error reading content from");
783 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
784 advise ("output_content_file", "fwrite");
791 (*ct
->c_ceclosefnx
) (ct
);
793 if (cc
!= NOTOK
&& fflush (fp
))
794 advise (ct
->c_storage
, "error writing to");
798 return cc
== NOTOK
? NOTOK
: OK
;
801 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
802 advise (ct
->c_file
, "unable to open for reading");
808 fseek (ct
->c_fp
, pos
, SEEK_SET
);
810 if (!strcmp (ct
->c_storage
, "-")) {
813 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
814 advise ("stdout", "unable to dup");
817 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
818 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
819 appending
? "a" : "w");
824 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
825 advise (ct
->c_storage
, "unable to fopen for %s",
826 appending
? "appending" : "writing");
832 * Copy a few of the header fields of the initial
833 * enclosing message/partial into the file.
836 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
837 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
839 if (pm
->pm_partno
== 1) {
840 copy_some_headers (fp
, ct
);
845 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
846 if ((pos
+= strlen (buffer
)) > last
) {
849 diff
= strlen (buffer
) - (pos
- last
);
854 * If this is the first content of a group of
855 * message/partial contents, then we only copy a few
856 * of the header fields of the enclosed message.
871 if (!uprf (buffer
, XXX_FIELD_PRF
)
872 && !uprf (buffer
, VRSN_FIELD
)
873 && !uprf (buffer
, "Subject:")
874 && !uprf (buffer
, "Encrypted:")
875 && !uprf (buffer
, "Message-ID:")) {
890 advise (ct
->c_storage
, "error writing to");
900 * Add a file to a folder.
902 * Return the new message number of the file
903 * when added to the folder. Return -1, if
908 output_content_folder (char *folder
, char *filename
)
913 /* Read the folder. */
914 if (!(mp
= folder_read(folder
, 0))) {
915 inform("unable to read folder %s", folder
);
918 /* Link file into folder */
919 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
921 /* free folder structure */
925 * Return msgnum. We are relying on the fact that
926 * msgnum will be -1, if folder_addmsg() had an error.
933 * Parse and expand the storage formatting string
934 * pointed to by "cp" into "buffer".
938 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
942 CI ci
= &ct
->c_ctinfo
;
945 * If storage string is "-", just copy it, and
946 * return (send content to standard output).
948 if (cp
[0] == '-' && cp
[1] == '\0') {
949 strncpy (buffer
, cp
, buflen
);
957 * If formatting string is a pathname that doesn't
958 * begin with '/', then preface the path with the
959 * appropriate directory.
961 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
962 if (!strcmp(dir
, "/"))
963 dir
= ""; /* Don't start with "//". */
964 snprintf (bp
, buflen
, "%s/", dir
);
972 /* We are processing a storage escape */
977 * Insert parameters from Content-Type.
978 * This is only valid for '|' commands.
980 if (buffer
[0] != '|' && buffer
[0] != '!') {
990 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
991 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
992 pm
->pm_name
, get_param_value(pm
, '?'));
1002 /* insert message number */
1003 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1007 /* insert part number with leading dot */
1009 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1013 /* insert part number without leading dot */
1015 strncpy (bp
, ct
->c_partno
, buflen
);
1019 /* insert content type */
1020 strncpy (bp
, ci
->ci_type
, buflen
);
1024 /* insert content subtype */
1025 strncpy (bp
, ci
->ci_subtype
, buflen
);
1029 /* insert the character % */
1039 /* Advance bp and decrement buflen */
1057 * Check if the content specifies a filename
1058 * in its MIME parameters.
1062 get_storeproc (CT ct
)
1068 * If the storeproc has already been defined,
1069 * we just return (for instance, if this content
1070 * is part of a "message/external".
1072 if (ct
->c_storeproc
)
1076 * If there's a Content-Disposition header and it has a filename,
1077 * use that (RFC-2183).
1080 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1081 use_param_as_filename(cp
)) {
1082 ct
->c_storeproc
= mh_xstrdup(cp
);
1090 * Check the attribute/value pairs, for the attribute "name".
1091 * If found, do a few sanity checks and copy the value into
1095 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1096 use_param_as_filename(cp
)) {
1097 ct
->c_storeproc
= mh_xstrdup(cp
);
1105 * Copy some of the header fields of the initial message/partial
1106 * message into the header of the reassembled message.
1110 copy_some_headers (FILE *out
, CT ct
)
1114 hp
= ct
->c_first_hf
; /* start at first header field */
1118 * A few of the header fields of the enclosing
1119 * messages are not copied.
1121 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1122 && strcasecmp (hp
->name
, VRSN_FIELD
)
1123 && strcasecmp (hp
->name
, "Subject")
1124 && strcasecmp (hp
->name
, "Encrypted")
1125 && strcasecmp (hp
->name
, "Message-ID"))
1126 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1127 hp
= hp
->next
; /* next header field */
1133 /******************************************************************************/
1134 /* -clobber support */
1136 static enum clobber_policy_t
1137 clobber_policy (const char *value
)
1139 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1140 return NMH_CLOBBER_ALWAYS
;
1142 if (! strcasecmp (value
, "auto")) {
1143 return NMH_CLOBBER_AUTO
;
1145 if (! strcasecmp (value
, "suffix")) {
1146 return NMH_CLOBBER_SUFFIX
;
1148 if (! strcasecmp (value
, "ask")) {
1149 return NMH_CLOBBER_ASK
;
1151 if (! strcasecmp (value
, "never")) {
1152 return NMH_CLOBBER_NEVER
;
1155 die("invalid argument, %s, to clobber", value
);
1160 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1162 const size_t max_versions
= 1000000;
1163 /* 8 = log max_versions + one for - or . + one for null terminator */
1164 const size_t buflen
= strlen (file
) + 8;
1165 char *buffer
= mh_xmalloc (buflen
);
1168 char *extension
= NULL
;
1169 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1170 ((extension
= strrchr (file
, '.')) != NULL
)) {
1171 *extension
++ = '\0';
1174 for (version
= 1; version
< max_versions
; ++version
) {
1177 switch (clobber_policy
) {
1178 case NMH_CLOBBER_AUTO
: {
1179 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1180 extension
== NULL
? "" : ".",
1181 extension
== NULL
? "" : extension
);
1185 case NMH_CLOBBER_SUFFIX
:
1186 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1190 /* Should never get here. */
1191 inform("will not overwrite %s, invalid clobber policy", buffer
);
1196 /* Actually (try to) create the file here to avoid a race
1197 condition on file naming + creation. This won't solve the
1198 problem with old NFS that doesn't support O_EXCL, though.
1199 Let the umask strip off permissions from 0666 as desired.
1200 That's what fopen () would do if it was creating the file. */
1201 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1202 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1203 S_IROTH
| S_IWOTH
)) >= 0) {
1211 if (version
>= max_versions
) {
1212 inform("will not overwrite %s, too many versions", buffer
);
1222 clobber_check (char *original_file
, mhstoreinfo_t info
)
1224 /* clobber policy return value
1225 * -------------- ------------
1226 * -always original_file
1227 * -auto original_file-<digits>.extension
1228 * -suffix original_file.<digits>
1229 * -ask original_file, 0, or another filename/path
1237 if (! strcmp (original_file
, "-")) {
1238 return original_file
;
1241 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1242 /* Save cwd for possible use in loop below. */
1245 cwd
= mh_xstrdup(original_file
);
1246 slash
= strrchr (cwd
, '/');
1251 /* original_file isn't a full path, which should only happen if
1261 file
= original_file
;
1262 check_again
= false;
1264 switch (info
->clobber_policy
) {
1265 case NMH_CLOBBER_ALWAYS
:
1268 case NMH_CLOBBER_SUFFIX
:
1269 case NMH_CLOBBER_AUTO
:
1270 if (stat (file
, &st
) == OK
) {
1271 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1273 ++info
->files_not_clobbered
;
1278 case NMH_CLOBBER_ASK
:
1279 if (stat (file
, &st
) == OK
) {
1280 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1281 static struct swit answer
[4] = {
1282 { "yes", 0, NMH_YES
},
1283 { "no", 0, NMH_NO
},
1284 { "rename", 0, NMH_RENAME
},
1288 if (isatty (fileno (stdin
))) {
1290 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1291 ans
= read_switch_multiword (prompt
, answer
);
1294 /* Overwrite, that's what nmh used to do. And warn. */
1295 inform("-clobber ask but no tty, so overwrite %s", file
);
1299 switch ((enum answers
) smatch (*ans
, answer
)) {
1305 ++info
->files_not_clobbered
;
1309 fputs("Enter filename or full path of the new file: ", stdout
);
1310 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1313 ++info
->files_not_clobbered
;
1315 trim_suffix_c(buf
, '\n');
1320 if (buf
[0] == '/') {
1321 /* Full path, use it. */
1322 file
= mh_xstrdup(buf
);
1324 /* Relative path. */
1325 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1335 case NMH_CLOBBER_NEVER
:
1336 if (stat (file
, &st
) == OK
) {
1337 /* Keep count of files that would have been clobbered,
1338 and return that as process exit status. */
1339 inform("will not overwrite %s with -clobber never", file
);
1342 ++info
->files_not_clobbered
;
1347 original_file
= file
;
1348 } while (check_again
);
1356 use_param_as_filename(const char *p
)
1358 /* Preserve result of original test that considered an empty string
1360 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1363 /* -clobber support */
1364 /******************************************************************************/