[asterisk-commits] tilghman: branch tilghman/issue16461 r272049 - /team/tilghman/issue16461/funcs/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jun 22 17:51:28 CDT 2010


Author: tilghman
Date: Tue Jun 22 17:51:25 2010
New Revision: 272049

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=272049
Log:
All write tests pass!

Modified:
    team/tilghman/issue16461/funcs/func_env.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=272049&r1=272048&r2=272049
==============================================================================
--- team/tilghman/issue16461/funcs/func_env.c (original)
+++ team/tilghman/issue16461/funcs/func_env.c Tue Jun 22 17:51:25 2010
@@ -730,7 +730,18 @@
 	if (args.argc < 4 || !strchr(args.options, 'l')) {
 		/* Character-based mode */
 
-		if (args.argc == 1) {
+		if (args.argc > 3 && strchr(args.options, 'a')) {
+			/* Append mode */
+			if (!(ff = fopen(args.filename, "a"))) {
+    			ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
+    			return 0;
+			}
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
+			return 0;
+		} else if (offset == 0 && length == LLONG_MAX) {
 			if (!(ff = fopen(args.filename, "w"))) {
 		   		ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
 		   		return 0;
@@ -740,17 +751,6 @@
 			}
     		fclose(ff);
 		   	return 0;
-		} else if (args.argc > 3 && strchr(args.options, 'a')) {
-			/* Append mode */
-			if (!(ff = fopen(args.filename, "a"))) {
-    			ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
-    			return 0;
-			}
-			if (fwrite(value, 1, vlength, ff) < vlength) {
-				ast_log(LOG_ERROR, "Short write?!!\n");
-			}
-			fclose(ff);
-			return 0;
 		}
 
 		if (!(ff = fopen(args.filename, "r+"))) {
@@ -871,7 +871,19 @@
 			return -1;
 		}
 
-		if (offset == 0 && length == LLONG_MAX) {
+		if (strchr(args.options, 'a')) {
+			/* Append to file */
+			if (!(ff = fopen(args.filename, "a"))) {
+				ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
+				return -1;
+			}
+			if (fwrite(value, 1, vlength, ff) < vlength) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
+				ast_log(LOG_ERROR, "Short write?!!\n");
+			}
+			fclose(ff);
+		} else if (offset == 0 && length == LLONG_MAX) {
 			/* Overwrite file */
 			off_t truncsize;
 			if (!(ff = fopen(args.filename, "w"))) {
@@ -888,18 +900,6 @@
 			if (truncate(args.filename, truncsize)) {
 				ast_log(LOG_ERROR, "Unable to truncate file: %s\n", strerror(errno));
 			}
-		} else if (strchr(args.options, 'a')) {
-			/* Append to file */
-			if (!(ff = fopen(args.filename, "a"))) {
-				ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
-				return -1;
-			}
-			if (fwrite(value, 1, vlength, ff) < vlength) {
-				ast_log(LOG_ERROR, "Short write?!!\n");
-			} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
-				ast_log(LOG_ERROR, "Short write?!!\n");
-			}
-			fclose(ff);
 		} else {
 			int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
 			char dos_state = 0, fbuf[4096];
@@ -930,7 +930,7 @@
 					if (fseeko(ff, i, SEEK_SET)) {
 						ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
 					}
-					if (i + sizeof(fbuf) <= flength) {
+					if (i + sizeof(fbuf) >= flength) {
 						memset(fbuf, 0, sizeof(fbuf));
 					}
 					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
@@ -942,7 +942,7 @@
 						LINE_COUNTER(pos, newline_format, count);
 
 						if (length < 0 && count * -1 == length) {
-							length_offset = i + (pos - fbuf) + 1;
+							length_offset = i + (pos - fbuf);
 						} else if (offset < 0 && count * -1 == (offset - 1)) {
 							/* Found our initial offset.  We're done with reverse motion! */
 							if (newline_format == FF_DOS) {
@@ -1005,7 +1005,7 @@
 
 			/* Positive line length */
 			if (length_offset < 0) {
-				fseeko(ff, SEEK_SET, offset_offset);
+				fseeko(ff, offset_offset, SEEK_SET);
 				for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
 					char *pos;
 					if (i + sizeof(fbuf) >= flength) {
@@ -1030,12 +1030,10 @@
 				}
 				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);
 					length_offset = flength;
 				}
 			}
-
-			ast_debug(1, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
-				args.offset, offset_offset, args.length, length_offset, vlength, flength);
 
 			/* Have offset_offset and length_offset now */
 			if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
@@ -1050,40 +1048,50 @@
 			} else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
 				/* More complex case - need to shorten file */
 				off_t cur;
+				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",
+					args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
+
 				fseeko(ff, offset_offset, SEEK_SET);
 				if (fwrite(value, 1, vlength, ff) < vlength) {
 					ast_log(LOG_ERROR, "Short write?!!\n");
 					fclose(ff);
 					return -1;
-				} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
+				} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) {
 					ast_log(LOG_ERROR, "Short write?!!\n");
 					fclose(ff);
 					return -1;
 				}
 				while ((cur = ftello(ff)) < flength) {
-					fseek(ff, length_offset - offset_offset, SEEK_CUR);
+					fseeko(ff, length_length - vlen, SEEK_CUR);
 					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
 						ast_log(LOG_ERROR, "Short read?!!\n");
 						fclose(ff);
 						return -1;
 					}
-					fseek(ff, cur + offset_offset - length_offset, SEEK_SET);
+					/* Seek to where we last stopped writing */
+					fseeko(ff, cur, SEEK_SET);
 					if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
 						ast_log(LOG_ERROR, "Short write?!!\n");
 						fclose(ff);
 						return -1;
 					}
 				}
-				fflush(ff);
-				if (ftruncate(fileno(ff), flength - (length_offset - offset_offset - vlength))) {
+				fclose(ff);
+				if (truncate(args.filename, flength - (length_length - vlen))) {
 					ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
 				}
-				fclose(ff);
 			} else {
 				/* Most complex case - need to lengthen file */
 				size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
 				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",
+					args.offset, offset_offset, args.length, length_offset, vlength, flength);
+
 				fseeko(ff, vlen - origlen, SEEK_END);
 				while (offset_offset + sizeof(fbuf) < ftello(ff)) {
 					ast_debug(1, "Tests should not get here\n");




More information about the asterisk-commits mailing list