Newer
Older
return -1;
}
Matthew Jordan
committed
if ((flength = ftello(ff)) < 0) {
ast_log(AST_LOG_ERROR, "Cannot determine end position of file '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
return -1;
}
/* For negative offset and/or negative length */
if (offset < 0 || length < 0) {
int64_t count = 0;
for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
char *pos;
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) {
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; pos--) {
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
LINE_COUNTER(pos, newline_format, count);
if (length < 0 && count * -1 == length) {
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) {
offset_offset = i + (pos - fbuf) + 2;
} else {
offset_offset = i + (pos - fbuf) + 1;
}
break;
}
}
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;
}
}
/* Positve line offset */
if (offset > 0) {
int64_t count = 0;
fseek(ff, 0, SEEK_SET);
for (i = 0; i < flength; i += sizeof(fbuf)) {
char *pos;
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) + 1;
break;
}
}
if (offset_offset >= 0) {
break;
}
}
}
if (offset_offset < 0) {
ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
fclose(ff);
return -1;
}
if (length == 0) {
length_offset = offset_offset;
} else if (length == LLONG_MAX) {
length_offset = flength;
}
/* Positive line length */
if (length_offset < 0) {
fseeko(ff, offset_offset, SEEK_SET);
for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
char *pos;
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);
if (current_length == length) {
length_offset = i + (pos - fbuf) + 1;
break;
}
}
if (length_offset >= 0) {
break;
}
}
if (length_offset < 0) {
/* Exceeds length of file */
ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
length_offset = flength;
}
}
/* Have offset_offset and length_offset now */
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) {
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 (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(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);
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, vlen - vlength, ff) < vlen - vlength) {
ast_log(LOG_ERROR, "Short write?!!\n");
fclose(ff);
return -1;
}
while ((cur = ftello(ff)) < flength) {
Matthew Jordan
committed
if (cur < 0) {
ast_log(AST_LOG_ERROR, "Unable to determine last write position for '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
return -1;
}
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
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;
}
/* 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;
}
}
fclose(ff);
if (truncate(args.filename, flength - (length_length - vlen))) {
ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
}
} 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(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
args.offset, offset_offset, args.length, length_offset, vlength, flength);
fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
while (offset_offset + sizeof(fbuf) < ftello(ff)) {
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
ast_log(LOG_ERROR, "Short read?!!\n");
fclose(ff);
return -1;
}
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;
}
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)) {
ast_log(LOG_ERROR, "Short read?!!\n");
fclose(ff);
return -1;
}
fseek(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))) {
ast_log(LOG_ERROR, "Short write?!!\n");
fclose(ff);
return -1;
Tilghman Lesher
committed
} else {
off_t curpos = ftello(ff);
foplen = lastwritten - curpos;
if (fwrite(fbuf, 1, foplen, ff) < foplen) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
}
fclose(ff);
}
}
}
return 0;
static struct ast_custom_function env_function = {
.read = env_read,
static struct ast_custom_function stat_function = {
.read = stat_read,
.read_max = 12,
static struct ast_custom_function file_function = {
.name = "FILE",
.read2 = file_read,
.write = file_write,
};
static struct ast_custom_function file_count_line_function = {
.name = "FILE_COUNT_LINE",
.read2 = file_count_line,
.read_max = 12,
};
static struct ast_custom_function file_format_function = {
.name = "FILE_FORMAT",
.read2 = file_format,
.read_max = 2,
static int unload_module(void)
{
int res = 0;
res |= ast_custom_function_unregister(&env_function);
res |= ast_custom_function_unregister(&stat_function);
res |= ast_custom_function_unregister(&file_function);
res |= ast_custom_function_unregister(&file_count_line_function);
res |= ast_custom_function_unregister(&file_format_function);
return res;
}
static int load_module(void)
{
int res = 0;
res |= ast_custom_function_register(&env_function);
res |= ast_custom_function_register_escalating(&stat_function, AST_CFE_READ);
res |= ast_custom_function_register_escalating(&file_function, AST_CFE_BOTH);
res |= ast_custom_function_register_escalating(&file_count_line_function, AST_CFE_READ);
res |= ast_custom_function_register_escalating(&file_format_function, AST_CFE_READ);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");