Use FILE * instead of fd for files to support buffering
[asterisk/asterisk.git] / file.c
diff --git a/file.c b/file.c
index 47a187d..c4fac49 100755 (executable)
--- a/file.c
+++ b/file.c
@@ -59,9 +59,9 @@ struct ast_format {
        /* Format of frames it uses/provides (one only) */
        int format;
        /* Open an input stream, and start playback */
-       struct ast_filestream * (*open)(int fd);
+       struct ast_filestream * (*open)(FILE * f);
        /* Open an output stream, of a given file descriptor and comment it appropriately if applicable */
-       struct ast_filestream * (*rewrite)(int fd, const char *comment);
+       struct ast_filestream * (*rewrite)(FILE *f, const char *comment);
        /* Write a frame to a channel */
        int (*write)(struct ast_filestream *, struct ast_frame *);
        /* seek num samples into file, whence(think normal seek) */
@@ -103,8 +103,8 @@ AST_MUTEX_DEFINE_STATIC(formatlock);
 static struct ast_format *formats = NULL;
 
 int ast_format_register(const char *name, const char *exts, int format,
-                                               struct ast_filestream * (*open)(int fd),
-                                               struct ast_filestream * (*rewrite)(int fd, const char *comment),
+                                               struct ast_filestream * (*open)(FILE *f),
+                                               struct ast_filestream * (*rewrite)(FILE *f, const char *comment),
                                                int (*write)(struct ast_filestream *, struct ast_frame *),
                                                int (*seek)(struct ast_filestream *, long sample_offset, int whence),
                                                int (*trunc)(struct ast_filestream *),
@@ -351,6 +351,7 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
        struct ast_filestream *s;
        int res=0, ret = 0;
        char *ext=NULL, *exts, *fn, *nfn;
+       FILE *bfile;
        struct ast_channel *chan = (struct ast_channel *)filename2;
        
        /* Start with negative response */
@@ -413,9 +414,10 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
                                                case ACTION_OPEN:
                                                        if ((ret < 0) && ((chan->writeformat & f->format) ||
                                                                                ((f->format >= AST_FORMAT_MAX_AUDIO) && fmt))) {
-                                                               ret = open(fn, O_RDONLY);
-                                                               if (ret >= 0) {
-                                                                       s = f->open(ret);
+                                                               bfile = fopen(fn, "r");
+                                                               if (bfile) {
+                                                                       ret = 1;
+                                                                       s = f->open(bfile);
                                                                        if (s) {
                                                                                s->lasttimeout = -1;
                                                                                s->fmt = f;
@@ -426,11 +428,14 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
                                                                                else
                                                                                        chan->vstream = s;
                                                                        } else {
-                                                                               close(ret);
-                                                                               ast_log(LOG_WARNING, "Unable to open fd on %s\n", fn);
+                                                                               fclose(bfile);
+                                                                               ast_log(LOG_WARNING, "Unable to open file on %s\n", fn);
+                                                                               ret = -1;
                                                                        }
-                                                               } else
+                                                               } else{
                                                                        ast_log(LOG_WARNING, "Couldn't open file %s\n", fn);
+                                                                       ret = -1;
+                                                               }
                                                        }
                                                        break;
                                                default:
@@ -472,7 +477,6 @@ struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char
               set it up.
                   
        */
-       int fd = -1;
        int fmts = -1;
        char filename2[256]="";
        char filename3[256];
@@ -508,8 +512,8 @@ struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char
        /* Set the channel to a format we can work with */
        res = ast_set_write_format(chan, fmts);
        
-       fd = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN);
-       if (fd >= 0)
+       res = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN);
+       if (res >= 0)
                return chan->stream;
        return NULL;
 }
@@ -819,7 +823,7 @@ int ast_streamfile(struct ast_channel *chan, const char *filename, const char *p
 
 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 {
-       int fd;
+       FILE *bfile;
        struct ast_format *f;
        struct ast_filestream *fs = NULL;
        char *fn;
@@ -834,13 +838,13 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
                        continue;
 
                fn = build_filename(filename, type);
-               fd = open(fn, flags);
-               if (fd >= 0) {
+               bfile = fopen(fn, "r");
+               if (bfile) {
                        errno = 0;
 
-                       if (!(fs = f->open(fd))) {
+                       if (!(fs = f->open(bfile))) {
                                ast_log(LOG_WARNING, "Unable to open %s\n", fn);
-                               close(fd);
+                               fclose(bfile);
                                free(fn);
                                continue;
                        }
@@ -866,6 +870,7 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 {
        int fd, myflags = 0;
+       FILE *bfile;
        struct ast_format *f;
        struct ast_filestream *fs = NULL;
        char *fn, *orig_fn = NULL;
@@ -893,11 +898,20 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
 
                fn = build_filename(filename, type);
                fd = open(fn, flags | myflags, mode);
+               if (fd > -1) {
+                       /* fdopen() the resulting file stream */
+                       bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
+                       if (!bfile) {
+                               ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
+                               close(fd);
+                               fd = -1;
+                       }
+               }
                
                if (option_cache_record_files && fd >= 0) {
                        char *c;
 
-                       close(fd);
+                       fclose(bfile);
                        /*
                          We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
                          What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
@@ -914,11 +928,20 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                        free(fn);
                        fn = buf;
                        fd = open(fn, flags | myflags, mode);
+                       if (fd > -1) {
+                               /* fdopen() the resulting file stream */
+                               bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
+                               if (!bfile) {
+                                       ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
+                                       close(fd);
+                                       fd = -1;
+                               }
+                       }
                }
                if (fd >= 0) {
                        errno = 0;
 
-                       if ((fs = f->rewrite(fd, comment))) {
+                       if ((fs = f->rewrite(bfile, comment))) {
                                fs->trans = NULL;
                                fs->fmt = f;
                                fs->flags = flags;