[asterisk-commits] tilghman: branch tilghman/issue16461 r272085 - in /team/tilghman/issue16461: ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jun 22 23:22:42 CDT 2010


Author: tilghman
Date: Tue Jun 22 23:22:39 2010
New Revision: 272085

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=272085
Log:
Longer tests, all succeed

Modified:
    team/tilghman/issue16461/funcs/func_env.c
    team/tilghman/issue16461/tests/test_func_file.c

Modified: team/tilghman/issue16461/funcs/func_env.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/issue16461/funcs/func_env.c?view=diff&rev=272085&r1=272084&r2=272085
==============================================================================
--- team/tilghman/issue16461/funcs/func_env.c (original)
+++ team/tilghman/issue16461/funcs/func_env.c Tue Jun 22 23:22:39 2010
@@ -821,27 +821,49 @@
 			/* Most complex -- need to open a gap */
 			char fbuf[4096];
 			off_t lastwritten = flength + vlength - length;
-			fseeko(ff, vlength - length, SEEK_END);
-			while (offset + sizeof(fbuf) < ftello(ff)) {
-				ast_debug(1, "This should not execute (yet) in our tests.\n");
-				fseeko(ff, -1 * (sizeof(fbuf) + (vlength - length)), SEEK_CUR);
+
+			/* Start reading exactly the buffer size back from the end. */
+			fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
+			while (offset < ftello(ff)) {
 				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
 					ast_log(LOG_ERROR, "Short read?!!\n");
 					fclose(ff);
 					return -1;
 				}
-				fseeko(ff, vlength - length, SEEK_CUR);
+				/* Since the read moved our file ptr forward, we reverse, but
+				 * seek an offset equal to the amount we want to extend the
+				 * file by */
+				fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR);
+
+				/* Note the location of this buffer -- we must not overwrite this position. */
+				lastwritten = ftello(ff);
+
 				if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
 					ast_log(LOG_ERROR, "Short write?!!\n");
 					fclose(ff);
 					return -1;
 				}
-				/* Note the location of this last buffer -- we must not overwrite this position. */
-				lastwritten = ftello(ff) - sizeof(fbuf);
-			}
-			fseek(ff, offset + length, SEEK_SET);
+
+				if (lastwritten < offset + sizeof(fbuf)) {
+					break;
+				}
+				/* Our file pointer is now either pointing to the end of the
+				 * file (new position) or a multiple of the fbuf size back from
+				 * that point.  Move back to where we want to start reading
+				 * again.  We never actually try to read beyond the end of the
+				 * file, so we don't have do deal with short reads, as we would
+				 * when we're shortening the file. */
+				fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR);
+			}
+
+			/* Last part of the file that we need to preserve */
+			if (fseeko(ff, offset + length, SEEK_SET)) {
+				ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff));
+			}
+
 			/* Doesn't matter how much we read -- just need to restrict the write */
-			if (fread(fbuf, 1, sizeof(fbuf), ff) && !feof(ff)) {
+			ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff));
+			if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
 				ast_log(LOG_ERROR, "Short read?!!\n");
 			}
 			fseek(ff, offset, SEEK_SET);
@@ -1030,7 +1052,7 @@
 				}
 				if (length_offset < 0) {
 					/* Exceeds length of file */
-					ast_debug(1, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
+					ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
 					length_offset = flength;
 				}
 			}
@@ -1051,7 +1073,7 @@
 				int64_t length_length = length_offset - offset_offset;
 				size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
 
-				ast_debug(1, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
+				ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
 					args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
 
 				fseeko(ff, offset_offset, SEEK_SET);
@@ -1089,25 +1111,26 @@
 				int64_t origlen = length_offset - offset_offset;
 				off_t lastwritten = flength + vlen - origlen;
 
-				ast_debug(1, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
+				ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
 					args.offset, offset_offset, args.length, length_offset, vlength, flength);
 
-				fseeko(ff, vlen - origlen, SEEK_END);
+				fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
 				while (offset_offset + sizeof(fbuf) < ftello(ff)) {
-					ast_debug(1, "Tests should not get here\n");
-					fseeko(ff, -1 * (sizeof(fbuf) + (vlen - origlen)), SEEK_CUR);
 					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
 						ast_log(LOG_ERROR, "Short read?!!\n");
 						fclose(ff);
 						return -1;
 					}
-					fseeko(ff, vlength - origlen, SEEK_CUR);
+					fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR);
 					if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
 						ast_log(LOG_ERROR, "Short write?!!\n");
 						fclose(ff);
 						return -1;
 					}
-					lastwritten = ftello(ff) - sizeof(fbuf);
+					if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) {
+						break;
+					}
+					fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR);
 				}
 				fseek(ff, length_offset, SEEK_SET);
 				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {

Modified: team/tilghman/issue16461/tests/test_func_file.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/issue16461/tests/test_func_file.c?view=diff&rev=272085&r1=272084&r2=272085
==============================================================================
--- team/tilghman/issue16461/tests/test_func_file.c (original)
+++ team/tilghman/issue16461/tests/test_func_file.c Tue Jun 22 23:22:39 2010
@@ -38,6 +38,9 @@
 #include "asterisk/module.h"
 #include "asterisk/test.h"
 #include "asterisk/pbx.h"
+
+#define C1024 \
+		"1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF"
 
 static struct {
 	const char *contents;
@@ -196,6 +199,25 @@
 	{ "123\n456\n789\n", "1,-1,ld", "", "123\n789\n" },
 	{ "123\n456\n789\n", "-2,1,ld", "", "123\n789\n" },
 	{ "123\n456\n789\n", "-2,-1,ld", "", "123\n789\n" },
+	/* Really long tests */
+	{ "1234567890ABCDEF" C1024 C1024 C1024 C1024 C1024,
+		"0,1", "a",
+		"a234567890ABCDEF" C1024 C1024 C1024 C1024 C1024 },
+	{ "1234567890ABCDEF" C1024 C1024 C1024 C1024 C1024,
+		"0,1", "abcd",
+		"abcd234567890ABCDEF" C1024 C1024 C1024 C1024 C1024 },
+	{ "1234567890ABCDEF" C1024 C1024 C1024 C1024 C1024,
+		"0,10", "abcd",
+		"abcdABCDEF" C1024 C1024 C1024 C1024 C1024 },
+	{ "1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n",
+		"0,1,l", "abcd",
+		"abcd\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n" },
+	{ "1234\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n",
+		"0,1,l", "abcd",
+		"abcd\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n" },
+	{ "1234\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n",
+		"0,1,l", "a",
+		"a\n1" C1024 "\n2" C1024 "\n3" C1024 "\n4" C1024 "\n5" C1024 "\n6" C1024 "\n" },
 };
 
 static char *file2display(struct ast_str **buf, ssize_t len, const char *input)
@@ -225,7 +247,7 @@
 	char dir[] = "/tmp/test_func_file.XXXXXX";
 	char file[80], expression[256];
 	struct ast_str *buf, *disp[2] = { NULL, NULL };
-	char fbuf[256];
+	char fbuf[8192];
 	FILE *fh;
 
 	switch (cmd) {




More information about the asterisk-commits mailing list