Merge "app_voicemail: Fix file copy error handling." into 13

certified/13.21
Jenkins2 8 years ago committed by Gerrit Code Review
commit 29b10b42bb

@ -4630,49 +4630,56 @@ static int copy(char *infile, char *outfile)
{ {
int ifd; int ifd;
int ofd; int ofd;
int res; int res = -1;
int len; int len;
char buf[4096]; char buf[4096];
#ifdef HARDLINK_WHEN_POSSIBLE #ifdef HARDLINK_WHEN_POSSIBLE
/* Hard link if possible; saves disk space & is faster */ /* Hard link if possible; saves disk space & is faster */
if (link(infile, outfile)) { if (!link(infile, outfile)) {
return 0;
}
#endif #endif
if ((ifd = open(infile, O_RDONLY)) < 0) { if ((ifd = open(infile, O_RDONLY)) < 0) {
ast_log(AST_LOG_WARNING, "Unable to open %s in read-only mode: %s\n", infile, strerror(errno)); ast_log(AST_LOG_WARNING, "Unable to open %s in read-only mode: %s\n", infile, strerror(errno));
return -1; return -1;
} }
if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, VOICEMAIL_FILE_MODE)) < 0) { if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, VOICEMAIL_FILE_MODE)) < 0) {
ast_log(AST_LOG_WARNING, "Unable to open %s in write-only mode: %s\n", outfile, strerror(errno)); ast_log(AST_LOG_WARNING, "Unable to open %s in write-only mode: %s\n", outfile, strerror(errno));
close(ifd); close(ifd);
return -1; return -1;
} }
do {
for (;;) {
int wrlen;
len = read(ifd, buf, sizeof(buf)); len = read(ifd, buf, sizeof(buf));
if (!len) {
res = 0;
break;
}
if (len < 0) { if (len < 0) {
ast_log(AST_LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno)); ast_log(AST_LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
close(ifd); break;
close(ofd); }
unlink(outfile);
} else if (len) { wrlen = write(ofd, buf, len);
res = write(ofd, buf, len); if (errno == ENOMEM || errno == ENOSPC || wrlen != len) {
if (errno == ENOMEM || errno == ENOSPC || res != len) { ast_log(AST_LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, wrlen, len, strerror(errno));
ast_log(AST_LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno)); break;
close(ifd);
close(ofd);
unlink(outfile);
} }
} }
} while (len);
close(ifd); close(ifd);
close(ofd); close(ofd);
return 0; if (res) {
#ifdef HARDLINK_WHEN_POSSIBLE unlink(outfile);
} else {
/* Hard link succeeded */
return 0;
} }
#endif
return res;
} }
/*! /*!

Loading…
Cancel
Save