Commit bf613ed8 bf613ed8c4661faa1e9c1de7f28a7e03c5652ec0 by Sergey Poznyakoff

temp_file_stream: Use mu_tempfile_hints to control how the file name is created.

* include/mailutils/stream.h (mu_temp_file_stream_create): Change signature.
* include/mailutils/sys/Makefile.am (sysinclude_HEADERS): Add temp_file_stream.h
* include/mailutils/util.h (mu_tempfile_hints): Remove const qualifiers.
* libmailutils/stream/temp_file_stream.c: Include sys/temp_file_stream.h.
(fd_temp_open): Use data from struct _mu_temp_file_stream.
(fd_temp_done): New function.
(mu_temp_file_stream_create): Take hints and flags as arguments.

* examples/mta.c: Update calls to mu_temp_file_stream_create.
* libproto/mbox/mbox.c: Likewise.
* libproto/pop/mbox.c: Likewise.
* maidag/deliver.c: Likewise.
* maidag/lmtp.c: Likewise.
* mh/burst.c: Likewise.
* mh/prompter.c: Likewise.
1 parent 8acde412
...@@ -198,7 +198,7 @@ make_tmp (mu_stream_t in) ...@@ -198,7 +198,7 @@ make_tmp (mu_stream_t in)
198 size_t size = 0, n; 198 size_t size = 0, n;
199 mu_message_t mesg; 199 mu_message_t mesg;
200 200
201 rc = mu_temp_file_stream_create (&out, NULL); 201 rc = mu_temp_file_stream_create (&out, NULL, 0);
202 if (rc) 202 if (rc)
203 { 203 {
204 mu_error (_("unable to open temporary file: %s"), mu_strerror (rc)); 204 mu_error (_("unable to open temporary file: %s"), mu_strerror (rc));
......
...@@ -192,7 +192,9 @@ int mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size, ...@@ -192,7 +192,9 @@ int mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size,
192 192
193 193
194 int mu_file_stream_create (mu_stream_t *pstream, const char *filename, int flags); 194 int mu_file_stream_create (mu_stream_t *pstream, const char *filename, int flags);
195 int mu_temp_file_stream_create (mu_stream_t *pstream, const char *dir); 195 struct mu_tempfile_hints;
196 int mu_temp_file_stream_create (mu_stream_t *pstream,
197 struct mu_tempfile_hints *hints, int flags);
196 int mu_fd_stream_create (mu_stream_t *pstream, char *filename, int fd, 198 int mu_fd_stream_create (mu_stream_t *pstream, char *filename, int fd,
197 int flags); 199 int flags);
198 200
......
...@@ -53,6 +53,7 @@ sysinclude_HEADERS = \ ...@@ -53,6 +53,7 @@ sysinclude_HEADERS = \
53 streamref.h\ 53 streamref.h\
54 streamtrans.h\ 54 streamtrans.h\
55 stream.h\ 55 stream.h\
56 temp_file_stream.h\
56 tls-stream.h\ 57 tls-stream.h\
57 url.h\ 58 url.h\
58 xscript-stream.h 59 xscript-stream.h
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2009 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef _MAILUTILS_SYS_TEMP_FILE_STREAM_H
18 #define _MAILUTILS_SYS_TEMP_FILE_STREAM_H
19
20 #include <mailutils/types.h>
21 #include <mailutils/stream.h>
22 #include <mailutils/sys/file_stream.h>
23 #include <mailutils/util.h>
24
25 struct _mu_temp_file_stream
26 {
27 struct _mu_file_stream stream;
28 struct mu_tempfile_hints hints;
29 int hflags;
30 void (*file_done) (struct _mu_stream *str);
31 };
32
33 #endif
34
35
...@@ -95,8 +95,8 @@ char *mu_make_file_name_suf (const char *dir, const char *file, ...@@ -95,8 +95,8 @@ char *mu_make_file_name_suf (const char *dir, const char *file,
95 95
96 struct mu_tempfile_hints 96 struct mu_tempfile_hints
97 { 97 {
98 const char *tmpdir; 98 char *tmpdir;
99 const char *suffix; 99 char *suffix;
100 }; 100 };
101 101
102 /*int mu_tempfile (const char *tmpdir, char **namep);*/ 102 /*int mu_tempfile (const char *tmpdir, char **namep);*/
......
...@@ -119,7 +119,7 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size, ...@@ -119,7 +119,7 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size,
119 *pcsz = total; 119 *pcsz = total;
120 /* FIXME: When EOF error code is implemented: 120 /* FIXME: When EOF error code is implemented:
121 else if (total == 0) 121 else if (total == 0)
122 status = EIO; 122 status = EOF;
123 */ 123 */
124 free (buf); 124 free (buf);
125 return status; 125 return status;
......
...@@ -29,36 +29,64 @@ ...@@ -29,36 +29,64 @@
29 #include <mailutils/nls.h> 29 #include <mailutils/nls.h>
30 #include <mailutils/stream.h> 30 #include <mailutils/stream.h>
31 #include <mailutils/sys/stream.h> 31 #include <mailutils/sys/stream.h>
32 #include <mailutils/sys/file_stream.h> 32 #include <mailutils/sys/temp_file_stream.h>
33 #include <mailutils/util.h> 33 #include <mailutils/util.h>
34 34
35 35
36 static int 36 static int
37 fd_temp_open (struct _mu_stream *str) 37 fd_temp_open (struct _mu_stream *str)
38 { 38 {
39 struct _mu_file_stream *fstr = (struct _mu_file_stream *) str; 39 struct _mu_temp_file_stream *fstr = (struct _mu_temp_file_stream *) str;
40 struct mu_tempfile_hints hints; 40 return mu_tempfile (&fstr->hints, fstr->hflags, &fstr->stream.fd, NULL);
41 }
41 42
42 hints.tmpdir = fstr->filename; 43 static void
43 return mu_tempfile (&hints, MU_TEMPFILE_TMPDIR, &fstr->fd, NULL); 44 fd_temp_done (struct _mu_stream *str)
45 {
46 struct _mu_temp_file_stream *fstr = (struct _mu_temp_file_stream *) str;
47 if (fstr->hflags & MU_TEMPFILE_TMPDIR)
48 free (fstr->hints.tmpdir);
49 if (fstr->hflags & MU_TEMPFILE_SUFFIX)
50 free (fstr->hints.suffix);
51 if (fstr->file_done)
52 fstr->file_done (&fstr->stream.stream);
44 } 53 }
45 54
46 int 55 int
47 mu_temp_file_stream_create (mu_stream_t *pstream, const char *dir) 56 mu_temp_file_stream_create (mu_stream_t *pstream,
57 struct mu_tempfile_hints *hints, int flags)
48 { 58 {
49 int rc; 59 int rc;
50 struct _mu_file_stream *str; 60 struct _mu_file_stream *str;
51 mu_stream_t stream; 61 mu_stream_t stream;
52 rc = _mu_file_stream_create (&str, 62 rc = _mu_file_stream_create (&str,
53 sizeof (struct _mu_file_stream), 63 sizeof (struct _mu_temp_file_stream),
54 dir, 64 NULL,
55 -1, 65 -1,
56 MU_STREAM_RDWR | MU_STREAM_SEEK | 66 MU_STREAM_RDWR | MU_STREAM_SEEK |
57 MU_STREAM_CREAT | 67 MU_STREAM_CREAT |
58 MU_STREAM_AUTOCLOSE); 68 MU_STREAM_AUTOCLOSE);
59 if (rc == 0) 69 if (rc == 0)
60 { 70 {
61 str->stream.open = fd_temp_open; 71 struct _mu_temp_file_stream *tstr = (struct _mu_temp_file_stream *)str;
72 tstr->stream.stream.open = fd_temp_open;
73 tstr->file_done = tstr->stream.stream.done;
74 tstr->stream.stream.done = fd_temp_done;
75
76 if ((flags & MU_TEMPFILE_TMPDIR) &&
77 (tstr->hints.tmpdir = strdup (hints->tmpdir)) == NULL)
78 {
79 mu_stream_unref ((mu_stream_t) str);
80 return ENOMEM;
81 }
82 if ((flags & MU_TEMPFILE_SUFFIX) &&
83 (tstr->hints.suffix = strdup (hints->suffix)) == NULL)
84 {
85 mu_stream_unref ((mu_stream_t) str);
86 return ENOMEM;
87 }
88 tstr->hflags = flags & ~MU_TEMPFILE_MKDIR;
89
62 str->flags = _MU_FILE_STREAM_TEMP; 90 str->flags = _MU_FILE_STREAM_TEMP;
63 stream = (mu_stream_t) str; 91 stream = (mu_stream_t) str;
64 rc = mu_stream_open (stream); 92 rc = mu_stream_open (stream);
......
...@@ -1391,7 +1391,7 @@ mbox_expunge0 (mu_mailbox_t mailbox, int remove_deleted) ...@@ -1391,7 +1391,7 @@ mbox_expunge0 (mu_mailbox_t mailbox, int remove_deleted)
1391 (status = mu_locker_lock (mailbox->locker)) != 0) 1391 (status = mu_locker_lock (mailbox->locker)) != 0)
1392 return status; 1392 return status;
1393 1393
1394 status = mu_temp_file_stream_create (&tempstr, NULL); 1394 status = mu_temp_file_stream_create (&tempstr, NULL, 0);
1395 if (status == 0) 1395 if (status == 0)
1396 { 1396 {
1397 sigset_t signalset; 1397 sigset_t signalset;
......
...@@ -361,7 +361,7 @@ _pop_message_get_stream (struct _pop3_message *mpm, mu_stream_t *pstr) ...@@ -361,7 +361,7 @@ _pop_message_get_stream (struct _pop3_message *mpm, mu_stream_t *pstr)
361 361
362 if (!mpd->cache) 362 if (!mpd->cache)
363 { 363 {
364 status = mu_temp_file_stream_create (&mpd->cache, NULL); 364 status = mu_temp_file_stream_create (&mpd->cache, NULL, 0);
365 if (status) 365 if (status)
366 /* FIXME: Try to recover first */ 366 /* FIXME: Try to recover first */
367 break; 367 break;
......
...@@ -34,7 +34,7 @@ make_tmp (const char *from) ...@@ -34,7 +34,7 @@ make_tmp (const char *from)
34 exit (EX_TEMPFAIL); 34 exit (EX_TEMPFAIL);
35 } 35 }
36 36
37 rc = mu_temp_file_stream_create (&out, NULL); 37 rc = mu_temp_file_stream_create (&out, NULL, 0);
38 if (rc) 38 if (rc)
39 { 39 {
40 maidag_error (_("unable to open temporary file: %s"), mu_strerror (rc)); 40 maidag_error (_("unable to open temporary file: %s"), mu_strerror (rc));
......
...@@ -366,7 +366,7 @@ cfun_data (mu_stream_t iostr, char *arg) ...@@ -366,7 +366,7 @@ cfun_data (mu_stream_t iostr, char *arg)
366 return 1; 366 return 1;
367 } 367 }
368 368
369 rc = mu_temp_file_stream_create (&tempstr, NULL); 369 rc = mu_temp_file_stream_create (&tempstr, NULL, 0);
370 if (rc) 370 if (rc)
371 { 371 {
372 maidag_error (_("unable to open temporary file: %s"), mu_strerror (rc)); 372 maidag_error (_("unable to open temporary file: %s"), mu_strerror (rc));
......
...@@ -396,7 +396,7 @@ flush_stream (struct burst_stream *bs, char *buf, size_t size) ...@@ -396,7 +396,7 @@ flush_stream (struct burst_stream *bs, char *buf, size_t size)
396 return; 396 return;
397 if (!bs->stream) 397 if (!bs->stream)
398 { 398 {
399 if ((rc = mu_temp_file_stream_create (&bs->stream, NULL))) 399 if ((rc = mu_temp_file_stream_create (&bs->stream, NULL, 0)))
400 { 400 {
401 mu_error (_("Cannot open temporary file: %s"), 401 mu_error (_("Cannot open temporary file: %s"),
402 mu_strerror (rc)); 402 mu_strerror (rc));
......
...@@ -193,7 +193,7 @@ main (int argc, char **argv) ...@@ -193,7 +193,7 @@ main (int argc, char **argv)
193 return 1; 193 return 1;
194 } 194 }
195 195
196 if ((rc = mu_temp_file_stream_create (&tmp, NULL))) 196 if ((rc = mu_temp_file_stream_create (&tmp, NULL, 0)))
197 { 197 {
198 mu_error (_("Cannot open temporary file: %s"), 198 mu_error (_("Cannot open temporary file: %s"),
199 mu_strerror (rc)); 199 mu_strerror (rc));
......