3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
20 enum clobber_policy_t
{
21 NMH_CLOBBER_ALWAYS
= 0,
28 static enum clobber_policy_t
clobber_policy (const char *);
31 CT
*cts
; /* Top-level list of contents to store. */
32 char *cwd
; /* cached current directory */
33 int autosw
; /* -auto enabled */
34 int verbosw
; /* -verbose enabled */
35 int files_not_clobbered
; /* output flag indicating that store failed
36 in order to not clobber an existing file */
38 /* The following must never be touched by a caller: they are for
39 internal use by the mhstoresbr functions. */
40 char *dir
; /* directory in which to store contents */
41 enum clobber_policy_t clobber_policy
; /* -clobber selection */
44 typedef struct mhstoreinfo
*mhstoreinfo_t
;
47 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
) {
48 mhstoreinfo_t info
= mh_xmalloc (sizeof *info
);
54 info
->files_not_clobbered
= 0;
56 info
->clobber_policy
= clobber_policy (csw
);
62 mhstoreinfo_free (mhstoreinfo_t info
) {
68 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
) {
69 return info
->files_not_clobbered
;
74 * Type for a compare function for qsort. This keeps
77 typedef int (*qsort_comp
) (const void *, const void *);
81 int part_ok (CT
, int);
82 int type_ok (CT
, int);
83 void flush_errors (void);
88 void store_all_messages (mhstoreinfo_t
);
93 static void store_single_message (CT
, mhstoreinfo_t
);
94 static int store_switch (CT
, mhstoreinfo_t
);
95 static int store_generic (CT
, mhstoreinfo_t
);
96 static int store_application (CT
, mhstoreinfo_t
);
97 static int store_multi (CT
, mhstoreinfo_t
);
98 static int store_partial (CT
, mhstoreinfo_t
);
99 static int store_external (CT
, mhstoreinfo_t
);
100 static int ct_compar (CT
*, CT
*);
101 static int store_content (CT
, CT
, mhstoreinfo_t
);
102 static int output_content_file (CT
, int);
103 static int output_content_folder (char *, char *);
104 static int parse_format_string (CT
, char *, char *, int, char *);
105 static void get_storeproc (CT
);
106 static int copy_some_headers (FILE *, CT
);
107 static char *clobber_check (char *, mhstoreinfo_t
);
110 * Main entry point to store content
111 * from a collection of messages.
115 store_all_messages (mhstoreinfo_t info
)
121 * Check for the directory in which to
122 * store any contents.
124 if ((cp
= context_find (nmhstorage
)) && *cp
)
125 info
->dir
= getcpy (cp
);
127 info
->dir
= getcpy (info
->cwd
);
129 for (ctp
= info
->cts
; *ctp
; ctp
++) {
131 store_single_message (ct
, info
);
139 * Entry point to store the content
140 * in a (single) message
144 store_single_message (CT ct
, mhstoreinfo_t info
)
146 if (type_ok (ct
, 1)) {
148 store_switch (ct
, info
);
153 if (ct
->c_ceclosefnx
)
154 (*ct
->c_ceclosefnx
) (ct
);
160 * Switching routine to store different content types
164 store_switch (CT ct
, mhstoreinfo_t info
)
166 switch (ct
->c_type
) {
168 return store_multi (ct
, info
);
171 switch (ct
->c_subtype
) {
172 case MESSAGE_PARTIAL
:
173 return store_partial (ct
, info
);
175 case MESSAGE_EXTERNAL
:
176 return store_external (ct
, info
);
180 return store_generic (ct
, info
);
185 return store_application (ct
, info
);
191 return store_generic (ct
, info
);
194 return OK
; /* NOT REACHED */
199 * Generic routine to store a MIME content.
200 * (audio, video, image, text, message/rfc822)
204 store_generic (CT ct
, mhstoreinfo_t info
)
207 * Check if the content specifies a filename.
208 * Don't bother with this for type "message"
209 * (only "message/rfc822" will use store_generic).
211 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
214 return store_content (ct
, NULL
, info
);
219 * Store content of type "application"
223 store_application (CT ct
, mhstoreinfo_t info
)
225 CI ci
= &ct
->c_ctinfo
;
227 /* Check if the content specifies a filename */
232 * If storeproc is not defined, and the content is type
233 * "application/octet-stream", we also check for various
234 * attribute/value pairs which specify if this a tar file.
236 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
237 int tarP
= 0, zP
= 0, gzP
= 0;
240 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
241 if (strcasecmp (cp
, "tar") == 0)
245 /* check for "conversions=compress" attribute */
246 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
247 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
248 if (strcasecmp (cp
, "compress") == 0 ||
249 strcasecmp (cp
, "x-compress") == 0) {
252 if (strcasecmp (cp
, "gzip") == 0 ||
253 strcasecmp (cp
, "x-gzip") == 0) {
259 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
260 : (gzP
? "%egzip -dc | tar tvf -"
261 : "%etar tvf -"), NULL
);
262 if (!ct
->c_storeproc
) {
264 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
265 : (gzP
? "| gzip -dc | tar xvpf -"
266 : "| tar xvpf -"), NULL
);
269 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
270 : (gzP
? "%m%P.tar.gz"
271 : "%m%P.tar"), NULL
);
277 return store_content (ct
, NULL
, info
);
282 * Store the content of a multipart message
286 store_multi (CT ct
, mhstoreinfo_t info
)
289 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
293 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
294 CT p
= part
->mp_part
;
296 if (part_ok (p
, 1) && type_ok (p
, 1)) {
298 /* Support mhstore -outfile. The MIME parser doesn't
299 load c_storage, so we know that p->c_storage is
301 p
->c_storage
= ct
->c_storage
;
303 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 advise (NULL
, "missing (at least) last part of multipart message");
355 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
356 adios (NULL
, "out of memory");
359 for (ctp
= info
->cts
; *ctp
; ctp
++) {
361 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
362 pm
= (struct partial
*) p
->c_ctparams
;
370 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
373 for (ctq
= base
; *ctq
; ctq
++) {
375 pm
= (struct partial
*) p
->c_ctparams
;
376 if (pm
->pm_marked
!= cur
) {
377 if (pm
->pm_marked
== cur
- 1) {
379 "duplicate part %d of %d part multipart message",
386 "missing %spart %d of %d part multipart message",
387 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
) {
407 free ((char *) base
);
411 for (; *ctq
; ctq
++) {
413 if (store_content (p
, ct
, info
) == NOTOK
)
417 free ((char *) base
);
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
)
448 && !strchr (cp
, '%')) {
449 if (!ct
->c_storeproc
)
450 ct
->c_storeproc
= add (cp
, NULL
);
452 p
->c_storeproc
= add (cp
, NULL
);
457 * Since we will let the Content structure for the
458 * external body substitute for the current content,
459 * we temporarily change its partno (number inside
460 * multipart), so everything looks right.
462 p
->c_partno
= ct
->c_partno
;
464 /* we probably need to check if content is really there */
466 /* Support mhstore -outfile. The MIME parser doesn't load
467 c_storage, so we know that p->c_storage is NULL here. */
468 p
->c_storage
= ct
->c_storage
;
470 result
= store_switch (p
, info
);
479 * Compare the numbering from two different
480 * message/partials (needed for sorting).
484 ct_compar (CT
*a
, CT
*b
)
486 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
487 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
489 return (am
->pm_marked
- bm
->pm_marked
);
494 * Store contents of a message or message part to
495 * a folder, a file, the standard output, or pass
496 * the contents to a command.
498 * If the current content to be saved is a followup part
499 * to a collection of messages of type "message/partial",
500 * then field "p" is a pointer to the Content structure
501 * to the first message/partial in the group.
505 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
507 int appending
= 0, msgnum
= 0;
508 int is_partial
= 0, first_partial
= 0;
509 int last_partial
= 0;
510 char *cp
, buffer
[BUFSIZ
];
513 * Do special processing for messages of
514 * type "message/partial".
516 * We first check if this content is of type
517 * "message/partial". If it is, then we need to check
518 * whether it is the first and/or last in the group.
520 * Then if "p" is a valid pointer, it points to the Content
521 * structure of the first partial in the group. So we copy
522 * the file name and/or folder name from that message. In
523 * this case, we also note that we will be appending.
525 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
526 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
528 /* Yep, it's a message/partial */
531 /* But is it the first and/or last in the collection? */
532 if (pm
->pm_partno
== 1)
534 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
538 * If "p" is a valid pointer, then it points to the
539 * Content structure for the first message in the group.
540 * So we just copy the filename or foldername information
541 * from the previous iteration of this function.
545 if (! ct
->c_storage
) {
546 ct
->c_storage
= add (p
->c_storage
, NULL
);
548 /* record the folder name */
550 ct
->c_folder
= add (p
->c_folder
, NULL
);
558 * Get storage formatting string.
560 * 1) If we have storeproc defined, then use that
561 * 2) Else check for a mhn-store-<type>/<subtype> entry
562 * 3) Else check for a mhn-store-<type> entry
563 * 4) Else if content is "message", use "+" (current folder)
564 * 5) Else use string "%m%P.%s".
566 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
567 CI ci
= &ct
->c_ctinfo
;
569 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
571 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
575 if (! ct
->c_storage
) {
577 * Check the beginning of storage formatting string
578 * to see if we are saving content to a folder.
580 if (*cp
== '+' || *cp
== '@') {
581 char *tmpfilenam
, *folder
;
583 /* Store content in temporary file for now */
584 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
585 adios(NULL
, "unable to create temporary file in %s",
588 ct
->c_storage
= add (tmpfilenam
, NULL
);
590 /* Get the folder name */
592 folder
= pluspath (cp
);
594 folder
= getfolder (1);
596 /* Check if folder exists */
597 create_folder(m_mailpath(folder
), 0, exit
);
599 /* Record the folder name */
600 ct
->c_folder
= add (folder
, NULL
);
609 * Parse and expand the storage formatting string
610 * in `cp' into `buffer'.
612 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
615 * If formatting begins with '|' or '!', then pass
616 * content to standard input of a command and return.
618 if (buffer
[0] == '|' || buffer
[0] == '!')
619 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
621 /* record the filename */
622 if ((ct
->c_storage
= clobber_check (add (buffer
, NULL
), info
)) ==
627 /* The output filename was explicitly specified, so use it. */
628 if ((ct
->c_storage
= clobber_check (add (ct
->c_storage
, NULL
), info
)) ==
635 /* flush the output stream */
638 /* Now save or append the content to a file */
639 if (output_content_file (ct
, appending
) == NOTOK
)
643 * If necessary, link the file into a folder and remove
644 * the temporary file. If this message is a partial,
645 * then only do this if it is the last one in the group.
647 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
648 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
649 (void) m_unlink (ct
->c_storage
);
656 * Now print out the name/number of the message
657 * that we are storing.
661 fprintf (stderr
, "reassembling partials ");
663 fprintf (stderr
, "%s", ct
->c_file
);
665 fprintf (stderr
, "%s,", ct
->c_file
);
667 fprintf (stderr
, "storing message %s", ct
->c_file
);
669 fprintf (stderr
, " part %s", ct
->c_partno
);
673 * Unless we are in the "middle" of group of message/partials,
674 * we now print the name of the file, folder, and/or message
675 * to which we are storing the content.
677 if (!is_partial
|| last_partial
) {
679 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
681 } else if (!strcmp(ct
->c_storage
, "-")) {
682 fprintf (stderr
, " to stdout\n");
684 int cwdlen
= strlen (info
->cwd
);
686 fprintf (stderr
, " as file %s\n",
687 strncmp (ct
->c_storage
, info
->cwd
, cwdlen
)
688 || ct
->c_storage
[cwdlen
] != '/'
689 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
699 * Output content to a file
703 output_content_file (CT ct
, int appending
)
706 char *file
, buffer
[BUFSIZ
];
711 * If the pathname is absolute, make sure
712 * all the relevant directories exist.
714 if (strchr(ct
->c_storage
, '/')
715 && make_intermediates (ct
->c_storage
) == NOTOK
)
718 if (ct
->c_encoding
!= CE_7BIT
) {
721 if (!ct
->c_ceopenfnx
) {
722 advise (NULL
, "don't know how to decode part %s of message %s",
723 ct
->c_partno
, ct
->c_file
);
727 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
729 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
731 if (!strcmp (file
, ct
->c_storage
)) {
732 (*ct
->c_ceclosefnx
) (ct
);
737 * Send to standard output
739 if (!strcmp (ct
->c_storage
, "-")) {
742 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
743 advise ("stdout", "unable to dup");
745 (*ct
->c_ceclosefnx
) (ct
);
748 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
749 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
750 appending
? "a" : "w");
758 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
759 advise (ct
->c_storage
, "unable to fopen for %s",
760 appending
? "appending" : "writing");
766 * Filter the header fields of the initial enclosing
767 * message/partial into the file.
769 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
770 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
772 if (pm
->pm_partno
== 1)
773 copy_some_headers (fp
, ct
);
777 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
779 advise (file
, "error reading content from");
786 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
792 (*ct
->c_ceclosefnx
) (ct
);
794 if (cc
!= NOTOK
&& fflush (fp
))
795 advise (ct
->c_storage
, "error writing to");
799 return (cc
!= NOTOK
? OK
: NOTOK
);
802 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
803 advise (ct
->c_file
, "unable to open for reading");
809 fseek (ct
->c_fp
, pos
, SEEK_SET
);
811 if (!strcmp (ct
->c_storage
, "-")) {
814 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
815 advise ("stdout", "unable to dup");
818 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
819 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
820 appending
? "a" : "w");
825 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
826 advise (ct
->c_storage
, "unable to fopen for %s",
827 appending
? "appending" : "writing");
833 * Copy a few of the header fields of the initial
834 * enclosing message/partial into the file.
837 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
838 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
840 if (pm
->pm_partno
== 1) {
841 copy_some_headers (fp
, ct
);
846 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
847 if ((pos
+= strlen (buffer
)) > last
) {
850 diff
= strlen (buffer
) - (pos
- last
);
855 * If this is the first content of a group of
856 * message/partial contents, then we only copy a few
857 * of the header fields of the enclosed message.
872 if (!uprf (buffer
, XXX_FIELD_PRF
)
873 && !uprf (buffer
, VRSN_FIELD
)
874 && !uprf (buffer
, "Subject:")
875 && !uprf (buffer
, "Encrypted:")
876 && !uprf (buffer
, "Message-ID:")) {
891 advise (ct
->c_storage
, "error writing to");
901 * Add a file to a folder.
903 * Return the new message number of the file
904 * when added to the folder. Return -1, if
909 output_content_folder (char *folder
, char *filename
)
914 /* Read the folder. */
915 if ((mp
= folder_read (folder
, 0))) {
916 /* Link file into folder */
917 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
919 advise (NULL
, "unable to read folder %s", folder
);
923 /* free folder structure */
927 * Return msgnum. We are relying on the fact that
928 * msgnum will be -1, if folder_addmsg() had an error.
935 * Parse and expand the storage formatting string
936 * pointed to by "cp" into "buffer".
940 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
944 CI ci
= &ct
->c_ctinfo
;
947 * If storage string is "-", just copy it, and
948 * return (send content to standard output).
950 if (cp
[0] == '-' && cp
[1] == '\0') {
951 strncpy (buffer
, cp
, buflen
);
959 * If formatting string is a pathname that doesn't
960 * begin with '/', then preface the path with the
961 * appropriate directory.
963 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
964 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
972 /* We are processing a storage escape */
977 * Insert parameters from Content-Type.
978 * This is only valid for '|' commands.
980 if (buffer
[0] != '|' && buffer
[0] != '!') {
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 withouth 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))
1084 && !strchr (cp
, '%')) {
1085 ct
->c_storeproc
= add (cp
, NULL
);
1094 * Check the attribute/value pairs, for the attribute "name".
1095 * If found, do a few sanity checks and copy the value into
1099 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0))
1104 && !strchr (cp
, '%')) {
1105 ct
->c_storeproc
= add (cp
, NULL
);
1114 * Copy some of the header fields of the initial message/partial
1115 * message into the header of the reassembled message.
1119 copy_some_headers (FILE *out
, CT ct
)
1123 hp
= ct
->c_first_hf
; /* start at first header field */
1127 * A few of the header fields of the enclosing
1128 * messages are not copied.
1130 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1131 && strcasecmp (hp
->name
, VRSN_FIELD
)
1132 && strcasecmp (hp
->name
, "Subject")
1133 && strcasecmp (hp
->name
, "Encrypted")
1134 && strcasecmp (hp
->name
, "Message-ID"))
1135 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1136 hp
= hp
->next
; /* next header field */
1142 /******************************************************************************/
1143 /* -clobber support */
1146 enum clobber_policy_t
1147 clobber_policy (const char *value
) {
1148 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1149 } else if (! strcasecmp (value
, "auto")) {
1150 return NMH_CLOBBER_AUTO
;
1151 } else if (! strcasecmp (value
, "suffix")) {
1152 return NMH_CLOBBER_SUFFIX
;
1153 } else if (! strcasecmp (value
, "ask")) {
1154 return NMH_CLOBBER_ASK
;
1155 } else if (! strcasecmp (value
, "never")) {
1156 return NMH_CLOBBER_NEVER
;
1158 adios (NULL
, "invalid argument, %s, to clobber", value
);
1161 return NMH_CLOBBER_ALWAYS
;
1166 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1167 const size_t max_versions
= 1000000;
1168 /* 8 = log max_versions + one for - or . + one for null terminator */
1169 const size_t buflen
= strlen (file
) + 8;
1170 char *buffer
= mh_xmalloc (buflen
);
1173 char *extension
= NULL
;
1174 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1175 ((extension
= strrchr (file
, '.')) != NULL
)) {
1176 *extension
++ = '\0';
1179 for (version
= 1; version
< max_versions
; ++version
) {
1182 switch (clobber_policy
) {
1183 case NMH_CLOBBER_AUTO
: {
1184 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1185 extension
== NULL
? "" : ".",
1186 extension
== NULL
? "" : extension
);
1190 case NMH_CLOBBER_SUFFIX
:
1191 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1195 /* Should never get here. */
1196 advise (NULL
, "will not overwrite %s, invalid clobber policy", buffer
);
1201 /* Actually (try to) create the file here to avoid a race
1202 condition on file naming + creation. This won't solve the
1203 problem with old NFS that doesn't support O_EXCL, though.
1204 Let the umask strip off permissions from 0666 as desired.
1205 That's what fopen () would do if it was creating the file. */
1206 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1207 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1208 S_IROTH
| S_IWOTH
)) >= 0) {
1216 if (version
>= max_versions
) {
1217 advise (NULL
, "will not overwrite %s, too many versions", buffer
);
1227 clobber_check (char *original_file
, mhstoreinfo_t info
) {
1228 /* clobber policy return value
1229 * -------------- ------------
1230 * -always original_file
1231 * -auto original_file-<digits>.extension
1232 * -suffix original_file.<digits>
1233 * -ask original_file, 0, or another filename/path
1241 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1242 /* Save cwd for possible use in loop below. */
1245 cwd
= add (original_file
, NULL
);
1246 slash
= strrchr (cwd
, '/');
1251 /* original_file wasn't a full path, which shouldn't happen. */
1259 file
= original_file
;
1262 switch (info
->clobber_policy
) {
1263 case NMH_CLOBBER_ALWAYS
:
1266 case NMH_CLOBBER_SUFFIX
:
1267 case NMH_CLOBBER_AUTO
:
1268 if (stat (file
, &st
) == OK
) {
1269 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1271 ++info
->files_not_clobbered
;
1276 case NMH_CLOBBER_ASK
:
1277 if (stat (file
, &st
) == OK
) {
1278 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1279 static struct swit answer
[4] = {
1280 { "yes", 0, NMH_YES
},
1281 { "no", 0, NMH_NO
},
1282 { "rename", 0, NMH_RENAME
},
1286 if (isatty (fileno (stdin
))) {
1288 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1289 ans
= getans (prompt
, answer
);
1292 /* Overwrite, that's what nmh used to do. And warn. */
1293 advise (NULL
, "-clobber ask but no tty, so overwrite %s", file
);
1297 switch ((enum answers
) smatch (*ans
, answer
)) {
1303 ++info
->files_not_clobbered
;
1307 printf ("Enter filename or full path of the new file: ");
1308 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1311 ++info
->files_not_clobbered
;
1313 char *newline
= strchr (buf
, '\n');
1321 if (buf
[0] == '/') {
1322 /* Full path, use it. */
1323 file
= add (buf
, NULL
);
1325 /* Relative path. */
1326 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : add (buf
, NULL
);
1336 case NMH_CLOBBER_NEVER
:
1337 if (stat (file
, &st
) == OK
) {
1338 /* Keep count of files that would have been clobbered,
1339 and return that as process exit status. */
1340 advise (NULL
, "will not overwrite %s with -clobber never", file
);
1343 ++info
->files_not_clobbered
;
1348 original_file
= file
;
1349 } while (check_again
);
1358 /* -clobber support */
1359 /******************************************************************************/