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.
13 #include <h/fmt_scan.h>
15 #include <h/mhparse.h>
18 #include "mhshowsbr.h"
19 #include "sbr/m_maildir.h"
20 #include "sbr/m_mktemp.h"
22 enum clobber_policy_t
{
23 NMH_CLOBBER_ALWAYS
= 0,
30 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
33 CT
*cts
; /* Top-level list of contents to store. */
34 char *cwd
; /* cached current directory */
35 int autosw
; /* -auto enabled */
36 int verbosw
; /* -verbose enabled */
37 int files_not_clobbered
; /* output flag indicating that store failed
38 in order to not clobber an existing file */
40 /* The following must never be touched by a caller: they are for
41 internal use by the mhstoresbr functions. */
42 char *dir
; /* directory in which to store contents */
43 enum clobber_policy_t clobber_policy
; /* -clobber selection */
46 static bool use_param_as_filename(const char *p
);
49 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
58 info
->files_not_clobbered
= 0;
60 info
->clobber_policy
= clobber_policy (csw
);
66 mhstoreinfo_free (mhstoreinfo_t info
)
74 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
76 return info
->files_not_clobbered
;
81 * Type for a compare function for qsort. This keeps
84 typedef int (*qsort_comp
) (const void *, const void *);
90 static void store_single_message (CT
, mhstoreinfo_t
);
91 static int store_switch (CT
, mhstoreinfo_t
);
92 static int store_generic (CT
, mhstoreinfo_t
);
93 static int store_application (CT
, mhstoreinfo_t
);
94 static int store_multi (CT
, mhstoreinfo_t
);
95 static int store_partial (CT
, mhstoreinfo_t
);
96 static int store_external (CT
, mhstoreinfo_t
);
97 static int ct_compar (CT
*, CT
*);
98 static int store_content (CT
, CT
, mhstoreinfo_t
);
99 static int output_content_file (CT
, int);
100 static int output_content_folder (char *, char *);
101 static int parse_format_string (CT
, char *, char *, int, char *);
102 static void get_storeproc (CT
);
103 static int copy_some_headers (FILE *, CT
);
104 static char *clobber_check (char *, mhstoreinfo_t
);
107 * Main entry point to store content
108 * from a collection of messages.
112 store_all_messages (mhstoreinfo_t info
)
118 * Check for the directory in which to
119 * store any contents.
121 if ((cp
= context_find (nmhstorage
)) && *cp
)
122 info
->dir
= mh_xstrdup(cp
);
124 info
->dir
= getcpy (info
->cwd
);
126 for (ctp
= info
->cts
; *ctp
; ctp
++) {
128 store_single_message (ct
, info
);
136 * Entry point to store the content
137 * in a (single) message
141 store_single_message (CT ct
, mhstoreinfo_t info
)
143 if (type_ok (ct
, 1)) {
145 store_switch (ct
, info
);
150 if (ct
->c_ceclosefnx
)
151 (*ct
->c_ceclosefnx
) (ct
);
157 * Switching routine to store different content types
161 store_switch (CT ct
, mhstoreinfo_t info
)
163 switch (ct
->c_type
) {
165 return store_multi (ct
, info
);
168 switch (ct
->c_subtype
) {
169 case MESSAGE_PARTIAL
:
170 return store_partial (ct
, info
);
172 case MESSAGE_EXTERNAL
:
173 return store_external (ct
, info
);
177 return store_generic (ct
, info
);
182 return store_application (ct
, info
);
188 return store_generic (ct
, info
);
191 return OK
; /* NOT REACHED */
196 * Generic routine to store a MIME content.
197 * (audio, video, image, text, message/rfc822)
201 store_generic (CT ct
, mhstoreinfo_t info
)
204 * Check if the content specifies a filename.
205 * Don't bother with this for type "message"
206 * (only "message/rfc822" will use store_generic).
208 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
211 return store_content (ct
, NULL
, info
);
216 * Store content of type "application"
220 store_application (CT ct
, mhstoreinfo_t info
)
222 CI ci
= &ct
->c_ctinfo
;
224 /* Check if the content specifies a filename */
229 * If storeproc is not defined, and the content is type
230 * "application/octet-stream", we also check for various
231 * attribute/value pairs which specify if this a tar file.
233 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
239 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
240 if (strcasecmp (cp
, "tar") == 0)
244 /* check for "conversions=compress" attribute */
245 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
246 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
247 if (strcasecmp (cp
, "compress") == 0 ||
248 strcasecmp (cp
, "x-compress") == 0) {
251 if (strcasecmp (cp
, "gzip") == 0 ||
252 strcasecmp (cp
, "x-gzip") == 0) {
258 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
259 : (gzP
? "%egzip -dc | tar tvf -"
260 : "%etar tvf -"), NULL
);
261 if (!ct
->c_storeproc
) {
263 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
264 : (gzP
? "| gzip -dc | tar xvpf -"
265 : "| tar xvpf -"), NULL
);
268 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
269 : (gzP
? "%m%P.tar.gz"
270 : "%m%P.tar"), NULL
);
276 return store_content (ct
, NULL
, info
);
281 * Store the content of a multipart message
285 store_multi (CT ct
, mhstoreinfo_t info
)
288 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
292 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
293 CT p
= part
->mp_part
;
295 if (part_ok (p
) && type_ok (p
, 1)) {
297 /* Support mhstore -outfile. The MIME parser doesn't
298 load c_storage, so we know that p->c_storage is
300 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
302 result
= store_switch (p
, info
);
304 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
314 * Reassemble and store the contents of a collection
315 * of messages of type "message/partial".
319 store_partial (CT ct
, mhstoreinfo_t info
)
324 struct partial
*pm
, *qm
;
326 qm
= (struct partial
*) ct
->c_ctparams
;
331 for (ctp
= info
->cts
; *ctp
; ctp
++) {
333 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
334 pm
= (struct partial
*) p
->c_ctparams
;
336 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
337 pm
->pm_marked
= pm
->pm_partno
;
349 inform("missing (at least) last part of multipart message");
353 base
= mh_xcalloc(i
+ 1, sizeof *base
);
355 for (ctp
= info
->cts
; *ctp
; ctp
++) {
357 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
358 pm
= (struct partial
*) p
->c_ctparams
;
366 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
369 for (ctq
= base
; *ctq
; ctq
++) {
371 pm
= (struct partial
*) p
->c_ctparams
;
372 if (pm
->pm_marked
== cur
) {
377 if (pm
->pm_marked
== cur
- 1) {
378 inform("duplicate part %d of %d part multipart message, continuing...",
384 inform("missing %spart %d of %d part multipart message",
385 cur
!= hi
? "(at least) " : "", cur
, hi
);
394 * Now cycle through the sorted list of messages of type
395 * "message/partial" and save/append them to a file.
400 if (store_content (ct
, NULL
, info
) == NOTOK
) {
406 for (; *ctq
; ctq
++) {
408 if (store_content (p
, ct
, info
) == NOTOK
)
418 * Store content from a message of type "message/external".
422 store_external (CT ct
, mhstoreinfo_t info
)
425 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
426 CT p
= e
->eb_content
;
432 * Check if the parameters for the external body
433 * specified a filename.
438 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
439 if (!ct
->c_storeproc
)
440 ct
->c_storeproc
= mh_xstrdup(cp
);
442 p
->c_storeproc
= mh_xstrdup(cp
);
447 * Since we will let the Content structure for the
448 * external body substitute for the current content,
449 * we temporarily change its partno (number inside
450 * multipart), so everything looks right.
452 p
->c_partno
= ct
->c_partno
;
454 /* we probably need to check if content is really there */
456 /* Support mhstore -outfile. The MIME parser doesn't load
457 c_storage, so we know that p->c_storage is NULL here. */
458 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
460 result
= store_switch (p
, info
);
468 * Compare the numbering from two different
469 * message/partials (needed for sorting).
473 ct_compar (CT
*a
, CT
*b
)
475 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
476 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
478 return am
->pm_marked
- bm
->pm_marked
;
483 * Store contents of a message or message part to
484 * a folder, a file, the standard output, or pass
485 * the contents to a command.
487 * If the current content to be saved is a followup part
488 * to a collection of messages of type "message/partial",
489 * then field "p" is a pointer to the Content structure
490 * to the first message/partial in the group.
494 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
496 bool appending
= false;
498 bool is_partial
= false;
499 bool first_partial
= false;
500 bool last_partial
= false;
501 char *cp
, buffer
[BUFSIZ
];
504 * Do special processing for messages of
505 * type "message/partial".
507 * We first check if this content is of type
508 * "message/partial". If it is, then we need to check
509 * whether it is the first and/or last in the group.
511 * Then if "p" is a valid pointer, it points to the Content
512 * structure of the first partial in the group. So we copy
513 * the file name and/or folder name from that message. In
514 * this case, we also note that we will be appending.
516 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
517 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
519 /* Yep, it's a message/partial */
522 /* But is it the first and/or last in the collection? */
523 if (pm
->pm_partno
== 1)
524 first_partial
= true;
525 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
529 * If "p" is a valid pointer, then it points to the
530 * Content structure for the first message in the group.
531 * So we just copy the filename or foldername information
532 * from the previous iteration of this function.
536 if (! ct
->c_storage
) {
537 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
539 /* record the folder name */
541 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
549 * Get storage formatting string.
551 * 1) If we have storeproc defined, then use that
552 * 2) Else check for a mhn-store-<type>/<subtype> entry
553 * 3) Else check for a mhn-store-<type> entry
554 * 4) Else if content is "message", use "+" (current folder)
555 * 5) Else use string "%m%P.%s".
557 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
558 CI ci
= &ct
->c_ctinfo
;
560 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
562 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
566 if (! ct
->c_storage
) {
568 * Check the beginning of storage formatting string
569 * to see if we are saving content to a folder.
571 if (*cp
== '+' || *cp
== '@') {
572 char *tmpfilenam
, *folder
;
574 /* Store content in temporary file for now */
575 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
576 die("unable to create temporary file in %s",
579 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
581 /* Get the folder name */
583 folder
= pluspath (cp
);
585 folder
= getfolder (1);
587 /* Check if folder exists */
588 create_folder(m_mailpath(folder
), 0, exit
);
590 /* Record the folder name */
591 ct
->c_folder
= mh_xstrdup(folder
);
600 * Parse and expand the storage formatting string
601 * in `cp' into `buffer'.
603 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
606 * If formatting begins with '|' or '!', then pass
607 * content to standard input of a command and return.
609 if (buffer
[0] == '|' || buffer
[0] == '!')
610 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
612 /* record the filename */
613 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
618 /* The output filename was explicitly specified, so use it. */
619 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
626 /* flush the output stream */
629 /* Now save or append the content to a file */
630 if (output_content_file (ct
, appending
) == NOTOK
)
634 * If necessary, link the file into a folder and remove
635 * the temporary file. If this message is a partial,
636 * then only do this if it is the last one in the group.
638 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
639 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
640 (void) m_unlink (ct
->c_storage
);
647 * Now print out the name/number of the message
648 * that we are storing.
652 fprintf (stderr
, "reassembling partials ");
654 fputs(ct
->c_file
, stderr
);
656 fprintf (stderr
, "%s,", ct
->c_file
);
658 fprintf (stderr
, "storing message %s", ct
->c_file
);
660 fprintf (stderr
, " part %s", ct
->c_partno
);
664 * Unless we are in the "middle" of group of message/partials,
665 * we now print the name of the file, folder, and/or message
666 * to which we are storing the content.
668 if (!is_partial
|| last_partial
) {
670 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
672 } else if (!strcmp(ct
->c_storage
, "-")) {
673 fprintf (stderr
, " to stdout\n");
675 int cwdlen
= strlen (info
->cwd
);
677 fprintf (stderr
, " as file %s\n",
678 !has_prefix(ct
->c_storage
, info
->cwd
)
679 || ct
->c_storage
[cwdlen
] != '/'
680 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
690 * Output content to a file
694 output_content_file (CT ct
, int appending
)
697 char *file
, buffer
[BUFSIZ
];
702 * If the pathname is absolute, make sure
703 * all the relevant directories exist.
705 if (strchr(ct
->c_storage
, '/')
706 && make_intermediates (ct
->c_storage
) == NOTOK
)
709 if (ct
->c_encoding
!= CE_7BIT
) {
712 if (!ct
->c_ceopenfnx
) {
713 inform("don't know how to decode part %s of message %s",
714 ct
->c_partno
, ct
->c_file
);
718 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
720 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
722 if (!strcmp (file
, ct
->c_storage
)) {
723 (*ct
->c_ceclosefnx
) (ct
);
728 * Send to standard output
730 if (!strcmp (ct
->c_storage
, "-")) {
733 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
734 advise ("stdout", "unable to dup");
736 (*ct
->c_ceclosefnx
) (ct
);
739 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
740 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
741 appending
? "a" : "w");
749 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
750 advise (ct
->c_storage
, "unable to fopen for %s",
751 appending
? "appending" : "writing");
757 * Filter the header fields of the initial enclosing
758 * message/partial into the file.
760 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
761 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
763 if (pm
->pm_partno
== 1)
764 copy_some_headers (fp
, ct
);
768 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
770 advise (file
, "error reading content from");
777 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
778 advise ("output_content_file", "fwrite");
785 (*ct
->c_ceclosefnx
) (ct
);
787 if (cc
!= NOTOK
&& fflush (fp
))
788 advise (ct
->c_storage
, "error writing to");
792 return cc
== NOTOK
? NOTOK
: OK
;
795 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
796 advise (ct
->c_file
, "unable to open for reading");
802 fseek (ct
->c_fp
, pos
, SEEK_SET
);
804 if (!strcmp (ct
->c_storage
, "-")) {
807 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
808 advise ("stdout", "unable to dup");
811 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
812 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
813 appending
? "a" : "w");
818 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
819 advise (ct
->c_storage
, "unable to fopen for %s",
820 appending
? "appending" : "writing");
826 * Copy a few of the header fields of the initial
827 * enclosing message/partial into the file.
830 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
831 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
833 if (pm
->pm_partno
== 1) {
834 copy_some_headers (fp
, ct
);
839 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
840 if ((pos
+= strlen (buffer
)) > last
) {
843 diff
= strlen (buffer
) - (pos
- last
);
848 * If this is the first content of a group of
849 * message/partial contents, then we only copy a few
850 * of the header fields of the enclosed message.
865 if (!uprf (buffer
, XXX_FIELD_PRF
)
866 && !uprf (buffer
, VRSN_FIELD
)
867 && !uprf (buffer
, "Subject:")
868 && !uprf (buffer
, "Encrypted:")
869 && !uprf (buffer
, "Message-ID:")) {
884 advise (ct
->c_storage
, "error writing to");
894 * Add a file to a folder.
896 * Return the new message number of the file
897 * when added to the folder. Return -1, if
902 output_content_folder (char *folder
, char *filename
)
907 /* Read the folder. */
908 if (!(mp
= folder_read(folder
, 0))) {
909 inform("unable to read folder %s", folder
);
912 /* Link file into folder */
913 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
915 /* free folder structure */
919 * Return msgnum. We are relying on the fact that
920 * msgnum will be -1, if folder_addmsg() had an error.
927 * Parse and expand the storage formatting string
928 * pointed to by "cp" into "buffer".
932 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
936 CI ci
= &ct
->c_ctinfo
;
939 * If storage string is "-", just copy it, and
940 * return (send content to standard output).
942 if (cp
[0] == '-' && cp
[1] == '\0') {
943 strncpy (buffer
, cp
, buflen
);
951 * If formatting string is a pathname that doesn't
952 * begin with '/', then preface the path with the
953 * appropriate directory.
955 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
956 if (!strcmp(dir
, "/"))
957 dir
= ""; /* Don't start with "//". */
958 snprintf (bp
, buflen
, "%s/", dir
);
966 /* We are processing a storage escape */
971 * Insert parameters from Content-Type.
972 * This is only valid for '|' commands.
974 if (buffer
[0] != '|' && buffer
[0] != '!') {
984 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
985 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
986 pm
->pm_name
, get_param_value(pm
, '?'));
996 /* insert message number */
997 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1001 /* insert part number with leading dot */
1003 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1007 /* insert part number without leading dot */
1009 strncpy (bp
, ct
->c_partno
, buflen
);
1013 /* insert content type */
1014 strncpy (bp
, ci
->ci_type
, buflen
);
1018 /* insert content subtype */
1019 strncpy (bp
, ci
->ci_subtype
, buflen
);
1023 /* insert the character % */
1033 /* Advance bp and decrement buflen */
1051 * Check if the content specifies a filename
1052 * in its MIME parameters.
1056 get_storeproc (CT ct
)
1062 * If the storeproc has already been defined,
1063 * we just return (for instance, if this content
1064 * is part of a "message/external".
1066 if (ct
->c_storeproc
)
1070 * If there's a Content-Disposition header and it has a filename,
1071 * use that (RFC-2183).
1074 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1075 use_param_as_filename(cp
)) {
1076 ct
->c_storeproc
= mh_xstrdup(cp
);
1084 * Check the attribute/value pairs, for the attribute "name".
1085 * If found, do a few sanity checks and copy the value into
1089 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1090 use_param_as_filename(cp
)) {
1091 ct
->c_storeproc
= mh_xstrdup(cp
);
1099 * Copy some of the header fields of the initial message/partial
1100 * message into the header of the reassembled message.
1104 copy_some_headers (FILE *out
, CT ct
)
1108 hp
= ct
->c_first_hf
; /* start at first header field */
1112 * A few of the header fields of the enclosing
1113 * messages are not copied.
1115 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1116 && strcasecmp (hp
->name
, VRSN_FIELD
)
1117 && strcasecmp (hp
->name
, "Subject")
1118 && strcasecmp (hp
->name
, "Encrypted")
1119 && strcasecmp (hp
->name
, "Message-ID"))
1120 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1121 hp
= hp
->next
; /* next header field */
1127 /******************************************************************************/
1128 /* -clobber support */
1130 static enum clobber_policy_t
1131 clobber_policy (const char *value
)
1133 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1134 return NMH_CLOBBER_ALWAYS
;
1136 if (! strcasecmp (value
, "auto")) {
1137 return NMH_CLOBBER_AUTO
;
1139 if (! strcasecmp (value
, "suffix")) {
1140 return NMH_CLOBBER_SUFFIX
;
1142 if (! strcasecmp (value
, "ask")) {
1143 return NMH_CLOBBER_ASK
;
1145 if (! strcasecmp (value
, "never")) {
1146 return NMH_CLOBBER_NEVER
;
1149 die("invalid argument, %s, to clobber", value
);
1154 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1156 const size_t max_versions
= 1000000;
1157 /* 8 = log max_versions + one for - or . + one for null terminator */
1158 const size_t buflen
= strlen (file
) + 8;
1159 char *buffer
= mh_xmalloc (buflen
);
1162 char *extension
= NULL
;
1163 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1164 ((extension
= strrchr (file
, '.')) != NULL
)) {
1165 *extension
++ = '\0';
1168 for (version
= 1; version
< max_versions
; ++version
) {
1171 switch (clobber_policy
) {
1172 case NMH_CLOBBER_AUTO
: {
1173 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1174 extension
== NULL
? "" : ".",
1175 extension
== NULL
? "" : extension
);
1179 case NMH_CLOBBER_SUFFIX
:
1180 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1184 /* Should never get here. */
1185 inform("will not overwrite %s, invalid clobber policy", buffer
);
1190 /* Actually (try to) create the file here to avoid a race
1191 condition on file naming + creation. This won't solve the
1192 problem with old NFS that doesn't support O_EXCL, though.
1193 Let the umask strip off permissions from 0666 as desired.
1194 That's what fopen () would do if it was creating the file. */
1195 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1196 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1197 S_IROTH
| S_IWOTH
)) >= 0) {
1205 if (version
>= max_versions
) {
1206 inform("will not overwrite %s, too many versions", buffer
);
1216 clobber_check (char *original_file
, mhstoreinfo_t info
)
1218 /* clobber policy return value
1219 * -------------- ------------
1220 * -always original_file
1221 * -auto original_file-<digits>.extension
1222 * -suffix original_file.<digits>
1223 * -ask original_file, 0, or another filename/path
1231 if (! strcmp (original_file
, "-")) {
1232 return original_file
;
1235 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1236 /* Save cwd for possible use in loop below. */
1239 cwd
= mh_xstrdup(original_file
);
1240 slash
= strrchr (cwd
, '/');
1245 /* original_file isn't a full path, which should only happen if
1255 file
= original_file
;
1256 check_again
= false;
1258 switch (info
->clobber_policy
) {
1259 case NMH_CLOBBER_ALWAYS
:
1262 case NMH_CLOBBER_SUFFIX
:
1263 case NMH_CLOBBER_AUTO
:
1264 if (stat (file
, &st
) == OK
) {
1265 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1267 ++info
->files_not_clobbered
;
1272 case NMH_CLOBBER_ASK
:
1273 if (stat (file
, &st
) == OK
) {
1274 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1275 static struct swit answer
[4] = {
1276 { "yes", 0, NMH_YES
},
1277 { "no", 0, NMH_NO
},
1278 { "rename", 0, NMH_RENAME
},
1282 if (isatty (fileno (stdin
))) {
1284 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1285 ans
= read_switch_multiword (prompt
, answer
);
1288 /* Overwrite, that's what nmh used to do. And warn. */
1289 inform("-clobber ask but no tty, so overwrite %s", file
);
1293 switch ((enum answers
) smatch (*ans
, answer
)) {
1299 ++info
->files_not_clobbered
;
1303 fputs("Enter filename or full path of the new file: ", stdout
);
1304 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1307 ++info
->files_not_clobbered
;
1309 trim_suffix_c(buf
, '\n');
1314 if (buf
[0] == '/') {
1315 /* Full path, use it. */
1316 file
= mh_xstrdup(buf
);
1318 /* Relative path. */
1319 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1329 case NMH_CLOBBER_NEVER
:
1330 if (stat (file
, &st
) == OK
) {
1331 /* Keep count of files that would have been clobbered,
1332 and return that as process exit status. */
1333 inform("will not overwrite %s with -clobber never", file
);
1336 ++info
->files_not_clobbered
;
1341 original_file
= file
;
1342 } while (check_again
);
1350 use_param_as_filename(const char *p
)
1352 /* Preserve result of original test that considered an empty string
1354 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1357 /* -clobber support */
1358 /******************************************************************************/