don't unlink files just because they can't be opened (bug )

clean up and reformat ast_readfile and ast_writefile


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6020 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.2-netsec
Kevin P. Fleming 20 years ago
parent bcc5eb829d
commit ca2f18839d

180
file.c

@ -807,59 +807,64 @@ 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) struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
{ {
int fd,myflags = 0; int fd;
struct ast_format *f; struct ast_format *f;
struct ast_filestream *fs=NULL; struct ast_filestream *fs = NULL;
char *fn; char *fn;
if (ast_mutex_lock(&formatlock)) { if (ast_mutex_lock(&formatlock)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return NULL; return NULL;
} }
f = formats;
while(f) { for (f = formats; f && !fs; f = f->next) {
if (exts_compare(f->exts, type)) { if (!exts_compare(f->exts, type))
fn = build_filename(filename, type); continue;
fd = open(fn, flags | myflags);
if (fd >= 0) { fn = build_filename(filename, type);
errno = 0; fd = open(fn, flags);
if ((fs = f->open(fd))) { if (fd >= 0) {
fs->trans = NULL; errno = 0;
fs->fmt = f;
fs->flags = flags; if (!(fs = f->open(fd))) {
fs->mode = mode; ast_log(LOG_WARNING, "Unable to open %s\n", fn);
fs->filename = strdup(filename); close(fd);
fs->vfs = NULL; free(fn);
} else { continue;
ast_log(LOG_WARNING, "Unable to open %s\n", fn); }
close(fd);
unlink(fn); fs->trans = NULL;
} fs->fmt = f;
} else if (errno != EEXIST) fs->flags = flags;
ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno)); fs->mode = mode;
free(fn); fs->filename = strdup(filename);
break; fs->vfs = NULL;
} } else if (errno != EEXIST)
f = f->next; ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
free(fn);
} }
ast_mutex_unlock(&formatlock); ast_mutex_unlock(&formatlock);
if (!f) if (!fs)
ast_log(LOG_WARNING, "No such format '%s'\n", type); ast_log(LOG_WARNING, "No such format '%s'\n", type);
return fs; return fs;
} }
struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode) 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; int fd, myflags = 0;
struct ast_format *f; struct ast_format *f;
struct ast_filestream *fs=NULL; struct ast_filestream *fs = NULL;
char *fn,*orig_fn=NULL; char *fn, *orig_fn = NULL;
char *buf=NULL; char *buf = NULL;
size_t size = 0; size_t size = 0;
if (ast_mutex_lock(&formatlock)) { if (ast_mutex_lock(&formatlock)) {
ast_log(LOG_WARNING, "Unable to lock format list\n"); ast_log(LOG_WARNING, "Unable to lock format list\n");
return NULL; return NULL;
} }
/* set the O_TRUNC flag if and only if there is no O_APPEND specified */ /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
if (flags & O_APPEND) { if (flags & O_APPEND) {
/* We really can't use O_APPEND as it will break WAV header updates */ /* We really can't use O_APPEND as it will break WAV header updates */
@ -870,69 +875,72 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
myflags |= O_WRONLY | O_CREAT; myflags |= O_WRONLY | O_CREAT;
f = formats; for (f = formats; f && !fs; f = f->next) {
while(f) { if (!exts_compare(f->exts, type))
if (exts_compare(f->exts, type)) { continue;
fn = build_filename(filename, type);
fd = open(fn, flags | myflags, mode);
if (option_cache_record_files && fd >= 0) {
close(fd);
/*
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.
*/
orig_fn = ast_strdupa(fn);
for (size=0;size<strlen(fn);size++) {
if (fn[size] == '/')
fn[size] = '_';
}
size += (strlen(record_cache_dir) + 10); fn = build_filename(filename, type);
buf = alloca(size); fd = open(fn, flags | myflags, mode);
memset(buf, 0, size);
snprintf(buf, size, "%s/%s", record_cache_dir, fn); if (option_cache_record_files && fd >= 0) {
free(fn); char *c;
fn=buf;
fd = open(fn, flags | myflags, mode); close(fd);
} /*
if (fd >= 0) { We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
errno = 0; What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
if ((fs = f->rewrite(fd, comment))) { */
fs->trans = NULL; orig_fn = ast_strdupa(fn);
fs->fmt = f; for (c = fn; *c; c++)
fs->flags = flags; if (*c == '/')
fs->mode = mode; *c = '_';
if (option_cache_record_files) {
fs->realfilename = build_filename(filename, type); size = strlen(fn) + strlen(record_cache_dir) + 2;
fs->filename = strdup(fn); buf = alloca(size);
} else { memset(buf, 0, size);
fs->realfilename = NULL; snprintf(buf, size, "%s/%s", record_cache_dir, fn);
fs->filename = strdup(filename); free(fn);
} fn = buf;
fs->vfs = NULL; fd = open(fn, flags | myflags, mode);
}
if (fd >= 0) {
errno = 0;
if ((fs = f->rewrite(fd, comment))) {
fs->trans = NULL;
fs->fmt = f;
fs->flags = flags;
fs->mode = mode;
if (orig_fn) {
fs->realfilename = strdup(orig_fn);
fs->filename = strdup(fn);
} else { } else {
ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn); fs->realfilename = NULL;
close(fd); fs->filename = strdup(filename);
unlink(fn);
if (orig_fn)
unlink(orig_fn);
} }
} else if (errno != EEXIST) { fs->vfs = NULL;
ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno)); } else {
if (orig_fn) ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
close(fd);
if (orig_fn) {
unlink(fn);
unlink(orig_fn); unlink(orig_fn);
}
} }
if (!buf) /* if buf != NULL then fn is already free and pointing to it */ } else if (errno != EEXIST) {
free(fn); ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
if (orig_fn)
break; unlink(orig_fn);
} }
f = f->next; /* if buf != NULL then fn is already free and pointing to it */
if (!buf)
free(fn);
} }
ast_mutex_unlock(&formatlock); ast_mutex_unlock(&formatlock);
if (!f) if (!fs)
ast_log(LOG_WARNING, "No such format '%s'\n", type); ast_log(LOG_WARNING, "No such format '%s'\n", type);
return fs; return fs;
} }

Loading…
Cancel
Save