func_env.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Environment related dialplan functions
  19. *
  20. * \ingroup functions
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include <sys/stat.h> /* stat(2) */
  28. #include "asterisk/module.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/app.h"
  33. #include "asterisk/file.h"
  34. /*** DOCUMENTATION
  35. <function name="ENV" language="en_US">
  36. <synopsis>
  37. Gets or sets the environment variable specified.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="varname" required="true">
  41. <para>Environment variable name</para>
  42. </parameter>
  43. </syntax>
  44. <description>
  45. <para>Variables starting with <literal>AST_</literal> are reserved to the system and may not be set.</para>
  46. </description>
  47. </function>
  48. <function name="STAT" language="en_US">
  49. <synopsis>
  50. Does a check on the specified file.
  51. </synopsis>
  52. <syntax>
  53. <parameter name="flag" required="true">
  54. <para>Flag may be one of the following:</para>
  55. <para>d - Checks if the file is a directory.</para>
  56. <para>e - Checks if the file exists.</para>
  57. <para>f - Checks if the file is a regular file.</para>
  58. <para>m - Returns the file mode (in octal)</para>
  59. <para>s - Returns the size (in bytes) of the file</para>
  60. <para>A - Returns the epoch at which the file was last accessed.</para>
  61. <para>C - Returns the epoch at which the inode was last changed.</para>
  62. <para>M - Returns the epoch at which the file was last modified.</para>
  63. </parameter>
  64. <parameter name="filename" required="true" />
  65. </syntax>
  66. <description>
  67. <note>
  68. <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
  69. is set to <literal>no</literal>, this function can only be executed from the
  70. dialplan, and not directly from external protocols.</para>
  71. </note>
  72. </description>
  73. </function>
  74. <function name="FILE" language="en_US">
  75. <synopsis>
  76. Read or write text file.
  77. </synopsis>
  78. <syntax>
  79. <parameter name="filename" required="true" />
  80. <parameter name="offset">
  81. <para>Maybe specified as any number. If negative, <replaceable>offset</replaceable> specifies the number
  82. of bytes back from the end of the file.</para>
  83. </parameter>
  84. <parameter name="length">
  85. <para>If specified, will limit the length of the data read to that size. If negative,
  86. trims <replaceable>length</replaceable> bytes from the end of the file.</para>
  87. </parameter>
  88. <parameter name="options">
  89. <optionlist>
  90. <option name="l">
  91. <para>Line mode: offset and length are assumed to be
  92. measured in lines, instead of byte offsets.</para>
  93. </option>
  94. <option name="a">
  95. <para>In write mode only, the append option is used to
  96. append to the end of the file, instead of overwriting
  97. the existing file.</para>
  98. </option>
  99. <option name="d">
  100. <para>In write mode and line mode only, this option does
  101. not automatically append a newline string to the end of
  102. a value. This is useful for deleting lines, instead of
  103. setting them to blank.</para>
  104. </option>
  105. </optionlist>
  106. </parameter>
  107. <parameter name="format">
  108. <para>The <replaceable>format</replaceable> parameter may be
  109. used to delimit the type of line terminators in line mode.</para>
  110. <optionlist>
  111. <option name="u">
  112. <para>Unix newline format.</para>
  113. </option>
  114. <option name="d">
  115. <para>DOS newline format.</para>
  116. </option>
  117. <option name="m">
  118. <para>Macintosh newline format.</para>
  119. </option>
  120. </optionlist>
  121. </parameter>
  122. </syntax>
  123. <description>
  124. <para>Read and write text file in character and line mode.</para>
  125. <para>Examples:</para>
  126. <para/>
  127. <para>Read mode (byte):</para>
  128. <para> ;reads the entire content of the file.</para>
  129. <para> Set(foo=${FILE(/tmp/test.txt)})</para>
  130. <para> ;reads from the 11th byte to the end of the file (i.e. skips the first 10).</para>
  131. <para> Set(foo=${FILE(/tmp/test.txt,10)})</para>
  132. <para> ;reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes).</para>
  133. <para> Set(foo=${FILE(/tmp/test.txt,10,10)})</para>
  134. <para/>
  135. <para>Read mode (line):</para>
  136. <para> ; reads the 3rd line of the file.</para>
  137. <para> Set(foo=${FILE(/tmp/test.txt,3,1,l)})</para>
  138. <para> ; reads the 3rd and 4th lines of the file.</para>
  139. <para> Set(foo=${FILE(/tmp/test.txt,3,2,l)})</para>
  140. <para> ; reads from the third line to the end of the file.</para>
  141. <para> Set(foo=${FILE(/tmp/test.txt,3,,l)})</para>
  142. <para> ; reads the last three lines of the file.</para>
  143. <para> Set(foo=${FILE(/tmp/test.txt,-3,,l)})</para>
  144. <para> ; reads the 3rd line of a DOS-formatted file.</para>
  145. <para> Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})</para>
  146. <para/>
  147. <para>Write mode (byte):</para>
  148. <para> ; truncate the file and write "bar"</para>
  149. <para> Set(FILE(/tmp/test.txt)=bar)</para>
  150. <para> ; Append "bar"</para>
  151. <para> Set(FILE(/tmp/test.txt,,,a)=bar)</para>
  152. <para> ; Replace the first byte with "bar" (replaces 1 character with 3)</para>
  153. <para> Set(FILE(/tmp/test.txt,0,1)=bar)</para>
  154. <para> ; Replace 10 bytes beginning at the 21st byte of the file with "bar"</para>
  155. <para> Set(FILE(/tmp/test.txt,20,10)=bar)</para>
  156. <para> ; Replace all bytes from the 21st with "bar"</para>
  157. <para> Set(FILE(/tmp/test.txt,20)=bar)</para>
  158. <para> ; Insert "bar" after the 4th character</para>
  159. <para> Set(FILE(/tmp/test.txt,4,0)=bar)</para>
  160. <para/>
  161. <para>Write mode (line):</para>
  162. <para> ; Replace the first line of the file with "bar"</para>
  163. <para> Set(FILE(/tmp/foo.txt,0,1,l)=bar)</para>
  164. <para> ; Replace the last line of the file with "bar"</para>
  165. <para> Set(FILE(/tmp/foo.txt,-1,,l)=bar)</para>
  166. <para> ; Append "bar" to the file with a newline</para>
  167. <para> Set(FILE(/tmp/foo.txt,,,al)=bar)</para>
  168. <note>
  169. <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
  170. is set to <literal>no</literal>, this function can only be executed from the
  171. dialplan, and not directly from external protocols.</para>
  172. </note>
  173. </description>
  174. <see-also>
  175. <ref type="function">FILE_COUNT_LINE</ref>
  176. <ref type="function">FILE_FORMAT</ref>
  177. </see-also>
  178. </function>
  179. <function name="FILE_COUNT_LINE" language="en_US">
  180. <synopsis>
  181. Obtains the number of lines of a text file.
  182. </synopsis>
  183. <syntax>
  184. <parameter name="filename" required="true" />
  185. <parameter name="format">
  186. <para>Format may be one of the following:</para>
  187. <optionlist>
  188. <option name="u">
  189. <para>Unix newline format.</para>
  190. </option>
  191. <option name="d">
  192. <para>DOS newline format.</para>
  193. </option>
  194. <option name="m">
  195. <para>Macintosh newline format.</para>
  196. </option>
  197. </optionlist>
  198. <note><para>If not specified, an attempt will be made to determine the newline format type.</para></note>
  199. </parameter>
  200. </syntax>
  201. <description>
  202. <para>Returns the number of lines, or <literal>-1</literal> on error.</para>
  203. <note>
  204. <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
  205. is set to <literal>no</literal>, this function can only be executed from the
  206. dialplan, and not directly from external protocols.</para>
  207. </note>
  208. </description>
  209. <see-also>
  210. <ref type="function">FILE</ref>
  211. <ref type="function">FILE_FORMAT</ref>
  212. </see-also>
  213. </function>
  214. <function name="FILE_FORMAT" language="en_US">
  215. <synopsis>
  216. Return the newline format of a text file.
  217. </synopsis>
  218. <syntax>
  219. <parameter name="filename" required="true" />
  220. </syntax>
  221. <description>
  222. <para>Return the line terminator type:</para>
  223. <para>'u' - Unix "\n" format</para>
  224. <para>'d' - DOS "\r\n" format</para>
  225. <para>'m' - Macintosh "\r" format</para>
  226. <para>'x' - Cannot be determined</para>
  227. <note>
  228. <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
  229. is set to <literal>no</literal>, this function can only be executed from the
  230. dialplan, and not directly from external protocols.</para>
  231. </note>
  232. </description>
  233. <see-also>
  234. <ref type="function">FILE</ref>
  235. <ref type="function">FILE_COUNT_LINE</ref>
  236. </see-also>
  237. </function>
  238. ***/
  239. static int env_read(struct ast_channel *chan, const char *cmd, char *data,
  240. char *buf, size_t len)
  241. {
  242. char *ret = NULL;
  243. *buf = '\0';
  244. if (data)
  245. ret = getenv(data);
  246. if (ret)
  247. ast_copy_string(buf, ret, len);
  248. return 0;
  249. }
  250. static int env_write(struct ast_channel *chan, const char *cmd, char *data,
  251. const char *value)
  252. {
  253. if (!ast_strlen_zero(data) && strncmp(data, "AST_", 4)) {
  254. if (!ast_strlen_zero(value)) {
  255. setenv(data, value, 1);
  256. } else {
  257. unsetenv(data);
  258. }
  259. }
  260. return 0;
  261. }
  262. static int stat_read(struct ast_channel *chan, const char *cmd, char *data,
  263. char *buf, size_t len)
  264. {
  265. char *action;
  266. struct stat s;
  267. ast_copy_string(buf, "0", len);
  268. action = strsep(&data, ",");
  269. if (stat(data, &s)) {
  270. return 0;
  271. } else {
  272. switch (*action) {
  273. case 'e':
  274. strcpy(buf, "1");
  275. break;
  276. case 's':
  277. snprintf(buf, len, "%d", (unsigned int) s.st_size);
  278. break;
  279. case 'f':
  280. snprintf(buf, len, "%d", S_ISREG(s.st_mode) ? 1 : 0);
  281. break;
  282. case 'd':
  283. snprintf(buf, len, "%d", S_ISDIR(s.st_mode) ? 1 : 0);
  284. break;
  285. case 'M':
  286. snprintf(buf, len, "%d", (int) s.st_mtime);
  287. break;
  288. case 'A':
  289. snprintf(buf, len, "%d", (int) s.st_mtime);
  290. break;
  291. case 'C':
  292. snprintf(buf, len, "%d", (int) s.st_ctime);
  293. break;
  294. case 'm':
  295. snprintf(buf, len, "%o", (int) s.st_mode);
  296. break;
  297. }
  298. }
  299. return 0;
  300. }
  301. enum file_format {
  302. FF_UNKNOWN = -1,
  303. FF_UNIX,
  304. FF_DOS,
  305. FF_MAC,
  306. };
  307. static int64_t count_lines(const char *filename, enum file_format newline_format)
  308. {
  309. int count = 0;
  310. char fbuf[4096];
  311. FILE *ff;
  312. if (!(ff = fopen(filename, "r"))) {
  313. ast_log(LOG_ERROR, "Unable to open '%s': %s\n", filename, strerror(errno));
  314. return -1;
  315. }
  316. while (fgets(fbuf, sizeof(fbuf), ff)) {
  317. char *next = fbuf, *first_cr = NULL, *first_nl = NULL;
  318. /* Must do it this way, because if the fileformat is FF_MAC, then Unix
  319. * assumptions about line-format will not come into play. */
  320. while (next) {
  321. if (newline_format == FF_DOS || newline_format == FF_MAC || newline_format == FF_UNKNOWN) {
  322. first_cr = strchr(next, '\r');
  323. }
  324. if (newline_format == FF_UNIX || newline_format == FF_UNKNOWN) {
  325. first_nl = strchr(next, '\n');
  326. }
  327. /* No terminators found in buffer */
  328. if (!first_cr && !first_nl) {
  329. break;
  330. }
  331. if (newline_format == FF_UNKNOWN) {
  332. if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
  333. if (first_nl && first_nl == first_cr + 1) {
  334. newline_format = FF_DOS;
  335. } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
  336. /* Get it on the next pass */
  337. fseek(ff, -1, SEEK_CUR);
  338. break;
  339. } else {
  340. newline_format = FF_MAC;
  341. first_nl = NULL;
  342. }
  343. } else {
  344. newline_format = FF_UNIX;
  345. first_cr = NULL;
  346. }
  347. /* Jump down into next section */
  348. }
  349. if (newline_format == FF_DOS) {
  350. if (first_nl && first_cr && first_nl == first_cr + 1) {
  351. next = first_nl + 1;
  352. count++;
  353. } else if (first_cr == &fbuf[sizeof(fbuf) - 2]) {
  354. /* Get it on the next pass */
  355. fseek(ff, -1, SEEK_CUR);
  356. break;
  357. }
  358. } else if (newline_format == FF_MAC) {
  359. if (first_cr) {
  360. next = first_cr + 1;
  361. count++;
  362. }
  363. } else if (newline_format == FF_UNIX) {
  364. if (first_nl) {
  365. next = first_nl + 1;
  366. count++;
  367. }
  368. }
  369. }
  370. }
  371. fclose(ff);
  372. return count;
  373. }
  374. static int file_count_line(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  375. {
  376. enum file_format newline_format = FF_UNKNOWN;
  377. int64_t count;
  378. AST_DECLARE_APP_ARGS(args,
  379. AST_APP_ARG(filename);
  380. AST_APP_ARG(format);
  381. );
  382. AST_STANDARD_APP_ARGS(args, data);
  383. if (args.argc > 1) {
  384. if (tolower(args.format[0]) == 'd') {
  385. newline_format = FF_DOS;
  386. } else if (tolower(args.format[0]) == 'm') {
  387. newline_format = FF_MAC;
  388. } else if (tolower(args.format[0]) == 'u') {
  389. newline_format = FF_UNIX;
  390. }
  391. }
  392. count = count_lines(args.filename, newline_format);
  393. ast_str_set(buf, len, "%" PRId64, count);
  394. return 0;
  395. }
  396. #define LINE_COUNTER(cptr, term, counter) \
  397. if (*cptr == '\n' && term == FF_UNIX) { \
  398. counter++; \
  399. } else if (*cptr == '\n' && term == FF_DOS && dos_state == 0) { \
  400. dos_state = 1; \
  401. } else if (*cptr == '\r' && term == FF_DOS && dos_state == 1) { \
  402. dos_state = 0; \
  403. counter++; \
  404. } else if (*cptr == '\r' && term == FF_MAC) { \
  405. counter++; \
  406. } else if (term == FF_DOS) { \
  407. dos_state = 0; \
  408. }
  409. static enum file_format file2format(const char *filename)
  410. {
  411. FILE *ff;
  412. char fbuf[4096];
  413. char *first_cr, *first_nl;
  414. enum file_format newline_format = FF_UNKNOWN;
  415. if (!(ff = fopen(filename, "r"))) {
  416. ast_log(LOG_ERROR, "Cannot open '%s': %s\n", filename, strerror(errno));
  417. return -1;
  418. }
  419. while (fgets(fbuf, sizeof(fbuf), ff)) {
  420. first_cr = strchr(fbuf, '\r');
  421. first_nl = strchr(fbuf, '\n');
  422. if (!first_cr && !first_nl) {
  423. continue;
  424. }
  425. if ((first_cr && !first_nl) || (first_cr && first_cr < first_nl)) {
  426. if (first_nl && first_nl == first_cr + 1) {
  427. newline_format = FF_DOS;
  428. } else if (first_cr && first_cr == &fbuf[sizeof(fbuf) - 2]) {
  429. /* Edge case: get it on the next pass */
  430. fseek(ff, -1, SEEK_CUR);
  431. continue;
  432. } else {
  433. newline_format = FF_MAC;
  434. }
  435. } else {
  436. newline_format = FF_UNIX;
  437. }
  438. break;
  439. }
  440. fclose(ff);
  441. return newline_format;
  442. }
  443. static int file_format(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  444. {
  445. enum file_format newline_format = file2format(data);
  446. ast_str_set(buf, len, "%c", newline_format == FF_UNIX ? 'u' : newline_format == FF_DOS ? 'd' : newline_format == FF_MAC ? 'm' : 'x');
  447. return 0;
  448. }
  449. static int file_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  450. {
  451. FILE *ff;
  452. int64_t offset = 0, length = LLONG_MAX;
  453. enum file_format format = FF_UNKNOWN;
  454. char fbuf[4096];
  455. int64_t flength, i; /* iterator needs to be signed, so it can go negative and terminate the loop */
  456. int64_t offset_offset = -1, length_offset = -1;
  457. char dos_state = 0;
  458. size_t readlen;
  459. AST_DECLARE_APP_ARGS(args,
  460. AST_APP_ARG(filename);
  461. AST_APP_ARG(offset);
  462. AST_APP_ARG(length);
  463. AST_APP_ARG(options);
  464. AST_APP_ARG(fileformat);
  465. );
  466. AST_STANDARD_APP_ARGS(args, data);
  467. if (args.argc > 1) {
  468. sscanf(args.offset, "%" SCNd64, &offset);
  469. }
  470. if (args.argc > 2) {
  471. sscanf(args.length, "%" SCNd64, &length);
  472. }
  473. if (args.argc < 4 || !strchr(args.options, 'l')) {
  474. /* Character-based mode */
  475. off_t off_i;
  476. if (!(ff = fopen(args.filename, "r"))) {
  477. ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno));
  478. return 0;
  479. }
  480. if (fseeko(ff, 0, SEEK_END) < 0) {
  481. ast_log(LOG_ERROR, "Cannot seek to end of '%s': %s\n", args.filename, strerror(errno));
  482. fclose(ff);
  483. return -1;
  484. }
  485. flength = ftello(ff);
  486. if (offset < 0) {
  487. fseeko(ff, offset, SEEK_END);
  488. if ((offset = ftello(ff)) < 0) {
  489. ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
  490. fclose(ff);
  491. return -1;
  492. }
  493. }
  494. if (length < 0) {
  495. fseeko(ff, length, SEEK_END);
  496. if ((length = ftello(ff)) - offset < 0) {
  497. /* Eliminates all results */
  498. fclose(ff);
  499. return -1;
  500. }
  501. } else if (length == LLONG_MAX) {
  502. fseeko(ff, 0, SEEK_END);
  503. length = ftello(ff);
  504. }
  505. ast_str_reset(*buf);
  506. fseeko(ff, offset, SEEK_SET);
  507. for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) {
  508. /* Calculate if we need to retrieve just a portion of the file in memory */
  509. size_t toappend = sizeof(fbuf);
  510. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  511. ast_log(LOG_ERROR, "Short read?!!\n");
  512. break;
  513. }
  514. /* Don't go past the length requested */
  515. if (off_i + toappend > offset + length) {
  516. toappend = length - off_i;
  517. }
  518. ast_str_append_substr(buf, len, fbuf, toappend);
  519. }
  520. fclose(ff);
  521. return 0;
  522. }
  523. /* Line-based read */
  524. if (args.argc == 5) {
  525. if (tolower(args.fileformat[0]) == 'd') {
  526. format = FF_DOS;
  527. } else if (tolower(args.fileformat[0]) == 'm') {
  528. format = FF_MAC;
  529. } else if (tolower(args.fileformat[0]) == 'u') {
  530. format = FF_UNIX;
  531. }
  532. }
  533. if (format == FF_UNKNOWN) {
  534. if ((format = file2format(args.filename)) == FF_UNKNOWN) {
  535. ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename);
  536. return -1;
  537. }
  538. }
  539. if (offset < 0 && length <= offset) {
  540. /* Length eliminates all content */
  541. return -1;
  542. } else if (offset == 0) {
  543. offset_offset = 0;
  544. }
  545. if (!(ff = fopen(args.filename, "r"))) {
  546. ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno));
  547. return -1;
  548. }
  549. if (fseek(ff, 0, SEEK_END)) {
  550. ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
  551. fclose(ff);
  552. return -1;
  553. }
  554. flength = ftello(ff);
  555. if (length == LLONG_MAX) {
  556. length_offset = flength;
  557. }
  558. /* For negative offset and/or negative length */
  559. if (offset < 0 || length < 0) {
  560. int64_t count = 0;
  561. /* Start with an even multiple of fbuf, so at the end of reading with a
  562. * 0 offset, we don't try to go past the beginning of the file. */
  563. for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
  564. size_t end;
  565. char *pos;
  566. if (fseeko(ff, i, SEEK_SET)) {
  567. ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
  568. }
  569. end = fread(fbuf, 1, sizeof(fbuf), ff);
  570. for (pos = end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
  571. LINE_COUNTER(pos, format, count);
  572. if (length < 0 && count * -1 == length) {
  573. length_offset = i + (pos - fbuf);
  574. } else if (offset < 0 && count * -1 == (offset - 1)) {
  575. /* Found our initial offset. We're done with reverse motion! */
  576. if (format == FF_DOS) {
  577. offset_offset = i + (pos - fbuf) + 2;
  578. } else {
  579. offset_offset = i + (pos - fbuf) + 1;
  580. }
  581. break;
  582. }
  583. }
  584. if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
  585. break;
  586. }
  587. }
  588. /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
  589. if (offset < 0 && offset_offset < 0 && offset == count * -1) {
  590. offset_offset = 0;
  591. }
  592. }
  593. /* Positve line offset */
  594. if (offset > 0) {
  595. int64_t count = 0;
  596. fseek(ff, 0, SEEK_SET);
  597. for (i = 0; i < flength; i += sizeof(fbuf)) {
  598. char *pos;
  599. if (i + sizeof(fbuf) <= flength) {
  600. /* Don't let previous values influence current counts, due to short reads */
  601. memset(fbuf, 0, sizeof(fbuf));
  602. }
  603. if (fread(fbuf, 1, sizeof(fbuf), ff) && !feof(ff)) {
  604. ast_log(LOG_ERROR, "Short read?!!\n");
  605. fclose(ff);
  606. return -1;
  607. }
  608. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  609. LINE_COUNTER(pos, format, count);
  610. if (count == offset) {
  611. offset_offset = i + (pos - fbuf) + 1;
  612. break;
  613. }
  614. }
  615. if (offset_offset >= 0) {
  616. break;
  617. }
  618. }
  619. }
  620. if (offset_offset < 0) {
  621. ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
  622. fclose(ff);
  623. return -1;
  624. }
  625. ast_str_reset(*buf);
  626. if (fseeko(ff, offset_offset, SEEK_SET)) {
  627. ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno));
  628. }
  629. /* If we have both offset_offset and length_offset, then grabbing the
  630. * buffer is simply a matter of just retrieving the file and adding it
  631. * to buf. Otherwise, we need to run byte-by-byte forward until the
  632. * length is complete. */
  633. if (length_offset >= 0) {
  634. ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
  635. for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
  636. if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) {
  637. ast_log(LOG_ERROR, "Short read?!!\n");
  638. }
  639. ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf), fbuf);
  640. ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
  641. }
  642. } else if (length == 0) {
  643. /* Nothing to do */
  644. } else {
  645. /* Positive line offset */
  646. int64_t current_length = 0;
  647. char dos_state = 0;
  648. ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
  649. for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
  650. char *pos;
  651. if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf) && !feof(ff)) {
  652. ast_log(LOG_ERROR, "Short read?!!\n");
  653. }
  654. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  655. LINE_COUNTER(pos, format, current_length);
  656. if (current_length == length) {
  657. length_offset = i + (pos - fbuf) + 1;
  658. break;
  659. }
  660. }
  661. ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
  662. ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? length_offset - i : flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i;
  663. if (length_offset >= 0) {
  664. break;
  665. }
  666. }
  667. }
  668. fclose(ff);
  669. return 0;
  670. }
  671. const char *format2term(enum file_format f) __attribute__((const));
  672. const char *format2term(enum file_format f)
  673. {
  674. const char *term[] = { "", "\n", "\r\n", "\r" };
  675. return term[f + 1];
  676. }
  677. static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  678. {
  679. AST_DECLARE_APP_ARGS(args,
  680. AST_APP_ARG(filename);
  681. AST_APP_ARG(offset);
  682. AST_APP_ARG(length);
  683. AST_APP_ARG(options);
  684. AST_APP_ARG(format);
  685. );
  686. int64_t offset = 0, length = LLONG_MAX;
  687. off_t flength, vlength;
  688. size_t foplen = 0;
  689. FILE *ff;
  690. AST_STANDARD_APP_ARGS(args, data);
  691. if (args.argc > 1) {
  692. sscanf(args.offset, "%" SCNd64, &offset);
  693. }
  694. if (args.argc > 2) {
  695. sscanf(args.length, "%" SCNd64, &length);
  696. }
  697. vlength = strlen(value);
  698. if (args.argc < 4 || !strchr(args.options, 'l')) {
  699. /* Character-based mode */
  700. if (args.argc > 3 && strchr(args.options, 'a')) {
  701. /* Append mode */
  702. if (!(ff = fopen(args.filename, "a"))) {
  703. ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
  704. return 0;
  705. }
  706. if (fwrite(value, 1, vlength, ff) < vlength) {
  707. ast_log(LOG_ERROR, "Short write?!!\n");
  708. }
  709. fclose(ff);
  710. return 0;
  711. } else if (offset == 0 && length == LLONG_MAX) {
  712. if (!(ff = fopen(args.filename, "w"))) {
  713. ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
  714. return 0;
  715. }
  716. if (fwrite(value, 1, vlength, ff) < vlength) {
  717. ast_log(LOG_ERROR, "Short write?!!\n");
  718. }
  719. fclose(ff);
  720. return 0;
  721. }
  722. if (!(ff = fopen(args.filename, "r+"))) {
  723. ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno));
  724. return 0;
  725. }
  726. fseeko(ff, 0, SEEK_END);
  727. flength = ftello(ff);
  728. if (offset < 0) {
  729. if (fseeko(ff, offset, SEEK_END)) {
  730. ast_log(LOG_ERROR, "Cannot seek to offset of '%s': %s\n", args.filename, strerror(errno));
  731. fclose(ff);
  732. return -1;
  733. }
  734. if ((offset = ftello(ff)) < 0) {
  735. ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
  736. fclose(ff);
  737. return -1;
  738. }
  739. }
  740. if (length < 0) {
  741. length = flength - offset + length;
  742. if (length < 0) {
  743. ast_log(LOG_ERROR, "Length '%s' exceeds the file length. No data will be written.\n", args.length);
  744. fclose(ff);
  745. return -1;
  746. }
  747. }
  748. fseeko(ff, offset, SEEK_SET);
  749. ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
  750. S_OR(args.offset, "(null)"), offset, S_OR(args.length, "(null)"), length, vlength, flength);
  751. if (length == vlength) {
  752. /* Simplest case, a straight replace */
  753. if (fwrite(value, 1, vlength, ff) < vlength) {
  754. ast_log(LOG_ERROR, "Short write?!!\n");
  755. }
  756. fclose(ff);
  757. } else if (length == LLONG_MAX) {
  758. /* Simple truncation */
  759. if (fwrite(value, 1, vlength, ff) < vlength) {
  760. ast_log(LOG_ERROR, "Short write?!!\n");
  761. }
  762. fclose(ff);
  763. if (truncate(args.filename, offset + vlength)) {
  764. ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
  765. }
  766. } else if (length > vlength) {
  767. /* More complex -- need to close a gap */
  768. char fbuf[4096];
  769. off_t cur;
  770. if (fwrite(value, 1, vlength, ff) < vlength) {
  771. ast_log(LOG_ERROR, "Short write?!!\n");
  772. }
  773. fseeko(ff, length - vlength, SEEK_CUR);
  774. while ((cur = ftello(ff)) < flength) {
  775. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  776. ast_log(LOG_ERROR, "Short read?!!\n");
  777. }
  778. fseeko(ff, cur + vlength - length, SEEK_SET);
  779. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  780. ast_log(LOG_ERROR, "Short write?!!\n");
  781. }
  782. /* Seek to where we stopped reading */
  783. if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) {
  784. /* Only reason for seek to fail is EOF */
  785. break;
  786. }
  787. }
  788. fclose(ff);
  789. if (truncate(args.filename, flength - (length - vlength))) {
  790. ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
  791. }
  792. } else {
  793. /* Most complex -- need to open a gap */
  794. char fbuf[4096];
  795. off_t lastwritten = flength + vlength - length;
  796. /* Start reading exactly the buffer size back from the end. */
  797. fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
  798. while (offset < ftello(ff)) {
  799. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  800. ast_log(LOG_ERROR, "Short read?!!\n");
  801. fclose(ff);
  802. return -1;
  803. }
  804. /* Since the read moved our file ptr forward, we reverse, but
  805. * seek an offset equal to the amount we want to extend the
  806. * file by */
  807. fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR);
  808. /* Note the location of this buffer -- we must not overwrite this position. */
  809. lastwritten = ftello(ff);
  810. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  811. ast_log(LOG_ERROR, "Short write?!!\n");
  812. fclose(ff);
  813. return -1;
  814. }
  815. if (lastwritten < offset + sizeof(fbuf)) {
  816. break;
  817. }
  818. /* Our file pointer is now either pointing to the end of the
  819. * file (new position) or a multiple of the fbuf size back from
  820. * that point. Move back to where we want to start reading
  821. * again. We never actually try to read beyond the end of the
  822. * file, so we don't have do deal with short reads, as we would
  823. * when we're shortening the file. */
  824. fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR);
  825. }
  826. /* Last part of the file that we need to preserve */
  827. if (fseeko(ff, offset + length, SEEK_SET)) {
  828. ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff));
  829. }
  830. /* Doesn't matter how much we read -- just need to restrict the write */
  831. ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff));
  832. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  833. ast_log(LOG_ERROR, "Short read?!!\n");
  834. }
  835. fseek(ff, offset, SEEK_SET);
  836. /* Write out the value, then write just up until where we last moved some data */
  837. if (fwrite(value, 1, vlength, ff) < vlength) {
  838. ast_log(LOG_ERROR, "Short write?!!\n");
  839. } else {
  840. off_t curpos = ftello(ff);
  841. foplen = lastwritten - curpos;
  842. if (fwrite(fbuf, 1, foplen, ff) < foplen) {
  843. ast_log(LOG_ERROR, "Short write?!!\n");
  844. }
  845. }
  846. fclose(ff);
  847. }
  848. } else {
  849. enum file_format newline_format = FF_UNKNOWN;
  850. /* Line mode */
  851. if (args.argc == 5) {
  852. if (tolower(args.format[0]) == 'u') {
  853. newline_format = FF_UNIX;
  854. } else if (tolower(args.format[0]) == 'm') {
  855. newline_format = FF_MAC;
  856. } else if (tolower(args.format[0]) == 'd') {
  857. newline_format = FF_DOS;
  858. }
  859. }
  860. if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
  861. ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
  862. return -1;
  863. }
  864. if (strchr(args.options, 'a')) {
  865. /* Append to file */
  866. if (!(ff = fopen(args.filename, "a"))) {
  867. ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
  868. return -1;
  869. }
  870. if (fwrite(value, 1, vlength, ff) < vlength) {
  871. ast_log(LOG_ERROR, "Short write?!!\n");
  872. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  873. ast_log(LOG_ERROR, "Short write?!!\n");
  874. }
  875. fclose(ff);
  876. } else if (offset == 0 && length == LLONG_MAX) {
  877. /* Overwrite file */
  878. off_t truncsize;
  879. if (!(ff = fopen(args.filename, "w"))) {
  880. ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
  881. return -1;
  882. }
  883. if (fwrite(value, 1, vlength, ff) < vlength) {
  884. ast_log(LOG_ERROR, "Short write?!!\n");
  885. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  886. ast_log(LOG_ERROR, "Short write?!!\n");
  887. }
  888. if ((truncsize = ftello(ff)) < 0) {
  889. ast_log(AST_LOG_ERROR, "Unable to determine truncate position of '%s': %s\n", args.filename, strerror(errno));
  890. }
  891. fclose(ff);
  892. if (truncsize >= 0 && truncate(args.filename, truncsize)) {
  893. ast_log(LOG_ERROR, "Unable to truncate file '%s': %s\n", args.filename, strerror(errno));
  894. return -1;
  895. }
  896. } else {
  897. int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
  898. char dos_state = 0, fbuf[4096];
  899. if (offset < 0 && length < offset) {
  900. /* Nonsense! */
  901. ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
  902. return -1;
  903. }
  904. if (!(ff = fopen(args.filename, "r+"))) {
  905. ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
  906. return -1;
  907. }
  908. if (fseek(ff, 0, SEEK_END)) {
  909. ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
  910. fclose(ff);
  911. return -1;
  912. }
  913. if ((flength = ftello(ff)) < 0) {
  914. ast_log(AST_LOG_ERROR, "Cannot determine end position of file '%s': %s\n", args.filename, strerror(errno));
  915. fclose(ff);
  916. return -1;
  917. }
  918. /* For negative offset and/or negative length */
  919. if (offset < 0 || length < 0) {
  920. int64_t count = 0;
  921. for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
  922. char *pos;
  923. if (fseeko(ff, i, SEEK_SET)) {
  924. ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
  925. }
  926. if (i + sizeof(fbuf) >= flength) {
  927. memset(fbuf, 0, sizeof(fbuf));
  928. }
  929. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  930. ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno));
  931. fclose(ff);
  932. return -1;
  933. }
  934. for (pos = fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
  935. LINE_COUNTER(pos, newline_format, count);
  936. if (length < 0 && count * -1 == length) {
  937. length_offset = i + (pos - fbuf);
  938. } else if (offset < 0 && count * -1 == (offset - 1)) {
  939. /* Found our initial offset. We're done with reverse motion! */
  940. if (newline_format == FF_DOS) {
  941. offset_offset = i + (pos - fbuf) + 2;
  942. } else {
  943. offset_offset = i + (pos - fbuf) + 1;
  944. }
  945. break;
  946. }
  947. }
  948. if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
  949. break;
  950. }
  951. }
  952. /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
  953. if (offset < 0 && offset_offset < 0 && offset == count * -1) {
  954. offset_offset = 0;
  955. }
  956. }
  957. /* Positve line offset */
  958. if (offset > 0) {
  959. int64_t count = 0;
  960. fseek(ff, 0, SEEK_SET);
  961. for (i = 0; i < flength; i += sizeof(fbuf)) {
  962. char *pos;
  963. if (i + sizeof(fbuf) >= flength) {
  964. memset(fbuf, 0, sizeof(fbuf));
  965. }
  966. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  967. ast_log(LOG_ERROR, "Short read?!!\n");
  968. fclose(ff);
  969. return -1;
  970. }
  971. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  972. LINE_COUNTER(pos, newline_format, count);
  973. if (count == offset) {
  974. offset_offset = i + (pos - fbuf) + 1;
  975. break;
  976. }
  977. }
  978. if (offset_offset >= 0) {
  979. break;
  980. }
  981. }
  982. }
  983. if (offset_offset < 0) {
  984. ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
  985. fclose(ff);
  986. return -1;
  987. }
  988. if (length == 0) {
  989. length_offset = offset_offset;
  990. } else if (length == LLONG_MAX) {
  991. length_offset = flength;
  992. }
  993. /* Positive line length */
  994. if (length_offset < 0) {
  995. fseeko(ff, offset_offset, SEEK_SET);
  996. for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
  997. char *pos;
  998. if (i + sizeof(fbuf) >= flength) {
  999. memset(fbuf, 0, sizeof(fbuf));
  1000. }
  1001. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  1002. ast_log(LOG_ERROR, "Short read?!!\n");
  1003. fclose(ff);
  1004. return -1;
  1005. }
  1006. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  1007. LINE_COUNTER(pos, newline_format, current_length);
  1008. if (current_length == length) {
  1009. length_offset = i + (pos - fbuf) + 1;
  1010. break;
  1011. }
  1012. }
  1013. if (length_offset >= 0) {
  1014. break;
  1015. }
  1016. }
  1017. if (length_offset < 0) {
  1018. /* Exceeds length of file */
  1019. ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
  1020. length_offset = flength;
  1021. }
  1022. }
  1023. /* Have offset_offset and length_offset now */
  1024. if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
  1025. /* Simple case - replacement of text inline */
  1026. fseeko(ff, offset_offset, SEEK_SET);
  1027. if (fwrite(value, 1, vlength, ff) < vlength) {
  1028. ast_log(LOG_ERROR, "Short write?!!\n");
  1029. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  1030. ast_log(LOG_ERROR, "Short write?!!\n");
  1031. }
  1032. fclose(ff);
  1033. } else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
  1034. /* More complex case - need to shorten file */
  1035. off_t cur;
  1036. int64_t length_length = length_offset - offset_offset;
  1037. size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
  1038. ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
  1039. args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
  1040. fseeko(ff, offset_offset, SEEK_SET);
  1041. if (fwrite(value, 1, vlength, ff) < vlength) {
  1042. ast_log(LOG_ERROR, "Short write?!!\n");
  1043. fclose(ff);
  1044. return -1;
  1045. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) {
  1046. ast_log(LOG_ERROR, "Short write?!!\n");
  1047. fclose(ff);
  1048. return -1;
  1049. }
  1050. while ((cur = ftello(ff)) < flength) {
  1051. if (cur < 0) {
  1052. ast_log(AST_LOG_ERROR, "Unable to determine last write position for '%s': %s\n", args.filename, strerror(errno));
  1053. fclose(ff);
  1054. return -1;
  1055. }
  1056. fseeko(ff, length_length - vlen, SEEK_CUR);
  1057. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  1058. ast_log(LOG_ERROR, "Short read?!!\n");
  1059. fclose(ff);
  1060. return -1;
  1061. }
  1062. /* Seek to where we last stopped writing */
  1063. fseeko(ff, cur, SEEK_SET);
  1064. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  1065. ast_log(LOG_ERROR, "Short write?!!\n");
  1066. fclose(ff);
  1067. return -1;
  1068. }
  1069. }
  1070. fclose(ff);
  1071. if (truncate(args.filename, flength - (length_length - vlen))) {
  1072. ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
  1073. }
  1074. } else {
  1075. /* Most complex case - need to lengthen file */
  1076. size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
  1077. int64_t origlen = length_offset - offset_offset;
  1078. off_t lastwritten = flength + vlen - origlen;
  1079. ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
  1080. args.offset, offset_offset, args.length, length_offset, vlength, flength);
  1081. fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
  1082. while (offset_offset + sizeof(fbuf) < ftello(ff)) {
  1083. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  1084. ast_log(LOG_ERROR, "Short read?!!\n");
  1085. fclose(ff);
  1086. return -1;
  1087. }
  1088. fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR);
  1089. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  1090. ast_log(LOG_ERROR, "Short write?!!\n");
  1091. fclose(ff);
  1092. return -1;
  1093. }
  1094. if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) {
  1095. break;
  1096. }
  1097. fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR);
  1098. }
  1099. fseek(ff, length_offset, SEEK_SET);
  1100. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  1101. ast_log(LOG_ERROR, "Short read?!!\n");
  1102. fclose(ff);
  1103. return -1;
  1104. }
  1105. fseek(ff, offset_offset, SEEK_SET);
  1106. if (fwrite(value, 1, vlength, ff) < vlength) {
  1107. ast_log(LOG_ERROR, "Short write?!!\n");
  1108. fclose(ff);
  1109. return -1;
  1110. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  1111. ast_log(LOG_ERROR, "Short write?!!\n");
  1112. fclose(ff);
  1113. return -1;
  1114. } else {
  1115. off_t curpos = ftello(ff);
  1116. foplen = lastwritten - curpos;
  1117. if (fwrite(fbuf, 1, foplen, ff) < foplen) {
  1118. ast_log(LOG_ERROR, "Short write?!!\n");
  1119. }
  1120. }
  1121. fclose(ff);
  1122. }
  1123. }
  1124. }
  1125. return 0;
  1126. }
  1127. static struct ast_custom_function env_function = {
  1128. .name = "ENV",
  1129. .read = env_read,
  1130. .write = env_write
  1131. };
  1132. static struct ast_custom_function stat_function = {
  1133. .name = "STAT",
  1134. .read = stat_read,
  1135. .read_max = 12,
  1136. };
  1137. static struct ast_custom_function file_function = {
  1138. .name = "FILE",
  1139. .read2 = file_read,
  1140. .write = file_write,
  1141. };
  1142. static struct ast_custom_function file_count_line_function = {
  1143. .name = "FILE_COUNT_LINE",
  1144. .read2 = file_count_line,
  1145. .read_max = 12,
  1146. };
  1147. static struct ast_custom_function file_format_function = {
  1148. .name = "FILE_FORMAT",
  1149. .read2 = file_format,
  1150. .read_max = 2,
  1151. };
  1152. static int unload_module(void)
  1153. {
  1154. int res = 0;
  1155. res |= ast_custom_function_unregister(&env_function);
  1156. res |= ast_custom_function_unregister(&stat_function);
  1157. res |= ast_custom_function_unregister(&file_function);
  1158. res |= ast_custom_function_unregister(&file_count_line_function);
  1159. res |= ast_custom_function_unregister(&file_format_function);
  1160. return res;
  1161. }
  1162. static int load_module(void)
  1163. {
  1164. int res = 0;
  1165. res |= ast_custom_function_register(&env_function);
  1166. res |= ast_custom_function_register_escalating(&stat_function, AST_CFE_READ);
  1167. res |= ast_custom_function_register_escalating(&file_function, AST_CFE_BOTH);
  1168. res |= ast_custom_function_register_escalating(&file_count_line_function, AST_CFE_READ);
  1169. res |= ast_custom_function_register_escalating(&file_format_function, AST_CFE_READ);
  1170. return res;
  1171. }
  1172. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");