| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "asterisk.h" |
| 29 |
|
| 30 |
#include <sys/stat.h> |
| 31 |
#include <libgen.h> |
| 32 |
|
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
#include "asterisk/file.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
static int env_read(struct ast_channel *chan, const char *cmd, char *data, |
| 368 |
char *buf, size_t len) |
| 369 |
{ |
| 370 |
char *ret = NULL; |
| 371 |
|
| 372 |
*buf = '\0'; |
| 373 |
|
| 374 |
if (data) |
| 375 |
ret = getenv(data); |
| 376 |
|
| 377 |
if (ret) |
| 378 |
ast_copy_string(buf, ret, len); |
| 379 |
|
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
static int env_write(struct ast_channel *chan, const char *cmd, char *data, |
| 384 |
const char *value) |
| 385 |
{ |
| 386 |
if (!ast_strlen_zero(data) && strncmp(data, "AST_", 4)) { |
| 387 |
if (!ast_strlen_zero(value)) { |
| 388 |
setenv(data, value, 1); |
| 389 |
} else { |
| 390 |
unsetenv(data); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
static int stat_read(struct ast_channel *chan, const char *cmd, char *data, |
| 398 |
char *buf, size_t len) |
| 399 |
{ |
| 400 |
char *action; |
| 401 |
struct stat s; |
| 402 |
|
| 403 |
ast_copy_string(buf, "0", len); |
| 404 |
|
| 405 |
action = strsep(&data, ","); |
| 406 |
if (stat(data, &s)) { |
| 407 |
return 0; |
| 408 |
} else { |
| 409 |
switch (*action) { |
| 410 |
case 'e': |
| 411 |
strcpy(buf, "1"); |
| 412 |
break; |
| 413 |
case 's': |
| 414 |
snprintf(buf, len, "%u", (unsigned int) s.st_size); |
| 415 |
break; |
| 416 |
case 'f': |
| 417 |
snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0); |
| 418 |
break; |
| 419 |
case 'd': |
| 420 |
snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0); |
| 421 |
break; |
| 422 |
case 'M': |
| 423 |
snprintf(buf, len, "%d", (int) s.st_mtime); |
| 424 |
break; |
| 425 |
case 'A': |
| 426 |
snprintf(buf, len, "%d", (int) s.st_mtime); |
| 427 |
break; |
| 428 |
case 'C': |
| 429 |
snprintf(buf, len, "%d", (int) s.st_ctime); |
| 430 |
break; |
| 431 |
case 'm': |
| 432 |
snprintf(buf, len, "%o", (unsigned int) s.st_mode); |
| 433 |
break; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
return 0; |
| 438 |
} |
| 439 |
|
| 440 |
enum file_format { |
| 441 |
FF_UNKNOWN = -1, |
| 442 |
FF_UNIX, |
| 443 |
FF_DOS, |
| 444 |
FF_MAC, |
| 445 |
}; |
| 446 |
|
| 447 |
static int64_t count_lines(const char *filename, enum file_format newline_format) |
| 448 |
{ |
| 449 |
int count = 0; |
| 450 |
char fbuf[4096]; |
| 451 |
FILE *ff; |
| 452 |
|
| 453 |
if (!(ff = fopen(filename, "r"))) { |
| 454 |
ast_log(LOG_ERROR, "Unable to open '%s': %s\n", filename, strerror(errno)); |
| 455 |
return -1; |
| 456 |
} |
| 457 |
|
| 458 |
while (fgets(fbuf, sizeof(fbuf), ff)) { |
| 459 |
char *next = fbuf, *first_cr = NULL, *first_nl = NULL; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
while (next) { |
| 464 |
if (newline_format == FF_DOS || newline_format == FF_MAC || newline_format == FF_UNKNOWN) { |
| 465 |
first_cr = strchr(next, '\r'); |
| 466 |
} |
| 467 |
if (newline_format == FF_UNIX || newline_format == FF_UNKNOWN) { |
| 468 |
first_nl = strchr(next, '\n'); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
if (!first_cr && !first_nl) { |
| 473 |
break; |
| 474 |
} |
| 475 |
|
| 476 |
if (newline_format == FF_UNKNOWN) { |
| 477 |
if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) { |
| 478 |
if (first_nl && first_nl == first_cr + 1) { |
| 479 |
newline_format = FF_DOS; |
| 480 |
} else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) { |
| 481 |
|
| 482 |
fseek(ff, -1, SEEK_CUR); |
| 483 |
break; |
| 484 |
} else { |
| 485 |
newline_format = FF_MAC; |
| 486 |
first_nl = NULL; |
| 487 |
} |
| 488 |
} else { |
| 489 |
newline_format = FF_UNIX; |
| 490 |
first_cr = NULL; |
| 491 |
} |
| 492 |
|
| 493 |
} |
| 494 |
|
| 495 |
if (newline_format == FF_DOS) { |
| 496 |
if (first_nl && first_cr && first_nl == first_cr + 1) { |
| 497 |
next = first_nl + 1; |
| 498 |
count++; |
| 499 |
} else if (first_cr == &fbuf[sizeof(fbuf) - 2]) { |
| 500 |
|
| 501 |
fseek(ff, -1, SEEK_CUR); |
| 502 |
break; |
| 503 |
} |
| 504 |
} else if (newline_format == FF_MAC) { |
| 505 |
if (first_cr) { |
| 506 |
next = first_cr + 1; |
| 507 |
count++; |
| 508 |
} |
| 509 |
} else if (newline_format == FF_UNIX) { |
| 510 |
if (first_nl) { |
| 511 |
next = first_nl + 1; |
| 512 |
count++; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
fclose(ff); |
| 518 |
|
| 519 |
return count; |
| 520 |
} |
| 521 |
|
| 522 |
static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| 523 |
{ |
| 524 |
enum file_format newline_format = FF_UNKNOWN; |
| 525 |
int64_t count; |
| 526 |
AST_DECLARE_APP_ARGS(args, |
| 527 |
AST_APP_ARG(filename); |
| 528 |
AST_APP_ARG(format); |
| 529 |
); |
| 530 |
|
| 531 |
AST_STANDARD_APP_ARGS(args, data); |
| 532 |
if (args.argc > 1) { |
| 533 |
if (tolower(args.format[0]) == 'd') { |
| 534 |
newline_format = FF_DOS; |
| 535 |
} else if (tolower(args.format[0]) == 'm') { |
| 536 |
newline_format = FF_MAC; |
| 537 |
} else if (tolower(args.format[0]) == 'u') { |
| 538 |
newline_format = FF_UNIX; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
count = count_lines(args.filename, newline_format); |
| 543 |
ast_str_set(buf, len, "%" PRId64, count); |
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
#define LINE_COUNTER(cptr, term, counter) \ |
| 548 |
if (*cptr == '\n' && term == FF_UNIX) { \ |
| 549 |
counter++; \ |
| 550 |
} else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \ |
| 551 |
dos_state = 1; \ |
| 552 |
} else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \ |
| 553 |
dos_state = 0; \ |
| 554 |
counter++; \ |
| 555 |
} else if (*cptr == '\r' && term == FF_MAC) { \ |
| 556 |
counter++; \ |
| 557 |
} else if (term == FF_DOS) { \ |
| 558 |
dos_state = 0; \ |
| 559 |
} |
| 560 |
|
| 561 |
static enum file_format file2format(const char *filename) |
| 562 |
{ |
| 563 |
FILE *ff; |
| 564 |
char fbuf[4096]; |
| 565 |
char *first_cr, *first_nl; |
| 566 |
enum file_format newline_format = FF_UNKNOWN; |
| 567 |
|
| 568 |
if (!(ff = fopen(filename, "r"))) { |
| 569 |
ast_log(LOG_ERROR, "Cannot open '%s': %s\n", filename, strerror(errno)); |
| 570 |
return -1; |
| 571 |
} |
| 572 |
|
| 573 |
while (fgets(fbuf, sizeof(fbuf), ff)) { |
| 574 |
first_cr = strchr(fbuf, '\r'); |
| 575 |
first_nl = strchr(fbuf, '\n'); |
| 576 |
|
| 577 |
if (!first_cr && !first_nl) { |
| 578 |
continue; |
| 579 |
} |
| 580 |
|
| 581 |
if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) { |
| 582 |
|
| 583 |
if (first_nl && first_nl == first_cr + 1) { |
| 584 |
newline_format = FF_DOS; |
| 585 |
} else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) { |
| 586 |
|
| 587 |
fseek(ff, -1, SEEK_CUR); |
| 588 |
continue; |
| 589 |
} else { |
| 590 |
newline_format = FF_MAC; |
| 591 |
} |
| 592 |
} else { |
| 593 |
newline_format = FF_UNIX; |
| 594 |
} |
| 595 |
break; |
| 596 |
} |
| 597 |
fclose(ff); |
| 598 |
return newline_format; |
| 599 |
} |
| 600 |
|
| 601 |
static int file_format(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| 602 |
{ |
| 603 |
enum file_format newline_format = file2format(data); |
| 604 |
ast_str_set(buf, len, "%c", newline_format == FF_UNIX ? 'u' : newline_format == FF_DOS ? 'd' : newline_format == FF_MAC ? 'm' : 'x'); |
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
static int file_dirname(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 609 |
{ |
| 610 |
char *ret = NULL; |
| 611 |
|
| 612 |
*buf = '\0'; |
| 613 |
|
| 614 |
if (data) { |
| 615 |
ret = dirname(data); |
| 616 |
} |
| 617 |
|
| 618 |
if (ret) { |
| 619 |
ast_copy_string(buf, ret, len); |
| 620 |
} |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
static int file_basename(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 626 |
{ |
| 627 |
char *ret = NULL; |
| 628 |
|
| 629 |
*buf = '\0'; |
| 630 |
|
| 631 |
if (data) { |
| 632 |
ret = basename(data); |
| 633 |
} |
| 634 |
|
| 635 |
if (ret) { |
| 636 |
ast_copy_string(buf, ret, len); |
| 637 |
} |
| 638 |
|
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len) |
| 643 |
{ |
| 644 |
FILE *ff; |
| 645 |
int64_t offset = 0, length = LLONG_MAX; |
| 646 |
enum file_format format = FF_UNKNOWN; |
| 647 |
char fbuf[4096]; |
| 648 |
int64_t flength, i; |
| 649 |
int64_t offset_offset = -1, length_offset = -1; |
| 650 |
char dos_state = 0; |
| 651 |
AST_DECLARE_APP_ARGS(args, |
| 652 |
AST_APP_ARG(filename); |
| 653 |
AST_APP_ARG(offset); |
| 654 |
AST_APP_ARG(length); |
| 655 |
AST_APP_ARG(options); |
| 656 |
AST_APP_ARG(fileformat); |
| 657 |
); |
| 658 |
|
| 659 |
AST_STANDARD_APP_ARGS(args, data); |
| 660 |
|
| 661 |
if (args.argc > 1) { |
| 662 |
sscanf(args.offset, "%" SCNd64, &offset); |
| 663 |
} |
| 664 |
if (args.argc > 2) { |
| 665 |
sscanf(args.length, "%" SCNd64, &length); |
| 666 |
} |
| 667 |
|
| 668 |
if (args.argc < 4 || !strchr(args.options, 'l')) { |
| 669 |
|
| 670 |
off_t off_i; |
| 671 |
|
| 672 |
if (!(ff = fopen(args.filename, "r"))) { |
| 673 |
ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno)); |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
if (fseeko(ff, 0, SEEK_END) < 0) { |
| 678 |
ast_log(LOG_ERROR, "Cannot seek to end of '%s': %s\n", args.filename, strerror(errno)); |
| 679 |
fclose(ff); |
| 680 |
return -1; |
| 681 |
} |
| 682 |
flength = ftello(ff); |
| 683 |
|
| 684 |
if (offset < 0) { |
| 685 |
fseeko(ff, offset, SEEK_END); |
| 686 |
if ((offset = ftello(ff)) < 0) { |
| 687 |
ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno)); |
| 688 |
fclose(ff); |
| 689 |
return -1; |
| 690 |
} |
| 691 |
} |
| 692 |
if (length < 0) { |
| 693 |
fseeko(ff, length, SEEK_END); |
| 694 |
if ((length = ftello(ff)) - offset < 0) { |
| 695 |
|
| 696 |
fclose(ff); |
| 697 |
return -1; |
| 698 |
} |
| 699 |
} else if (length == LLONG_MAX) { |
| 700 |
fseeko(ff, 0, SEEK_END); |
| 701 |
length = ftello(ff); |
| 702 |
} |
| 703 |
|
| 704 |
ast_str_reset(*buf); |
| 705 |
|
| 706 |
fseeko(ff, offset, SEEK_SET); |
| 707 |
for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) { |
| 708 |
|
| 709 |
size_t toappend = sizeof(fbuf); |
| 710 |
|
| 711 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 712 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 713 |
break; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
if (off_i + toappend > offset + length) { |
| 718 |
toappend = MIN(offset + length - off_i, flength - off_i); |
| 719 |
} |
| 720 |
|
| 721 |
ast_str_append_substr(buf, len, fbuf, toappend); |
| 722 |
} |
| 723 |
fclose(ff); |
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
if (args.argc == 5) { |
| 729 |
if (tolower(args.fileformat[0]) == 'd') { |
| 730 |
format = FF_DOS; |
| 731 |
} else if (tolower(args.fileformat[0]) == 'm') { |
| 732 |
format = FF_MAC; |
| 733 |
} else if (tolower(args.fileformat[0]) == 'u') { |
| 734 |
format = FF_UNIX; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
if (format == FF_UNKNOWN) { |
| 739 |
if ((format = file2format(args.filename)) == FF_UNKNOWN) { |
| 740 |
ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename); |
| 741 |
return -1; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
if (offset < 0 && length <= offset) { |
| 746 |
|
| 747 |
return -1; |
| 748 |
} else if (offset == 0) { |
| 749 |
offset_offset = 0; |
| 750 |
} |
| 751 |
|
| 752 |
if (!(ff = fopen(args.filename, "r"))) { |
| 753 |
ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno)); |
| 754 |
return -1; |
| 755 |
} |
| 756 |
|
| 757 |
if (fseek(ff, 0, SEEK_END)) { |
| 758 |
ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno)); |
| 759 |
fclose(ff); |
| 760 |
return -1; |
| 761 |
} |
| 762 |
|
| 763 |
flength = ftello(ff); |
| 764 |
|
| 765 |
if (length == LLONG_MAX) { |
| 766 |
length_offset = flength; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
if (offset < 0 || length < 0) { |
| 771 |
int64_t count = 0; |
| 772 |
|
| 773 |
|
| 774 |
for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) { |
| 775 |
size_t end; |
| 776 |
char *pos; |
| 777 |
if (fseeko(ff, i, SEEK_SET)) { |
| 778 |
ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno)); |
| 779 |
} |
| 780 |
end = fread(fbuf, 1, sizeof(fbuf), ff); |
| 781 |
for (pos = (end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1); pos >= fbuf; pos--) { |
| 782 |
LINE_COUNTER(pos, format, count); |
| 783 |
|
| 784 |
if (length < 0 && count * -1 == length) { |
| 785 |
length_offset = i + (pos - fbuf); |
| 786 |
} else if (offset < 0 && count * -1 == (offset - 1)) { |
| 787 |
|
| 788 |
if (format == FF_DOS) { |
| 789 |
offset_offset = i + (pos - fbuf) + 2; |
| 790 |
} else { |
| 791 |
offset_offset = i + (pos - fbuf) + 1; |
| 792 |
} |
| 793 |
break; |
| 794 |
} |
| 795 |
} |
| 796 |
if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) { |
| 797 |
break; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
if (offset < 0 && offset_offset < 0 && offset == count * -1) { |
| 802 |
offset_offset = 0; |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
if (offset > 0) { |
| 808 |
int64_t count = 0; |
| 809 |
fseek(ff, 0, SEEK_SET); |
| 810 |
for (i = 0; i < flength; i += sizeof(fbuf)) { |
| 811 |
char *pos; |
| 812 |
if (i + sizeof(fbuf) <= flength) { |
| 813 |
|
| 814 |
memset(fbuf, 0, sizeof(fbuf)); |
| 815 |
} |
| 816 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 817 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 818 |
fclose(ff); |
| 819 |
return -1; |
| 820 |
} |
| 821 |
for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) { |
| 822 |
LINE_COUNTER(pos, format, count); |
| 823 |
|
| 824 |
if (count == offset) { |
| 825 |
offset_offset = i + (pos - fbuf) + 1; |
| 826 |
break; |
| 827 |
} |
| 828 |
} |
| 829 |
if (offset_offset >= 0) { |
| 830 |
break; |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
if (offset_offset < 0) { |
| 836 |
ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset); |
| 837 |
fclose(ff); |
| 838 |
return -1; |
| 839 |
} |
| 840 |
|
| 841 |
ast_str_reset(*buf); |
| 842 |
if (fseeko(ff, offset_offset, SEEK_SET)) { |
| 843 |
ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno)); |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
if (length_offset >= 0) { |
| 851 |
ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset); |
| 852 |
for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) { |
| 853 |
if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) { |
| 854 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 855 |
} |
| 856 |
ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", (int64_t)(i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf)), fbuf); |
| 857 |
ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf)); |
| 858 |
} |
| 859 |
} else if (length == 0) { |
| 860 |
|
| 861 |
} else { |
| 862 |
|
| 863 |
int64_t current_length = 0; |
| 864 |
char dos_state = 0; |
| 865 |
ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset); |
| 866 |
for (i = offset_offset; i < flength; i += sizeof(fbuf)) { |
| 867 |
char *pos; |
| 868 |
size_t bytes_read; |
| 869 |
if ((bytes_read = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf) && !feof(ff)) { |
| 870 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 871 |
fclose(ff); |
| 872 |
return -1; |
| 873 |
} |
| 874 |
for (pos = fbuf; pos < fbuf + bytes_read; pos++) { |
| 875 |
LINE_COUNTER(pos, format, current_length); |
| 876 |
|
| 877 |
if (current_length == length) { |
| 878 |
length_offset = i + (pos - fbuf) + 1; |
| 879 |
break; |
| 880 |
} |
| 881 |
} |
| 882 |
ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i); |
| 883 |
ast_str_append_substr(buf, len, fbuf, (length_offset >= 0) ? length_offset - i : (flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i); |
| 884 |
|
| 885 |
if (length_offset >= 0) { |
| 886 |
break; |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
fclose(ff); |
| 892 |
return 0; |
| 893 |
} |
| 894 |
|
| 895 |
const char *format2term(enum file_format f) __attribute__((const)); |
| 896 |
const char *format2term(enum file_format f) |
| 897 |
{ |
| 898 |
const char *term[] = { "", "\n", "\r\n", "\r" }; |
| 899 |
return term[f + 1]; |
| 900 |
} |
| 901 |
|
| 902 |
static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 903 |
{ |
| 904 |
AST_DECLARE_APP_ARGS(args, |
| 905 |
AST_APP_ARG(filename); |
| 906 |
AST_APP_ARG(offset); |
| 907 |
AST_APP_ARG(length); |
| 908 |
AST_APP_ARG(options); |
| 909 |
AST_APP_ARG(format); |
| 910 |
); |
| 911 |
int64_t offset = 0, length = LLONG_MAX; |
| 912 |
off_t flength, vlength; |
| 913 |
size_t foplen = 0; |
| 914 |
FILE *ff; |
| 915 |
|
| 916 |
AST_STANDARD_APP_ARGS(args, data); |
| 917 |
|
| 918 |
if (args.argc > 1) { |
| 919 |
sscanf(args.offset, "%" SCNd64, &offset); |
| 920 |
} |
| 921 |
if (args.argc > 2) { |
| 922 |
sscanf(args.length, "%" SCNd64, &length); |
| 923 |
} |
| 924 |
|
| 925 |
vlength = strlen(value); |
| 926 |
|
| 927 |
if (args.argc < 4 || !strchr(args.options, 'l')) { |
| 928 |
|
| 929 |
|
| 930 |
if (args.argc > 3 && strchr(args.options, 'a')) { |
| 931 |
|
| 932 |
if (!(ff = fopen(args.filename, "a"))) { |
| 933 |
ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno)); |
| 934 |
return 0; |
| 935 |
} |
| 936 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 937 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 938 |
} |
| 939 |
fclose(ff); |
| 940 |
return 0; |
| 941 |
} else if (offset == 0 && length == LLONG_MAX) { |
| 942 |
if (!(ff = fopen(args.filename, "w"))) { |
| 943 |
ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno)); |
| 944 |
return 0; |
| 945 |
} |
| 946 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 947 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 948 |
} |
| 949 |
fclose(ff); |
| 950 |
return 0; |
| 951 |
} |
| 952 |
|
| 953 |
if (!(ff = fopen(args.filename, "r+"))) { |
| 954 |
ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno)); |
| 955 |
return 0; |
| 956 |
} |
| 957 |
fseeko(ff, 0, SEEK_END); |
| 958 |
flength = ftello(ff); |
| 959 |
|
| 960 |
if (offset < 0) { |
| 961 |
if (fseeko(ff, offset, SEEK_END)) { |
| 962 |
ast_log(LOG_ERROR, "Cannot seek to offset of '%s': %s\n", args.filename, strerror(errno)); |
| 963 |
fclose(ff); |
| 964 |
return -1; |
| 965 |
} |
| 966 |
if ((offset = ftello(ff)) < 0) { |
| 967 |
ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno)); |
| 968 |
fclose(ff); |
| 969 |
return -1; |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
if (length < 0) { |
| 974 |
length = flength - offset + length; |
| 975 |
if (length < 0) { |
| 976 |
ast_log(LOG_ERROR, "Length '%s' exceeds the file length. No data will be written.\n", args.length); |
| 977 |
fclose(ff); |
| 978 |
return -1; |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
fseeko(ff, offset, SEEK_SET); |
| 983 |
|
| 984 |
ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n", |
| 985 |
S_OR(args.offset, "(null)"), offset, S_OR(args.length, "(null)"), length, vlength, flength); |
| 986 |
|
| 987 |
if (length == vlength) { |
| 988 |
|
| 989 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 990 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 991 |
} |
| 992 |
fclose(ff); |
| 993 |
} else if (length == LLONG_MAX) { |
| 994 |
|
| 995 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 996 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 997 |
} |
| 998 |
fclose(ff); |
| 999 |
if (truncate(args.filename, offset + vlength)) { |
| 1000 |
ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno)); |
| 1001 |
} |
| 1002 |
} else if (length > vlength) { |
| 1003 |
|
| 1004 |
char fbuf[4096]; |
| 1005 |
off_t cur; |
| 1006 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 1007 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1008 |
} |
| 1009 |
fseeko(ff, length - vlength, SEEK_CUR); |
| 1010 |
while ((cur = ftello(ff)) < flength) { |
| 1011 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 1012 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1013 |
} |
| 1014 |
fseeko(ff, cur + vlength - length, SEEK_SET); |
| 1015 |
if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) { |
| 1016 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1017 |
} |
| 1018 |
|
| 1019 |
if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) { |
| 1020 |
|
| 1021 |
break; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
fclose(ff); |
| 1025 |
if (truncate(args.filename, flength - (length - vlength))) { |
| 1026 |
ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno)); |
| 1027 |
} |
| 1028 |
} else { |
| 1029 |
|
| 1030 |
char fbuf[4096]; |
| 1031 |
off_t lastwritten = flength + vlength - length; |
| 1032 |
|
| 1033 |
|
| 1034 |
fseeko(ff, flength - sizeof(fbuf), SEEK_SET); |
| 1035 |
while (offset < ftello(ff)) { |
| 1036 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) { |
| 1037 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1038 |
fclose(ff); |
| 1039 |
return -1; |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR); |
| 1045 |
|
| 1046 |
|
| 1047 |
lastwritten = ftello(ff); |
| 1048 |
|
| 1049 |
if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) { |
| 1050 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1051 |
fclose(ff); |
| 1052 |
return -1; |
| 1053 |
} |
| 1054 |
|
| 1055 |
if (lastwritten < offset + sizeof(fbuf)) { |
| 1056 |
break; |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR); |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
if (fseeko(ff, offset + length, SEEK_SET)) { |
| 1069 |
ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff)); |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff)); |
| 1074 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 1075 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1076 |
} |
| 1077 |
fseek(ff, offset, SEEK_SET); |
| 1078 |
|
| 1079 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 1080 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1081 |
} else { |
| 1082 |
off_t curpos = ftello(ff); |
| 1083 |
foplen = lastwritten - curpos; |
| 1084 |
if (fwrite(fbuf, 1, foplen, ff) < foplen) { |
| 1085 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
fclose(ff); |
| 1089 |
} |
| 1090 |
} else { |
| 1091 |
enum file_format newline_format = FF_UNKNOWN; |
| 1092 |
|
| 1093 |
|
| 1094 |
if (args.argc == 5) { |
| 1095 |
if (tolower(args.format[0]) == 'u') { |
| 1096 |
newline_format = FF_UNIX; |
| 1097 |
} else if (tolower(args.format[0]) == 'm') { |
| 1098 |
newline_format = FF_MAC; |
| 1099 |
} else if (tolower(args.format[0]) == 'd') { |
| 1100 |
newline_format = FF_DOS; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) { |
| 1104 |
ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename); |
| 1105 |
return -1; |
| 1106 |
} |
| 1107 |
|
| 1108 |
if (strchr(args.options, 'a')) { |
| 1109 |
|
| 1110 |
if (!(ff = fopen(args.filename, "a"))) { |
| 1111 |
ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno)); |
| 1112 |
return -1; |
| 1113 |
} |
| 1114 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 1115 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1116 |
} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) { |
| 1117 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1118 |
} |
| 1119 |
fclose(ff); |
| 1120 |
} else if (offset == 0 && length == LLONG_MAX) { |
| 1121 |
|
| 1122 |
off_t truncsize; |
| 1123 |
if (!(ff = fopen(args.filename, "w"))) { |
| 1124 |
ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno)); |
| 1125 |
return -1; |
| 1126 |
} |
| 1127 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 1128 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1129 |
} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) { |
| 1130 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1131 |
} |
| 1132 |
if ((truncsize = ftello(ff)) < 0) { |
| 1133 |
ast_log(AST_LOG_ERROR, "Unable to determine truncate position of '%s': %s\n", args.filename, strerror(errno)); |
| 1134 |
} |
| 1135 |
fclose(ff); |
| 1136 |
if (truncsize >= 0 && truncate(args.filename, truncsize)) { |
| 1137 |
ast_log(LOG_ERROR, "Unable to truncate file '%s': %s\n", args.filename, strerror(errno)); |
| 1138 |
return -1; |
| 1139 |
} |
| 1140 |
} else { |
| 1141 |
int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0; |
| 1142 |
char dos_state = 0, fbuf[4096]; |
| 1143 |
|
| 1144 |
if (offset < 0 && length < offset) { |
| 1145 |
|
| 1146 |
ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n"); |
| 1147 |
return -1; |
| 1148 |
} |
| 1149 |
|
| 1150 |
if (!(ff = fopen(args.filename, "r+"))) { |
| 1151 |
ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno)); |
| 1152 |
return -1; |
| 1153 |
} |
| 1154 |
|
| 1155 |
if (fseek(ff, 0, SEEK_END)) { |
| 1156 |
ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno)); |
| 1157 |
fclose(ff); |
| 1158 |
return -1; |
| 1159 |
} |
| 1160 |
if ((flength = ftello(ff)) < 0) { |
| 1161 |
ast_log(AST_LOG_ERROR, "Cannot determine end position of file '%s': %s\n", args.filename, strerror(errno)); |
| 1162 |
fclose(ff); |
| 1163 |
return -1; |
| 1164 |
} |
| 1165 |
|
| 1166 |
|
| 1167 |
if (offset < 0 || length < 0) { |
| 1168 |
int64_t count = 0; |
| 1169 |
for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) { |
| 1170 |
char *pos; |
| 1171 |
if (fseeko(ff, i, SEEK_SET)) { |
| 1172 |
ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno)); |
| 1173 |
} |
| 1174 |
if (i + sizeof(fbuf) >= flength) { |
| 1175 |
memset(fbuf, 0, sizeof(fbuf)); |
| 1176 |
} |
| 1177 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 1178 |
ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno)); |
| 1179 |
fclose(ff); |
| 1180 |
return -1; |
| 1181 |
} |
| 1182 |
for (pos = fbuf + sizeof(fbuf) - 1; pos >= fbuf; pos--) { |
| 1183 |
LINE_COUNTER(pos, newline_format, count); |
| 1184 |
|
| 1185 |
if (length < 0 && count * -1 == length) { |
| 1186 |
length_offset = i + (pos - fbuf); |
| 1187 |
} else if (offset < 0 && count * -1 == (offset - 1)) { |
| 1188 |
|
| 1189 |
if (newline_format == FF_DOS) { |
| 1190 |
offset_offset = i + (pos - fbuf) + 2; |
| 1191 |
} else { |
| 1192 |
offset_offset = i + (pos - fbuf) + 1; |
| 1193 |
} |
| 1194 |
break; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) { |
| 1198 |
break; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
if (offset < 0 && offset_offset < 0 && offset == count * -1) { |
| 1203 |
offset_offset = 0; |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
if (offset > 0) { |
| 1209 |
int64_t count = 0; |
| 1210 |
fseek(ff, 0, SEEK_SET); |
| 1211 |
for (i = 0; i < flength; i += sizeof(fbuf)) { |
| 1212 |
char *pos; |
| 1213 |
if (i + sizeof(fbuf) >= flength) { |
| 1214 |
memset(fbuf, 0, sizeof(fbuf)); |
| 1215 |
} |
| 1216 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 1217 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1218 |
fclose(ff); |
| 1219 |
return -1; |
| 1220 |
} |
| 1221 |
for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) { |
| 1222 |
LINE_COUNTER(pos, newline_format, count); |
| 1223 |
|
| 1224 |
if (count == offset) { |
| 1225 |
offset_offset = i + (pos - fbuf) + 1; |
| 1226 |
break; |
| 1227 |
} |
| 1228 |
} |
| 1229 |
if (offset_offset >= 0) { |
| 1230 |
break; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (offset_offset < 0) { |
| 1236 |
ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset); |
| 1237 |
fclose(ff); |
| 1238 |
return -1; |
| 1239 |
} |
| 1240 |
|
| 1241 |
if (length == 0) { |
| 1242 |
length_offset = offset_offset; |
| 1243 |
} else if (length == LLONG_MAX) { |
| 1244 |
length_offset = flength; |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
if (length_offset < 0) { |
| 1249 |
fseeko(ff, offset_offset, SEEK_SET); |
| 1250 |
for (i = offset_offset; i < flength; i += sizeof(fbuf)) { |
| 1251 |
char *pos; |
| 1252 |
if (i + sizeof(fbuf) >= flength) { |
| 1253 |
memset(fbuf, 0, sizeof(fbuf)); |
| 1254 |
} |
| 1255 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 1256 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1257 |
fclose(ff); |
| 1258 |
return -1; |
| 1259 |
} |
| 1260 |
for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) { |
| 1261 |
LINE_COUNTER(pos, newline_format, current_length); |
| 1262 |
|
| 1263 |
if (current_length == length) { |
| 1264 |
length_offset = i + (pos - fbuf) + 1; |
| 1265 |
break; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
if (length_offset >= 0) { |
| 1269 |
break; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
if (length_offset < 0) { |
| 1273 |
|
| 1274 |
ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength); |
| 1275 |
length_offset = flength; |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) { |
| 1281 |
|
| 1282 |
fseeko(ff, offset_offset, SEEK_SET); |
| 1283 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 1284 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1285 |
} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) { |
| 1286 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1287 |
} |
| 1288 |
fclose(ff); |
| 1289 |
} else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) { |
| 1290 |
|
| 1291 |
off_t cur; |
| 1292 |
int64_t length_length = length_offset - offset_offset; |
| 1293 |
size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format))); |
| 1294 |
|
| 1295 |
ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n", |
| 1296 |
args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength); |
| 1297 |
|
| 1298 |
fseeko(ff, offset_offset, SEEK_SET); |
| 1299 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 1300 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1301 |
fclose(ff); |
| 1302 |
return -1; |
| 1303 |
} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) { |
| 1304 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1305 |
fclose(ff); |
| 1306 |
return -1; |
| 1307 |
} |
| 1308 |
while ((cur = ftello(ff)) < flength) { |
| 1309 |
if (cur < 0) { |
| 1310 |
ast_log(AST_LOG_ERROR, "Unable to determine last write position for '%s': %s\n", args.filename, strerror(errno)); |
| 1311 |
fclose(ff); |
| 1312 |
return -1; |
| 1313 |
} |
| 1314 |
fseeko(ff, length_length - vlen, SEEK_CUR); |
| 1315 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 1316 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1317 |
fclose(ff); |
| 1318 |
return -1; |
| 1319 |
} |
| 1320 |
|
| 1321 |
fseeko(ff, cur, SEEK_SET); |
| 1322 |
if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) { |
| 1323 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1324 |
fclose(ff); |
| 1325 |
return -1; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
fclose(ff); |
| 1329 |
if (truncate(args.filename, flength - (length_length - vlen))) { |
| 1330 |
ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno)); |
| 1331 |
} |
| 1332 |
} else { |
| 1333 |
|
| 1334 |
size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format))); |
| 1335 |
int64_t origlen = length_offset - offset_offset; |
| 1336 |
off_t lastwritten = flength + vlen - origlen; |
| 1337 |
|
| 1338 |
ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n", |
| 1339 |
args.offset, offset_offset, args.length, length_offset, vlength, flength); |
| 1340 |
|
| 1341 |
fseeko(ff, flength - sizeof(fbuf), SEEK_SET); |
| 1342 |
while (offset_offset + sizeof(fbuf) < ftello(ff)) { |
| 1343 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) { |
| 1344 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1345 |
fclose(ff); |
| 1346 |
return -1; |
| 1347 |
} |
| 1348 |
fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR); |
| 1349 |
if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) { |
| 1350 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1351 |
fclose(ff); |
| 1352 |
return -1; |
| 1353 |
} |
| 1354 |
if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) { |
| 1355 |
break; |
| 1356 |
} |
| 1357 |
fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR); |
| 1358 |
} |
| 1359 |
fseek(ff, length_offset, SEEK_SET); |
| 1360 |
if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) { |
| 1361 |
ast_log(LOG_ERROR, "Short read?!!\n"); |
| 1362 |
fclose(ff); |
| 1363 |
return -1; |
| 1364 |
} |
| 1365 |
fseek(ff, offset_offset, SEEK_SET); |
| 1366 |
if (fwrite(value, 1, vlength, ff) < vlength) { |
| 1367 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1368 |
fclose(ff); |
| 1369 |
return -1; |
| 1370 |
} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) { |
| 1371 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1372 |
fclose(ff); |
| 1373 |
return -1; |
| 1374 |
} else { |
| 1375 |
off_t curpos = ftello(ff); |
| 1376 |
foplen = lastwritten - curpos; |
| 1377 |
if (fwrite(fbuf, 1, foplen, ff) < foplen) { |
| 1378 |
ast_log(LOG_ERROR, "Short write?!!\n"); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
fclose(ff); |
| 1382 |
} |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
return 0; |
| 1387 |
} |
| 1388 |
|
| 1389 |
static struct ast_custom_function env_function = { |
| 1390 |
.name = "ENV", |
| 1391 |
.read = env_read, |
| 1392 |
.write = env_write |
| 1393 |
}; |
| 1394 |
|
| 1395 |
static struct ast_custom_function stat_function = { |
| 1396 |
.name = "STAT", |
| 1397 |
.read = stat_read, |
| 1398 |
.read_max = 12, |
| 1399 |
}; |
| 1400 |
|
| 1401 |
static struct ast_custom_function file_function = { |
| 1402 |
.name = "FILE", |
| 1403 |
.read2 = file_read, |
| 1404 |
.write = file_write, |
| 1405 |
}; |
| 1406 |
|
| 1407 |
static struct ast_custom_function file_count_line_function = { |
| 1408 |
.name = "FILE_COUNT_LINE", |
| 1409 |
.read2 = file_count_line, |
| 1410 |
.read_max = 12, |
| 1411 |
}; |
| 1412 |
|
| 1413 |
static struct ast_custom_function file_format_function = { |
| 1414 |
.name = "FILE_FORMAT", |
| 1415 |
.read2 = file_format, |
| 1416 |
.read_max = 2, |
| 1417 |
}; |
| 1418 |
|
| 1419 |
static struct ast_custom_function file_dirname_function = { |
| 1420 |
.name = "DIRNAME", |
| 1421 |
.read = file_dirname, |
| 1422 |
.read_max = 12, |
| 1423 |
}; |
| 1424 |
|
| 1425 |
static struct ast_custom_function file_basename_function = { |
| 1426 |
.name = "BASENAME", |
| 1427 |
.read = file_basename, |
| 1428 |
.read_max = 12, |
| 1429 |
}; |
| 1430 |
|
| 1431 |
static int unload_module(void) |
| 1432 |
{ |
| 1433 |
int res = 0; |
| 1434 |
|
| 1435 |
res |= ast_custom_function_unregister(&env_function); |
| 1436 |
res |= ast_custom_function_unregister(&stat_function); |
| 1437 |
res |= ast_custom_function_unregister(&file_function); |
| 1438 |
res |= ast_custom_function_unregister(&file_count_line_function); |
| 1439 |
res |= ast_custom_function_unregister(&file_format_function); |
| 1440 |
res |= ast_custom_function_unregister(&file_dirname_function); |
| 1441 |
res |= ast_custom_function_unregister(&file_basename_function); |
| 1442 |
|
| 1443 |
return res; |
| 1444 |
} |
| 1445 |
|
| 1446 |
static int load_module(void) |
| 1447 |
{ |
| 1448 |
int res = 0; |
| 1449 |
|
| 1450 |
res |= ast_custom_function_register(&env_function); |
| 1451 |
res |= ast_custom_function_register_escalating(&stat_function, AST_CFE_READ); |
| 1452 |
res |= ast_custom_function_register_escalating(&file_function, AST_CFE_BOTH); |
| 1453 |
res |= ast_custom_function_register_escalating(&file_count_line_function, AST_CFE_READ); |
| 1454 |
res |= ast_custom_function_register_escalating(&file_format_function, AST_CFE_READ); |
| 1455 |
res |= ast_custom_function_register(&file_dirname_function); |
| 1456 |
res |= ast_custom_function_register(&file_basename_function); |
| 1457 |
|
| 1458 |
return res; |
| 1459 |
} |
| 1460 |
|
| 1461 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions"); |