mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-05 20:20:07 +00:00
don't unlink files just because they can't be opened (bug #4641)
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
This commit is contained in:
188
file.c
188
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);
|
|
||||||
|
fn = build_filename(filename, type);
|
||||||
|
fd = open(fn, flags | myflags, mode);
|
||||||
|
|
||||||
|
if (option_cache_record_files && fd >= 0) {
|
||||||
|
char *c;
|
||||||
|
|
||||||
|
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 (c = fn; *c; c++)
|
||||||
|
if (*c == '/')
|
||||||
|
*c = '_';
|
||||||
|
|
||||||
|
size = strlen(fn) + strlen(record_cache_dir) + 2;
|
||||||
|
buf = alloca(size);
|
||||||
|
memset(buf, 0, size);
|
||||||
|
snprintf(buf, size, "%s/%s", record_cache_dir, fn);
|
||||||
|
free(fn);
|
||||||
|
fn = buf;
|
||||||
fd = open(fn, flags | myflags, mode);
|
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);
|
|
||||||
buf = alloca(size);
|
|
||||||
memset(buf, 0, size);
|
|
||||||
snprintf(buf, size, "%s/%s", record_cache_dir, fn);
|
|
||||||
free(fn);
|
|
||||||
fn=buf;
|
|
||||||
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 (option_cache_record_files) {
|
|
||||||
fs->realfilename = build_filename(filename, type);
|
|
||||||
fs->filename = strdup(fn);
|
|
||||||
} else {
|
|
||||||
fs->realfilename = NULL;
|
|
||||||
fs->filename = strdup(filename);
|
|
||||||
}
|
|
||||||
fs->vfs = NULL;
|
|
||||||
} else {
|
|
||||||
ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
|
|
||||||
close(fd);
|
|
||||||
unlink(fn);
|
|
||||||
if (orig_fn)
|
|
||||||
unlink(orig_fn);
|
|
||||||
}
|
|
||||||
} else if (errno != EEXIST) {
|
|
||||||
ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
|
|
||||||
if (orig_fn)
|
|
||||||
unlink(orig_fn);
|
|
||||||
}
|
|
||||||
if (!buf) /* if buf != NULL then fn is already free and pointing to it */
|
|
||||||
free(fn);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
f = f->next;
|
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 {
|
||||||
|
fs->realfilename = NULL;
|
||||||
|
fs->filename = strdup(filename);
|
||||||
|
}
|
||||||
|
fs->vfs = NULL;
|
||||||
|
} else {
|
||||||
|
ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
|
||||||
|
close(fd);
|
||||||
|
if (orig_fn) {
|
||||||
|
unlink(fn);
|
||||||
|
unlink(orig_fn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (errno != EEXIST) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
|
||||||
|
if (orig_fn)
|
||||||
|
unlink(orig_fn);
|
||||||
|
}
|
||||||
|
/* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user