Index: asterisk.c
===================================================================
RCS file: /usr/cvsroot/asterisk/asterisk.c,v
retrieving revision 1.119
diff -u -r1.119 asterisk.c
--- asterisk.c	31 Aug 2004 13:32:11 -0000	1.119
+++ asterisk.c	2 Sep 2004 04:21:29 -0000
@@ -76,9 +76,11 @@
 int option_initcrypto=0;
 int option_nocolor;
 int option_dumpcore = 0;
+int option_cache_record_files = 0;
 int option_overrideconfig = 0;
 int option_reconnect = 0;
 int fully_booted = 0;
+char record_cache_dir[AST_CACHE_DIR_LEN] = "/tmp";
 
 static int ast_socket = -1;		/* UNIX Socket for allowing remote control */
 static int ast_consock = -1;		/* UNIX Socket for controlling another asterisk */
@@ -1473,6 +1475,7 @@
 	printf("   -q              Quiet mode (supress output)\n");
 	printf("   -r              Connect to Asterisk on this machine\n");
 	printf("   -R              Connect to Asterisk, and attempt to reconnect if disconnected\n");
+	printf("   -t              Record soundfiles in /tmp and move them where they belong after they are done.\n");
 	printf("   -v              Increase verbosity (multiple v's = more verbose)\n");
 	printf("   -x <cmd>        Execute command <cmd> (only valid with -r)\n");
 	printf("\n");
@@ -1531,6 +1534,33 @@
 		}
 		v = v->next;
 	}
+	v = ast_variable_browse(cfg, "options");
+	while(v) {
+		if(!strcasecmp(v->name, "verbose")) {
+			option_verbose= atoi(v->value);
+		} else if (!strcasecmp(v->name, "debug")) {
+			option_debug= ast_true(v->value);
+		} else if (!strcasecmp(v->name, "nofork")) {
+            option_nofork = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "quiet")) {
+            option_quiet = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "console")) {
+            option_console = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "highpriority")) {
+            option_highpriority = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "initcrypto")) {
+            option_initcrypto = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "nocolor")) {
+            option_nocolor = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "dumpcore")) {
+            option_dumpcore = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "cache_record_files")) {
+            option_cache_record_files = ast_true(v->value);
+		}  else if (!strcasecmp(v->name, "record_cache_dir")) {
+			strncpy(record_cache_dir,v->value,AST_CACHE_DIR_LEN);
+		}
+		v = v->next;
+	}
 	ast_destroy(cfg);
 }
 
@@ -1580,7 +1610,7 @@
 	}
 	*/
 	/* Check for options */
-	while((c=getopt(argc, argv, "hfdvVqprRgcinx:U:G:C:")) != -1) {
+	while((c=getopt(argc, argv, "thfdvVqprRgcinx:U:G:C:")) != -1) {
 		switch(c) {
 		case 'd':
 			option_debug++;
@@ -1615,6 +1645,9 @@
 		case 'q':
 			option_quiet++;
 			break;
+		case 't':
+			option_cache_record_files++;
+			break;
 		case 'x':
 			option_exec++;
 			xarg = optarg;
@@ -1822,8 +1855,13 @@
 		printf(term_quit());
 		exit(1);
 	}
-	/* reload logger in case a custom config handler binded to logger.conf*/
+	/* sync cust config and reload some internals in case a custom config handler binded to them */
+	read_ast_cust_config();
 	reload_logger(0);
+    reload_manager();
+    ast_enum_reload();
+    ast_rtp_reload();
+
 
 	/* We might have the option of showing a console, but for now just
 	   do nothing... */
Index: file.c
===================================================================
RCS file: /usr/cvsroot/asterisk/file.c,v
retrieving revision 1.47
diff -u -r1.47 file.c
--- file.c	15 Jul 2004 14:37:09 -0000	1.47
+++ file.c	2 Sep 2004 04:21:30 -0000
@@ -22,6 +22,7 @@
 #include <asterisk/translate.h>
 #include <asterisk/utils.h>
 #include <asterisk/lock.h>
+#include <asterisk/app.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -71,6 +72,7 @@
 	int flags;
 	mode_t mode;
 	char *filename;
+	char *realfilename;
 	/* Video file stream */
 	struct ast_filestream *vfs;
 	/* Transparently translate from another format -- just once */
@@ -649,6 +651,8 @@
 
 int ast_closestream(struct ast_filestream *f)
 {
+	char *cmd = NULL;
+	size_t size = 0;
 	/* Stop a running stream if there is one */
 	if (f->owner) {
 		if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
@@ -671,10 +675,24 @@
 		ast_translator_free_path(f->trans);
 		f->trans = NULL;
 	}
-	if (f->filename)
-		free(f->filename);
-	f->filename = NULL;
+
+	if (f->realfilename && f->filename) {
+			size = strlen(f->filename) + strlen(f->realfilename) + 15;
+			cmd = alloca(size);
+			memset(cmd,0,size);
+			snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
+			ast_safe_system(cmd);
+	}
 	f->fmt->close(f);
+	if (f->filename) {
+		free(f->filename);
+		f->filename = NULL;
+	}
+	if (f->realfilename) {
+		free(f->realfilename);
+		f->realfilename = NULL;
+	}
+
 	return 0;
 }
 
@@ -816,8 +834,11 @@
 	int fd,myflags = 0;
 	struct ast_format *f;
 	struct ast_filestream *fs=NULL;
-	char *fn;
+	char *fn,*orig_fn=NULL;
 	char *ext;
+	char *buf=NULL;
+	size_t size = 0;
+
 	if (ast_mutex_lock(&formatlock)) {
 		ast_log(LOG_WARNING, "Unable to lock format list\n");
 		return NULL;
@@ -833,11 +854,31 @@
 		if (exts_compare(f->exts, type)) {
 			char *stringp=NULL;
 			/* XXX Implement check XXX */
-			ext = strdup(f->exts);
+			ext = ast_strdupa(f->exts);
 			stringp=ext;
 			ext = strsep(&stringp, "|");
 			fn = build_filename(filename, ext);
 			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))) {
@@ -845,17 +886,30 @@
 					fs->fmt = f;
 					fs->flags = flags;
 					fs->mode = mode;
-					fs->filename = strdup(filename);
+					if (option_cache_record_files) {
+						fs->realfilename = build_filename(filename, ext);
+						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)
+			} else if (errno != EEXIST) {
 				ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
-			free(fn);
-			free(ext);
+				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;
Index: include/asterisk/options.h
===================================================================
RCS file: /usr/cvsroot/asterisk/include/asterisk/options.h,v
retrieving revision 1.8
diff -u -r1.8 options.h
--- include/asterisk/options.h	20 Mar 2004 21:13:12 -0000	1.8
+++ include/asterisk/options.h	2 Sep 2004 04:21:30 -0000
@@ -18,6 +18,8 @@
 extern "C" {
 #endif
 
+#define AST_CACHE_DIR_LEN 512
+
 extern int option_verbose;
 extern int option_debug;
 extern int option_nofork;
@@ -26,10 +28,12 @@
 extern int option_initcrypto;
 extern int option_nocolor;
 extern int fully_booted;
+extern int option_cache_record_files;
 extern char defaultlanguage[];
 extern time_t ast_startuptime;
 extern time_t ast_lastreloadtime;
 extern int ast_mainpid;
+extern char record_cache_dir[AST_CACHE_DIR_LEN];
 
 #define VERBOSE_PREFIX_1 " "
 #define VERBOSE_PREFIX_2 "  == "
