CI: Various updates to buildAsterisk.sh
[asterisk/asterisk.git] / main / file.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Generic File Format Support.
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include <dirent.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 #include <math.h>
36
37 #include "asterisk/_private.h"  /* declare ast_file_init() */
38 #include "asterisk/paths.h"     /* use ast_config_AST_DATA_DIR */
39 #include "asterisk/mod_format.h"
40 #include "asterisk/cli.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/sched.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/app.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/linkedlists.h"
49 #include "asterisk/module.h"
50 #include "asterisk/astobj2.h"
51 #include "asterisk/test.h"
52 #include "asterisk/stasis.h"
53 #include "asterisk/json.h"
54 #include "asterisk/stasis_system.h"
55 #include "asterisk/media_cache.h"
56
57 /*! \brief
58  * The following variable controls the layout of localized sound files.
59  * If 0, use the historical layout with prefix just before the filename
60  * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
61  * if 1 put the prefix at the beginning of the filename
62  * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
63  * The latter permits a language to be entirely in one directory.
64  *
65  * This is settable in asterisk.conf.
66  */
67 int ast_language_is_prefix = 1;
68
69 static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
70
71 STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type);
72 STASIS_MESSAGE_TYPE_DEFN(ast_format_unregister_type);
73
74 static struct ast_json *json_array_from_list(const char *list, const char *sep)
75 {
76         RAII_VAR(struct ast_json *, array, ast_json_array_create(), ast_json_unref);
77         char *stringp, *ext;
78
79         stringp = ast_strdupa(list);    /* this is in the stack so does not need to be freed */
80         if (!array || !stringp) {
81                 return NULL;
82         }
83
84         while ((ext = strsep(&stringp, sep))) {
85                 if (ast_json_array_append(array, ast_json_string_create(ext))) {
86                         return NULL;
87                 }
88         }
89
90         return ast_json_ref(array);
91 }
92
93 static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
94 {
95         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
96         RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
97         RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
98
99         if (!type) {
100                 return -1;
101         }
102
103         json_object = ast_json_pack("{s: s, s: o}",
104                 "format", f->name,
105                 "extensions", json_array_from_list(f->exts, "|"));
106         if (!json_object) {
107                 return -1;
108         }
109
110         json_payload = ast_json_payload_create(json_object);
111         if (!json_payload) {
112                 return -1;
113         }
114
115         msg = stasis_message_create(type, json_payload);
116         if (!msg) {
117                 return -1;
118         }
119
120         stasis_publish(ast_system_topic(), msg);
121         return 0;
122 }
123
124 int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
125 {
126         struct ast_format_def *tmp;
127
128         AST_RWLIST_WRLOCK(&formats);
129         AST_RWLIST_TRAVERSE(&formats, tmp, list) {
130                 if (!strcasecmp(f->name, tmp->name)) {
131                         AST_RWLIST_UNLOCK(&formats);
132                         ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
133                         return -1;
134                 }
135         }
136         if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
137                 AST_RWLIST_UNLOCK(&formats);
138                 return -1;
139         }
140         *tmp = *f;
141         tmp->module = mod;
142         if (tmp->buf_size) {
143                 /*
144                  * Align buf_size properly, rounding up to the machine-specific
145                  * alignment for pointers.
146                  */
147                 struct _test_align { void *a, *b; } p;
148                 int align = (char *)&p.b - (char *)&p.a;
149                 tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
150         }
151
152         memset(&tmp->list, 0, sizeof(tmp->list));
153
154         AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
155         AST_RWLIST_UNLOCK(&formats);
156         ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
157         publish_format_update(f, ast_format_register_type());
158
159         return 0;
160 }
161
162 int ast_format_def_unregister(const char *name)
163 {
164         struct ast_format_def *tmp;
165         int res = -1;
166
167         AST_RWLIST_WRLOCK(&formats);
168         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
169                 if (!strcasecmp(name, tmp->name)) {
170                         AST_RWLIST_REMOVE_CURRENT(list);
171                         publish_format_update(tmp, ast_format_unregister_type());
172                         ast_free(tmp);
173                         res = 0;
174                 }
175         }
176         AST_RWLIST_TRAVERSE_SAFE_END;
177         AST_RWLIST_UNLOCK(&formats);
178
179         if (!res)
180                 ast_verb(2, "Unregistered format %s\n", name);
181         else
182                 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
183
184         return res;
185 }
186
187 int ast_stopstream(struct ast_channel *tmp)
188 {
189         ast_channel_lock(tmp);
190
191         /* Stop a running stream if there is one */
192         if (ast_channel_stream(tmp)) {
193                 ast_closestream(ast_channel_stream(tmp));
194                 ast_channel_stream_set(tmp, NULL);
195                 if (ast_channel_oldwriteformat(tmp) && ast_set_write_format(tmp, ast_channel_oldwriteformat(tmp)))
196                         ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_format_get_name(ast_channel_oldwriteformat(tmp)));
197         }
198         /* Stop the video stream too */
199         if (ast_channel_vstream(tmp) != NULL) {
200                 ast_closestream(ast_channel_vstream(tmp));
201                 ast_channel_vstream_set(tmp, NULL);
202         }
203
204         ast_channel_unlock(tmp);
205
206         return 0;
207 }
208
209 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
210 {
211         int res = -1;
212         if (f->frametype == AST_FRAME_VIDEO) {
213                 if (ast_format_get_type(fs->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
214                         /* This is the audio portion.  Call the video one... */
215                         if (!fs->vfs && fs->filename) {
216                                 const char *type = ast_format_get_name(f->subclass.format);
217                                 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
218                                 ast_debug(1, "Opened video output file\n");
219                         }
220                         if (fs->vfs)
221                                 return ast_writestream(fs->vfs, f);
222                         /* else ignore */
223                         return 0;
224                 }
225         } else if (f->frametype != AST_FRAME_VOICE) {
226                 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
227                 return -1;
228         }
229         if (ast_format_cmp(f->subclass.format, fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) {
230                 res =  fs->fmt->write(fs, f);
231                 if (res < 0)
232                         ast_log(LOG_WARNING, "Natural write failed\n");
233                 else if (res > 0)
234                         ast_log(LOG_WARNING, "Huh??\n");
235         } else {
236                 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
237                        the one we've setup a translator for, we do the "wrong thing" XXX */
238                 if (fs->trans && (ast_format_cmp(f->subclass.format, fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) {
239                         ast_translator_free_path(fs->trans);
240                         fs->trans = NULL;
241                 }
242                 if (!fs->trans) {
243                         fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass.format);
244                 }
245                 if (!fs->trans) {
246                         ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
247                                 fs->fmt->name, ast_format_get_name(f->subclass.format));
248                 } else {
249                         struct ast_frame *trf;
250                         ao2_replace(fs->lastwriteformat, f->subclass.format);
251                         /* Get the translated frame but don't consume the original in case they're using it on another stream */
252                         if ((trf = ast_translate(fs->trans, f, 0))) {
253                                 struct ast_frame *cur;
254
255                                 /* the translator may have returned multiple frames, so process them */
256                                 for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
257                                         if ((res = fs->fmt->write(fs, trf))) {
258                                                 ast_log(LOG_WARNING, "Translated frame write failed\n");
259                                                 break;
260                                         }
261                                 }
262                                 ast_frfree(trf);
263                         } else {
264                                 res = 0;
265                         }
266                 }
267         }
268         return res;
269 }
270
271 static int copy(const char *infile, const char *outfile)
272 {
273         int ifd, ofd, len;
274         char buf[4096]; /* XXX make it lerger. */
275
276         if ((ifd = open(infile, O_RDONLY)) < 0) {
277                 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
278                 return -1;
279         }
280         if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
281                 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
282                 close(ifd);
283                 return -1;
284         }
285         while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
286                 int res;
287                 if (len < 0) {
288                         ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
289                         break;
290                 }
291                 /* XXX handle partial writes */
292                 res = write(ofd, buf, len);
293                 if (res != len) {
294                         ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
295                         len = -1; /* error marker */
296                         break;
297                 }
298         }
299         close(ifd);
300         close(ofd);
301         if (len < 0) {
302                 unlink(outfile);
303                 return -1; /* error */
304         }
305         return 0;       /* success */
306 }
307
308 /*!
309  * \brief construct a filename. Absolute pathnames are preserved,
310  * relative names are prefixed by the sounds/ directory.
311  * The wav49 suffix is replaced by 'WAV'.
312  * Returns a malloc'ed string to be freed by the caller.
313  */
314 static char *build_filename(const char *filename, const char *ext)
315 {
316         char *fn = NULL;
317
318         if (!strcmp(ext, "wav49"))
319                 ext = "WAV";
320
321         if (filename[0] == '/') {
322                 if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) {
323                         fn = NULL;
324                 }
325         } else {
326                 if (ast_asprintf(&fn, "%s/sounds/%s.%s",
327                              ast_config_AST_DATA_DIR, filename, ext) < 0) {
328                         fn = NULL;
329                 }
330         }
331         return fn;
332 }
333
334 /* compare type against the list 'exts' */
335 /* XXX need a better algorithm */
336 static int type_in_list(const char *list, const char *type, int (*cmp)(const char *s1, const char *s2))
337 {
338         char *stringp = ast_strdupa(list), *item;
339
340         while ((item = strsep(&stringp, "|"))) {
341                 if (!cmp(item, type)) {
342                         return 1;
343                 }
344         }
345
346         return 0;
347 }
348
349 #define exts_compare(list, type) (type_in_list((list), (type), strcmp))
350
351 /*!
352  * \internal
353  * \brief Close the file stream by canceling any pending read / write callbacks
354  */
355 static void filestream_close(struct ast_filestream *f)
356 {
357         enum ast_media_type format_type = ast_format_get_type(f->fmt->format);
358
359         if (!f->owner) {
360                 return;
361         }
362
363         /* Stop a running stream if there is one */
364         switch (format_type)
365         {
366         case AST_MEDIA_TYPE_AUDIO:
367                 ast_channel_stream_set(f->owner, NULL);
368                 AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_streamid, ast_channel_streamid_set);
369                 ast_settimeout(f->owner, 0, NULL, NULL);
370                 break;
371         case AST_MEDIA_TYPE_VIDEO:
372                 ast_channel_vstream_set(f->owner, NULL);
373                 AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_vstreamid, ast_channel_vstreamid_set);
374                 break;
375         default:
376                 ast_log(AST_LOG_WARNING, "Unable to schedule deletion of filestream with unsupported type %s\n", f->fmt->name);
377                 break;
378         }
379 }
380
381 static void filestream_destructor(void *arg)
382 {
383         struct ast_filestream *f = arg;
384         int status;
385         int pid = -1;
386
387         /* Stop a running stream if there is one */
388         filestream_close(f);
389
390         /* destroy the translator on exit */
391         if (f->trans)
392                 ast_translator_free_path(f->trans);
393
394         if (f->fmt->close) {
395                 void (*closefn)(struct ast_filestream *) = f->fmt->close;
396                 closefn(f);
397         }
398
399         if (f->f) {
400                 fclose(f->f);
401         }
402
403         if (f->realfilename && f->filename) {
404                 pid = ast_safe_fork(0);
405                 if (!pid) {
406                         execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL);
407                         _exit(1);
408                 }
409                 else if (pid > 0) {
410                         /* Block the parent until the move is complete.*/
411                         waitpid(pid, &status, 0);
412                 }
413         }
414
415         ast_free(f->filename);
416         ast_free(f->realfilename);
417         if (f->vfs)
418                 ast_closestream(f->vfs);
419         ast_free(f->write_buffer);
420         ast_free((void *)f->orig_chan_name);
421         ao2_cleanup(f->lastwriteformat);
422         ao2_cleanup(f->fr.subclass.format);
423         ast_module_unref(f->fmt->module);
424 }
425
426 static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *bfile)
427 {
428         struct ast_filestream *s;
429         int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;    /* total allocation size */
430
431         if (!ast_module_running_ref(fmt->module)) {
432                 return NULL;
433         }
434
435         s = ao2_alloc(l, filestream_destructor);
436         if (!s) {
437                 ast_module_unref(fmt->module);
438                 return NULL;
439         }
440         s->fmt = fmt;
441         s->f = bfile;
442
443         if (fmt->desc_size)
444                 s->_private = ((char *)(s + 1)) + fmt->buf_size;
445         if (fmt->buf_size)
446                 s->buf = (char *)(s + 1);
447         s->fr.src = fmt->name;
448
449         if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_AUDIO) {
450                 s->fr.frametype = AST_FRAME_VOICE;
451         } else if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_VIDEO) {
452                 s->fr.frametype = AST_FRAME_VIDEO;
453         }
454         s->fr.mallocd = 0;
455         s->fr.subclass.format = ao2_bump(fmt->format);
456
457         return s;
458 }
459
460 /*
461  * Default implementations of open and rewrite.
462  * Only use them if you don't have expensive stuff to do.
463  */
464 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
465
466 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
467 {
468         struct ast_format_def *f = s->fmt;
469         int ret = -1;
470         int (*openfn)(struct ast_filestream *s);
471
472         if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s))
473                 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
474         else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
475                 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
476         else {
477                 /* preliminary checks succeed. */
478                 ret = 0;
479         }
480         return ret;
481 }
482
483 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
484 {
485         return fn_wrapper(s, comment, WRAP_REWRITE);
486 }
487
488 static int open_wrapper(struct ast_filestream *s)
489 {
490         return fn_wrapper(s, NULL, WRAP_OPEN);
491 }
492
493 enum file_action {
494         ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
495         ACTION_DELETE,  /* delete file, return 0 on success, -1 on error */
496         ACTION_RENAME,  /* rename file. return 0 on success, -1 on error */
497         ACTION_OPEN,
498         ACTION_COPY     /* copy file. return 0 on success, -1 on error */
499 };
500
501 /*!
502  * \internal
503  * \brief perform various actions on a file. Second argument
504  * \note arg2 depends on the command:
505  *      unused for DELETE
506  *  optional ast_format_cap holding all the formats found for a file, for EXISTS.
507  *      destination file name (const char *) for COPY and RENAME
508  *      struct ast_channel * for OPEN
509  * if fmt is NULL, OPEN will return the first matching entry,
510  * whereas other functions will run on all matching entries.
511  */
512 static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
513 {
514         struct ast_format_def *f;
515         int res = (action == ACTION_EXISTS) ? 0 : -1;
516
517         AST_RWLIST_RDLOCK(&formats);
518         /* Check for a specific format */
519         AST_RWLIST_TRAVERSE(&formats, f, list) {
520                 char *stringp, *ext = NULL;
521
522                 if (fmt && !exts_compare(f->exts, fmt))
523                         continue;
524
525                 /* Look for a file matching the supported extensions.
526                  * The file must exist, and for OPEN, must match
527                  * one of the formats supported by the channel.
528                  */
529                 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
530                 while ( (ext = strsep(&stringp, "|")) ) {
531                         struct stat st;
532                         char *fn = build_filename(filename, ext);
533
534                         if (fn == NULL)
535                                 continue;
536
537                         if ( stat(fn, &st) ) { /* file not existent */
538                                 ast_free(fn);
539                                 continue;
540                         }
541                         /* for 'OPEN' we need to be sure that the format matches
542                          * what the channel can process
543                          */
544                         if (action == ACTION_OPEN) {
545                                 struct ast_channel *chan = (struct ast_channel *)arg2;
546                                 FILE *bfile;
547                                 struct ast_filestream *s;
548
549                                 if ((ast_format_cmp(ast_channel_writeformat(chan), f->format) == AST_FORMAT_CMP_NOT_EQUAL) &&
550                                      !(((ast_format_get_type(f->format) == AST_MEDIA_TYPE_AUDIO) && fmt) ||
551                                           ((ast_format_get_type(f->format) == AST_MEDIA_TYPE_VIDEO) && fmt))) {
552                                         ast_free(fn);
553                                         continue;       /* not a supported format */
554                                 }
555                                 if ( (bfile = fopen(fn, "r")) == NULL) {
556                                         ast_free(fn);
557                                         continue;       /* cannot open file */
558                                 }
559                                 s = get_filestream(f, bfile);
560                                 if (!s) {
561                                         fclose(bfile);
562                                         ast_free(fn);   /* cannot allocate descriptor */
563                                         continue;
564                                 }
565                                 if (open_wrapper(s)) {
566                                         ast_free(fn);
567                                         ast_closestream(s);
568                                         continue;       /* cannot run open on file */
569                                 }
570                                 if (st.st_size == 0) {
571                                         ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
572                                 }
573                                 /* ok this is good for OPEN */
574                                 res = 1;        /* found */
575                                 s->lasttimeout = -1;
576                                 s->fmt = f;
577                                 s->trans = NULL;
578                                 s->filename = NULL;
579                                 if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
580                                         if (ast_channel_stream(chan))
581                                                 ast_closestream(ast_channel_stream(chan));
582                                         ast_channel_stream_set(chan, s);
583                                 } else {
584                                         if (ast_channel_vstream(chan))
585                                                 ast_closestream(ast_channel_vstream(chan));
586                                         ast_channel_vstream_set(chan, s);
587                                 }
588                                 ast_free(fn);
589                                 break;
590                         }
591                         switch (action) {
592                         case ACTION_OPEN:
593                                 break;  /* will never get here */
594
595                         case ACTION_EXISTS:     /* return the matching format */
596                                 /* if arg2 is present, it is a format capabilities structure.
597                                  * Add this format to the set of formats this file can be played in */
598                                 if (arg2) {
599                                         ast_format_cap_append((struct ast_format_cap *) arg2, f->format, 0);
600                                 }
601                                 res = 1; /* file does exist and format it exists in is returned in arg2 */
602                                 break;
603
604                         case ACTION_DELETE:
605                                 if ( (res = unlink(fn)) )
606                                         ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
607                                 break;
608
609                         case ACTION_RENAME:
610                         case ACTION_COPY: {
611                                 char *nfn = build_filename((const char *)arg2, ext);
612                                 if (!nfn)
613                                         ast_log(LOG_WARNING, "Out of memory\n");
614                                 else {
615                                         res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
616                                         if (res)
617                                                 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
618                                                         action == ACTION_COPY ? "copy" : "rename",
619                                                          fn, nfn, strerror(errno));
620                                         ast_free(nfn);
621                                 }
622                             }
623                                 break;
624
625                         default:
626                                 ast_log(LOG_WARNING, "Unknown helper %u\n", action);
627                         }
628                         ast_free(fn);
629                 }
630         }
631         AST_RWLIST_UNLOCK(&formats);
632         return res;
633 }
634
635 static int is_absolute_path(const char *filename)
636 {
637         return filename[0] == '/';
638 }
639
640 /*!
641  * \brief test if a file exists for a given format.
642  * \note result_cap is OPTIONAL
643  * \retval 1, true and result_cap represents format capabilities file exists in.
644  * \retval 0, false
645  */
646 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
647                            char *buf, int buflen, struct ast_format_cap *result_cap)
648 {
649         if (buf == NULL) {
650                 return 0;
651         }
652
653         if (!ast_media_cache_retrieve(filename, NULL, buf, buflen)) {
654                 return filehelper(buf, result_cap, NULL, ACTION_EXISTS);
655         }
656
657         if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
658                 if (lang) {
659                         snprintf(buf, buflen, "%s/%s", lang, filename);
660                 } else {
661                         snprintf(buf, buflen, "%s", filename);
662                 }
663         } else { /* old layout */
664                 strcpy(buf, filename);  /* first copy the full string */
665                 if (lang) {
666                         /* insert the language and suffix if needed */
667                         const char *c = strrchr(filename, '/');
668                         int offset = c ? c - filename + 1 : 0;  /* points right after the last '/' */
669                         snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
670                 }
671         }
672
673         return filehelper(buf, result_cap, fmt, ACTION_EXISTS);
674 }
675
676 /*!
677  * \brief helper routine to locate a file with a given format
678  * and language preference.
679  *
680  * \note Try preflang, preflang with stripped '_' suffices, or NULL.
681  *
682  * \note The last parameter(s) point to a buffer of sufficient size,
683  * which on success is filled with the matching filename.
684  *
685  * \param filename Name of the file.
686  * \param fmt Format to look for the file in. OPTIONAL
687  * \param preflang The perfered language
688  * \param buf Returns the matching filename
689  * \param buflen Size of the buf
690  * \param result_cap OPTIONAL format capabilities result structure
691  *        returns what formats the file was found in.
692  *
693  * \retval 1, true. file exists and result format is set
694  * \retval 0, false. file does not exist.
695  */
696 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
697                            char *buf, int buflen, struct ast_format_cap *result_cap)
698 {
699         char *lang;
700
701         if (buf == NULL) {
702                 return 0;
703         }
704
705         /* We try languages in the following order:
706          *    preflang (may include dialect and style codes)
707          *    lang (preflang without dialect - if any)
708          *    <none>
709          *    default (unless the same as preflang or lang without dialect)
710          */
711
712         lang = ast_strdupa(preflang);
713
714         /* Try preferred language, including removing any style or dialect codes */
715         while (!ast_strlen_zero(lang)) {
716                 char *end;
717
718                 if (fileexists_test(filename, fmt, lang, buf, buflen, result_cap)) {
719                         return 1;
720                 }
721
722                 if ((end = strrchr(lang, '_')) != NULL) {
723                         *end = '\0';
724                         continue;
725                 }
726
727                 break;
728         }
729
730         /* Try without any language */
731         if (fileexists_test(filename, fmt, NULL, buf, buflen, result_cap)) {
732                 return 1;
733         }
734
735         /* Finally try the default language unless it was already tried before */
736         if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
737                 if ((fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen, result_cap)) > 0) {
738                         return 1;
739                 }
740         }
741
742         return 0;
743 }
744
745 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
746 {
747         return ast_openstream_full(chan, filename, preflang, 0);
748 }
749
750 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
751 {
752         /*
753          * Use fileexists_core() to find a file in a compatible
754          * language and format, set up a suitable translator,
755          * and open the stream.
756          */
757         struct ast_format_cap *file_fmt_cap;
758         int res;
759         int buflen;
760         char *buf;
761
762         if (!asis) {
763                 /* do this first, otherwise we detect the wrong writeformat */
764                 ast_stopstream(chan);
765                 if (ast_channel_generator(chan))
766                         ast_deactivate_generator(chan);
767         }
768         if (preflang == NULL)
769                 preflang = "";
770         buflen = strlen(preflang) + strlen(filename) + 4;
771         buf = ast_alloca(buflen);
772
773         if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
774                 return NULL;
775         }
776         if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
777                 !ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) {
778
779                 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
780                 ao2_ref(file_fmt_cap, -1);
781                 return NULL;
782         }
783
784         /* Set the channel to a format we can work with and save off the previous format. */
785         ast_channel_lock(chan);
786         ast_channel_set_oldwriteformat(chan, ast_channel_writeformat(chan));
787         /* Set the channel to the best format that exists for the file. */
788         res = ast_set_write_format_from_cap(chan, file_fmt_cap);
789         ast_channel_unlock(chan);
790         /* don't need this anymore now that the channel's write format is set. */
791         ao2_ref(file_fmt_cap, -1);
792
793         if (res == -1) {        /* No format available that works with this channel */
794                 return NULL;
795         }
796         res = filehelper(buf, chan, NULL, ACTION_OPEN);
797         if (res >= 0)
798                 return ast_channel_stream(chan);
799         return NULL;
800 }
801
802 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
803 {
804         /* As above, but for video. But here we don't have translators
805          * so we must enforce a format.
806          */
807         struct ast_format_cap *nativeformats, *tmp_cap;
808         char *buf;
809         int buflen;
810         int i, fd;
811
812         if (preflang == NULL) {
813                 preflang = "";
814         }
815         buflen = strlen(preflang) + strlen(filename) + 4;
816         buf = ast_alloca(buflen);
817
818         ast_channel_lock(chan);
819         nativeformats = ao2_bump(ast_channel_nativeformats(chan));
820         ast_channel_unlock(chan);
821
822         /* is the channel capable of video without translation ?*/
823         if (!ast_format_cap_has_type(nativeformats, AST_MEDIA_TYPE_VIDEO)) {
824                 ao2_cleanup(nativeformats);
825                 return NULL;
826         }
827         if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
828                 ao2_cleanup(nativeformats);
829                 return NULL;
830         }
831         /* Video is supported, so see what video formats exist for this file */
832         if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
833                 ao2_ref(tmp_cap, -1);
834                 ao2_cleanup(nativeformats);
835                 return NULL;
836         }
837
838         /* iterate over file formats and pick the first one compatible with the channel's native formats */
839         for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
840                 struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
841
842                 if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
843                         !ast_format_cap_iscompatible(nativeformats, tmp_cap)) {
844                         ao2_ref(format, -1);
845                         continue;
846                 }
847
848                 fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
849                 if (fd >= 0) {
850                         ao2_ref(format, -1);
851                         ao2_ref(tmp_cap, -1);
852                         ao2_cleanup(nativeformats);
853                         return ast_channel_vstream(chan);
854                 }
855                 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
856                 ao2_ref(format, -1);
857         }
858         ao2_ref(tmp_cap, -1);
859         ao2_cleanup(nativeformats);
860
861         return NULL;
862 }
863
864 static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext)
865 {
866         struct ast_frame *fr, *new_fr;
867
868         if (!s || !s->fmt) {
869                 return NULL;
870         }
871
872         if (!(fr = s->fmt->read(s, whennext))) {
873                 return NULL;
874         }
875
876         if (!(new_fr = ast_frisolate(fr))) {
877                 ast_frfree(fr);
878                 return NULL;
879         }
880
881         if (new_fr != fr) {
882                 ast_frfree(fr);
883                 fr = new_fr;
884         }
885
886         return fr;
887 }
888
889 struct ast_frame *ast_readframe(struct ast_filestream *s)
890 {
891         int whennext = 0;
892
893         return read_frame(s, &whennext);
894 }
895
896 enum fsread_res {
897         FSREAD_FAILURE,
898         FSREAD_SUCCESS_SCHED,
899         FSREAD_SUCCESS_NOSCHED,
900 };
901
902 static int ast_fsread_audio(const void *data);
903
904 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
905 {
906         int whennext = 0;
907
908         while (!whennext) {
909                 struct ast_frame *fr;
910
911                 if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
912                         goto return_failure;
913                 }
914
915                 fr = read_frame(s, &whennext);
916
917                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
918                         if (fr) {
919                                 ast_debug(2, "Failed to write frame\n");
920                                 ast_frfree(fr);
921                         }
922                         goto return_failure;
923                 }
924
925                 if (fr) {
926                         ast_frfree(fr);
927                 }
928         }
929
930         if (whennext != s->lasttimeout) {
931                 if (ast_channel_timingfd(s->owner) > -1) {
932                         float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format);
933                         unsigned int rate;
934
935                         rate = (unsigned int) roundf(samp_rate / ((float) whennext));
936
937                         ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1);
938                 } else {
939                         ast_channel_streamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_get_sample_rate(s->fmt->format) / 1000), ast_fsread_audio, s));
940                 }
941                 s->lasttimeout = whennext;
942                 return FSREAD_SUCCESS_NOSCHED;
943         }
944         return FSREAD_SUCCESS_SCHED;
945
946 return_failure:
947         ast_channel_streamid_set(s->owner, -1);
948         ast_settimeout(s->owner, 0, NULL, NULL);
949         return FSREAD_FAILURE;
950 }
951
952 static int ast_fsread_audio(const void *data)
953 {
954         struct ast_filestream *fs = (struct ast_filestream *)data;
955         enum fsread_res res;
956
957         res = ast_readaudio_callback(fs);
958
959         if (res == FSREAD_SUCCESS_SCHED)
960                 return 1;
961
962         return 0;
963 }
964
965 static int ast_fsread_video(const void *data);
966
967 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
968 {
969         int whennext = 0;
970
971         while (!whennext) {
972                 struct ast_frame *fr = read_frame(s, &whennext);
973
974                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
975                         if (fr) {
976                                 ast_debug(2, "Failed to write frame\n");
977                                 ast_frfree(fr);
978                         }
979                         ast_channel_vstreamid_set(s->owner, -1);
980                         return FSREAD_FAILURE;
981                 }
982
983                 if (fr) {
984                         ast_frfree(fr);
985                 }
986         }
987
988         if (whennext != s->lasttimeout) {
989                 ast_channel_vstreamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_get_sample_rate(s->fmt->format) / 1000), ast_fsread_video, s));
990                 s->lasttimeout = whennext;
991                 return FSREAD_SUCCESS_NOSCHED;
992         }
993
994         return FSREAD_SUCCESS_SCHED;
995 }
996
997 static int ast_fsread_video(const void *data)
998 {
999         struct ast_filestream *fs = (struct ast_filestream *)data;
1000         enum fsread_res res;
1001
1002         res = ast_readvideo_callback(fs);
1003
1004         if (res == FSREAD_SUCCESS_SCHED)
1005                 return 1;
1006
1007         return 0;
1008 }
1009
1010 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
1011 {
1012         s->owner = chan;
1013         return 0;
1014 }
1015
1016 int ast_playstream(struct ast_filestream *s)
1017 {
1018         enum fsread_res res;
1019
1020         if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
1021                 res = ast_readaudio_callback(s);
1022         else
1023                 res = ast_readvideo_callback(s);
1024
1025         return (res == FSREAD_FAILURE) ? -1 : 0;
1026 }
1027
1028 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
1029 {
1030         return fs->fmt->seek(fs, sample_offset, whence);
1031 }
1032
1033 int ast_truncstream(struct ast_filestream *fs)
1034 {
1035         return fs->fmt->trunc(fs);
1036 }
1037
1038 off_t ast_tellstream(struct ast_filestream *fs)
1039 {
1040         return fs->fmt->tell(fs);
1041 }
1042
1043 int ast_ratestream(struct ast_filestream *fs)
1044 {
1045         return ast_format_get_sample_rate(fs->fmt->format);
1046 }
1047
1048 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
1049 {
1050         return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1051 }
1052
1053 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
1054 {
1055         return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1056 }
1057
1058 int ast_closestream(struct ast_filestream *f)
1059 {
1060         /* This used to destroy the filestream, but it now just decrements a refcount.
1061          * We close the stream in order to quit queuing frames now, because we might
1062          * change the writeformat, which could result in a subsequent write error, if
1063          * the format is different. */
1064         if (f == NULL) {
1065                 return 0;
1066         }
1067         filestream_close(f);
1068         ao2_ref(f, -1);
1069         return 0;
1070 }
1071
1072
1073 /*
1074  * Look the various language-specific places where a file could exist.
1075  */
1076 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
1077 {
1078         char *buf;
1079         int buflen;
1080
1081         if (preflang == NULL)
1082                 preflang = "";
1083         buflen = strlen(preflang) + strlen(filename) + 4;       /* room for everything */
1084         buf = ast_alloca(buflen);
1085         return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
1086 }
1087
1088 int ast_filedelete(const char *filename, const char *fmt)
1089 {
1090         return filehelper(filename, NULL, fmt, ACTION_DELETE);
1091 }
1092
1093 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
1094 {
1095         return filehelper(filename, filename2, fmt, ACTION_RENAME);
1096 }
1097
1098 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
1099 {
1100         return filehelper(filename, filename2, fmt, ACTION_COPY);
1101 }
1102
1103 static int __ast_file_read_dirs(const char *path, ast_file_on_file on_file,
1104                                 void *obj, int max_depth)
1105 {
1106         DIR *dir;
1107         struct dirent *entry;
1108         int res;
1109
1110         if (!(dir = opendir(path))) {
1111                 ast_log(LOG_ERROR, "Error opening directory - %s: %s\n",
1112                         path, strerror(errno));
1113                 return -1;
1114         }
1115
1116         --max_depth;
1117
1118         res = 0;
1119
1120         while ((entry = readdir(dir)) != NULL && !errno) {
1121                 int is_file = 0;
1122                 int is_dir = 0;
1123                 RAII_VAR(char *, full_path, NULL, ast_free);
1124
1125                 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
1126                         continue;
1127                 }
1128
1129 /*
1130  * If the dirent structure has a d_type use it to determine if we are dealing with
1131  * a file or directory. Unfortunately if it doesn't have it, or if the type is
1132  * unknown, or a link then we'll need to use the stat function instead.
1133  */
1134 #ifdef _DIRENT_HAVE_D_TYPE
1135                 if (entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK) {
1136                         is_file = entry->d_type == DT_REG;
1137                         is_dir = entry->d_type == DT_DIR;
1138                 } else
1139 #endif
1140                 {
1141                         struct stat statbuf;
1142
1143                         /*
1144                          * Don't use alloca or we risk blowing out the stack if recursing
1145                          * into subdirectories.
1146                          */
1147                         full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
1148                         if (!full_path) {
1149                                 return -1;
1150                         }
1151                         sprintf(full_path, "%s/%s", path, entry->d_name);
1152
1153                         if (stat(full_path, &statbuf)) {
1154                                 ast_log(LOG_ERROR, "Error reading path stats - %s: %s\n",
1155                                         full_path, strerror(errno));
1156                                 /*
1157                                  * Output an error, but keep going. It could just be
1158                                  * a broken link and other files could be fine.
1159                                  */
1160                                 continue;
1161                         }
1162
1163                         is_file = S_ISREG(statbuf.st_mode);
1164                         is_dir = S_ISDIR(statbuf.st_mode);
1165                 }
1166
1167                 if (is_file) {
1168                         /* If the handler returns non-zero then stop */
1169                         if ((res = on_file(path, entry->d_name, obj))) {
1170                                 break;
1171                         }
1172                         /* Otherwise move on to next item in directory */
1173                         continue;
1174                 }
1175
1176                 if (!is_dir) {
1177                         ast_debug(5, "Skipping %s: not a regular file or directory\n", full_path);
1178                         continue;
1179                 }
1180
1181                 /* Only re-curse into sub-directories if not at the max depth */
1182                 if (max_depth != 0) {
1183                         if (!full_path) {
1184                                 /* Don't use alloca.  See note above. */
1185                                 full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
1186                                 if (!full_path) {
1187                                         return -1;
1188                                 }
1189                                 sprintf(full_path, "%s/%s", path, entry->d_name);
1190                         }
1191
1192                         if ((res = __ast_file_read_dirs(full_path, on_file, obj, max_depth))) {
1193                                 break;
1194                         }
1195                 }
1196         }
1197
1198         closedir(dir);
1199
1200         if (!res && errno) {
1201                 ast_log(LOG_ERROR, "Error while reading directories - %s: %s\n",
1202                         path, strerror(errno));
1203                 res = -1;
1204         }
1205
1206         return res;
1207 }
1208
1209 #if !defined(__GLIBC__)
1210 /*!
1211  * \brief Lock to hold when iterating over directories.
1212  *
1213  * Currently, 'readdir' is not required to be thread-safe. In most modern implementations
1214  * it should be safe to make concurrent calls into 'readdir' that specify different directory
1215  * streams (glibc would be one of these). However, since it is potentially unsafe for some
1216  * implementations we'll use our own locking in order to achieve synchronization for those.
1217  */
1218 AST_MUTEX_DEFINE_STATIC(read_dirs_lock);
1219 #endif
1220
1221 int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
1222 {
1223         int res;
1224
1225         errno = 0;
1226
1227 #if !defined(__GLIBC__)
1228         ast_mutex_lock(&read_dirs_lock);
1229 #endif
1230
1231         res = __ast_file_read_dirs(dir_name, on_file, obj, max_depth);
1232
1233 #if !defined(__GLIBC__)
1234         ast_mutex_unlock(&read_dirs_lock);
1235 #endif
1236
1237         return res;
1238 }
1239
1240 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
1241 {
1242         struct ast_filestream *fs;
1243         struct ast_filestream *vfs=NULL;
1244         off_t pos;
1245         int seekattempt;
1246         int res;
1247
1248         fs = ast_openstream(chan, filename, preflang);
1249         if (!fs) {
1250                 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
1251                 ast_channel_lock(chan);
1252                 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
1253                         filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
1254                 ast_channel_unlock(chan);
1255                 return -1;
1256         }
1257
1258         /* check to see if there is any data present (not a zero length file),
1259          * done this way because there is no where for ast_openstream_full to
1260          * return the file had no data. */
1261         pos = ftello(fs->f);
1262         seekattempt = fseeko(fs->f, -1, SEEK_END);
1263         if (seekattempt) {
1264                 if (errno == EINVAL) {
1265                         /* Zero-length file, as opposed to a pipe */
1266                         return 0;
1267                 } else {
1268                         ast_seekstream(fs, 0, SEEK_SET);
1269                 }
1270         } else {
1271                 fseeko(fs->f, pos, SEEK_SET);
1272         }
1273
1274         vfs = ast_openvstream(chan, filename, preflang);
1275         if (vfs) {
1276                 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
1277         }
1278
1279         if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
1280                 fs->orig_chan_name = ast_strdup(ast_channel_name(chan));
1281         if (ast_applystream(chan, fs))
1282                 return -1;
1283         if (vfs && ast_applystream(chan, vfs))
1284                 return -1;
1285         ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", filename, ast_channel_name(chan));
1286         res = ast_playstream(fs);
1287         if (!res && vfs)
1288                 res = ast_playstream(vfs);
1289
1290         if (VERBOSITY_ATLEAST(3)) {
1291                 ast_channel_lock(chan);
1292                 ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), filename, ast_format_get_name(ast_channel_writeformat(chan)), preflang ? preflang : "default");
1293                 ast_channel_unlock(chan);
1294         }
1295
1296         return res;
1297 }
1298
1299 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1300 {
1301         FILE *bfile;
1302         struct ast_format_def *f;
1303         struct ast_filestream *fs = NULL;
1304         char *fn;
1305         int format_found = 0;
1306
1307         AST_RWLIST_RDLOCK(&formats);
1308
1309         AST_RWLIST_TRAVERSE(&formats, f, list) {
1310                 fs = NULL;
1311                 if (!exts_compare(f->exts, type))
1312                         continue;
1313                 else
1314                         format_found = 1;
1315
1316                 fn = build_filename(filename, type);
1317                 if (!fn) {
1318                         continue;
1319                 }
1320                 errno = 0;
1321                 bfile = fopen(fn, "r");
1322
1323                 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1324                         ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1325                         if (fs) {
1326                                 ast_closestream(fs);
1327                         }
1328                         fs = NULL;
1329                         bfile = NULL;
1330                         ast_free(fn);
1331                         break;
1332                 }
1333                 /* found it */
1334                 fs->trans = NULL;
1335                 fs->fmt = f;
1336                 fs->flags = flags;
1337                 fs->mode = mode;
1338                 fs->filename = ast_strdup(filename);
1339                 fs->vfs = NULL;
1340                 ast_free(fn);
1341                 break;
1342         }
1343
1344         AST_RWLIST_UNLOCK(&formats);
1345         if (!format_found)
1346                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1347
1348         return fs;
1349 }
1350
1351 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1352 {
1353         int fd, myflags = 0;
1354         /* compiler claims this variable can be used before initialization... */
1355         FILE *bfile = NULL;
1356         struct ast_format_def *f;
1357         struct ast_filestream *fs = NULL;
1358         char *buf = NULL;
1359         size_t size = 0;
1360         int format_found = 0;
1361
1362         AST_RWLIST_RDLOCK(&formats);
1363
1364         /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1365         /* We really can't use O_APPEND as it will break WAV header updates */
1366         if (flags & O_APPEND) {
1367                 flags &= ~O_APPEND;
1368         } else {
1369                 myflags = O_TRUNC;
1370         }
1371
1372         myflags |= O_WRONLY | O_CREAT;
1373
1374         /* XXX need to fix this - we should just do the fopen,
1375          * not open followed by fdopen()
1376          */
1377         AST_RWLIST_TRAVERSE(&formats, f, list) {
1378                 char *fn, *orig_fn = NULL;
1379                 if (fs)
1380                         break;
1381
1382                 if (!exts_compare(f->exts, type))
1383                         continue;
1384                 else
1385                         format_found = 1;
1386
1387                 fn = build_filename(filename, type);
1388                 if (!fn) {
1389                         continue;
1390                 }
1391                 fd = open(fn, flags | myflags, mode);
1392                 if (fd > -1) {
1393                         /* fdopen() the resulting file stream */
1394                         bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1395                         if (!bfile) {
1396                                 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1397                                 close(fd);
1398                                 fd = -1;
1399                         }
1400                 }
1401
1402                 if (ast_opt_cache_record_files && (fd > -1)) {
1403                         char *c;
1404
1405                         fclose(bfile);  /* this also closes fd */
1406                         /*
1407                           We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1408                           What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1409                         */
1410                         orig_fn = ast_strdupa(fn);
1411                         for (c = fn; *c; c++)
1412                                 if (*c == '/')
1413                                         *c = '_';
1414
1415                         size = strlen(fn) + strlen(record_cache_dir) + 2;
1416                         buf = ast_alloca(size);
1417                         strcpy(buf, record_cache_dir);
1418                         strcat(buf, "/");
1419                         strcat(buf, fn);
1420                         ast_free(fn);
1421                         fn = buf;
1422                         fd = open(fn, flags | myflags, mode);
1423                         if (fd > -1) {
1424                                 /* fdopen() the resulting file stream */
1425                                 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1426                                 if (!bfile) {
1427                                         ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1428                                         close(fd);
1429                                         fd = -1;
1430                                 }
1431                         }
1432                 }
1433                 if (fd > -1) {
1434                         errno = 0;
1435                         fs = get_filestream(f, bfile);
1436                         if (fs) {
1437                                 if ((fs->write_buffer = ast_malloc(32768))) {
1438                                         setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1439                                 }
1440                         }
1441                         if (!fs || rewrite_wrapper(fs, comment)) {
1442                                 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1443                                 close(fd);
1444                                 if (orig_fn) {
1445                                         unlink(fn);
1446                                         unlink(orig_fn);
1447                                 }
1448                                 if (fs) {
1449                                         ast_closestream(fs);
1450                                         fs = NULL;
1451                                 }
1452                                 if (!buf) {
1453                                         ast_free(fn);
1454                                 }
1455                                 continue;
1456                         }
1457                         fs->trans = NULL;
1458                         fs->fmt = f;
1459                         fs->flags = flags;
1460                         fs->mode = mode;
1461                         if (orig_fn) {
1462                                 fs->realfilename = ast_strdup(orig_fn);
1463                                 fs->filename = ast_strdup(fn);
1464                         } else {
1465                                 fs->realfilename = NULL;
1466                                 fs->filename = ast_strdup(filename);
1467                         }
1468                         fs->vfs = NULL;
1469                         /* If truncated, we'll be at the beginning; if not truncated, then append */
1470                         f->seek(fs, 0, SEEK_END);
1471                 } else if (errno != EEXIST) {
1472                         ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1473                         if (orig_fn)
1474                                 unlink(orig_fn);
1475                 }
1476                 /* if buf != NULL then fn is already free and pointing to it */
1477                 if (!buf)
1478                         ast_free(fn);
1479         }
1480
1481         AST_RWLIST_UNLOCK(&formats);
1482
1483         if (!format_found)
1484                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1485
1486         return fs;
1487 }
1488
1489 static void waitstream_control(struct ast_channel *c,
1490                 enum ast_waitstream_fr_cb_values type,
1491                 ast_waitstream_fr_cb cb,
1492                 int skip_ms)
1493 {
1494         switch (type)
1495         {
1496         case AST_WAITSTREAM_CB_FASTFORWARD:
1497                 {
1498                         int eoftest;
1499                         ast_stream_fastforward(ast_channel_stream(c), skip_ms);
1500                         eoftest = fgetc(ast_channel_stream(c)->f);
1501                         if (feof(ast_channel_stream(c)->f)) {
1502                                 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1503                         } else {
1504                                 ungetc(eoftest, ast_channel_stream(c)->f);
1505                         }
1506                 }
1507                 break;
1508         case AST_WAITSTREAM_CB_REWIND:
1509                 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1510                 break;
1511         default:
1512                 break;
1513         }
1514
1515         if (cb) {
1516                 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1517                 cb(c, ms_len, type);
1518         }
1519
1520         ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1521                 "Control: %s\r\n"
1522                 "SkipMs: %d\r\n",
1523                 ast_channel_name(c),
1524                 (type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind",
1525                 skip_ms);
1526 }
1527
1528 /*!
1529  * \brief the core of all waitstream() functions
1530  */
1531 static int waitstream_core(struct ast_channel *c,
1532         const char *breakon,
1533         const char *forward,
1534         const char *reverse,
1535         int skip_ms,
1536         int audiofd,
1537         int cmdfd,
1538         const char *context,
1539         ast_waitstream_fr_cb cb)
1540 {
1541         const char *orig_chan_name = NULL;
1542
1543         int err = 0;
1544
1545         if (!breakon)
1546                 breakon = "";
1547         if (!forward)
1548                 forward = "";
1549         if (!reverse)
1550                 reverse = "";
1551
1552         /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1553         ast_channel_set_flag(c, AST_FLAG_END_DTMF_ONLY);
1554
1555         if (ast_test_flag(ast_channel_flags(c), AST_FLAG_MASQ_NOSTREAM))
1556                 orig_chan_name = ast_strdupa(ast_channel_name(c));
1557
1558         if (ast_channel_stream(c) && cb) {
1559                 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1560                 cb(c, ms_len, AST_WAITSTREAM_CB_START);
1561         }
1562
1563         while (ast_channel_stream(c)) {
1564                 int res;
1565                 int ms;
1566
1567                 if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) {
1568                         ast_stopstream(c);
1569                         err = 1;
1570                         break;
1571                 }
1572
1573                 ms = ast_sched_wait(ast_channel_sched(c));
1574
1575                 if (ms < 0 && !ast_channel_timingfunc(c)) {
1576                         ast_stopstream(c);
1577                         break;
1578                 }
1579                 if (ms < 0)
1580                         ms = 1000;
1581                 if (cmdfd < 0) {
1582                         res = ast_waitfor(c, ms);
1583                         if (res < 0) {
1584                                 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1585                                 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1586                                 return res;
1587                         }
1588                 } else {
1589                         int outfd;
1590                         struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1591                         if (!rchan && (outfd < 0) && (ms)) {
1592                                 /* Continue */
1593                                 if (errno == EINTR)
1594                                         continue;
1595                                 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1596                                 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1597                                 return -1;
1598                         } else if (outfd > -1) { /* this requires cmdfd set */
1599                                 /* The FD we were watching has something waiting */
1600                                 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1601                                 return 1;
1602                         }
1603                         /* if rchan is set, it is 'c' */
1604                         res = rchan ? 1 : 0; /* map into 'res' values */
1605                 }
1606                 if (res > 0) {
1607                         struct ast_frame *fr = ast_read(c);
1608                         if (!fr) {
1609                                 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1610                                 return -1;
1611                         }
1612                         switch (fr->frametype) {
1613                         case AST_FRAME_DTMF_END:
1614                                 if (context) {
1615                                         const char exten[2] = { fr->subclass.integer, '\0' };
1616                                         if (ast_exists_extension(c, context, exten, 1,
1617                                                 S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
1618                                                 res = fr->subclass.integer;
1619                                                 ast_frfree(fr);
1620                                                 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1621                                                 return res;
1622                                         }
1623                                 } else {
1624                                         res = fr->subclass.integer;
1625                                         if (strchr(forward, res)) {
1626                                                 waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1627                                         } else if (strchr(reverse, res)) {
1628                                                 waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1629                                         } else if (strchr(breakon, res)) {
1630                                                 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1631                                                         "Control: %s\r\n",
1632                                                         ast_channel_name(c),
1633                                                         "Break");
1634
1635                                                 ast_frfree(fr);
1636                                                 ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1637                                                 return res;
1638                                         }
1639                                 }
1640                                 break;
1641                         case AST_FRAME_CONTROL:
1642                                 switch (fr->subclass.integer) {
1643                                 case AST_CONTROL_STREAM_STOP:
1644                                 case AST_CONTROL_STREAM_SUSPEND:
1645                                 case AST_CONTROL_STREAM_RESTART:
1646                                         /* Fall-through and break out */
1647                                         ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1648                                                 "Control: %s\r\n",
1649                                                 ast_channel_name(c),
1650                                                 "Break");
1651                                         res = fr->subclass.integer;
1652                                         ast_frfree(fr);
1653                                         ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1654                                         return res;
1655                                 case AST_CONTROL_STREAM_REVERSE:
1656                                         if (!skip_ms) {
1657                                                 skip_ms = 3000;
1658                                         }
1659                                         waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1660                                         break;
1661                                 case AST_CONTROL_STREAM_FORWARD:
1662                                         if (!skip_ms) {
1663                                                 skip_ms = 3000;
1664                                         }
1665                                         waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1666                                         break;
1667                                 case AST_CONTROL_HANGUP:
1668                                 case AST_CONTROL_BUSY:
1669                                 case AST_CONTROL_CONGESTION:
1670                                         ast_frfree(fr);
1671                                         ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1672                                         return -1;
1673                                 case AST_CONTROL_RINGING:
1674                                 case AST_CONTROL_ANSWER:
1675                                 case AST_CONTROL_VIDUPDATE:
1676                                 case AST_CONTROL_SRCUPDATE:
1677                                 case AST_CONTROL_SRCCHANGE:
1678                                 case AST_CONTROL_HOLD:
1679                                 case AST_CONTROL_UNHOLD:
1680                                 case AST_CONTROL_CONNECTED_LINE:
1681                                 case AST_CONTROL_REDIRECTING:
1682                                 case AST_CONTROL_AOC:
1683                                 case AST_CONTROL_UPDATE_RTP_PEER:
1684                                 case AST_CONTROL_PVT_CAUSE_CODE:
1685                                 case -1:
1686                                         /* Unimportant */
1687                                         break;
1688                                 default:
1689                                         ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass.integer);
1690                                 }
1691                                 break;
1692                         case AST_FRAME_VOICE:
1693                                 /* Write audio if appropriate */
1694                                 if (audiofd > -1) {
1695                                         if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1696                                                 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1697                                         }
1698                                 }
1699                         default:
1700                                 /* Ignore all others */
1701                                 break;
1702                         }
1703                         ast_frfree(fr);
1704                 }
1705                 ast_sched_runq(ast_channel_sched(c));
1706         }
1707
1708         ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1709
1710         return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0;
1711 }
1712
1713 int ast_waitstream_fr_w_cb(struct ast_channel *c,
1714         const char *breakon,
1715         const char *forward,
1716         const char *reverse,
1717         int ms,
1718         ast_waitstream_fr_cb cb)
1719 {
1720         return waitstream_core(c, breakon, forward, reverse, ms,
1721                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
1722 }
1723
1724 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1725 {
1726         return waitstream_core(c, breakon, forward, reverse, ms,
1727                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
1728 }
1729
1730 /*! \internal
1731  * \brief Clean up the return value of a waitstream call
1732  *
1733  * It's possible for a control frame to come in from an external source and break the
1734  * playback. From a consumer of most ast_waitstream_* function callers, this should
1735  * appear like normal playback termination, i.e., return 0 and not the value of the
1736  * control frame.
1737  */
1738 static int sanitize_waitstream_return(int return_value)
1739 {
1740         switch (return_value) {
1741         case AST_CONTROL_STREAM_STOP:
1742         case AST_CONTROL_STREAM_SUSPEND:
1743         case AST_CONTROL_STREAM_RESTART:
1744                 /* Fall through and set return_value to 0 */
1745                 return_value = 0;
1746                 break;
1747         default:
1748                 /* Do nothing */
1749                 break;
1750         }
1751
1752         return return_value;
1753 }
1754
1755 int ast_waitstream(struct ast_channel *c, const char *breakon)
1756 {
1757         int res;
1758
1759         res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
1760
1761         return sanitize_waitstream_return(res);
1762 }
1763
1764 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1765 {
1766         int res;
1767
1768         res = waitstream_core(c, breakon, NULL, NULL, 0,
1769                 audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
1770
1771         return sanitize_waitstream_return(res);
1772 }
1773
1774 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1775 {
1776         int res;
1777
1778         /* Waitstream, with return in the case of a valid 1 digit extension */
1779         /* in the current or specified context being pressed */
1780         if (!context)
1781                 context = ast_channel_context(c);
1782         res = waitstream_core(c, NULL, NULL, NULL, 0,
1783                 -1, -1, context, NULL /* no callback */);
1784
1785         return sanitize_waitstream_return(res);
1786 }
1787
1788 /*
1789  * if the file name is non-empty, try to play it.
1790  * Return 0 if success, -1 if error, digit if interrupted by a digit.
1791  * If digits == "" then we can simply check for non-zero.
1792  */
1793 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1794 {
1795         int res = 0;
1796         if (!ast_strlen_zero(file)) {
1797                 res = ast_streamfile(chan, file, ast_channel_language(chan));
1798                 if (!res) {
1799                         res = ast_waitstream(chan, digits);
1800                 }
1801         }
1802         return res;
1803 }
1804
1805 char *ast_format_str_reduce(char *fmts)
1806 {
1807         struct ast_format_def *f;
1808         struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
1809         char *fmts_str[AST_MAX_FORMATS];
1810         char *stringp, *type;
1811         char *orig = fmts;
1812         int i, j, x, first, found = 0;
1813         int len = strlen(fmts) + 1;
1814         int res;
1815
1816         if (AST_RWLIST_RDLOCK(&formats)) {
1817                 ast_log(LOG_WARNING, "Unable to lock format list\n");
1818                 return NULL;
1819         }
1820
1821         stringp = ast_strdupa(fmts);
1822
1823         for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1824                 AST_RWLIST_TRAVERSE(&formats, f, list) {
1825                         if (exts_compare(f->exts, type)) {
1826                                 found = 1;
1827                                 break;
1828                         }
1829                 }
1830
1831                 fmts_str[x] = type;
1832                 if (found) {
1833                         fmts_ptr[x] = f;
1834                 } else {
1835                         fmts_ptr[x] = NULL;
1836                 }
1837         }
1838         AST_RWLIST_UNLOCK(&formats);
1839
1840         first = 1;
1841         for (i = 0; i < x; i++) {
1842                 /* ignore invalid entries */
1843                 if (!fmts_ptr[i]) {
1844                         ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1845                         continue;
1846                 }
1847
1848                 /* special handling for the first entry */
1849                 if (first) {
1850                         res = snprintf(fmts, len, "%s", fmts_str[i]);
1851                         fmts += res;
1852                         len -= res;
1853                         first = 0;
1854                         continue;
1855                 }
1856
1857                 found = 0;
1858                 for (j = 0; j < i; j++) {
1859                         /* this is a duplicate */
1860                         if (fmts_ptr[j] == fmts_ptr[i]) {
1861                                 found = 1;
1862                                 break;
1863                         }
1864                 }
1865
1866                 if (!found) {
1867                         res = snprintf(fmts, len, "|%s", fmts_str[i]);
1868                         fmts += res;
1869                         len -= res;
1870                 }
1871         }
1872
1873         if (first) {
1874                 ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1875                 return NULL;
1876         }
1877
1878         return orig;
1879 }
1880
1881 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1882 {
1883 #define FORMAT "%-10s %-10s %-20s\n"
1884 #define FORMAT2 "%-10s %-10s %-20s\n"
1885         struct ast_format_def *f;
1886         int count_fmt = 0;
1887
1888         switch (cmd) {
1889         case CLI_INIT:
1890                 e->command = "core show file formats";
1891                 e->usage =
1892                         "Usage: core show file formats\n"
1893                         "       Displays currently registered file formats (if any).\n";
1894                 return NULL;
1895         case CLI_GENERATE:
1896                 return NULL;
1897         }
1898
1899         if (a->argc != 4)
1900                 return CLI_SHOWUSAGE;
1901
1902         ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1903         ast_cli(a->fd, FORMAT, "------", "----", "----------");
1904
1905         AST_RWLIST_RDLOCK(&formats);
1906         AST_RWLIST_TRAVERSE(&formats, f, list) {
1907                 ast_cli(a->fd, FORMAT2, ast_format_get_name(f->format), f->name, f->exts);
1908                 count_fmt++;
1909         }
1910         AST_RWLIST_UNLOCK(&formats);
1911         ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1912         return CLI_SUCCESS;
1913 #undef FORMAT
1914 #undef FORMAT2
1915 }
1916
1917 struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
1918 {
1919         struct ast_format_def *f;
1920         SCOPED_RDLOCK(lock, &formats.lock);
1921         AST_RWLIST_TRAVERSE(&formats, f, list) {
1922                 if (exts_compare(f->exts, file_ext)) {
1923                         return f->format;
1924                 }
1925         }
1926
1927         return NULL;
1928 }
1929
1930 int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity)
1931 {
1932         struct ast_format_def *f;
1933         SCOPED_RDLOCK(lock, &formats.lock);
1934
1935         ast_assert(buffer && capacity);
1936
1937         AST_RWLIST_TRAVERSE(&formats, f, list) {
1938                 if (type_in_list(f->mime_types, mime_type, strcasecmp)) {
1939                         size_t item_len = strcspn(f->exts, "|");
1940                         size_t bytes_written = snprintf(buffer, capacity, ".%.*s", (int) item_len, f->exts);
1941                         if (bytes_written < capacity) {
1942                                 /* Only return success if we didn't truncate */
1943                                 return 1;
1944                         }
1945                 }
1946         }
1947
1948         return 0;
1949 }
1950
1951 static struct ast_cli_entry cli_file[] = {
1952         AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1953 };
1954
1955 static void file_shutdown(void)
1956 {
1957         ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
1958         STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type);
1959         STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type);
1960 }
1961
1962 int ast_file_init(void)
1963 {
1964         STASIS_MESSAGE_TYPE_INIT(ast_format_register_type);
1965         STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type);
1966         ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
1967         ast_register_cleanup(file_shutdown);
1968         return 0;
1969 }