Newer
Older
fseeko(ff, cur + vlength - length, SEEK_SET);
if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
ast_log(LOG_ERROR, "Short write?!!\n");
}
/* Seek to where we stopped reading */
if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) {
/* Only reason for seek to fail is EOF */
break;
}
}
fclose(ff);
if (truncate(args.filename, flength - (length - vlength))) {
ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
Tilghman Lesher
committed
}
} else {
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
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
/* Most complex -- need to open a gap */
char fbuf[4096];
off_t lastwritten = flength + vlength - length;
/* 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;
}
/* 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;
}
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 */
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);
/* Write out the value, then write just up until where we last moved some data */
if (fwrite(value, 1, vlength, ff) < vlength) {
ast_log(LOG_ERROR, "Short write?!!\n");
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");
}
Tilghman Lesher
committed
}
fclose(ff);
} else {
enum file_format newline_format = FF_UNKNOWN;
Tilghman Lesher
committed
/* Line mode */
if (args.argc == 5) {
if (tolower(args.format[0]) == 'u') {
newline_format = FF_UNIX;
} else if (tolower(args.format[0]) == 'm') {
newline_format = FF_MAC;
} else if (tolower(args.format[0]) == 'd') {
newline_format = FF_DOS;
}
}
if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
return -1;
}
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
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"))) {
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 (!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");
}
Matthew Jordan
committed
if ((truncsize = ftello(ff)) < 0) {
ast_log(AST_LOG_ERROR, "Unable to determine truncate position of '%s': %s\n", args.filename, strerror(errno));
}
fclose(ff);
Matthew Jordan
committed
if (truncsize >= 0 && truncate(args.filename, truncsize)) {
ast_log(LOG_ERROR, "Unable to truncate file '%s': %s\n", args.filename, strerror(errno));
return -1;
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
}
} else {
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) {
/* Nonsense! */
ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
return -1;
}
if (!(ff = fopen(args.filename, "r+"))) {
ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
return -1;
}
if (fseek(ff, 0, SEEK_END)) {
ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
fclose(ff);
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--) {
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
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;
}
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
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 struct ast_custom_function file_dirname_function = {
.name = "DIRNAME",
.read = file_dirname,
.read_max = 12,
};
static struct ast_custom_function file_basename_function = {
.name = "BASENAME",
.read = file_basename,
.read_max = 12,
};
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);
res |= ast_custom_function_unregister(&file_dirname_function);
res |= ast_custom_function_unregister(&file_basename_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);
res |= ast_custom_function_register(&file_dirname_function);
res |= ast_custom_function_register(&file_basename_function);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");