diff --git a/main/file.c b/main/file.c
index d29dc2e9d6e5682f09a44a12523df57678f09815..c2ab096cd004bd98483c10e2f7ff5710dfad831d 100644
--- a/main/file.c
+++ b/main/file.c
@@ -1113,6 +1113,11 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
 		if (fd > -1) {
 			errno = 0;
 			fs = get_filestream(f, bfile);
+			if (fs) {
+				if ((fs->write_buffer = ast_malloc(32768))) {
+					setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
+				}
+			}
 			if (!fs || rewrite_wrapper(fs, comment)) {
 				ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
 				close(fd);
@@ -1139,11 +1144,6 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
 			}
 			fs->vfs = NULL;
 			/* If truncated, we'll be at the beginning; if not truncated, then append */
-
-			if ((fs->write_buffer = ast_malloc(32768))){
-				setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
-			}
-
 			f->seek(fs, 0, SEEK_END);
 		} else if (errno != EEXIST) {
 			ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));