Commit 23c2ce92 23c2ce925f6aa8e154aea8043c3d581bbbdc800c by Sergey Poznyakoff

(mu_temp_file_stream_create): New function.

1 parent 22ce7745
......@@ -43,23 +43,25 @@ extern "C" { /*}*/
/* Functions useful to users of the pre-defined stream types. */
extern int mu_file_stream_create (mu_stream_t *stream, const char* filename,
int flags);
int flags);
extern int mu_temp_file_stream_create (mu_stream_t *stream, const char *dir);
extern int mu_tcp_stream_create (mu_stream_t *stream, const char* host,
int port, int flags);
int port, int flags);
extern int mu_mapfile_stream_create (mu_stream_t *stream, const char* filename,
int flags);
int flags);
extern int mu_memory_stream_create (mu_stream_t *stream, const char* filename,
int flags);
int flags);
extern int mu_encoder_stream_create (mu_stream_t *stream, mu_stream_t iostream,
const char *encoding);
const char *encoding);
extern int mu_decoder_stream_create (mu_stream_t *stream, mu_stream_t iostream,
const char *encoding);
const char *encoding);
extern int mu_stdio_stream_create (mu_stream_t *stream, FILE* stdio,
int flags);
int flags);
extern int mu_prog_stream_create (mu_stream_t *stream, const char *progname,
int flags);
int flags);
int mu_filter_prog_stream_create (mu_stream_t *stream, const char *progname,
mu_stream_t input);
mu_stream_t input);
extern void mu_stream_destroy (mu_stream_t *, void *owner);
......@@ -67,7 +69,7 @@ extern int mu_stream_open (mu_stream_t);
extern int mu_stream_close (mu_stream_t);
extern int mu_stream_is_seekable (mu_stream_t);
extern int mu_stream_get_transport2 (mu_stream_t stream, mu_transport_t *pt,
mu_transport_t *pt2);
mu_transport_t *pt2);
extern int mu_stream_get_transport (mu_stream_t stream, mu_transport_t *pt);
extern int mu_stream_read (mu_stream_t, char *, size_t, mu_off_t, size_t *);
......
......@@ -47,7 +47,7 @@ struct _file_stream
{
FILE *file;
mu_off_t offset;
int tempfile;
char *filename;
mu_stream_t cache;
};
......@@ -392,6 +392,22 @@ _file_close (mu_stream_t stream)
}
static int
_temp_file_open (mu_stream_t stream)
{
struct _file_stream *fs = mu_stream_get_owner (stream);
int fd;
fd = mu_tempfile (fs->filename, NULL);
if (fd == -1)
return errno;
fs->file = fdopen (fd, "r+b");
if (fs->file == NULL)
return errno;
return 0;
}
static int
_file_open (mu_stream_t stream)
{
struct _file_stream *fs = mu_stream_get_owner (stream);
......@@ -550,6 +566,53 @@ mu_file_stream_create (mu_stream_t *stream, const char* filename, int flags)
}
int
mu_temp_file_stream_create (mu_stream_t *stream, const char *dir)
{
struct _file_stream *fs;
int ret;
if (stream == NULL)
return MU_ERR_OUT_PTR_NULL;
fs = calloc (1, sizeof (struct _file_stream));
if (fs == NULL)
return ENOMEM;
fs->tempfile = 1;
if (!dir)
fs->filename = dir;
else if ((fs->filename = strdup(dir)) == NULL)
{
free (fs);
return ENOMEM;
}
ret = mu_stream_create (stream,
MU_STREAM_RDWR|MU_STREAM_CREAT|MU_STREAM_NO_CHECK,
fs);
if (ret != 0)
{
free (fs);
return ret;
}
mu_stream_set_open (*stream, _temp_file_open, fs);
mu_stream_set_close (*stream, _file_close, fs);
mu_stream_set_get_transport2 (*stream, _file_get_transport2, fs);
mu_stream_set_read (*stream, _file_read, fs);
mu_stream_set_readline (*stream, _file_readline, fs);
mu_stream_set_write (*stream, _file_write, fs);
mu_stream_set_truncate (*stream, _file_truncate, fs);
mu_stream_set_size (*stream, _file_size, fs);
mu_stream_set_flush (*stream, _file_flush, fs);
mu_stream_set_destroy (*stream, _file_destroy, fs);
mu_stream_set_strerror (*stream, _file_strerror, fs);
mu_stream_set_wait (*stream, _file_wait, fs);
return 0;
}
int
mu_stdio_stream_create (mu_stream_t *stream, FILE *file, int flags)
{
struct _file_stream *fs;
......