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)
size_t size = 0, n;
mu_message_t mesg;
rc = mu_temp_file_stream_create (&out, NULL);
rc = mu_temp_file_stream_create (&out, NULL, 0);
if (rc)
{
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,
int mu_file_stream_create (mu_stream_t *pstream, const char *filename, int flags);
int mu_temp_file_stream_create (mu_stream_t *pstream, const char *dir);
struct mu_tempfile_hints;
int mu_temp_file_stream_create (mu_stream_t *pstream,
struct mu_tempfile_hints *hints, int flags);
int mu_fd_stream_create (mu_stream_t *pstream, char *filename, int fd,
int flags);
......
......@@ -53,6 +53,7 @@ sysinclude_HEADERS = \
streamref.h\
streamtrans.h\
stream.h\
temp_file_stream.h\
tls-stream.h\
url.h\
xscript-stream.h
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _MAILUTILS_SYS_TEMP_FILE_STREAM_H
#define _MAILUTILS_SYS_TEMP_FILE_STREAM_H
#include <mailutils/types.h>
#include <mailutils/stream.h>
#include <mailutils/sys/file_stream.h>
#include <mailutils/util.h>
struct _mu_temp_file_stream
{
struct _mu_file_stream stream;
struct mu_tempfile_hints hints;
int hflags;
void (*file_done) (struct _mu_stream *str);
};
#endif
......@@ -95,8 +95,8 @@ char *mu_make_file_name_suf (const char *dir, const char *file,
struct mu_tempfile_hints
{
const char *tmpdir;
const char *suffix;
char *tmpdir;
char *suffix;
};
/*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,
*pcsz = total;
/* FIXME: When EOF error code is implemented:
else if (total == 0)
status = EIO;
status = EOF;
*/
free (buf);
return status;
......
......@@ -29,36 +29,64 @@
#include <mailutils/nls.h>
#include <mailutils/stream.h>
#include <mailutils/sys/stream.h>
#include <mailutils/sys/file_stream.h>
#include <mailutils/sys/temp_file_stream.h>
#include <mailutils/util.h>
static int
fd_temp_open (struct _mu_stream *str)
{
struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
struct mu_tempfile_hints hints;
struct _mu_temp_file_stream *fstr = (struct _mu_temp_file_stream *) str;
return mu_tempfile (&fstr->hints, fstr->hflags, &fstr->stream.fd, NULL);
}
hints.tmpdir = fstr->filename;
return mu_tempfile (&hints, MU_TEMPFILE_TMPDIR, &fstr->fd, NULL);
static void
fd_temp_done (struct _mu_stream *str)
{
struct _mu_temp_file_stream *fstr = (struct _mu_temp_file_stream *) str;
if (fstr->hflags & MU_TEMPFILE_TMPDIR)
free (fstr->hints.tmpdir);
if (fstr->hflags & MU_TEMPFILE_SUFFIX)
free (fstr->hints.suffix);
if (fstr->file_done)
fstr->file_done (&fstr->stream.stream);
}
int
mu_temp_file_stream_create (mu_stream_t *pstream, const char *dir)
mu_temp_file_stream_create (mu_stream_t *pstream,
struct mu_tempfile_hints *hints, int flags)
{
int rc;
struct _mu_file_stream *str;
mu_stream_t stream;
rc = _mu_file_stream_create (&str,
sizeof (struct _mu_file_stream),
dir,
sizeof (struct _mu_temp_file_stream),
NULL,
-1,
MU_STREAM_RDWR | MU_STREAM_SEEK |
MU_STREAM_CREAT |
MU_STREAM_AUTOCLOSE);
if (rc == 0)
{
str->stream.open = fd_temp_open;
struct _mu_temp_file_stream *tstr = (struct _mu_temp_file_stream *)str;
tstr->stream.stream.open = fd_temp_open;
tstr->file_done = tstr->stream.stream.done;
tstr->stream.stream.done = fd_temp_done;
if ((flags & MU_TEMPFILE_TMPDIR) &&
(tstr->hints.tmpdir = strdup (hints->tmpdir)) == NULL)
{
mu_stream_unref ((mu_stream_t) str);
return ENOMEM;
}
if ((flags & MU_TEMPFILE_SUFFIX) &&
(tstr->hints.suffix = strdup (hints->suffix)) == NULL)
{
mu_stream_unref ((mu_stream_t) str);
return ENOMEM;
}
tstr->hflags = flags & ~MU_TEMPFILE_MKDIR;
str->flags = _MU_FILE_STREAM_TEMP;
stream = (mu_stream_t) str;
rc = mu_stream_open (stream);
......
......@@ -1391,7 +1391,7 @@ mbox_expunge0 (mu_mailbox_t mailbox, int remove_deleted)
(status = mu_locker_lock (mailbox->locker)) != 0)
return status;
status = mu_temp_file_stream_create (&tempstr, NULL);
status = mu_temp_file_stream_create (&tempstr, NULL, 0);
if (status == 0)
{
sigset_t signalset;
......
......@@ -361,7 +361,7 @@ _pop_message_get_stream (struct _pop3_message *mpm, mu_stream_t *pstr)
if (!mpd->cache)
{
status = mu_temp_file_stream_create (&mpd->cache, NULL);
status = mu_temp_file_stream_create (&mpd->cache, NULL, 0);
if (status)
/* FIXME: Try to recover first */
break;
......
......@@ -34,7 +34,7 @@ make_tmp (const char *from)
exit (EX_TEMPFAIL);
}
rc = mu_temp_file_stream_create (&out, NULL);
rc = mu_temp_file_stream_create (&out, NULL, 0);
if (rc)
{
maidag_error (_("unable to open temporary file: %s"), mu_strerror (rc));
......
......@@ -366,7 +366,7 @@ cfun_data (mu_stream_t iostr, char *arg)
return 1;
}
rc = mu_temp_file_stream_create (&tempstr, NULL);
rc = mu_temp_file_stream_create (&tempstr, NULL, 0);
if (rc)
{
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)
return;
if (!bs->stream)
{
if ((rc = mu_temp_file_stream_create (&bs->stream, NULL)))
if ((rc = mu_temp_file_stream_create (&bs->stream, NULL, 0)))
{
mu_error (_("Cannot open temporary file: %s"),
mu_strerror (rc));
......
......@@ -193,7 +193,7 @@ main (int argc, char **argv)
return 1;
}
if ((rc = mu_temp_file_stream_create (&tmp, NULL)))
if ((rc = mu_temp_file_stream_create (&tmp, NULL, 0)))
{
mu_error (_("Cannot open temporary file: %s"),
mu_strerror (rc));
......