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);
86 int show_content_aux (CT
, int, int, char *, char *);
91 void store_all_messages (mhstoreinfo_t
);
96 static void store_single_message (CT
, mhstoreinfo_t
);
97 static int store_switch (CT
, mhstoreinfo_t
);
98 static int store_generic (CT
, mhstoreinfo_t
);
99 static int store_application (CT
, mhstoreinfo_t
);
100 static int store_multi (CT
, mhstoreinfo_t
);
101 static int store_partial (CT
, mhstoreinfo_t
);
102 static int store_external (CT
, mhstoreinfo_t
);
103 static int ct_compar (CT
*, CT
*);
104 static int store_content (CT
, CT
, mhstoreinfo_t
);
105 static int output_content_file (CT
, int);
106 static int output_content_folder (char *, char *);
107 static int parse_format_string (CT
, char *, char *, int, char *);
108 static void get_storeproc (CT
);
109 static int copy_some_headers (FILE *, CT
);
110 static char *clobber_check (char *, mhstoreinfo_t
);
113 * Main entry point to store content
114 * from a collection of messages.
118 store_all_messages (mhstoreinfo_t info
)
124 * Check for the directory in which to
125 * store any contents.
127 if ((cp
= context_find (nmhstorage
)) && *cp
)
128 info
->dir
= getcpy (cp
);
130 info
->dir
= getcpy (info
->cwd
);
132 for (ctp
= info
->cts
; *ctp
; ctp
++) {
134 store_single_message (ct
, info
);
142 * Entry point to store the content
143 * in a (single) message
147 store_single_message (CT ct
, mhstoreinfo_t info
)
149 if (type_ok (ct
, 1)) {
151 store_switch (ct
, info
);
156 if (ct
->c_ceclosefnx
)
157 (*ct
->c_ceclosefnx
) (ct
);
163 * Switching routine to store different content types
167 store_switch (CT ct
, mhstoreinfo_t info
)
169 switch (ct
->c_type
) {
171 return store_multi (ct
, info
);
174 switch (ct
->c_subtype
) {
175 case MESSAGE_PARTIAL
:
176 return store_partial (ct
, info
);
178 case MESSAGE_EXTERNAL
:
179 return store_external (ct
, info
);
183 return store_generic (ct
, info
);
188 return store_application (ct
, info
);
194 return store_generic (ct
, info
);
197 return OK
; /* NOT REACHED */
202 * Generic routine to store a MIME content.
203 * (audio, video, image, text, message/rfc822)
207 store_generic (CT ct
, mhstoreinfo_t info
)
210 * Check if the content specifies a filename.
211 * Don't bother with this for type "message"
212 * (only "message/rfc822" will use store_generic).
214 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
217 return store_content (ct
, NULL
, info
);
222 * Store content of type "application"
226 store_application (CT ct
, mhstoreinfo_t info
)
228 CI ci
= &ct
->c_ctinfo
;
230 /* Check if the content specifies a filename */
235 * If storeproc is not defined, and the content is type
236 * "application/octet-stream", we also check for various
237 * attribute/value pairs which specify if this a tar file.
239 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
240 int tarP
= 0, zP
= 0, gzP
= 0;
243 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
244 if (strcasecmp (cp
, "tar") == 0)
248 /* check for "conversions=compress" attribute */
249 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
250 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
251 if (strcasecmp (cp
, "compress") == 0 ||
252 strcasecmp (cp
, "x-compress") == 0) {
255 if (strcasecmp (cp
, "gzip") == 0 ||
256 strcasecmp (cp
, "x-gzip") == 0) {
262 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
263 : (gzP
? "%egzip -dc | tar tvf -"
264 : "%etar tvf -"), NULL
);
265 if (!ct
->c_storeproc
) {
267 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
268 : (gzP
? "| gzip -dc | tar xvpf -"
269 : "| tar xvpf -"), NULL
);
272 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
273 : (gzP
? "%m%P.tar.gz"
274 : "%m%P.tar"), NULL
);
280 return store_content (ct
, NULL
, info
);
285 * Store the content of a multipart message
289 store_multi (CT ct
, mhstoreinfo_t info
)
292 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
296 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
297 CT p
= part
->mp_part
;
299 if (part_ok (p
, 1) && type_ok (p
, 1)) {
301 /* Support mhstore -outfile. The MIME parser doesn't
302 load c_storage, so we know that p->c_storage is
304 p
->c_storage
= ct
->c_storage
;
306 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 advise (NULL
, "missing (at least) last part of multipart message");
358 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
359 adios (NULL
, "out of memory");
362 for (ctp
= info
->cts
; *ctp
; ctp
++) {
364 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
365 pm
= (struct partial
*) p
->c_ctparams
;
373 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
376 for (ctq
= base
; *ctq
; ctq
++) {
378 pm
= (struct partial
*) p
->c_ctparams
;
379 if (pm
->pm_marked
!= cur
) {
380 if (pm
->pm_marked
== cur
- 1) {
382 "duplicate part %d of %d part multipart message",
389 "missing %spart %d of %d part multipart message",
390 cur
!= hi
? "(at least) " : "", cur
, hi
);
402 * Now cycle through the sorted list of messages of type
403 * "message/partial" and save/append them to a file.
408 if (store_content (ct
, NULL
, info
) == NOTOK
) {
410 free ((char *) base
);
414 for (; *ctq
; ctq
++) {
416 if (store_content (p
, ct
, info
) == NOTOK
)
420 free ((char *) base
);
426 * Store content from a message of type "message/external".
430 store_external (CT ct
, mhstoreinfo_t info
)
433 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
434 CT p
= e
->eb_content
;
440 * Check if the parameters for the external body
441 * specified a filename.
446 if ((cp
= e
->eb_name
)
451 && !strchr (cp
, '%')) {
452 if (!ct
->c_storeproc
)
453 ct
->c_storeproc
= add (cp
, NULL
);
455 p
->c_storeproc
= add (cp
, NULL
);
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
= ct
->c_storage
;
473 result
= store_switch (p
, info
);
482 * Compare the numbering from two different
483 * message/partials (needed for sorting).
487 ct_compar (CT
*a
, CT
*b
)
489 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
490 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
492 return (am
->pm_marked
- bm
->pm_marked
);
497 * Store contents of a message or message part to
498 * a folder, a file, the standard output, or pass
499 * the contents to a command.
501 * If the current content to be saved is a followup part
502 * to a collection of messages of type "message/partial",
503 * then field "p" is a pointer to the Content structure
504 * to the first message/partial in the group.
508 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
510 int appending
= 0, msgnum
= 0;
511 int is_partial
= 0, first_partial
= 0;
512 int last_partial
= 0;
513 char *cp
, buffer
[BUFSIZ
];
516 * Do special processing for messages of
517 * type "message/partial".
519 * We first check if this content is of type
520 * "message/partial". If it is, then we need to check
521 * whether it is the first and/or last in the group.
523 * Then if "p" is a valid pointer, it points to the Content
524 * structure of the first partial in the group. So we copy
525 * the file name and/or folder name from that message. In
526 * this case, we also note that we will be appending.
528 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
529 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
531 /* Yep, it's a message/partial */
534 /* But is it the first and/or last in the collection? */
535 if (pm
->pm_partno
== 1)
537 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
541 * If "p" is a valid pointer, then it points to the
542 * Content structure for the first message in the group.
543 * So we just copy the filename or foldername information
544 * from the previous iteration of this function.
548 if (! ct
->c_storage
) {
549 ct
->c_storage
= add (p
->c_storage
, NULL
);
551 /* record the folder name */
553 ct
->c_folder
= add (p
->c_folder
, NULL
);
561 * Get storage formatting string.
563 * 1) If we have storeproc defined, then use that
564 * 2) Else check for a mhn-store-<type>/<subtype> entry
565 * 3) Else check for a mhn-store-<type> entry
566 * 4) Else if content is "message", use "+" (current folder)
567 * 5) Else use string "%m%P.%s".
569 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
570 CI ci
= &ct
->c_ctinfo
;
572 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
573 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
574 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
575 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
576 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
577 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
582 if (! ct
->c_storage
) {
584 * Check the beginning of storage formatting string
585 * to see if we are saving content to a folder.
587 if (*cp
== '+' || *cp
== '@') {
588 char *tmpfilenam
, *folder
;
590 /* Store content in temporary file for now */
591 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
592 adios(NULL
, "unable to create temporary file in %s",
595 ct
->c_storage
= add (tmpfilenam
, NULL
);
597 /* Get the folder name */
599 folder
= pluspath (cp
);
601 folder
= getfolder (1);
603 /* Check if folder exists */
604 create_folder(m_mailpath(folder
), 0, exit
);
606 /* Record the folder name */
607 ct
->c_folder
= add (folder
, NULL
);
616 * Parse and expand the storage formatting string
617 * in `cp' into `buffer'.
619 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
622 * If formatting begins with '|' or '!', then pass
623 * content to standard input of a command and return.
625 if (buffer
[0] == '|' || buffer
[0] == '!')
626 return show_content_aux (ct
, 1, 0, buffer
+ 1, info
->dir
);
628 /* record the filename */
629 if ((ct
->c_storage
= clobber_check (add (buffer
, NULL
), info
)) ==
634 /* The output filename was explicitly specified, so use it. */
635 if ((ct
->c_storage
= clobber_check (add (ct
->c_storage
, NULL
), info
)) ==
642 /* flush the output stream */
645 /* Now save or append the content to a file */
646 if (output_content_file (ct
, appending
) == NOTOK
)
650 * If necessary, link the file into a folder and remove
651 * the temporary file. If this message is a partial,
652 * then only do this if it is the last one in the group.
654 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
655 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
656 (void) m_unlink (ct
->c_storage
);
663 * Now print out the name/number of the message
664 * that we are storing.
668 fprintf (stderr
, "reassembling partials ");
670 fprintf (stderr
, "%s", ct
->c_file
);
672 fprintf (stderr
, "%s,", ct
->c_file
);
674 fprintf (stderr
, "storing message %s", ct
->c_file
);
676 fprintf (stderr
, " part %s", ct
->c_partno
);
680 * Unless we are in the "middle" of group of message/partials,
681 * we now print the name of the file, folder, and/or message
682 * to which we are storing the content.
684 if (!is_partial
|| last_partial
) {
686 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
688 } else if (!strcmp(ct
->c_storage
, "-")) {
689 fprintf (stderr
, " to stdout\n");
691 int cwdlen
= strlen (info
->cwd
);
693 fprintf (stderr
, " as file %s\n",
694 strncmp (ct
->c_storage
, info
->cwd
, cwdlen
)
695 || ct
->c_storage
[cwdlen
] != '/'
696 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
706 * Output content to a file
710 output_content_file (CT ct
, int appending
)
713 char *file
, buffer
[BUFSIZ
];
718 * If the pathname is absolute, make sure
719 * all the relevant directories exist.
721 if (strchr(ct
->c_storage
, '/')
722 && make_intermediates (ct
->c_storage
) == NOTOK
)
725 if (ct
->c_encoding
!= CE_7BIT
) {
728 if (!ct
->c_ceopenfnx
) {
729 advise (NULL
, "don't know how to decode part %s of message %s",
730 ct
->c_partno
, ct
->c_file
);
734 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
736 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
738 if (!strcmp (file
, ct
->c_storage
)) {
739 (*ct
->c_ceclosefnx
) (ct
);
744 * Send to standard output
746 if (!strcmp (ct
->c_storage
, "-")) {
749 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
750 advise ("stdout", "unable to dup");
752 (*ct
->c_ceclosefnx
) (ct
);
755 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
756 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
757 appending
? "a" : "w");
765 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
766 advise (ct
->c_storage
, "unable to fopen for %s",
767 appending
? "appending" : "writing");
773 * Filter the header fields of the initial enclosing
774 * message/partial into the file.
776 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
777 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
779 if (pm
->pm_partno
== 1)
780 copy_some_headers (fp
, ct
);
784 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
786 advise (file
, "error reading content from");
793 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
799 (*ct
->c_ceclosefnx
) (ct
);
801 if (cc
!= NOTOK
&& fflush (fp
))
802 advise (ct
->c_storage
, "error writing to");
806 return (cc
!= NOTOK
? OK
: NOTOK
);
809 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
810 advise (ct
->c_file
, "unable to open for reading");
816 fseek (ct
->c_fp
, pos
, SEEK_SET
);
818 if (!strcmp (ct
->c_storage
, "-")) {
821 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
822 advise ("stdout", "unable to dup");
825 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
826 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
827 appending
? "a" : "w");
832 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
833 advise (ct
->c_storage
, "unable to fopen for %s",
834 appending
? "appending" : "writing");
840 * Copy a few of the header fields of the initial
841 * enclosing message/partial into the file.
844 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
845 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
847 if (pm
->pm_partno
== 1) {
848 copy_some_headers (fp
, ct
);
853 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
854 if ((pos
+= strlen (buffer
)) > last
) {
857 diff
= strlen (buffer
) - (pos
- last
);
862 * If this is the first content of a group of
863 * message/partial contents, then we only copy a few
864 * of the header fields of the enclosed message.
879 if (!uprf (buffer
, XXX_FIELD_PRF
)
880 && !uprf (buffer
, VRSN_FIELD
)
881 && !uprf (buffer
, "Subject:")
882 && !uprf (buffer
, "Encrypted:")
883 && !uprf (buffer
, "Message-ID:")) {
898 advise (ct
->c_storage
, "error writing to");
908 * Add a file to a folder.
910 * Return the new message number of the file
911 * when added to the folder. Return -1, if
916 output_content_folder (char *folder
, char *filename
)
921 /* Read the folder. */
922 if ((mp
= folder_read (folder
, 0))) {
923 /* Link file into folder */
924 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
926 advise (NULL
, "unable to read folder %s", folder
);
930 /* free folder structure */
934 * Return msgnum. We are relying on the fact that
935 * msgnum will be -1, if folder_addmsg() had an error.
942 * Parse and expand the storage formatting string
943 * pointed to by "cp" into "buffer".
947 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
951 CI ci
= &ct
->c_ctinfo
;
954 * If storage string is "-", just copy it, and
955 * return (send content to standard output).
957 if (cp
[0] == '-' && cp
[1] == '\0') {
958 strncpy (buffer
, cp
, buflen
);
966 * If formatting string is a pathname that doesn't
967 * begin with '/', then preface the path with the
968 * appropriate directory.
970 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
971 snprintf (bp
, buflen
, "%s/", dir
[1] ? 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] != '!') {
996 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
997 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
998 pm
->pm_name
, get_param_value(pm
, '?'));
1008 /* insert message number */
1009 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
1013 /* insert part number with leading dot */
1015 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
1019 /* insert part number withouth leading dot */
1021 strncpy (bp
, ct
->c_partno
, buflen
);
1025 /* insert content type */
1026 strncpy (bp
, ci
->ci_type
, buflen
);
1030 /* insert content subtype */
1031 strncpy (bp
, ci
->ci_subtype
, buflen
);
1035 /* insert the character % */
1045 /* Advance bp and decrement buflen */
1063 * Check if the content specifies a filename
1064 * in its MIME parameters.
1068 get_storeproc (CT ct
)
1074 * If the storeproc has already been defined,
1075 * we just return (for instance, if this content
1076 * is part of a "message/external".
1078 if (ct
->c_storeproc
)
1082 * If there's a Content-Disposition header and it has a filename,
1083 * use that (RFC-2183).
1086 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0))
1091 && !strchr (cp
, '%')) {
1092 ct
->c_storeproc
= add (cp
, NULL
);
1101 * Check the attribute/value pairs, for the attribute "name".
1102 * If found, do a few sanity checks and copy the value into
1106 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0))
1111 && !strchr (cp
, '%')) {
1112 ct
->c_storeproc
= add (cp
, NULL
);
1121 * Copy some of the header fields of the initial message/partial
1122 * message into the header of the reassembled message.
1126 copy_some_headers (FILE *out
, CT ct
)
1130 hp
= ct
->c_first_hf
; /* start at first header field */
1134 * A few of the header fields of the enclosing
1135 * messages are not copied.
1137 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1138 && strcasecmp (hp
->name
, VRSN_FIELD
)
1139 && strcasecmp (hp
->name
, "Subject")
1140 && strcasecmp (hp
->name
, "Encrypted")
1141 && strcasecmp (hp
->name
, "Message-ID"))
1142 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1143 hp
= hp
->next
; /* next header field */
1149 /******************************************************************************/
1150 /* -clobber support */
1153 enum clobber_policy_t
1154 clobber_policy (const char *value
) {
1155 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1156 } else if (! strcasecmp (value
, "auto")) {
1157 return NMH_CLOBBER_AUTO
;
1158 } else if (! strcasecmp (value
, "suffix")) {
1159 return NMH_CLOBBER_SUFFIX
;
1160 } else if (! strcasecmp (value
, "ask")) {
1161 return NMH_CLOBBER_ASK
;
1162 } else if (! strcasecmp (value
, "never")) {
1163 return NMH_CLOBBER_NEVER
;
1165 adios (NULL
, "invalid argument, %s, to clobber", value
);
1168 return NMH_CLOBBER_ALWAYS
;
1173 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1174 const size_t max_versions
= 1000000;
1175 /* 8 = log max_versions + one for - or . + one for null terminator */
1176 const size_t buflen
= strlen (file
) + 8;
1177 char *buffer
= mh_xmalloc (buflen
);
1180 char *extension
= NULL
;
1181 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1182 ((extension
= strrchr (file
, '.')) != NULL
)) {
1183 *extension
++ = '\0';
1186 for (version
= 1; version
< max_versions
; ++version
) {
1189 switch (clobber_policy
) {
1190 case NMH_CLOBBER_AUTO
: {
1191 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1192 extension
== NULL
? "" : ".",
1193 extension
== NULL
? "" : extension
);
1197 case NMH_CLOBBER_SUFFIX
:
1198 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1202 /* Should never get here. */
1203 advise (NULL
, "will not overwrite %s, invalid clobber policy", buffer
);
1208 /* Actually (try to) create the file here to avoid a race
1209 condition on file naming + creation. This won't solve the
1210 problem with old NFS that doesn't support O_EXCL, though.
1211 Let the umask strip off permissions from 0666 as desired.
1212 That's what fopen () would do if it was creating the file. */
1213 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1214 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1215 S_IROTH
| S_IWOTH
)) >= 0) {
1223 if (version
>= max_versions
) {
1224 advise (NULL
, "will not overwrite %s, too many versions", buffer
);
1234 clobber_check (char *original_file
, mhstoreinfo_t info
) {
1235 /* clobber policy return value
1236 * -------------- ------------
1237 * -always original_file
1238 * -auto original_file-<digits>.extension
1239 * -suffix original_file.<digits>
1240 * -ask original_file, 0, or another filename/path
1248 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1249 /* Save cwd for possible use in loop below. */
1252 cwd
= add (original_file
, NULL
);
1253 slash
= strrchr (cwd
, '/');
1258 /* original_file wasn't a full path, which shouldn't happen. */
1266 file
= original_file
;
1269 switch (info
->clobber_policy
) {
1270 case NMH_CLOBBER_ALWAYS
:
1273 case NMH_CLOBBER_SUFFIX
:
1274 case NMH_CLOBBER_AUTO
:
1275 if (stat (file
, &st
) == OK
) {
1276 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1278 ++info
->files_not_clobbered
;
1283 case NMH_CLOBBER_ASK
:
1284 if (stat (file
, &st
) == OK
) {
1285 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1286 static struct swit answer
[4] = {
1287 { "yes", 0, NMH_YES
},
1288 { "no", 0, NMH_NO
},
1289 { "rename", 0, NMH_RENAME
},
1293 if (isatty (fileno (stdin
))) {
1295 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1296 ans
= getans (prompt
, answer
);
1299 /* Overwrite, that's what nmh used to do. And warn. */
1300 advise (NULL
, "-clobber ask but no tty, so overwrite %s", file
);
1304 switch ((enum answers
) smatch (*ans
, answer
)) {
1310 ++info
->files_not_clobbered
;
1314 printf ("Enter filename or full path of the new file: ");
1315 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1318 ++info
->files_not_clobbered
;
1320 char *newline
= strchr (buf
, '\n');
1328 if (buf
[0] == '/') {
1329 /* Full path, use it. */
1330 file
= add (buf
, NULL
);
1332 /* Relative path. */
1333 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : add (buf
, NULL
);
1343 case NMH_CLOBBER_NEVER
:
1344 if (stat (file
, &st
) == OK
) {
1345 /* Keep count of files that would have been clobbered,
1346 and return that as process exit status. */
1347 advise (NULL
, "will not overwrite %s with -clobber never", file
);
1350 ++info
->files_not_clobbered
;
1355 original_file
= file
;
1356 } while (check_again
);
1365 /* -clobber support */
1366 /******************************************************************************/