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.
10 #include "sbr/error.h"
15 #include "h/fmt_scan.h"
17 #include "h/mhparse.h"
20 #include "mhshowsbr.h"
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_mktemp.h"
24 enum clobber_policy_t
{
25 NMH_CLOBBER_ALWAYS
= 0,
32 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
35 CT
*cts
; /* Top-level list of contents to store. */
36 char *cwd
; /* cached current directory */
37 int autosw
; /* -auto enabled */
38 int verbosw
; /* -verbose enabled */
39 int files_not_clobbered
; /* output flag indicating that store failed
40 in order to not clobber an existing file */
42 /* The following must never be touched by a caller: they are for
43 internal use by the mhstoresbr functions. */
44 char *dir
; /* directory in which to store contents */
45 enum clobber_policy_t clobber_policy
; /* -clobber selection */
48 static bool use_param_as_filename(const char *p
);
51 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
60 info
->files_not_clobbered
= 0;
62 info
->clobber_policy
= clobber_policy (csw
);
68 mhstoreinfo_free (mhstoreinfo_t info
)
76 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
78 return info
->files_not_clobbered
;
83 * Type for a compare function for qsort. This keeps
86 typedef int (*qsort_comp
) (const void *, const void *);
92 static void store_single_message (CT
, mhstoreinfo_t
);
93 static int store_switch (CT
, mhstoreinfo_t
);
94 static int store_generic (CT
, mhstoreinfo_t
);
95 static int store_application (CT
, mhstoreinfo_t
);
96 static int store_multi (CT
, mhstoreinfo_t
);
97 static int store_partial (CT
, mhstoreinfo_t
);
98 static int store_external (CT
, mhstoreinfo_t
);
99 static int ct_compar (CT
*, CT
*);
100 static int store_content (CT
, CT
, mhstoreinfo_t
);
101 static int output_content_file (CT
, int);
102 static int output_content_folder (char *, char *);
103 static int parse_format_string (CT
, char *, char *, int, char *);
104 static void get_storeproc (CT
);
105 static int copy_some_headers (FILE *, CT
);
106 static char *clobber_check (char *, mhstoreinfo_t
);
109 * Main entry point to store content
110 * from a collection of messages.
114 store_all_messages (mhstoreinfo_t info
)
120 * Check for the directory in which to
121 * store any contents.
123 if ((cp
= context_find (nmhstorage
)) && *cp
)
124 info
->dir
= mh_xstrdup(cp
);
126 info
->dir
= getcpy (info
->cwd
);
128 for (ctp
= info
->cts
; *ctp
; ctp
++) {
130 store_single_message (ct
, info
);
138 * Entry point to store the content
139 * in a (single) message
143 store_single_message (CT ct
, mhstoreinfo_t info
)
145 if (type_ok (ct
, 1)) {
147 store_switch (ct
, info
);
152 if (ct
->c_ceclosefnx
)
153 (*ct
->c_ceclosefnx
) (ct
);
159 * Switching routine to store different content types
163 store_switch (CT ct
, mhstoreinfo_t info
)
165 switch (ct
->c_type
) {
167 return store_multi (ct
, info
);
170 switch (ct
->c_subtype
) {
171 case MESSAGE_PARTIAL
:
172 return store_partial (ct
, info
);
174 case MESSAGE_EXTERNAL
:
175 return store_external (ct
, info
);
179 return store_generic (ct
, info
);
184 return store_application (ct
, info
);
190 return store_generic (ct
, info
);
193 return OK
; /* NOT REACHED */
198 * Generic routine to store a MIME content.
199 * (audio, video, image, text, message/rfc822)
203 store_generic (CT ct
, mhstoreinfo_t info
)
206 * Check if the content specifies a filename.
207 * Don't bother with this for type "message"
208 * (only "message/rfc822" will use store_generic).
210 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
213 return store_content (ct
, NULL
, info
);
218 * Store content of type "application"
222 store_application (CT ct
, mhstoreinfo_t info
)
224 CI ci
= &ct
->c_ctinfo
;
226 /* Check if the content specifies a filename */
231 * If storeproc is not defined, and the content is type
232 * "application/octet-stream", we also check for various
233 * attribute/value pairs which specify if this a tar file.
235 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
241 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
242 if (strcasecmp (cp
, "tar") == 0)
246 /* check for "conversions=compress" attribute */
247 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
248 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
249 if (strcasecmp (cp
, "compress") == 0 ||
250 strcasecmp (cp
, "x-compress") == 0) {
253 if (strcasecmp (cp
, "gzip") == 0 ||
254 strcasecmp (cp
, "x-gzip") == 0) {
260 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
261 : (gzP
? "%egzip -dc | tar tvf -"
262 : "%etar tvf -"), NULL
);
263 if (!ct
->c_storeproc
) {
265 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
266 : (gzP
? "| gzip -dc | tar xvpf -"
267 : "| tar xvpf -"), NULL
);
270 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
271 : (gzP
? "%m%P.tar.gz"
272 : "%m%P.tar"), NULL
);
278 return store_content (ct
, NULL
, info
);
283 * Store the content of a multipart message
287 store_multi (CT ct
, mhstoreinfo_t info
)
290 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
294 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
295 CT p
= part
->mp_part
;
297 if (part_ok (p
) && type_ok (p
, 1)) {
299 /* Support mhstore -outfile. The MIME parser doesn't
300 load c_storage, so we know that p->c_storage is
302 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
304 result
= store_switch (p
, info
);
306 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
316 * Reassemble and store the contents of a collection
317 * of messages of type "message/partial".
321 store_partial (CT ct
, mhstoreinfo_t info
)
326 struct partial
*pm
, *qm
;
328 qm
= (struct partial
*) ct
->c_ctparams
;
333 for (ctp
= info
->cts
; *ctp
; ctp
++) {
335 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
336 pm
= (struct partial
*) p
->c_ctparams
;
338 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
339 pm
->pm_marked
= pm
->pm_partno
;
351 inform("missing (at least) last part of multipart message");
355 base
= mh_xcalloc(i
+ 1, sizeof *base
);
357 for (ctp
= info
->cts
; *ctp
; ctp
++) {
359 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
360 pm
= (struct partial
*) p
->c_ctparams
;
368 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
371 for (ctq
= base
; *ctq
; ctq
++) {
373 pm
= (struct partial
*) p
->c_ctparams
;
374 if (pm
->pm_marked
== cur
) {
379 if (pm
->pm_marked
== cur
- 1) {
380 inform("duplicate part %d of %d part multipart message, continuing...",
386 inform("missing %spart %d of %d part multipart message",
387 cur
!= hi
? "(at least) " : "", cur
, hi
);
396 * Now cycle through the sorted list of messages of type
397 * "message/partial" and save/append them to a file.
402 if (store_content (ct
, NULL
, info
) == NOTOK
) {
408 for (; *ctq
; ctq
++) {
410 if (store_content (p
, ct
, info
) == NOTOK
)
420 * Store content from a message of type "message/external".
424 store_external (CT ct
, mhstoreinfo_t info
)
427 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
428 CT p
= e
->eb_content
;
434 * Check if the parameters for the external body
435 * specified a filename.
440 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
441 if (!ct
->c_storeproc
)
442 ct
->c_storeproc
= mh_xstrdup(cp
);
444 p
->c_storeproc
= mh_xstrdup(cp
);
449 * Since we will let the Content structure for the
450 * external body substitute for the current content,
451 * we temporarily change its partno (number inside
452 * multipart), so everything looks right.
454 p
->c_partno
= ct
->c_partno
;
456 /* we probably need to check if content is really there */
458 /* Support mhstore -outfile. The MIME parser doesn't load
459 c_storage, so we know that p->c_storage is NULL here. */
460 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
462 result
= store_switch (p
, info
);
470 * Compare the numbering from two different
471 * message/partials (needed for sorting).
475 ct_compar (CT
*a
, CT
*b
)
477 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
478 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
480 return am
->pm_marked
- bm
->pm_marked
;
485 * Store contents of a message or message part to
486 * a folder, a file, the standard output, or pass
487 * the contents to a command.
489 * If the current content to be saved is a followup part
490 * to a collection of messages of type "message/partial",
491 * then field "p" is a pointer to the Content structure
492 * to the first message/partial in the group.
496 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
498 bool appending
= false;
500 bool is_partial
= false;
501 bool first_partial
= false;
502 bool last_partial
= false;
503 char *cp
, buffer
[BUFSIZ
];
506 * Do special processing for messages of
507 * type "message/partial".
509 * We first check if this content is of type
510 * "message/partial". If it is, then we need to check
511 * whether it is the first and/or last in the group.
513 * Then if "p" is a valid pointer, it points to the Content
514 * structure of the first partial in the group. So we copy
515 * the file name and/or folder name from that message. In
516 * this case, we also note that we will be appending.
518 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
519 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
521 /* Yep, it's a message/partial */
524 /* But is it the first and/or last in the collection? */
525 if (pm
->pm_partno
== 1)
526 first_partial
= true;
527 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
531 * If "p" is a valid pointer, then it points to the
532 * Content structure for the first message in the group.
533 * So we just copy the filename or foldername information
534 * from the previous iteration of this function.
538 if (! ct
->c_storage
) {
539 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
541 /* record the folder name */
543 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
551 * Get storage formatting string.
553 * 1) If we have storeproc defined, then use that
554 * 2) Else check for a mhn-store-<type>/<subtype> entry
555 * 3) Else check for a mhn-store-<type> entry
556 * 4) Else if content is "message", use "+" (current folder)
557 * 5) Else use string "%m%P.%s".
559 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
560 CI ci
= &ct
->c_ctinfo
;
562 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
564 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
568 if (! ct
->c_storage
) {
570 * Check the beginning of storage formatting string
571 * to see if we are saving content to a folder.
573 if (*cp
== '+' || *cp
== '@') {
574 char *tmpfilenam
, *folder
;
576 /* Store content in temporary file for now */
577 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
578 die("unable to create temporary file in %s",
581 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
583 /* Get the folder name */
585 folder
= pluspath (cp
);
587 folder
= getfolder (1);
589 /* Check if folder exists */
590 create_folder(m_mailpath(folder
), 0, exit
);
592 /* Record the folder name */
593 ct
->c_folder
= mh_xstrdup(folder
);
602 * Parse and expand the storage formatting string
603 * in `cp' into `buffer'.
605 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
608 * If formatting begins with '|' or '!', then pass
609 * content to standard input of a command and return.
611 if (buffer
[0] == '|' || buffer
[0] == '!')
612 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
614 /* record the filename */
615 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
620 /* The output filename was explicitly specified, so use it. */
621 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
628 /* flush the output stream */
631 /* Now save or append the content to a file */
632 if (output_content_file (ct
, appending
) == NOTOK
)
636 * If necessary, link the file into a folder and remove
637 * the temporary file. If this message is a partial,
638 * then only do this if it is the last one in the group.
640 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
641 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
642 (void) m_unlink (ct
->c_storage
);
649 * Now print out the name/number of the message
650 * that we are storing.
654 fprintf (stderr
, "reassembling partials ");
656 fputs(ct
->c_file
, stderr
);
658 fprintf (stderr
, "%s,", ct
->c_file
);
660 fprintf (stderr
, "storing message %s", ct
->c_file
);
662 fprintf (stderr
, " part %s", ct
->c_partno
);
666 * Unless we are in the "middle" of group of message/partials,
667 * we now print the name of the file, folder, and/or message
668 * to which we are storing the content.
670 if (!is_partial
|| last_partial
) {
672 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
674 } else if (!strcmp(ct
->c_storage
, "-")) {
675 fprintf (stderr
, " to stdout\n");
677 int cwdlen
= strlen (info
->cwd
);
679 fprintf (stderr
, " as file %s\n",
680 !has_prefix(ct
->c_storage
, info
->cwd
)
681 || ct
->c_storage
[cwdlen
] != '/'
682 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
692 * Output content to a file
696 output_content_file (CT ct
, int appending
)
699 char *file
, buffer
[BUFSIZ
];
704 * If the pathname is absolute, make sure
705 * all the relevant directories exist.
707 if (strchr(ct
->c_storage
, '/')
708 && make_intermediates (ct
->c_storage
) == NOTOK
)
711 if (ct
->c_encoding
!= CE_7BIT
) {
714 if (!ct
->c_ceopenfnx
) {
715 inform("don't know how to decode part %s of message %s",
716 ct
->c_partno
, ct
->c_file
);
720 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
722 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
724 if (!strcmp (file
, ct
->c_storage
)) {
725 (*ct
->c_ceclosefnx
) (ct
);
730 * Send to standard output
732 if (!strcmp (ct
->c_storage
, "-")) {
735 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
736 advise ("stdout", "unable to dup");
738 (*ct
->c_ceclosefnx
) (ct
);
741 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
742 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
743 appending
? "a" : "w");
751 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
752 advise (ct
->c_storage
, "unable to fopen for %s",
753 appending
? "appending" : "writing");
759 * Filter the header fields of the initial enclosing
760 * message/partial into the file.
762 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
763 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
765 if (pm
->pm_partno
== 1)
766 copy_some_headers (fp
, ct
);
770 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
772 advise (file
, "error reading content from");
779 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
780 advise ("output_content_file", "fwrite");
787 (*ct
->c_ceclosefnx
) (ct
);
789 if (cc
!= NOTOK
&& fflush (fp
))
790 advise (ct
->c_storage
, "error writing to");
794 return cc
== NOTOK
? NOTOK
: OK
;
797 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
798 advise (ct
->c_file
, "unable to open for reading");
804 fseek (ct
->c_fp
, pos
, SEEK_SET
);
806 if (!strcmp (ct
->c_storage
, "-")) {
809 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
810 advise ("stdout", "unable to dup");
813 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
814 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
815 appending
? "a" : "w");
820 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
821 advise (ct
->c_storage
, "unable to fopen for %s",
822 appending
? "appending" : "writing");
828 * Copy a few of the header fields of the initial
829 * enclosing message/partial into the file.
832 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
833 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
835 if (pm
->pm_partno
== 1) {
836 copy_some_headers (fp
, ct
);
841 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
842 if ((pos
+= strlen (buffer
)) > last
) {
845 diff
= strlen (buffer
) - (pos
- last
);
850 * If this is the first content of a group of
851 * message/partial contents, then we only copy a few
852 * of the header fields of the enclosed message.
867 if (!uprf (buffer
, XXX_FIELD_PRF
)
868 && !uprf (buffer
, VRSN_FIELD
)
869 && !uprf (buffer
, "Subject:")
870 && !uprf (buffer
, "Encrypted:")
871 && !uprf (buffer
, "Message-ID:")) {
886 advise (ct
->c_storage
, "error writing to");
896 * Add a file to a folder.
898 * Return the new message number of the file
899 * when added to the folder. Return -1, if
904 output_content_folder (char *folder
, char *filename
)
909 /* Read the folder. */
910 if (!(mp
= folder_read(folder
, 0))) {
911 inform("unable to read folder %s", folder
);
914 /* Link file into folder */
915 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
917 /* free folder structure */
921 * Return msgnum. We are relying on the fact that
922 * msgnum will be -1, if folder_addmsg() had an error.
929 * Parse and expand the storage formatting string
930 * pointed to by "cp" into "buffer".
934 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
938 CI ci
= &ct
->c_ctinfo
;
941 * If storage string is "-", just copy it, and
942 * return (send content to standard output).
944 if (cp
[0] == '-' && cp
[1] == '\0') {
945 strncpy (buffer
, cp
, buflen
);
953 * If formatting string is a pathname that doesn't
954 * begin with '/', then preface the path with the
955 * appropriate directory.
957 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
958 if (!strcmp(dir
, "/"))
959 dir
= ""; /* Don't start with "//". */
960 snprintf (bp
, buflen
, "%s/", dir
);
968 /* We are processing a storage escape */
973 * Insert parameters from Content-Type.
974 * This is only valid for '|' commands.
976 if (buffer
[0] != '|' && buffer
[0] != '!') {
986 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
987 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
988 pm
->pm_name
, get_param_value(pm
, '?'));
998 /* insert message number */
999 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1003 /* insert part number with leading dot */
1005 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1009 /* insert part number without leading dot */
1011 strncpy (bp
, ct
->c_partno
, buflen
);
1015 /* insert content type */
1016 strncpy (bp
, ci
->ci_type
, buflen
);
1020 /* insert content subtype */
1021 strncpy (bp
, ci
->ci_subtype
, buflen
);
1025 /* insert the character % */
1035 /* Advance bp and decrement buflen */
1053 * Check if the content specifies a filename
1054 * in its MIME parameters.
1058 get_storeproc (CT ct
)
1064 * If the storeproc has already been defined,
1065 * we just return (for instance, if this content
1066 * is part of a "message/external".
1068 if (ct
->c_storeproc
)
1072 * If there's a Content-Disposition header and it has a filename,
1073 * use that (RFC-2183).
1076 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1077 use_param_as_filename(cp
)) {
1078 ct
->c_storeproc
= mh_xstrdup(cp
);
1086 * Check the attribute/value pairs, for the attribute "name".
1087 * If found, do a few sanity checks and copy the value into
1091 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1092 use_param_as_filename(cp
)) {
1093 ct
->c_storeproc
= mh_xstrdup(cp
);
1101 * Copy some of the header fields of the initial message/partial
1102 * message into the header of the reassembled message.
1106 copy_some_headers (FILE *out
, CT ct
)
1110 hp
= ct
->c_first_hf
; /* start at first header field */
1114 * A few of the header fields of the enclosing
1115 * messages are not copied.
1117 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1118 && strcasecmp (hp
->name
, VRSN_FIELD
)
1119 && strcasecmp (hp
->name
, "Subject")
1120 && strcasecmp (hp
->name
, "Encrypted")
1121 && strcasecmp (hp
->name
, "Message-ID"))
1122 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1123 hp
= hp
->next
; /* next header field */
1129 /******************************************************************************/
1130 /* -clobber support */
1132 static enum clobber_policy_t
1133 clobber_policy (const char *value
)
1135 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1136 return NMH_CLOBBER_ALWAYS
;
1138 if (! strcasecmp (value
, "auto")) {
1139 return NMH_CLOBBER_AUTO
;
1141 if (! strcasecmp (value
, "suffix")) {
1142 return NMH_CLOBBER_SUFFIX
;
1144 if (! strcasecmp (value
, "ask")) {
1145 return NMH_CLOBBER_ASK
;
1147 if (! strcasecmp (value
, "never")) {
1148 return NMH_CLOBBER_NEVER
;
1151 die("invalid argument, %s, to clobber", value
);
1156 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1158 const size_t max_versions
= 1000000;
1159 /* 8 = log max_versions + one for - or . + one for null terminator */
1160 const size_t buflen
= strlen (file
) + 8;
1161 char *buffer
= mh_xmalloc (buflen
);
1164 char *extension
= NULL
;
1165 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1166 ((extension
= strrchr (file
, '.')) != NULL
)) {
1167 *extension
++ = '\0';
1170 for (version
= 1; version
< max_versions
; ++version
) {
1173 switch (clobber_policy
) {
1174 case NMH_CLOBBER_AUTO
: {
1175 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1176 extension
== NULL
? "" : ".",
1177 extension
== NULL
? "" : extension
);
1181 case NMH_CLOBBER_SUFFIX
:
1182 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1186 /* Should never get here. */
1187 inform("will not overwrite %s, invalid clobber policy", buffer
);
1192 /* Actually (try to) create the file here to avoid a race
1193 condition on file naming + creation. This won't solve the
1194 problem with old NFS that doesn't support O_EXCL, though.
1195 Let the umask strip off permissions from 0666 as desired.
1196 That's what fopen () would do if it was creating the file. */
1197 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1198 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1199 S_IROTH
| S_IWOTH
)) >= 0) {
1207 if (version
>= max_versions
) {
1208 inform("will not overwrite %s, too many versions", buffer
);
1218 clobber_check (char *original_file
, mhstoreinfo_t info
)
1220 /* clobber policy return value
1221 * -------------- ------------
1222 * -always original_file
1223 * -auto original_file-<digits>.extension
1224 * -suffix original_file.<digits>
1225 * -ask original_file, 0, or another filename/path
1233 if (! strcmp (original_file
, "-")) {
1234 return original_file
;
1237 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1238 /* Save cwd for possible use in loop below. */
1241 cwd
= mh_xstrdup(original_file
);
1242 slash
= strrchr (cwd
, '/');
1247 /* original_file isn't a full path, which should only happen if
1257 file
= original_file
;
1258 check_again
= false;
1260 switch (info
->clobber_policy
) {
1261 case NMH_CLOBBER_ALWAYS
:
1264 case NMH_CLOBBER_SUFFIX
:
1265 case NMH_CLOBBER_AUTO
:
1266 if (stat (file
, &st
) == OK
) {
1267 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1269 ++info
->files_not_clobbered
;
1274 case NMH_CLOBBER_ASK
:
1275 if (stat (file
, &st
) == OK
) {
1276 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1277 static struct swit answer
[4] = {
1278 { "yes", 0, NMH_YES
},
1279 { "no", 0, NMH_NO
},
1280 { "rename", 0, NMH_RENAME
},
1284 if (isatty (fileno (stdin
))) {
1286 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1287 ans
= read_switch_multiword (prompt
, answer
);
1290 /* Overwrite, that's what nmh used to do. And warn. */
1291 inform("-clobber ask but no tty, so overwrite %s", file
);
1295 switch ((enum answers
) smatch (*ans
, answer
)) {
1301 ++info
->files_not_clobbered
;
1305 fputs("Enter filename or full path of the new file: ", stdout
);
1306 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1309 ++info
->files_not_clobbered
;
1311 trim_suffix_c(buf
, '\n');
1316 if (buf
[0] == '/') {
1317 /* Full path, use it. */
1318 file
= mh_xstrdup(buf
);
1320 /* Relative path. */
1321 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1331 case NMH_CLOBBER_NEVER
:
1332 if (stat (file
, &st
) == OK
) {
1333 /* Keep count of files that would have been clobbered,
1334 and return that as process exit status. */
1335 inform("will not overwrite %s with -clobber never", file
);
1338 ++info
->files_not_clobbered
;
1343 original_file
= file
;
1344 } while (check_again
);
1352 use_param_as_filename(const char *p
)
1354 /* Preserve result of original test that considered an empty string
1356 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1359 /* -clobber support */
1360 /******************************************************************************/