]> diplodocus.org Git - nmh/blob - uip/packf.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / packf.c
1 /* packf.c -- pack a nmh folder into a file
2 *
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.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/dropsbr.h>
11 #include <h/utils.h>
12 #include "sbr/m_maildir.h"
13
14 #define PACKF_SWITCHES \
15 X("file name", 0, FILESW) \
16 X("mbox", 0, MBOXSW) \
17 X("mmdf", 0, MMDFSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
20
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(PACKF);
23 #undef X
24
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(PACKF, switches);
27 #undef X
28
29 static int md = NOTOK;
30 static int mbx_style = MBOX_FORMAT;
31
32 static void mbxclose_done(int) NORETURN;
33
34 char *file = NULL;
35
36
37 int
38 main (int argc, char **argv)
39 {
40 int fd, msgnum;
41 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
42 char **argp, **arguments;
43 struct msgs_array msgs = { 0, 0, NULL };
44 struct msgs *mp;
45 struct stat st;
46
47 if (nmh_init(argv[0], 1)) { return 1; }
48
49 done=mbxclose_done;
50
51 arguments = getarguments (invo_name, argc, argv, 1);
52 argp = arguments;
53
54 /*
55 * Parse arguments
56 */
57 while ((cp = *argp++)) {
58 if (*cp == '-') {
59 switch (smatch (++cp, switches)) {
60 case AMBIGSW:
61 ambigsw (cp, switches);
62 done (1);
63 case UNKWNSW:
64 adios (NULL, "-%s unknown", cp);
65
66 case HELPSW:
67 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
68 invo_name);
69 print_help (buf, switches, 1);
70 done (0);
71 case VERSIONSW:
72 print_version(invo_name);
73 done (0);
74
75 case FILESW:
76 if (file)
77 adios (NULL, "only one file at a time!");
78 if (!(file = *argp++) || *file == '-')
79 adios (NULL, "missing argument to %s", argp[-2]);
80 continue;
81
82 case MBOXSW:
83 mbx_style = MBOX_FORMAT;
84 continue;
85 case MMDFSW:
86 mbx_style = MMDF_FORMAT;
87 continue;
88 }
89 }
90 if (*cp == '+' || *cp == '@') {
91 if (folder)
92 adios (NULL, "only one folder at a time!");
93 folder = pluspath (cp);
94 } else
95 app_msgarg(&msgs, cp);
96 }
97
98 if (!file)
99 file = "./msgbox";
100 file = path (file, TFILE);
101
102 /*
103 * Check if file to be created (or appended to)
104 * exists. If not, ask for confirmation.
105 */
106 if (stat (file, &st) == NOTOK) {
107 if (errno != ENOENT)
108 adios (file, "error on file");
109 cp = concat ("Create file \"", file, "\"? ", NULL);
110 if (!read_yes_or_no_if_tty (cp))
111 done (1);
112 free (cp);
113 }
114
115 if (!context_find ("path"))
116 free (path ("./", TFOLDER));
117
118 /* default is to pack whole folder */
119 if (!msgs.size)
120 app_msgarg(&msgs, "all");
121
122 if (!folder)
123 folder = getfolder (1);
124 maildir = m_maildir (folder);
125
126 if (chdir (maildir) == NOTOK)
127 adios (maildir, "unable to change directory to ");
128
129 /* read folder and create message structure */
130 if (!(mp = folder_read (folder, 1)))
131 adios (NULL, "unable to read folder %s", folder);
132
133 /* check for empty folder */
134 if (mp->nummsg == 0)
135 adios (NULL, "no messages in %s", folder);
136
137 /* parse all the message ranges/sequences and set SELECTED */
138 for (msgnum = 0; msgnum < msgs.size; msgnum++)
139 if (!m_convert (mp, msgs.msgs[msgnum]))
140 done (1);
141 seq_setprev (mp); /* set the previous-sequence */
142
143 /* open and lock new maildrop file */
144 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
145 adios (file, "unable to open");
146
147 /* copy all the SELECTED messages to the file */
148 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
149 if (is_selected(mp, msgnum)) {
150 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
151 admonish (msgnam, "unable to read message");
152 break;
153 }
154
155 if (mbx_copy (file, mbx_style, md, fd, NULL) == NOTOK)
156 adios (file, "error writing to file");
157
158 close (fd);
159 }
160
161 /* close and unlock maildrop file */
162 mbx_close (file, md);
163
164 context_replace (pfolder, folder); /* update current folder */
165 if (mp->hghsel != mp->curmsg)
166 seq_setcur (mp, mp->lowsel);
167 seq_save (mp);
168 context_save (); /* save the context file */
169 folder_free (mp); /* free folder/message structure */
170 done (0);
171 return 1;
172 }
173
174 static void NORETURN
175 mbxclose_done (int status)
176 {
177 mbx_close (file, md);
178 exit (status);
179 }