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_free.h"
10 #include "sbr/folder_addmsg.h"
11 #include "sbr/context_find.h"
13 #include "sbr/error.h"
18 #include "h/fmt_scan.h"
20 #include "h/mhparse.h"
23 #include "mhshowsbr.h"
24 #include "sbr/m_maildir.h"
25 #include "sbr/m_mktemp.h"
27 enum clobber_policy_t
{
28 NMH_CLOBBER_ALWAYS
= 0,
35 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
38 CT
*cts
; /* Top-level list of contents to store. */
39 char *cwd
; /* cached current directory */
40 int autosw
; /* -auto enabled */
41 int verbosw
; /* -verbose enabled */
42 int files_not_clobbered
; /* output flag indicating that store failed
43 in order to not clobber an existing file */
45 /* The following must never be touched by a caller: they are for
46 internal use by the mhstoresbr functions. */
47 char *dir
; /* directory in which to store contents */
48 enum clobber_policy_t clobber_policy
; /* -clobber selection */
51 static bool use_param_as_filename(const char *p
);
54 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
63 info
->files_not_clobbered
= 0;
65 info
->clobber_policy
= clobber_policy (csw
);
71 mhstoreinfo_free (mhstoreinfo_t info
)
79 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
81 return info
->files_not_clobbered
;
86 * Type for a compare function for qsort. This keeps
89 typedef int (*qsort_comp
) (const void *, const void *);
95 static void store_single_message (CT
, mhstoreinfo_t
);
96 static int store_switch (CT
, mhstoreinfo_t
);
97 static int store_generic (CT
, mhstoreinfo_t
);
98 static int store_application (CT
, mhstoreinfo_t
);
99 static int store_multi (CT
, mhstoreinfo_t
);
100 static int store_partial (CT
, mhstoreinfo_t
);
101 static int store_external (CT
, mhstoreinfo_t
);
102 static int ct_compar (CT
*, CT
*);
103 static int store_content (CT
, CT
, mhstoreinfo_t
);
104 static int output_content_file (CT
, int);
105 static int output_content_folder (char *, char *);
106 static int parse_format_string (CT
, char *, char *, int, char *);
107 static void get_storeproc (CT
);
108 static int copy_some_headers (FILE *, CT
);
109 static char *clobber_check (char *, mhstoreinfo_t
);
112 * Main entry point to store content
113 * from a collection of messages.
117 store_all_messages (mhstoreinfo_t info
)
123 * Check for the directory in which to
124 * store any contents.
126 if ((cp
= context_find (nmhstorage
)) && *cp
)
127 info
->dir
= mh_xstrdup(cp
);
129 info
->dir
= getcpy (info
->cwd
);
131 for (ctp
= info
->cts
; *ctp
; ctp
++) {
133 store_single_message (ct
, info
);
141 * Entry point to store the content
142 * in a (single) message
146 store_single_message (CT ct
, mhstoreinfo_t info
)
148 if (type_ok (ct
, 1)) {
150 store_switch (ct
, info
);
155 if (ct
->c_ceclosefnx
)
156 (*ct
->c_ceclosefnx
) (ct
);
162 * Switching routine to store different content types
166 store_switch (CT ct
, mhstoreinfo_t info
)
168 switch (ct
->c_type
) {
170 return store_multi (ct
, info
);
173 switch (ct
->c_subtype
) {
174 case MESSAGE_PARTIAL
:
175 return store_partial (ct
, info
);
177 case MESSAGE_EXTERNAL
:
178 return store_external (ct
, info
);
182 return store_generic (ct
, info
);
187 return store_application (ct
, info
);
193 return store_generic (ct
, info
);
196 return OK
; /* NOT REACHED */
201 * Generic routine to store a MIME content.
202 * (audio, video, image, text, message/rfc822)
206 store_generic (CT ct
, mhstoreinfo_t info
)
209 * Check if the content specifies a filename.
210 * Don't bother with this for type "message"
211 * (only "message/rfc822" will use store_generic).
213 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
216 return store_content (ct
, NULL
, info
);
221 * Store content of type "application"
225 store_application (CT ct
, mhstoreinfo_t info
)
227 CI ci
= &ct
->c_ctinfo
;
229 /* Check if the content specifies a filename */
234 * If storeproc is not defined, and the content is type
235 * "application/octet-stream", we also check for various
236 * attribute/value pairs which specify if this a tar file.
238 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
244 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
245 if (strcasecmp (cp
, "tar") == 0)
249 /* check for "conversions=compress" attribute */
250 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
251 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
252 if (strcasecmp (cp
, "compress") == 0 ||
253 strcasecmp (cp
, "x-compress") == 0) {
256 if (strcasecmp (cp
, "gzip") == 0 ||
257 strcasecmp (cp
, "x-gzip") == 0) {
263 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
264 : (gzP
? "%egzip -dc | tar tvf -"
265 : "%etar tvf -"), NULL
);
266 if (!ct
->c_storeproc
) {
268 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
269 : (gzP
? "| gzip -dc | tar xvpf -"
270 : "| tar xvpf -"), NULL
);
273 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
274 : (gzP
? "%m%P.tar.gz"
275 : "%m%P.tar"), NULL
);
281 return store_content (ct
, NULL
, info
);
286 * Store the content of a multipart message
290 store_multi (CT ct
, mhstoreinfo_t info
)
293 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
297 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
298 CT p
= part
->mp_part
;
300 if (part_ok (p
) && type_ok (p
, 1)) {
302 /* Support mhstore -outfile. The MIME parser doesn't
303 load c_storage, so we know that p->c_storage is
305 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
307 result
= store_switch (p
, info
);
309 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
319 * Reassemble and store the contents of a collection
320 * of messages of type "message/partial".
324 store_partial (CT ct
, mhstoreinfo_t info
)
329 struct partial
*pm
, *qm
;
331 qm
= (struct partial
*) ct
->c_ctparams
;
336 for (ctp
= info
->cts
; *ctp
; ctp
++) {
338 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
339 pm
= (struct partial
*) p
->c_ctparams
;
341 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
342 pm
->pm_marked
= pm
->pm_partno
;
354 inform("missing (at least) last part of multipart message");
358 base
= mh_xcalloc(i
+ 1, sizeof *base
);
360 for (ctp
= info
->cts
; *ctp
; ctp
++) {
362 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
363 pm
= (struct partial
*) p
->c_ctparams
;
371 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
374 for (ctq
= base
; *ctq
; ctq
++) {
376 pm
= (struct partial
*) p
->c_ctparams
;
377 if (pm
->pm_marked
== cur
) {
382 if (pm
->pm_marked
== cur
- 1) {
383 inform("duplicate part %d of %d part multipart message, continuing...",
389 inform("missing %spart %d of %d part multipart message",
390 cur
!= hi
? "(at least) " : "", cur
, hi
);
399 * Now cycle through the sorted list of messages of type
400 * "message/partial" and save/append them to a file.
405 if (store_content (ct
, NULL
, info
) == NOTOK
) {
411 for (; *ctq
; ctq
++) {
413 if (store_content (p
, ct
, info
) == NOTOK
)
423 * Store content from a message of type "message/external".
427 store_external (CT ct
, mhstoreinfo_t info
)
430 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
431 CT p
= e
->eb_content
;
437 * Check if the parameters for the external body
438 * specified a filename.
443 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
444 if (!ct
->c_storeproc
)
445 ct
->c_storeproc
= mh_xstrdup(cp
);
447 p
->c_storeproc
= mh_xstrdup(cp
);
452 * Since we will let the Content structure for the
453 * external body substitute for the current content,
454 * we temporarily change its partno (number inside
455 * multipart), so everything looks right.
457 p
->c_partno
= ct
->c_partno
;
459 /* we probably need to check if content is really there */
461 /* Support mhstore -outfile. The MIME parser doesn't load
462 c_storage, so we know that p->c_storage is NULL here. */
463 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
465 result
= store_switch (p
, info
);
473 * Compare the numbering from two different
474 * message/partials (needed for sorting).
478 ct_compar (CT
*a
, CT
*b
)
480 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
481 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
483 return am
->pm_marked
- bm
->pm_marked
;
488 * Store contents of a message or message part to
489 * a folder, a file, the standard output, or pass
490 * the contents to a command.
492 * If the current content to be saved is a followup part
493 * to a collection of messages of type "message/partial",
494 * then field "p" is a pointer to the Content structure
495 * to the first message/partial in the group.
499 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
501 bool appending
= false;
503 bool is_partial
= false;
504 bool first_partial
= false;
505 bool last_partial
= false;
506 char *cp
, buffer
[BUFSIZ
];
509 * Do special processing for messages of
510 * type "message/partial".
512 * We first check if this content is of type
513 * "message/partial". If it is, then we need to check
514 * whether it is the first and/or last in the group.
516 * Then if "p" is a valid pointer, it points to the Content
517 * structure of the first partial in the group. So we copy
518 * the file name and/or folder name from that message. In
519 * this case, we also note that we will be appending.
521 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
522 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
524 /* Yep, it's a message/partial */
527 /* But is it the first and/or last in the collection? */
528 if (pm
->pm_partno
== 1)
529 first_partial
= true;
530 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
534 * If "p" is a valid pointer, then it points to the
535 * Content structure for the first message in the group.
536 * So we just copy the filename or foldername information
537 * from the previous iteration of this function.
541 if (! ct
->c_storage
) {
542 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
544 /* record the folder name */
546 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
554 * Get storage formatting string.
556 * 1) If we have storeproc defined, then use that
557 * 2) Else check for a mhn-store-<type>/<subtype> entry
558 * 3) Else check for a mhn-store-<type> entry
559 * 4) Else if content is "message", use "+" (current folder)
560 * 5) Else use string "%m%P.%s".
562 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
563 CI ci
= &ct
->c_ctinfo
;
565 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
567 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
571 if (! ct
->c_storage
) {
573 * Check the beginning of storage formatting string
574 * to see if we are saving content to a folder.
576 if (*cp
== '+' || *cp
== '@') {
577 char *tmpfilenam
, *folder
;
579 /* Store content in temporary file for now */
580 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
581 die("unable to create temporary file in %s",
584 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
586 /* Get the folder name */
588 folder
= pluspath (cp
);
590 folder
= getfolder (1);
592 /* Check if folder exists */
593 create_folder(m_mailpath(folder
), 0, exit
);
595 /* Record the folder name */
596 ct
->c_folder
= mh_xstrdup(folder
);
605 * Parse and expand the storage formatting string
606 * in `cp' into `buffer'.
608 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
611 * If formatting begins with '|' or '!', then pass
612 * content to standard input of a command and return.
614 if (buffer
[0] == '|' || buffer
[0] == '!')
615 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
617 /* record the filename */
618 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
623 /* The output filename was explicitly specified, so use it. */
624 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
631 /* flush the output stream */
634 /* Now save or append the content to a file */
635 if (output_content_file (ct
, appending
) == NOTOK
)
639 * If necessary, link the file into a folder and remove
640 * the temporary file. If this message is a partial,
641 * then only do this if it is the last one in the group.
643 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
644 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
645 (void) m_unlink (ct
->c_storage
);
652 * Now print out the name/number of the message
653 * that we are storing.
657 fprintf (stderr
, "reassembling partials ");
659 fputs(ct
->c_file
, stderr
);
661 fprintf (stderr
, "%s,", ct
->c_file
);
663 fprintf (stderr
, "storing message %s", ct
->c_file
);
665 fprintf (stderr
, " part %s", ct
->c_partno
);
669 * Unless we are in the "middle" of group of message/partials,
670 * we now print the name of the file, folder, and/or message
671 * to which we are storing the content.
673 if (!is_partial
|| last_partial
) {
675 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
677 } else if (!strcmp(ct
->c_storage
, "-")) {
678 fprintf (stderr
, " to stdout\n");
680 int cwdlen
= strlen (info
->cwd
);
682 fprintf (stderr
, " as file %s\n",
683 !has_prefix(ct
->c_storage
, info
->cwd
)
684 || ct
->c_storage
[cwdlen
] != '/'
685 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
695 * Output content to a file
699 output_content_file (CT ct
, int appending
)
702 char *file
, buffer
[BUFSIZ
];
707 * If the pathname is absolute, make sure
708 * all the relevant directories exist.
710 if (strchr(ct
->c_storage
, '/')
711 && make_intermediates (ct
->c_storage
) == NOTOK
)
714 if (ct
->c_encoding
!= CE_7BIT
) {
717 if (!ct
->c_ceopenfnx
) {
718 inform("don't know how to decode part %s of message %s",
719 ct
->c_partno
, ct
->c_file
);
723 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
725 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
727 if (!strcmp (file
, ct
->c_storage
)) {
728 (*ct
->c_ceclosefnx
) (ct
);
733 * Send to standard output
735 if (!strcmp (ct
->c_storage
, "-")) {
738 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
739 advise ("stdout", "unable to dup");
741 (*ct
->c_ceclosefnx
) (ct
);
744 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
745 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
746 appending
? "a" : "w");
754 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
755 advise (ct
->c_storage
, "unable to fopen for %s",
756 appending
? "appending" : "writing");
762 * Filter the header fields of the initial enclosing
763 * message/partial into the file.
765 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
766 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
768 if (pm
->pm_partno
== 1)
769 copy_some_headers (fp
, ct
);
773 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
775 advise (file
, "error reading content from");
782 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
783 advise ("output_content_file", "fwrite");
790 (*ct
->c_ceclosefnx
) (ct
);
792 if (cc
!= NOTOK
&& fflush (fp
))
793 advise (ct
->c_storage
, "error writing to");
797 return cc
== NOTOK
? NOTOK
: OK
;
800 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
801 advise (ct
->c_file
, "unable to open for reading");
807 fseek (ct
->c_fp
, pos
, SEEK_SET
);
809 if (!strcmp (ct
->c_storage
, "-")) {
812 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
813 advise ("stdout", "unable to dup");
816 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
817 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
818 appending
? "a" : "w");
823 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
824 advise (ct
->c_storage
, "unable to fopen for %s",
825 appending
? "appending" : "writing");
831 * Copy a few of the header fields of the initial
832 * enclosing message/partial into the file.
835 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
836 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
838 if (pm
->pm_partno
== 1) {
839 copy_some_headers (fp
, ct
);
844 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
845 if ((pos
+= strlen (buffer
)) > last
) {
848 diff
= strlen (buffer
) - (pos
- last
);
853 * If this is the first content of a group of
854 * message/partial contents, then we only copy a few
855 * of the header fields of the enclosed message.
870 if (!uprf (buffer
, XXX_FIELD_PRF
)
871 && !uprf (buffer
, VRSN_FIELD
)
872 && !uprf (buffer
, "Subject:")
873 && !uprf (buffer
, "Encrypted:")
874 && !uprf (buffer
, "Message-ID:")) {
889 advise (ct
->c_storage
, "error writing to");
899 * Add a file to a folder.
901 * Return the new message number of the file
902 * when added to the folder. Return -1, if
907 output_content_folder (char *folder
, char *filename
)
912 /* Read the folder. */
913 if (!(mp
= folder_read(folder
, 0))) {
914 inform("unable to read folder %s", folder
);
917 /* Link file into folder */
918 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
920 /* free folder structure */
924 * Return msgnum. We are relying on the fact that
925 * msgnum will be -1, if folder_addmsg() had an error.
932 * Parse and expand the storage formatting string
933 * pointed to by "cp" into "buffer".
937 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
941 CI ci
= &ct
->c_ctinfo
;
944 * If storage string is "-", just copy it, and
945 * return (send content to standard output).
947 if (cp
[0] == '-' && cp
[1] == '\0') {
948 strncpy (buffer
, cp
, buflen
);
956 * If formatting string is a pathname that doesn't
957 * begin with '/', then preface the path with the
958 * appropriate directory.
960 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
961 if (!strcmp(dir
, "/"))
962 dir
= ""; /* Don't start with "//". */
963 snprintf (bp
, buflen
, "%s/", dir
);
971 /* We are processing a storage escape */
976 * Insert parameters from Content-Type.
977 * This is only valid for '|' commands.
979 if (buffer
[0] != '|' && buffer
[0] != '!') {
989 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
990 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
991 pm
->pm_name
, get_param_value(pm
, '?'));
1001 /* insert message number */
1002 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1006 /* insert part number with leading dot */
1008 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1012 /* insert part number without leading dot */
1014 strncpy (bp
, ct
->c_partno
, buflen
);
1018 /* insert content type */
1019 strncpy (bp
, ci
->ci_type
, buflen
);
1023 /* insert content subtype */
1024 strncpy (bp
, ci
->ci_subtype
, buflen
);
1028 /* insert the character % */
1038 /* Advance bp and decrement buflen */
1056 * Check if the content specifies a filename
1057 * in its MIME parameters.
1061 get_storeproc (CT ct
)
1067 * If the storeproc has already been defined,
1068 * we just return (for instance, if this content
1069 * is part of a "message/external".
1071 if (ct
->c_storeproc
)
1075 * If there's a Content-Disposition header and it has a filename,
1076 * use that (RFC-2183).
1079 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1080 use_param_as_filename(cp
)) {
1081 ct
->c_storeproc
= mh_xstrdup(cp
);
1089 * Check the attribute/value pairs, for the attribute "name".
1090 * If found, do a few sanity checks and copy the value into
1094 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1095 use_param_as_filename(cp
)) {
1096 ct
->c_storeproc
= mh_xstrdup(cp
);
1104 * Copy some of the header fields of the initial message/partial
1105 * message into the header of the reassembled message.
1109 copy_some_headers (FILE *out
, CT ct
)
1113 hp
= ct
->c_first_hf
; /* start at first header field */
1117 * A few of the header fields of the enclosing
1118 * messages are not copied.
1120 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1121 && strcasecmp (hp
->name
, VRSN_FIELD
)
1122 && strcasecmp (hp
->name
, "Subject")
1123 && strcasecmp (hp
->name
, "Encrypted")
1124 && strcasecmp (hp
->name
, "Message-ID"))
1125 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1126 hp
= hp
->next
; /* next header field */
1132 /******************************************************************************/
1133 /* -clobber support */
1135 static enum clobber_policy_t
1136 clobber_policy (const char *value
)
1138 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1139 return NMH_CLOBBER_ALWAYS
;
1141 if (! strcasecmp (value
, "auto")) {
1142 return NMH_CLOBBER_AUTO
;
1144 if (! strcasecmp (value
, "suffix")) {
1145 return NMH_CLOBBER_SUFFIX
;
1147 if (! strcasecmp (value
, "ask")) {
1148 return NMH_CLOBBER_ASK
;
1150 if (! strcasecmp (value
, "never")) {
1151 return NMH_CLOBBER_NEVER
;
1154 die("invalid argument, %s, to clobber", value
);
1159 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1161 const size_t max_versions
= 1000000;
1162 /* 8 = log max_versions + one for - or . + one for null terminator */
1163 const size_t buflen
= strlen (file
) + 8;
1164 char *buffer
= mh_xmalloc (buflen
);
1167 char *extension
= NULL
;
1168 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1169 ((extension
= strrchr (file
, '.')) != NULL
)) {
1170 *extension
++ = '\0';
1173 for (version
= 1; version
< max_versions
; ++version
) {
1176 switch (clobber_policy
) {
1177 case NMH_CLOBBER_AUTO
: {
1178 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1179 extension
== NULL
? "" : ".",
1180 extension
== NULL
? "" : extension
);
1184 case NMH_CLOBBER_SUFFIX
:
1185 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1189 /* Should never get here. */
1190 inform("will not overwrite %s, invalid clobber policy", buffer
);
1195 /* Actually (try to) create the file here to avoid a race
1196 condition on file naming + creation. This won't solve the
1197 problem with old NFS that doesn't support O_EXCL, though.
1198 Let the umask strip off permissions from 0666 as desired.
1199 That's what fopen () would do if it was creating the file. */
1200 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1201 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1202 S_IROTH
| S_IWOTH
)) >= 0) {
1210 if (version
>= max_versions
) {
1211 inform("will not overwrite %s, too many versions", buffer
);
1221 clobber_check (char *original_file
, mhstoreinfo_t info
)
1223 /* clobber policy return value
1224 * -------------- ------------
1225 * -always original_file
1226 * -auto original_file-<digits>.extension
1227 * -suffix original_file.<digits>
1228 * -ask original_file, 0, or another filename/path
1236 if (! strcmp (original_file
, "-")) {
1237 return original_file
;
1240 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1241 /* Save cwd for possible use in loop below. */
1244 cwd
= mh_xstrdup(original_file
);
1245 slash
= strrchr (cwd
, '/');
1250 /* original_file isn't a full path, which should only happen if
1260 file
= original_file
;
1261 check_again
= false;
1263 switch (info
->clobber_policy
) {
1264 case NMH_CLOBBER_ALWAYS
:
1267 case NMH_CLOBBER_SUFFIX
:
1268 case NMH_CLOBBER_AUTO
:
1269 if (stat (file
, &st
) == OK
) {
1270 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1272 ++info
->files_not_clobbered
;
1277 case NMH_CLOBBER_ASK
:
1278 if (stat (file
, &st
) == OK
) {
1279 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1280 static struct swit answer
[4] = {
1281 { "yes", 0, NMH_YES
},
1282 { "no", 0, NMH_NO
},
1283 { "rename", 0, NMH_RENAME
},
1287 if (isatty (fileno (stdin
))) {
1289 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1290 ans
= read_switch_multiword (prompt
, answer
);
1293 /* Overwrite, that's what nmh used to do. And warn. */
1294 inform("-clobber ask but no tty, so overwrite %s", file
);
1298 switch ((enum answers
) smatch (*ans
, answer
)) {
1304 ++info
->files_not_clobbered
;
1308 fputs("Enter filename or full path of the new file: ", stdout
);
1309 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1312 ++info
->files_not_clobbered
;
1314 trim_suffix_c(buf
, '\n');
1319 if (buf
[0] == '/') {
1320 /* Full path, use it. */
1321 file
= mh_xstrdup(buf
);
1323 /* Relative path. */
1324 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1334 case NMH_CLOBBER_NEVER
:
1335 if (stat (file
, &st
) == OK
) {
1336 /* Keep count of files that would have been clobbered,
1337 and return that as process exit status. */
1338 inform("will not overwrite %s with -clobber never", file
);
1341 ++info
->files_not_clobbered
;
1346 original_file
= file
;
1347 } while (check_again
);
1355 use_param_as_filename(const char *p
)
1357 /* Preserve result of original test that considered an empty string
1359 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1362 /* -clobber support */
1363 /******************************************************************************/