Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv23387

Modified Files:
        file.c 
Log Message:
Use FILE * instead of fd for files to support buffering


Index: file.c
===================================================================
RCS file: /usr/cvsroot/asterisk/file.c,v
retrieving revision 1.76
retrieving revision 1.77
diff -u -d -r1.76 -r1.77
--- file.c      14 Sep 2005 20:46:49 -0000      1.76
+++ file.c      16 Oct 2005 16:12:51 -0000      1.77
@@ -59,9 +59,9 @@
        /* 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 @@
 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 @@
        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 @@
                                                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 @@
                                                                                
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 @@
               set it up.
                   
        */
-       int fd = -1;
        int fmts = -1;
        char filename2[256]="";
        char filename3[256];
@@ -508,8 +512,8 @@
        /* 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 @@
 
 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 @@
                        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_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 @@
 
                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 @@
                        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;

_______________________________________________
Asterisk-Cvs mailing list
[email protected]
http://lists.digium.com/mailman/listinfo/asterisk-cvs

Reply via email to