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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Jun 19 00:11:11 CDT 2010


Author: tilghman
Date: Sat Jun 19 00:11:08 2010
New Revision: 271517

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=271517
Log:
Fixed the last of the read tests

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=271517&r1=271516&r2=271517
==============================================================================
--- team/tilghman/issue16461/funcs/func_env.c (original)
+++ team/tilghman/issue16461/funcs/func_env.c Sat Jun 19 00:11:08 2010
@@ -76,7 +76,6 @@
 		<syntax>
 			<parameter name="filename" required="true">
 				<para>File to write or modify.</para>
-			</argument>
 			</parameter>
 			<parameter name="offset">
 				<para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
@@ -567,6 +566,10 @@
 	}
 	flength = ftello(ff);
 
+	if (length == LLONG_MAX) {
+		length_offset = flength;
+	}
+
 	/* For negative offset and/or negative length */
 	if (offset < 0 || length < 0) {
 		int64_t count = 0;
@@ -614,7 +617,7 @@
 				LINE_COUNTER(pos, format, count);
 
 				if (count == offset) {
-					offset_offset = i + (pos - fbuf);
+					offset_offset = i + (pos - fbuf) + 1;
 					break;
 				}
 			}
@@ -631,18 +634,21 @@
 	}
 
 	ast_str_reset(*buf);
-	fseeko(ff, SEEK_SET, offset_offset);
+	if (fseeko(ff, offset_offset, SEEK_SET)) {
+		ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno));
+	}
 
 	/* If we have both offset_offset and length_offset, then grabbing the
 	 * buffer is simply a matter of just retrieving the file and adding it
 	 * to buf.  Otherwise, we need to run byte-by-byte forward until the
 	 * length is complete. */
 	if (length_offset >= 0) {
-		ast_debug(1, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
+		ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
 		for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
-			if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf))) {
-				/* This is expected on the last time through the loop */
-			}
+			if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) {
+				ast_log(LOG_ERROR, "Short read?!!\n");
+			}
+			ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf), fbuf);
 			ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
 		}
 	} else if (length == 0) {
@@ -651,11 +657,11 @@
 		/* Positive line offset */
 		int64_t current_length = 0;
 		char dos_state = 0;
-		ast_debug(1, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
+		ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
 		for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
 			char *pos;
-			if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
-				memset(&fbuf[readlen], 0, sizeof(fbuf) - readlen);
+			if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf) && !feof(ff)) {
+				ast_log(LOG_ERROR, "Short read?!!\n");
 			}
 			for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
 				LINE_COUNTER(pos, format, current_length);
@@ -665,8 +671,8 @@
 					break;
 				}
 			}
-			ast_debug(1, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
-			ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? length_offset - i : sizeof(fbuf));
+			ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
+			ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? length_offset - i : flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i;
 
 			if (length_offset >= 0) {
 				break;
@@ -742,8 +748,15 @@
 		fseeko(ff, 0, SEEK_END);
 		flength = ftello(ff);
 
+		if (offset < 0) {
+			if (fseeko(ff, offset, SEEK_END)) {
+				ast_log(LOG_ERROR, "Cannot seek to offset: %s\n", strerror(errno));
+			}
+			offset = ftello(ff);
+		}
+
 		if (length < 0) {
-			length += flength;
+			length = flength - offset + length;
 			if (length < 0) {
 				ast_log(LOG_ERROR, "Length '%s' exceeds the file length.  No data will be written.\n", args.length);
 				fclose(ff);
@@ -751,7 +764,10 @@
 			}
 		}
 
-		fseeko(ff, offset, offset < 0 ? SEEK_END : SEEK_SET);
+		fseeko(ff, offset, SEEK_SET);
+
+		ast_debug(1, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
+			args.offset, offset, args.length, length, vlength, flength);
 
 		if (length == vlength) {
 			/* Simplest case, a straight replace */
@@ -764,23 +780,23 @@
 			if (fwrite(value, 1, vlength, ff) < vlength) {
 				ast_log(LOG_ERROR, "Short write?!!\n");
 			}
-			fflush(ff);
-			if (ftruncate(fileno(ff), ftello(ff))) {
+			fclose(ff);
+			if (truncate(args.filename, offset + vlength)) {
 				ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
 			}
-			fclose(ff);
 		} else if (length > vlength) {
 			/* More complex -- need to close a gap */
 			char fbuf[4096];
+			off_t cur;
 			if (fwrite(value, 1, vlength, ff) < vlength) {
 				ast_log(LOG_ERROR, "Short write?!!\n");
 			}
-			while (ftello(ff) < flength) {
-				fseeko(ff, length - vlength, SEEK_CUR);
+			fseeko(ff, length - vlength, SEEK_CUR);
+			while ((cur = ftello(ff)) < flength) {
 				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
 					ast_log(LOG_ERROR, "Short read?!!\n");
 				}
-				fseeko(ff, vlength - length, SEEK_CUR);
+				fseeko(ff, cur + vlength - length, SEEK_CUR);
 				if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
 					ast_log(LOG_ERROR, "Short write?!!\n");
 				}

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=271517&r1=271516&r2=271517
==============================================================================
--- team/tilghman/issue16461/tests/test_func_file.c (original)
+++ team/tilghman/issue16461/tests/test_func_file.c Sat Jun 19 00:11:08 2010
@@ -255,13 +255,8 @@
 		fclose(fh);
 
 		if (strcmp(fbuf, write_tests[i].contents2)) {
-			if (strchr(write_tests[i].contents2, '\n')) {
-				ast_test_status_update(test, "Expression 'FILE(...,%s)=%s' did not produce a file with the expected result\n",
-					write_tests[i].args, write_tests[i].value);
-			} else {
-				ast_test_status_update(test, "Expression 'FILE(...,%s)=%s' did not produce ('%s') the expected result ('%s')\n",
-					write_tests[i].args, write_tests[i].value, file2display(&disp[0], 0, fbuf), file2display(&disp[1], 0, write_tests[i].contents2));
-			}
+			ast_test_status_update(test, "Expression 'FILE(...,%s)=%s' did not produce ('%s') the expected result ('%s')\n",
+				write_tests[i].args, write_tests[i].value, file2display(&disp[0], 0, fbuf), file2display(&disp[1], 0, write_tests[i].contents2));
 			res = AST_TEST_FAIL;
 		}
 	}




More information about the asterisk-commits mailing list