[asterisk-commits] tilghman: branch tilghman/issue16461 r271656 - in /team/tilghman/issue16461: ...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Jun 21 17:39:59 CDT 2010
Author: tilghman
Date: Mon Jun 21 17:39:55 2010
New Revision: 271656
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=271656
Log:
Some bug fixes write line mode...
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=271656&r1=271655&r2=271656
==============================================================================
--- team/tilghman/issue16461/funcs/func_env.c (original)
+++ team/tilghman/issue16461/funcs/func_env.c Mon Jun 21 17:39:55 2010
@@ -616,6 +616,15 @@
fseek(ff, 0, SEEK_SET);
for (i = 0; i < flength; i += sizeof(fbuf)) {
char *pos;
+ if (i + sizeof(fbuf) <= flength) {
+ /* Don't let previous values influence current counts, due to short reads */
+ memset(fbuf, 0, sizeof(fbuf));
+ }
+ if (fread(fbuf, 1, sizeof(fbuf), ff) && !feof(ff)) {
+ ast_log(LOG_ERROR, "Short read?!!\n");
+ fclose(ff);
+ return -1;
+ }
for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
LINE_COUNTER(pos, format, count);
@@ -769,7 +778,7 @@
fseeko(ff, offset, SEEK_SET);
- 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, args.length, length, vlength, flength);
if (length == vlength) {
@@ -863,29 +872,36 @@
}
if (offset == 0 && length == LLONG_MAX) {
+ /* Overwrite file */
+ off_t truncsize;
if (!(ff = fopen(args.filename, "w"))) {
ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
return -1;
}
if (fwrite(value, 1, vlength, ff) < vlength) {
ast_log(LOG_ERROR, "Short write?!!\n");
- } else if (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, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
+ truncsize = ftello(ff);
fclose(ff);
+ 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 (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, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
fclose(ff);
} else {
- int64_t offset_offset = -1, length_offset = -1, flength, i, current_length = 0;
+ int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
char dos_state = 0, fbuf[4096];
if (offset < 0 && length < offset) {
@@ -910,20 +926,24 @@
if (offset < 0 || length < 0) {
int64_t count = 0;
for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
- size_t end;
char *pos;
if (fseeko(ff, i, SEEK_SET)) {
ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
}
- if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
- memset(fbuf + end, 0, sizeof(fbuf) - end);
+ if (i + sizeof(fbuf) <= flength) {
+ memset(fbuf, 0, sizeof(fbuf));
+ }
+ if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
+ ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno));
+ fclose(ff);
+ return -1;
}
for (pos = fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
LINE_COUNTER(pos, newline_format, count);
if (length < 0 && count * -1 == length) {
length_offset = i + (pos - fbuf) + 1;
- } else if (count * -1 == offset) {
+ } else if (offset < 0 && count * -1 == (offset - 1)) {
/* Found our initial offset. We're done with reverse motion! */
if (newline_format == FF_DOS) {
offset_offset = i + (pos - fbuf) + 2;
@@ -933,9 +953,13 @@
break;
}
}
- if (offset_offset >= 0) {
+ if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
break;
}
+ }
+ /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
+ if (offset < 0 && offset_offset < 0 && offset == count * -1) {
+ offset_offset = 0;
}
}
@@ -944,16 +968,20 @@
int64_t count = 0;
fseek(ff, 0, SEEK_SET);
for (i = 0; i < flength; i += sizeof(fbuf)) {
- size_t end;
char *pos;
- if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
- memset(fbuf + end, 0, sizeof(fbuf) - end);
+ if (i + sizeof(fbuf) >= flength) {
+ memset(fbuf, 0, sizeof(fbuf));
+ }
+ if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
+ ast_log(LOG_ERROR, "Short read?!!\n");
+ fclose(ff);
+ return -1;
}
for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
LINE_COUNTER(pos, newline_format, count);
if (count == offset) {
- offset_offset = i + (pos - fbuf);
+ offset_offset = i + (pos - fbuf) + 1;
break;
}
}
@@ -979,10 +1007,14 @@
if (length_offset < 0) {
fseeko(ff, SEEK_SET, offset_offset);
for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
- size_t end;
char *pos;
- if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
- memset(fbuf + end, 0, sizeof(fbuf) - end);
+ if (i + sizeof(fbuf) >= flength) {
+ memset(fbuf, 0, sizeof(fbuf));
+ }
+ if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
+ ast_log(LOG_ERROR, "Short read?!!\n");
+ fclose(ff);
+ return -1;
}
for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
LINE_COUNTER(pos, newline_format, current_length);
@@ -1002,8 +1034,11 @@
}
}
+ 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 + strlen(format2term(newline_format))) {
+ if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
/* Simple case - replacement of text inline */
fseeko(ff, offset_offset, SEEK_SET);
if (fwrite(value, 1, vlength, ff) < vlength) {
@@ -1027,7 +1062,7 @@
}
while ((cur = ftello(ff)) < flength) {
fseek(ff, length_offset - offset_offset, SEEK_CUR);
- if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+ if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
ast_log(LOG_ERROR, "Short read?!!\n");
fclose(ff);
return -1;
@@ -1051,6 +1086,7 @@
off_t lastwritten = flength + vlen - origlen;
fseeko(ff, vlen - origlen, SEEK_END);
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");
@@ -1066,7 +1102,7 @@
lastwritten = ftello(ff) - sizeof(fbuf);
}
fseek(ff, length_offset, SEEK_SET);
- if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
+ if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
ast_log(LOG_ERROR, "Short read?!!\n");
fclose(ff);
return -1;
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=271656&r1=271655&r2=271656
==============================================================================
--- team/tilghman/issue16461/tests/test_func_file.c (original)
+++ team/tilghman/issue16461/tests/test_func_file.c Mon Jun 21 17:39:55 2010
@@ -339,6 +339,8 @@
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;
+ } else {
+ ast_test_status_update(test, "Expression 'FILE(...,%s)=%s'... OK!\n", write_tests[i].args, write_tests[i].value);
}
}
More information about the asterisk-commits
mailing list