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/read_switch_multiword.h"
10 #include "sbr/concat.h"
11 #include "sbr/smatch.h"
12 #include "sbr/r1bindex.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/folder_addmsg.h"
19 #include "sbr/context_find.h"
21 #include "sbr/error.h"
26 #include "h/fmt_scan.h"
28 #include "h/mhparse.h"
31 #include "mhshowsbr.h"
32 #include "sbr/m_maildir.h"
33 #include "sbr/m_mktemp.h"
35 enum clobber_policy_t
{
36 NMH_CLOBBER_ALWAYS
= 0,
43 static enum clobber_policy_t
clobber_policy (const char *) PURE
;
46 CT
*cts
; /* Top-level list of contents to store. */
47 char *cwd
; /* cached current directory */
48 int autosw
; /* -auto enabled */
49 int verbosw
; /* -verbose enabled */
50 int files_not_clobbered
; /* output flag indicating that store failed
51 in order to not clobber an existing file */
53 /* The following must never be touched by a caller: they are for
54 internal use by the mhstoresbr functions. */
55 char *dir
; /* directory in which to store contents */
56 enum clobber_policy_t clobber_policy
; /* -clobber selection */
59 static bool use_param_as_filename(const char *p
);
62 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
)
71 info
->files_not_clobbered
= 0;
73 info
->clobber_policy
= clobber_policy (csw
);
79 mhstoreinfo_free (mhstoreinfo_t info
)
87 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
)
89 return info
->files_not_clobbered
;
94 * Type for a compare function for qsort. This keeps
97 typedef int (*qsort_comp
) (const void *, const void *);
103 static void store_single_message (CT
, mhstoreinfo_t
);
104 static int store_switch (CT
, mhstoreinfo_t
);
105 static int store_generic (CT
, mhstoreinfo_t
);
106 static int store_application (CT
, mhstoreinfo_t
);
107 static int store_multi (CT
, mhstoreinfo_t
);
108 static int store_partial (CT
, mhstoreinfo_t
);
109 static int store_external (CT
, mhstoreinfo_t
);
110 static int ct_compar (CT
*, CT
*);
111 static int store_content (CT
, CT
, mhstoreinfo_t
);
112 static int output_content_file (CT
, int);
113 static int output_content_folder (char *, char *);
114 static int parse_format_string (CT
, char *, char *, int, char *);
115 static void get_storeproc (CT
);
116 static int copy_some_headers (FILE *, CT
);
117 static char *clobber_check (char *, mhstoreinfo_t
);
120 * Main entry point to store content
121 * from a collection of messages.
125 store_all_messages (mhstoreinfo_t info
)
131 * Check for the directory in which to
132 * store any contents.
134 if ((cp
= context_find (nmhstorage
)) && *cp
)
135 info
->dir
= mh_xstrdup(cp
);
137 info
->dir
= getcpy (info
->cwd
);
139 for (ctp
= info
->cts
; *ctp
; ctp
++) {
141 store_single_message (ct
, info
);
149 * Entry point to store the content
150 * in a (single) message
154 store_single_message (CT ct
, mhstoreinfo_t info
)
156 if (type_ok (ct
, 1)) {
158 store_switch (ct
, info
);
163 if (ct
->c_ceclosefnx
)
164 (*ct
->c_ceclosefnx
) (ct
);
170 * Switching routine to store different content types
174 store_switch (CT ct
, mhstoreinfo_t info
)
176 switch (ct
->c_type
) {
178 return store_multi (ct
, info
);
181 switch (ct
->c_subtype
) {
182 case MESSAGE_PARTIAL
:
183 return store_partial (ct
, info
);
185 case MESSAGE_EXTERNAL
:
186 return store_external (ct
, info
);
190 return store_generic (ct
, info
);
195 return store_application (ct
, info
);
201 return store_generic (ct
, info
);
204 return OK
; /* NOT REACHED */
209 * Generic routine to store a MIME content.
210 * (audio, video, image, text, message/rfc822)
214 store_generic (CT ct
, mhstoreinfo_t info
)
217 * Check if the content specifies a filename.
218 * Don't bother with this for type "message"
219 * (only "message/rfc822" will use store_generic).
221 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
224 return store_content (ct
, NULL
, info
);
229 * Store content of type "application"
233 store_application (CT ct
, mhstoreinfo_t info
)
235 CI ci
= &ct
->c_ctinfo
;
237 /* Check if the content specifies a filename */
242 * If storeproc is not defined, and the content is type
243 * "application/octet-stream", we also check for various
244 * attribute/value pairs which specify if this a tar file.
246 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
252 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
253 if (strcasecmp (cp
, "tar") == 0)
257 /* check for "conversions=compress" attribute */
258 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
259 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
260 if (strcasecmp (cp
, "compress") == 0 ||
261 strcasecmp (cp
, "x-compress") == 0) {
264 if (strcasecmp (cp
, "gzip") == 0 ||
265 strcasecmp (cp
, "x-gzip") == 0) {
271 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
272 : (gzP
? "%egzip -dc | tar tvf -"
273 : "%etar tvf -"), NULL
);
274 if (!ct
->c_storeproc
) {
276 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
277 : (gzP
? "| gzip -dc | tar xvpf -"
278 : "| tar xvpf -"), NULL
);
281 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
282 : (gzP
? "%m%P.tar.gz"
283 : "%m%P.tar"), NULL
);
289 return store_content (ct
, NULL
, info
);
294 * Store the content of a multipart message
298 store_multi (CT ct
, mhstoreinfo_t info
)
301 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
305 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
306 CT p
= part
->mp_part
;
308 if (part_ok (p
) && type_ok (p
, 1)) {
310 /* Support mhstore -outfile. The MIME parser doesn't
311 load c_storage, so we know that p->c_storage is
313 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
315 result
= store_switch (p
, info
);
317 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
327 * Reassemble and store the contents of a collection
328 * of messages of type "message/partial".
332 store_partial (CT ct
, mhstoreinfo_t info
)
337 struct partial
*pm
, *qm
;
339 qm
= (struct partial
*) ct
->c_ctparams
;
344 for (ctp
= info
->cts
; *ctp
; ctp
++) {
346 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
347 pm
= (struct partial
*) p
->c_ctparams
;
349 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
350 pm
->pm_marked
= pm
->pm_partno
;
362 inform("missing (at least) last part of multipart message");
366 base
= mh_xcalloc(i
+ 1, sizeof *base
);
368 for (ctp
= info
->cts
; *ctp
; ctp
++) {
370 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
371 pm
= (struct partial
*) p
->c_ctparams
;
379 qsort(base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
382 for (ctq
= base
; *ctq
; ctq
++) {
384 pm
= (struct partial
*) p
->c_ctparams
;
385 if (pm
->pm_marked
== cur
) {
390 if (pm
->pm_marked
== cur
- 1) {
391 inform("duplicate part %d of %d part multipart message, continuing...",
397 inform("missing %spart %d of %d part multipart message",
398 cur
!= hi
? "(at least) " : "", cur
, hi
);
407 * Now cycle through the sorted list of messages of type
408 * "message/partial" and save/append them to a file.
413 if (store_content (ct
, NULL
, info
) == NOTOK
) {
419 for (; *ctq
; ctq
++) {
421 if (store_content (p
, ct
, info
) == NOTOK
)
431 * Store content from a message of type "message/external".
435 store_external (CT ct
, mhstoreinfo_t info
)
438 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
439 CT p
= e
->eb_content
;
445 * Check if the parameters for the external body
446 * specified a filename.
451 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
452 if (!ct
->c_storeproc
)
453 ct
->c_storeproc
= mh_xstrdup(cp
);
455 p
->c_storeproc
= mh_xstrdup(cp
);
460 * Since we will let the Content structure for the
461 * external body substitute for the current content,
462 * we temporarily change its partno (number inside
463 * multipart), so everything looks right.
465 p
->c_partno
= ct
->c_partno
;
467 /* we probably need to check if content is really there */
469 /* Support mhstore -outfile. The MIME parser doesn't load
470 c_storage, so we know that p->c_storage is NULL here. */
471 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
473 result
= store_switch (p
, info
);
481 * Compare the numbering from two different
482 * message/partials (needed for sorting).
486 ct_compar (CT
*a
, CT
*b
)
488 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
489 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
491 return am
->pm_marked
- bm
->pm_marked
;
496 * Store contents of a message or message part to
497 * a folder, a file, the standard output, or pass
498 * the contents to a command.
500 * If the current content to be saved is a followup part
501 * to a collection of messages of type "message/partial",
502 * then field "p" is a pointer to the Content structure
503 * to the first message/partial in the group.
507 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
509 bool appending
= false;
511 bool is_partial
= false;
512 bool first_partial
= false;
513 bool last_partial
= false;
514 char *cp
, buffer
[BUFSIZ
];
517 * Do special processing for messages of
518 * type "message/partial".
520 * We first check if this content is of type
521 * "message/partial". If it is, then we need to check
522 * whether it is the first and/or last in the group.
524 * Then if "p" is a valid pointer, it points to the Content
525 * structure of the first partial in the group. So we copy
526 * the file name and/or folder name from that message. In
527 * this case, we also note that we will be appending.
529 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
530 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
532 /* Yep, it's a message/partial */
535 /* But is it the first and/or last in the collection? */
536 if (pm
->pm_partno
== 1)
537 first_partial
= true;
538 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
542 * If "p" is a valid pointer, then it points to the
543 * Content structure for the first message in the group.
544 * So we just copy the filename or foldername information
545 * from the previous iteration of this function.
549 if (! ct
->c_storage
) {
550 ct
->c_storage
= mh_xstrdup(FENDNULL(p
->c_storage
));
552 /* record the folder name */
554 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
562 * Get storage formatting string.
564 * 1) If we have storeproc defined, then use that
565 * 2) Else check for a mhn-store-<type>/<subtype> entry
566 * 3) Else check for a mhn-store-<type> entry
567 * 4) Else if content is "message", use "+" (current folder)
568 * 5) Else use string "%m%P.%s".
570 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
571 CI ci
= &ct
->c_ctinfo
;
573 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
575 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
579 if (! ct
->c_storage
) {
581 * Check the beginning of storage formatting string
582 * to see if we are saving content to a folder.
584 if (*cp
== '+' || *cp
== '@') {
585 char *tmpfilenam
, *folder
;
587 /* Store content in temporary file for now */
588 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
589 die("unable to create temporary file in %s",
592 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
594 /* Get the folder name */
596 folder
= pluspath (cp
);
598 folder
= getfolder (1);
600 /* Check if folder exists */
601 create_folder(m_mailpath(folder
), 0, exit
);
603 /* Record the folder name */
604 ct
->c_folder
= mh_xstrdup(folder
);
613 * Parse and expand the storage formatting string
614 * in `cp' into `buffer'.
616 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
619 * If formatting begins with '|' or '!', then pass
620 * content to standard input of a command and return.
622 if (buffer
[0] == '|' || buffer
[0] == '!')
623 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
625 /* record the filename */
626 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
631 /* The output filename was explicitly specified, so use it. */
632 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
639 /* flush the output stream */
642 /* Now save or append the content to a file */
643 if (output_content_file (ct
, appending
) == NOTOK
)
647 * If necessary, link the file into a folder and remove
648 * the temporary file. If this message is a partial,
649 * then only do this if it is the last one in the group.
651 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
652 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
653 (void) m_unlink (ct
->c_storage
);
660 * Now print out the name/number of the message
661 * that we are storing.
665 fprintf (stderr
, "reassembling partials ");
667 fputs(ct
->c_file
, stderr
);
669 fprintf (stderr
, "%s,", ct
->c_file
);
671 fprintf (stderr
, "storing message %s", ct
->c_file
);
673 fprintf (stderr
, " part %s", ct
->c_partno
);
677 * Unless we are in the "middle" of group of message/partials,
678 * we now print the name of the file, folder, and/or message
679 * to which we are storing the content.
681 if (!is_partial
|| last_partial
) {
683 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
685 } else if (!strcmp(ct
->c_storage
, "-")) {
686 fprintf (stderr
, " to stdout\n");
688 int cwdlen
= strlen (info
->cwd
);
690 fprintf (stderr
, " as file %s\n",
691 !has_prefix(ct
->c_storage
, info
->cwd
)
692 || ct
->c_storage
[cwdlen
] != '/'
693 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
703 * Output content to a file
707 output_content_file (CT ct
, int appending
)
710 char *file
, buffer
[BUFSIZ
];
715 * If the pathname is absolute, make sure
716 * all the relevant directories exist.
718 if (strchr(ct
->c_storage
, '/')
719 && make_intermediates (ct
->c_storage
) == NOTOK
)
722 if (ct
->c_encoding
!= CE_7BIT
) {
725 if (!ct
->c_ceopenfnx
) {
726 inform("don't know how to decode part %s of message %s",
727 ct
->c_partno
, ct
->c_file
);
731 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
733 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
735 if (!strcmp (file
, ct
->c_storage
)) {
736 (*ct
->c_ceclosefnx
) (ct
);
741 * Send to standard output
743 if (!strcmp (ct
->c_storage
, "-")) {
746 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
747 advise ("stdout", "unable to dup");
749 (*ct
->c_ceclosefnx
) (ct
);
752 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
753 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
754 appending
? "a" : "w");
762 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
763 advise (ct
->c_storage
, "unable to fopen for %s",
764 appending
? "appending" : "writing");
770 * Filter the header fields of the initial enclosing
771 * message/partial into the file.
773 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
774 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
776 if (pm
->pm_partno
== 1)
777 copy_some_headers (fp
, ct
);
781 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
783 advise (file
, "error reading content from");
790 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
791 advise ("output_content_file", "fwrite");
798 (*ct
->c_ceclosefnx
) (ct
);
800 if (cc
!= NOTOK
&& fflush (fp
))
801 advise (ct
->c_storage
, "error writing to");
805 return cc
== NOTOK
? NOTOK
: OK
;
808 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
809 advise (ct
->c_file
, "unable to open for reading");
815 fseek (ct
->c_fp
, pos
, SEEK_SET
);
817 if (!strcmp (ct
->c_storage
, "-")) {
820 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
821 advise ("stdout", "unable to dup");
824 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
825 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
826 appending
? "a" : "w");
831 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
832 advise (ct
->c_storage
, "unable to fopen for %s",
833 appending
? "appending" : "writing");
839 * Copy a few of the header fields of the initial
840 * enclosing message/partial into the file.
843 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
844 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
846 if (pm
->pm_partno
== 1) {
847 copy_some_headers (fp
, ct
);
852 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
853 if ((pos
+= strlen (buffer
)) > last
) {
856 diff
= strlen (buffer
) - (pos
- last
);
861 * If this is the first content of a group of
862 * message/partial contents, then we only copy a few
863 * of the header fields of the enclosed message.
878 if (!uprf (buffer
, XXX_FIELD_PRF
)
879 && !uprf (buffer
, VRSN_FIELD
)
880 && !uprf (buffer
, "Subject:")
881 && !uprf (buffer
, "Encrypted:")
882 && !uprf (buffer
, "Message-ID:")) {
897 advise (ct
->c_storage
, "error writing to");
907 * Add a file to a folder.
909 * Return the new message number of the file
910 * when added to the folder. Return -1, if
915 output_content_folder (char *folder
, char *filename
)
920 /* Read the folder. */
921 if (!(mp
= folder_read(folder
, 0))) {
922 inform("unable to read folder %s", folder
);
925 /* Link file into folder */
926 msgnum
= folder_addmsg(&mp
, filename
, 0, 0, 0, 0, NULL
);
928 /* free folder structure */
932 * Return msgnum. We are relying on the fact that
933 * msgnum will be -1, if folder_addmsg() had an error.
940 * Parse and expand the storage formatting string
941 * pointed to by "cp" into "buffer".
945 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
949 CI ci
= &ct
->c_ctinfo
;
952 * If storage string is "-", just copy it, and
953 * return (send content to standard output).
955 if (cp
[0] == '-' && cp
[1] == '\0') {
956 strncpy (buffer
, cp
, buflen
);
964 * If formatting string is a pathname that doesn't
965 * begin with '/', then preface the path with the
966 * appropriate directory.
968 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
969 if (!strcmp(dir
, "/"))
970 dir
= ""; /* Don't start with "//". */
971 snprintf (bp
, buflen
, "%s/", dir
);
979 /* We are processing a storage escape */
984 * Insert parameters from Content-Type.
985 * This is only valid for '|' commands.
987 if (buffer
[0] != '|' && buffer
[0] != '!') {
997 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
998 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
999 pm
->pm_name
, get_param_value(pm
, '?'));
1009 /* insert message number */
1010 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1014 /* insert part number with leading dot */
1016 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1020 /* insert part number without leading dot */
1022 strncpy (bp
, ct
->c_partno
, buflen
);
1026 /* insert content type */
1027 strncpy (bp
, ci
->ci_type
, buflen
);
1031 /* insert content subtype */
1032 strncpy (bp
, ci
->ci_subtype
, buflen
);
1036 /* insert the character % */
1046 /* Advance bp and decrement buflen */
1064 * Check if the content specifies a filename
1065 * in its MIME parameters.
1069 get_storeproc (CT ct
)
1075 * If the storeproc has already been defined,
1076 * we just return (for instance, if this content
1077 * is part of a "message/external".
1079 if (ct
->c_storeproc
)
1083 * If there's a Content-Disposition header and it has a filename,
1084 * use that (RFC-2183).
1087 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1088 use_param_as_filename(cp
)) {
1089 ct
->c_storeproc
= mh_xstrdup(cp
);
1097 * Check the attribute/value pairs, for the attribute "name".
1098 * If found, do a few sanity checks and copy the value into
1102 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1103 use_param_as_filename(cp
)) {
1104 ct
->c_storeproc
= mh_xstrdup(cp
);
1112 * Copy some of the header fields of the initial message/partial
1113 * message into the header of the reassembled message.
1117 copy_some_headers (FILE *out
, CT ct
)
1121 hp
= ct
->c_first_hf
; /* start at first header field */
1125 * A few of the header fields of the enclosing
1126 * messages are not copied.
1128 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1129 && strcasecmp (hp
->name
, VRSN_FIELD
)
1130 && strcasecmp (hp
->name
, "Subject")
1131 && strcasecmp (hp
->name
, "Encrypted")
1132 && strcasecmp (hp
->name
, "Message-ID"))
1133 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1134 hp
= hp
->next
; /* next header field */
1140 /******************************************************************************/
1141 /* -clobber support */
1143 static enum clobber_policy_t
1144 clobber_policy (const char *value
)
1146 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1147 return NMH_CLOBBER_ALWAYS
;
1149 if (! strcasecmp (value
, "auto")) {
1150 return NMH_CLOBBER_AUTO
;
1152 if (! strcasecmp (value
, "suffix")) {
1153 return NMH_CLOBBER_SUFFIX
;
1155 if (! strcasecmp (value
, "ask")) {
1156 return NMH_CLOBBER_ASK
;
1158 if (! strcasecmp (value
, "never")) {
1159 return NMH_CLOBBER_NEVER
;
1162 die("invalid argument, %s, to clobber", value
);
1167 next_version (char *file
, enum clobber_policy_t clobber_policy
)
1169 const size_t max_versions
= 1000000;
1170 /* 8 = log max_versions + one for - or . + one for null terminator */
1171 const size_t buflen
= strlen (file
) + 8;
1172 char *buffer
= mh_xmalloc (buflen
);
1175 char *extension
= NULL
;
1176 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1177 ((extension
= strrchr (file
, '.')) != NULL
)) {
1178 *extension
++ = '\0';
1181 for (version
= 1; version
< max_versions
; ++version
) {
1184 switch (clobber_policy
) {
1185 case NMH_CLOBBER_AUTO
: {
1186 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1187 extension
== NULL
? "" : ".",
1188 extension
== NULL
? "" : extension
);
1192 case NMH_CLOBBER_SUFFIX
:
1193 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1197 /* Should never get here. */
1198 inform("will not overwrite %s, invalid clobber policy", buffer
);
1203 /* Actually (try to) create the file here to avoid a race
1204 condition on file naming + creation. This won't solve the
1205 problem with old NFS that doesn't support O_EXCL, though.
1206 Let the umask strip off permissions from 0666 as desired.
1207 That's what fopen () would do if it was creating the file. */
1208 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1209 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1210 S_IROTH
| S_IWOTH
)) >= 0) {
1218 if (version
>= max_versions
) {
1219 inform("will not overwrite %s, too many versions", buffer
);
1229 clobber_check (char *original_file
, mhstoreinfo_t info
)
1231 /* clobber policy return value
1232 * -------------- ------------
1233 * -always original_file
1234 * -auto original_file-<digits>.extension
1235 * -suffix original_file.<digits>
1236 * -ask original_file, 0, or another filename/path
1244 if (! strcmp (original_file
, "-")) {
1245 return original_file
;
1248 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1249 /* Save cwd for possible use in loop below. */
1252 cwd
= mh_xstrdup(original_file
);
1253 slash
= strrchr (cwd
, '/');
1258 /* original_file isn't a full path, which should only happen if
1268 file
= original_file
;
1269 check_again
= false;
1271 switch (info
->clobber_policy
) {
1272 case NMH_CLOBBER_ALWAYS
:
1275 case NMH_CLOBBER_SUFFIX
:
1276 case NMH_CLOBBER_AUTO
:
1277 if (stat (file
, &st
) == OK
) {
1278 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1280 ++info
->files_not_clobbered
;
1285 case NMH_CLOBBER_ASK
:
1286 if (stat (file
, &st
) == OK
) {
1287 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1288 static struct swit answer
[4] = {
1289 { "yes", 0, NMH_YES
},
1290 { "no", 0, NMH_NO
},
1291 { "rename", 0, NMH_RENAME
},
1295 if (isatty (fileno (stdin
))) {
1297 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1298 ans
= read_switch_multiword (prompt
, answer
);
1301 /* Overwrite, that's what nmh used to do. And warn. */
1302 inform("-clobber ask but no tty, so overwrite %s", file
);
1306 switch ((enum answers
) smatch (*ans
, answer
)) {
1312 ++info
->files_not_clobbered
;
1316 fputs("Enter filename or full path of the new file: ", stdout
);
1317 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1320 ++info
->files_not_clobbered
;
1322 trim_suffix_c(buf
, '\n');
1327 if (buf
[0] == '/') {
1328 /* Full path, use it. */
1329 file
= mh_xstrdup(buf
);
1331 /* Relative path. */
1332 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1342 case NMH_CLOBBER_NEVER
:
1343 if (stat (file
, &st
) == OK
) {
1344 /* Keep count of files that would have been clobbered,
1345 and return that as process exit status. */
1346 inform("will not overwrite %s with -clobber never", file
);
1349 ++info
->files_not_clobbered
;
1354 original_file
= file
;
1355 } while (check_again
);
1363 use_param_as_filename(const char *p
)
1365 /* Preserve result of original test that considered an empty string
1367 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1370 /* -clobber support */
1371 /******************************************************************************/