(mu_temp_file_stream_create): New function.
Showing
2 changed files
with
66 additions
and
1 deletions
... | @@ -44,6 +44,8 @@ extern "C" { /*}*/ | ... | @@ -44,6 +44,8 @@ extern "C" { /*}*/ |
44 | 44 | ||
45 | extern int mu_file_stream_create (mu_stream_t *stream, const char* filename, | 45 | extern int mu_file_stream_create (mu_stream_t *stream, const char* filename, |
46 | int flags); | 46 | int flags); |
47 | extern int mu_temp_file_stream_create (mu_stream_t *stream, const char *dir); | ||
48 | |||
47 | extern int mu_tcp_stream_create (mu_stream_t *stream, const char* host, | 49 | extern int mu_tcp_stream_create (mu_stream_t *stream, const char* host, |
48 | int port, int flags); | 50 | int port, int flags); |
49 | extern int mu_mapfile_stream_create (mu_stream_t *stream, const char* filename, | 51 | extern int mu_mapfile_stream_create (mu_stream_t *stream, const char* filename, | ... | ... |
... | @@ -47,7 +47,7 @@ struct _file_stream | ... | @@ -47,7 +47,7 @@ struct _file_stream |
47 | { | 47 | { |
48 | FILE *file; | 48 | FILE *file; |
49 | mu_off_t offset; | 49 | mu_off_t offset; |
50 | 50 | int tempfile; | |
51 | char *filename; | 51 | char *filename; |
52 | mu_stream_t cache; | 52 | mu_stream_t cache; |
53 | }; | 53 | }; |
... | @@ -392,6 +392,22 @@ _file_close (mu_stream_t stream) | ... | @@ -392,6 +392,22 @@ _file_close (mu_stream_t stream) |
392 | } | 392 | } |
393 | 393 | ||
394 | static int | 394 | static int |
395 | _temp_file_open (mu_stream_t stream) | ||
396 | { | ||
397 | struct _file_stream *fs = mu_stream_get_owner (stream); | ||
398 | int fd; | ||
399 | |||
400 | fd = mu_tempfile (fs->filename, NULL); | ||
401 | if (fd == -1) | ||
402 | return errno; | ||
403 | fs->file = fdopen (fd, "r+b"); | ||
404 | if (fs->file == NULL) | ||
405 | return errno; | ||
406 | |||
407 | return 0; | ||
408 | } | ||
409 | |||
410 | static int | ||
395 | _file_open (mu_stream_t stream) | 411 | _file_open (mu_stream_t stream) |
396 | { | 412 | { |
397 | struct _file_stream *fs = mu_stream_get_owner (stream); | 413 | 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) | ... | @@ -550,6 +566,53 @@ mu_file_stream_create (mu_stream_t *stream, const char* filename, int flags) |
550 | } | 566 | } |
551 | 567 | ||
552 | int | 568 | int |
569 | mu_temp_file_stream_create (mu_stream_t *stream, const char *dir) | ||
570 | { | ||
571 | struct _file_stream *fs; | ||
572 | int ret; | ||
573 | |||
574 | if (stream == NULL) | ||
575 | return MU_ERR_OUT_PTR_NULL; | ||
576 | |||
577 | fs = calloc (1, sizeof (struct _file_stream)); | ||
578 | if (fs == NULL) | ||
579 | return ENOMEM; | ||
580 | fs->tempfile = 1; | ||
581 | |||
582 | if (!dir) | ||
583 | fs->filename = dir; | ||
584 | else if ((fs->filename = strdup(dir)) == NULL) | ||
585 | { | ||
586 | free (fs); | ||
587 | return ENOMEM; | ||
588 | } | ||
589 | |||
590 | ret = mu_stream_create (stream, | ||
591 | MU_STREAM_RDWR|MU_STREAM_CREAT|MU_STREAM_NO_CHECK, | ||
592 | fs); | ||
593 | if (ret != 0) | ||
594 | { | ||
595 | free (fs); | ||
596 | return ret; | ||
597 | } | ||
598 | |||
599 | mu_stream_set_open (*stream, _temp_file_open, fs); | ||
600 | mu_stream_set_close (*stream, _file_close, fs); | ||
601 | mu_stream_set_get_transport2 (*stream, _file_get_transport2, fs); | ||
602 | mu_stream_set_read (*stream, _file_read, fs); | ||
603 | mu_stream_set_readline (*stream, _file_readline, fs); | ||
604 | mu_stream_set_write (*stream, _file_write, fs); | ||
605 | mu_stream_set_truncate (*stream, _file_truncate, fs); | ||
606 | mu_stream_set_size (*stream, _file_size, fs); | ||
607 | mu_stream_set_flush (*stream, _file_flush, fs); | ||
608 | mu_stream_set_destroy (*stream, _file_destroy, fs); | ||
609 | mu_stream_set_strerror (*stream, _file_strerror, fs); | ||
610 | mu_stream_set_wait (*stream, _file_wait, fs); | ||
611 | |||
612 | return 0; | ||
613 | } | ||
614 | |||
615 | int | ||
553 | mu_stdio_stream_create (mu_stream_t *stream, FILE *file, int flags) | 616 | mu_stdio_stream_create (mu_stream_t *stream, FILE *file, int flags) |
554 | { | 617 | { |
555 | struct _file_stream *fs; | 618 | struct _file_stream *fs; | ... | ... |
-
Please register or sign in to post a comment