func_env.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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, "%u", (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", 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. AST_DECLARE_APP_ARGS(args,
  459. AST_APP_ARG(filename);
  460. AST_APP_ARG(offset);
  461. AST_APP_ARG(length);
  462. AST_APP_ARG(options);
  463. AST_APP_ARG(fileformat);
  464. );
  465. AST_STANDARD_APP_ARGS(args, data);
  466. if (args.argc > 1) {
  467. sscanf(args.offset, "%" SCNd64, &offset);
  468. }
  469. if (args.argc > 2) {
  470. sscanf(args.length, "%" SCNd64, &length);
  471. }
  472. if (args.argc < 4 || !strchr(args.options, 'l')) {
  473. /* Character-based mode */
  474. off_t off_i;
  475. if (!(ff = fopen(args.filename, "r"))) {
  476. ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", args.filename, strerror(errno));
  477. return 0;
  478. }
  479. if (fseeko(ff, 0, SEEK_END) < 0) {
  480. ast_log(LOG_ERROR, "Cannot seek to end of '%s': %s\n", args.filename, strerror(errno));
  481. fclose(ff);
  482. return -1;
  483. }
  484. flength = ftello(ff);
  485. if (offset < 0) {
  486. fseeko(ff, offset, SEEK_END);
  487. if ((offset = ftello(ff)) < 0) {
  488. ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
  489. fclose(ff);
  490. return -1;
  491. }
  492. }
  493. if (length < 0) {
  494. fseeko(ff, length, SEEK_END);
  495. if ((length = ftello(ff)) - offset < 0) {
  496. /* Eliminates all results */
  497. fclose(ff);
  498. return -1;
  499. }
  500. } else if (length == LLONG_MAX) {
  501. fseeko(ff, 0, SEEK_END);
  502. length = ftello(ff);
  503. }
  504. ast_str_reset(*buf);
  505. fseeko(ff, offset, SEEK_SET);
  506. for (off_i = ftello(ff); off_i < flength && off_i < offset + length; off_i += sizeof(fbuf)) {
  507. /* Calculate if we need to retrieve just a portion of the file in memory */
  508. size_t toappend = sizeof(fbuf);
  509. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  510. ast_log(LOG_ERROR, "Short read?!!\n");
  511. break;
  512. }
  513. /* Don't go past the length requested */
  514. if (off_i + toappend > offset + length) {
  515. toappend = length - off_i;
  516. }
  517. ast_str_append_substr(buf, len, fbuf, toappend);
  518. }
  519. fclose(ff);
  520. return 0;
  521. }
  522. /* Line-based read */
  523. if (args.argc == 5) {
  524. if (tolower(args.fileformat[0]) == 'd') {
  525. format = FF_DOS;
  526. } else if (tolower(args.fileformat[0]) == 'm') {
  527. format = FF_MAC;
  528. } else if (tolower(args.fileformat[0]) == 'u') {
  529. format = FF_UNIX;
  530. }
  531. }
  532. if (format == FF_UNKNOWN) {
  533. if ((format = file2format(args.filename)) == FF_UNKNOWN) {
  534. ast_log(LOG_WARNING, "'%s' is not a line-based file\n", args.filename);
  535. return -1;
  536. }
  537. }
  538. if (offset < 0 && length <= offset) {
  539. /* Length eliminates all content */
  540. return -1;
  541. } else if (offset == 0) {
  542. offset_offset = 0;
  543. }
  544. if (!(ff = fopen(args.filename, "r"))) {
  545. ast_log(LOG_ERROR, "Cannot open '%s': %s\n", args.filename, strerror(errno));
  546. return -1;
  547. }
  548. if (fseek(ff, 0, SEEK_END)) {
  549. ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
  550. fclose(ff);
  551. return -1;
  552. }
  553. flength = ftello(ff);
  554. if (length == LLONG_MAX) {
  555. length_offset = flength;
  556. }
  557. /* For negative offset and/or negative length */
  558. if (offset < 0 || length < 0) {
  559. int64_t count = 0;
  560. /* Start with an even multiple of fbuf, so at the end of reading with a
  561. * 0 offset, we don't try to go past the beginning of the file. */
  562. for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
  563. size_t end;
  564. char *pos;
  565. if (fseeko(ff, i, SEEK_SET)) {
  566. ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
  567. }
  568. end = fread(fbuf, 1, sizeof(fbuf), ff);
  569. for (pos = (end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1); pos > fbuf - 1; pos--) {
  570. LINE_COUNTER(pos, format, count);
  571. if (length < 0 && count * -1 == length) {
  572. length_offset = i + (pos - fbuf);
  573. } else if (offset < 0 && count * -1 == (offset - 1)) {
  574. /* Found our initial offset. We're done with reverse motion! */
  575. if (format == FF_DOS) {
  576. offset_offset = i + (pos - fbuf) + 2;
  577. } else {
  578. offset_offset = i + (pos - fbuf) + 1;
  579. }
  580. break;
  581. }
  582. }
  583. if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
  584. break;
  585. }
  586. }
  587. /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
  588. if (offset < 0 && offset_offset < 0 && offset == count * -1) {
  589. offset_offset = 0;
  590. }
  591. }
  592. /* Positve line offset */
  593. if (offset > 0) {
  594. int64_t count = 0;
  595. fseek(ff, 0, SEEK_SET);
  596. for (i = 0; i < flength; i += sizeof(fbuf)) {
  597. char *pos;
  598. if (i + sizeof(fbuf) <= flength) {
  599. /* Don't let previous values influence current counts, due to short reads */
  600. memset(fbuf, 0, sizeof(fbuf));
  601. }
  602. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  603. ast_log(LOG_ERROR, "Short read?!!\n");
  604. fclose(ff);
  605. return -1;
  606. }
  607. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  608. LINE_COUNTER(pos, format, count);
  609. if (count == offset) {
  610. offset_offset = i + (pos - fbuf) + 1;
  611. break;
  612. }
  613. }
  614. if (offset_offset >= 0) {
  615. break;
  616. }
  617. }
  618. }
  619. if (offset_offset < 0) {
  620. ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
  621. fclose(ff);
  622. return -1;
  623. }
  624. ast_str_reset(*buf);
  625. if (fseeko(ff, offset_offset, SEEK_SET)) {
  626. ast_log(LOG_ERROR, "fseeko failed: %s\n", strerror(errno));
  627. }
  628. /* If we have both offset_offset and length_offset, then grabbing the
  629. * buffer is simply a matter of just retrieving the file and adding it
  630. * to buf. Otherwise, we need to run byte-by-byte forward until the
  631. * length is complete. */
  632. if (length_offset >= 0) {
  633. ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
  634. for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
  635. if (fread(fbuf, 1, i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf), ff) < (i + sizeof(fbuf) > flength ? flength - i : sizeof(fbuf))) {
  636. ast_log(LOG_ERROR, "Short read?!!\n");
  637. }
  638. ast_debug(3, "Appending first %" PRId64" bytes of fbuf=%s\n", (int64_t)(i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf)), fbuf);
  639. ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
  640. }
  641. } else if (length == 0) {
  642. /* Nothing to do */
  643. } else {
  644. /* Positive line offset */
  645. int64_t current_length = 0;
  646. char dos_state = 0;
  647. ast_debug(3, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
  648. for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
  649. char *pos;
  650. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  651. ast_log(LOG_ERROR, "Short read?!!\n");
  652. fclose(ff);
  653. return -1;
  654. }
  655. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  656. LINE_COUNTER(pos, format, current_length);
  657. if (current_length == length) {
  658. length_offset = i + (pos - fbuf) + 1;
  659. break;
  660. }
  661. }
  662. ast_debug(3, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
  663. ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? length_offset - i : flength > i + sizeof(fbuf)) ? sizeof(fbuf) : flength - i;
  664. if (length_offset >= 0) {
  665. break;
  666. }
  667. }
  668. }
  669. fclose(ff);
  670. return 0;
  671. }
  672. const char *format2term(enum file_format f) __attribute__((const));
  673. const char *format2term(enum file_format f)
  674. {
  675. const char *term[] = { "", "\n", "\r\n", "\r" };
  676. return term[f + 1];
  677. }
  678. static int file_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  679. {
  680. AST_DECLARE_APP_ARGS(args,
  681. AST_APP_ARG(filename);
  682. AST_APP_ARG(offset);
  683. AST_APP_ARG(length);
  684. AST_APP_ARG(options);
  685. AST_APP_ARG(format);
  686. );
  687. int64_t offset = 0, length = LLONG_MAX;
  688. off_t flength, vlength;
  689. size_t foplen = 0;
  690. FILE *ff;
  691. AST_STANDARD_APP_ARGS(args, data);
  692. if (args.argc > 1) {
  693. sscanf(args.offset, "%" SCNd64, &offset);
  694. }
  695. if (args.argc > 2) {
  696. sscanf(args.length, "%" SCNd64, &length);
  697. }
  698. vlength = strlen(value);
  699. if (args.argc < 4 || !strchr(args.options, 'l')) {
  700. /* Character-based mode */
  701. if (args.argc > 3 && strchr(args.options, 'a')) {
  702. /* Append mode */
  703. if (!(ff = fopen(args.filename, "a"))) {
  704. ast_log(LOG_WARNING, "Cannot open file '%s' for appending: %s\n", args.filename, strerror(errno));
  705. return 0;
  706. }
  707. if (fwrite(value, 1, vlength, ff) < vlength) {
  708. ast_log(LOG_ERROR, "Short write?!!\n");
  709. }
  710. fclose(ff);
  711. return 0;
  712. } else if (offset == 0 && length == LLONG_MAX) {
  713. if (!(ff = fopen(args.filename, "w"))) {
  714. ast_log(LOG_WARNING, "Cannot open file '%s' for writing: %s\n", args.filename, strerror(errno));
  715. return 0;
  716. }
  717. if (fwrite(value, 1, vlength, ff) < vlength) {
  718. ast_log(LOG_ERROR, "Short write?!!\n");
  719. }
  720. fclose(ff);
  721. return 0;
  722. }
  723. if (!(ff = fopen(args.filename, "r+"))) {
  724. ast_log(LOG_WARNING, "Cannot open file '%s' for modification: %s\n", args.filename, strerror(errno));
  725. return 0;
  726. }
  727. fseeko(ff, 0, SEEK_END);
  728. flength = ftello(ff);
  729. if (offset < 0) {
  730. if (fseeko(ff, offset, SEEK_END)) {
  731. ast_log(LOG_ERROR, "Cannot seek to offset of '%s': %s\n", args.filename, strerror(errno));
  732. fclose(ff);
  733. return -1;
  734. }
  735. if ((offset = ftello(ff)) < 0) {
  736. ast_log(AST_LOG_ERROR, "Cannot determine offset position of '%s': %s\n", args.filename, strerror(errno));
  737. fclose(ff);
  738. return -1;
  739. }
  740. }
  741. if (length < 0) {
  742. length = flength - offset + length;
  743. if (length < 0) {
  744. ast_log(LOG_ERROR, "Length '%s' exceeds the file length. No data will be written.\n", args.length);
  745. fclose(ff);
  746. return -1;
  747. }
  748. }
  749. fseeko(ff, offset, SEEK_SET);
  750. ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
  751. S_OR(args.offset, "(null)"), offset, S_OR(args.length, "(null)"), length, vlength, flength);
  752. if (length == vlength) {
  753. /* Simplest case, a straight replace */
  754. if (fwrite(value, 1, vlength, ff) < vlength) {
  755. ast_log(LOG_ERROR, "Short write?!!\n");
  756. }
  757. fclose(ff);
  758. } else if (length == LLONG_MAX) {
  759. /* Simple truncation */
  760. if (fwrite(value, 1, vlength, ff) < vlength) {
  761. ast_log(LOG_ERROR, "Short write?!!\n");
  762. }
  763. fclose(ff);
  764. if (truncate(args.filename, offset + vlength)) {
  765. ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
  766. }
  767. } else if (length > vlength) {
  768. /* More complex -- need to close a gap */
  769. char fbuf[4096];
  770. off_t cur;
  771. if (fwrite(value, 1, vlength, ff) < vlength) {
  772. ast_log(LOG_ERROR, "Short write?!!\n");
  773. }
  774. fseeko(ff, length - vlength, SEEK_CUR);
  775. while ((cur = ftello(ff)) < flength) {
  776. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  777. ast_log(LOG_ERROR, "Short read?!!\n");
  778. }
  779. fseeko(ff, cur + vlength - length, SEEK_SET);
  780. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  781. ast_log(LOG_ERROR, "Short write?!!\n");
  782. }
  783. /* Seek to where we stopped reading */
  784. if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) {
  785. /* Only reason for seek to fail is EOF */
  786. break;
  787. }
  788. }
  789. fclose(ff);
  790. if (truncate(args.filename, flength - (length - vlength))) {
  791. ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
  792. }
  793. } else {
  794. /* Most complex -- need to open a gap */
  795. char fbuf[4096];
  796. off_t lastwritten = flength + vlength - length;
  797. /* Start reading exactly the buffer size back from the end. */
  798. fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
  799. while (offset < ftello(ff)) {
  800. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  801. ast_log(LOG_ERROR, "Short read?!!\n");
  802. fclose(ff);
  803. return -1;
  804. }
  805. /* Since the read moved our file ptr forward, we reverse, but
  806. * seek an offset equal to the amount we want to extend the
  807. * file by */
  808. fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR);
  809. /* Note the location of this buffer -- we must not overwrite this position. */
  810. lastwritten = ftello(ff);
  811. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  812. ast_log(LOG_ERROR, "Short write?!!\n");
  813. fclose(ff);
  814. return -1;
  815. }
  816. if (lastwritten < offset + sizeof(fbuf)) {
  817. break;
  818. }
  819. /* Our file pointer is now either pointing to the end of the
  820. * file (new position) or a multiple of the fbuf size back from
  821. * that point. Move back to where we want to start reading
  822. * again. We never actually try to read beyond the end of the
  823. * file, so we don't have do deal with short reads, as we would
  824. * when we're shortening the file. */
  825. fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR);
  826. }
  827. /* Last part of the file that we need to preserve */
  828. if (fseeko(ff, offset + length, SEEK_SET)) {
  829. ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff));
  830. }
  831. /* Doesn't matter how much we read -- just need to restrict the write */
  832. ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff));
  833. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  834. ast_log(LOG_ERROR, "Short read?!!\n");
  835. }
  836. fseek(ff, offset, SEEK_SET);
  837. /* Write out the value, then write just up until where we last moved some data */
  838. if (fwrite(value, 1, vlength, ff) < vlength) {
  839. ast_log(LOG_ERROR, "Short write?!!\n");
  840. } else {
  841. off_t curpos = ftello(ff);
  842. foplen = lastwritten - curpos;
  843. if (fwrite(fbuf, 1, foplen, ff) < foplen) {
  844. ast_log(LOG_ERROR, "Short write?!!\n");
  845. }
  846. }
  847. fclose(ff);
  848. }
  849. } else {
  850. enum file_format newline_format = FF_UNKNOWN;
  851. /* Line mode */
  852. if (args.argc == 5) {
  853. if (tolower(args.format[0]) == 'u') {
  854. newline_format = FF_UNIX;
  855. } else if (tolower(args.format[0]) == 'm') {
  856. newline_format = FF_MAC;
  857. } else if (tolower(args.format[0]) == 'd') {
  858. newline_format = FF_DOS;
  859. }
  860. }
  861. if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
  862. ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
  863. return -1;
  864. }
  865. if (strchr(args.options, 'a')) {
  866. /* Append to file */
  867. if (!(ff = fopen(args.filename, "a"))) {
  868. ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
  869. return -1;
  870. }
  871. if (fwrite(value, 1, vlength, ff) < vlength) {
  872. ast_log(LOG_ERROR, "Short write?!!\n");
  873. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  874. ast_log(LOG_ERROR, "Short write?!!\n");
  875. }
  876. fclose(ff);
  877. } else if (offset == 0 && length == LLONG_MAX) {
  878. /* Overwrite file */
  879. off_t truncsize;
  880. if (!(ff = fopen(args.filename, "w"))) {
  881. ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
  882. return -1;
  883. }
  884. if (fwrite(value, 1, vlength, ff) < vlength) {
  885. ast_log(LOG_ERROR, "Short write?!!\n");
  886. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  887. ast_log(LOG_ERROR, "Short write?!!\n");
  888. }
  889. if ((truncsize = ftello(ff)) < 0) {
  890. ast_log(AST_LOG_ERROR, "Unable to determine truncate position of '%s': %s\n", args.filename, strerror(errno));
  891. }
  892. fclose(ff);
  893. if (truncsize >= 0 && truncate(args.filename, truncsize)) {
  894. ast_log(LOG_ERROR, "Unable to truncate file '%s': %s\n", args.filename, strerror(errno));
  895. return -1;
  896. }
  897. } else {
  898. int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
  899. char dos_state = 0, fbuf[4096];
  900. if (offset < 0 && length < offset) {
  901. /* Nonsense! */
  902. ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
  903. return -1;
  904. }
  905. if (!(ff = fopen(args.filename, "r+"))) {
  906. ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
  907. return -1;
  908. }
  909. if (fseek(ff, 0, SEEK_END)) {
  910. ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
  911. fclose(ff);
  912. return -1;
  913. }
  914. if ((flength = ftello(ff)) < 0) {
  915. ast_log(AST_LOG_ERROR, "Cannot determine end position of file '%s': %s\n", args.filename, strerror(errno));
  916. fclose(ff);
  917. return -1;
  918. }
  919. /* For negative offset and/or negative length */
  920. if (offset < 0 || length < 0) {
  921. int64_t count = 0;
  922. for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
  923. char *pos;
  924. if (fseeko(ff, i, SEEK_SET)) {
  925. ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
  926. }
  927. if (i + sizeof(fbuf) >= flength) {
  928. memset(fbuf, 0, sizeof(fbuf));
  929. }
  930. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  931. ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno));
  932. fclose(ff);
  933. return -1;
  934. }
  935. for (pos = fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
  936. LINE_COUNTER(pos, newline_format, count);
  937. if (length < 0 && count * -1 == length) {
  938. length_offset = i + (pos - fbuf);
  939. } else if (offset < 0 && count * -1 == (offset - 1)) {
  940. /* Found our initial offset. We're done with reverse motion! */
  941. if (newline_format == FF_DOS) {
  942. offset_offset = i + (pos - fbuf) + 2;
  943. } else {
  944. offset_offset = i + (pos - fbuf) + 1;
  945. }
  946. break;
  947. }
  948. }
  949. if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
  950. break;
  951. }
  952. }
  953. /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
  954. if (offset < 0 && offset_offset < 0 && offset == count * -1) {
  955. offset_offset = 0;
  956. }
  957. }
  958. /* Positve line offset */
  959. if (offset > 0) {
  960. int64_t count = 0;
  961. fseek(ff, 0, SEEK_SET);
  962. for (i = 0; i < flength; i += sizeof(fbuf)) {
  963. char *pos;
  964. if (i + sizeof(fbuf) >= flength) {
  965. memset(fbuf, 0, sizeof(fbuf));
  966. }
  967. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  968. ast_log(LOG_ERROR, "Short read?!!\n");
  969. fclose(ff);
  970. return -1;
  971. }
  972. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  973. LINE_COUNTER(pos, newline_format, count);
  974. if (count == offset) {
  975. offset_offset = i + (pos - fbuf) + 1;
  976. break;
  977. }
  978. }
  979. if (offset_offset >= 0) {
  980. break;
  981. }
  982. }
  983. }
  984. if (offset_offset < 0) {
  985. ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
  986. fclose(ff);
  987. return -1;
  988. }
  989. if (length == 0) {
  990. length_offset = offset_offset;
  991. } else if (length == LLONG_MAX) {
  992. length_offset = flength;
  993. }
  994. /* Positive line length */
  995. if (length_offset < 0) {
  996. fseeko(ff, offset_offset, SEEK_SET);
  997. for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
  998. char *pos;
  999. if (i + sizeof(fbuf) >= flength) {
  1000. memset(fbuf, 0, sizeof(fbuf));
  1001. }
  1002. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  1003. ast_log(LOG_ERROR, "Short read?!!\n");
  1004. fclose(ff);
  1005. return -1;
  1006. }
  1007. for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
  1008. LINE_COUNTER(pos, newline_format, current_length);
  1009. if (current_length == length) {
  1010. length_offset = i + (pos - fbuf) + 1;
  1011. break;
  1012. }
  1013. }
  1014. if (length_offset >= 0) {
  1015. break;
  1016. }
  1017. }
  1018. if (length_offset < 0) {
  1019. /* Exceeds length of file */
  1020. ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
  1021. length_offset = flength;
  1022. }
  1023. }
  1024. /* Have offset_offset and length_offset now */
  1025. if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
  1026. /* Simple case - replacement of text inline */
  1027. fseeko(ff, offset_offset, SEEK_SET);
  1028. if (fwrite(value, 1, vlength, ff) < vlength) {
  1029. ast_log(LOG_ERROR, "Short write?!!\n");
  1030. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  1031. ast_log(LOG_ERROR, "Short write?!!\n");
  1032. }
  1033. fclose(ff);
  1034. } else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
  1035. /* More complex case - need to shorten file */
  1036. off_t cur;
  1037. int64_t length_length = length_offset - offset_offset;
  1038. size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
  1039. ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
  1040. args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
  1041. fseeko(ff, offset_offset, SEEK_SET);
  1042. if (fwrite(value, 1, vlength, ff) < vlength) {
  1043. ast_log(LOG_ERROR, "Short write?!!\n");
  1044. fclose(ff);
  1045. return -1;
  1046. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) {
  1047. ast_log(LOG_ERROR, "Short write?!!\n");
  1048. fclose(ff);
  1049. return -1;
  1050. }
  1051. while ((cur = ftello(ff)) < flength) {
  1052. if (cur < 0) {
  1053. ast_log(AST_LOG_ERROR, "Unable to determine last write position for '%s': %s\n", args.filename, strerror(errno));
  1054. fclose(ff);
  1055. return -1;
  1056. }
  1057. fseeko(ff, length_length - vlen, SEEK_CUR);
  1058. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  1059. ast_log(LOG_ERROR, "Short read?!!\n");
  1060. fclose(ff);
  1061. return -1;
  1062. }
  1063. /* Seek to where we last stopped writing */
  1064. fseeko(ff, cur, SEEK_SET);
  1065. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  1066. ast_log(LOG_ERROR, "Short write?!!\n");
  1067. fclose(ff);
  1068. return -1;
  1069. }
  1070. }
  1071. fclose(ff);
  1072. if (truncate(args.filename, flength - (length_length - vlen))) {
  1073. ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
  1074. }
  1075. } else {
  1076. /* Most complex case - need to lengthen file */
  1077. size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
  1078. int64_t origlen = length_offset - offset_offset;
  1079. off_t lastwritten = flength + vlen - origlen;
  1080. ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
  1081. args.offset, offset_offset, args.length, length_offset, vlength, flength);
  1082. fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
  1083. while (offset_offset + sizeof(fbuf) < ftello(ff)) {
  1084. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  1085. ast_log(LOG_ERROR, "Short read?!!\n");
  1086. fclose(ff);
  1087. return -1;
  1088. }
  1089. fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR);
  1090. if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
  1091. ast_log(LOG_ERROR, "Short write?!!\n");
  1092. fclose(ff);
  1093. return -1;
  1094. }
  1095. if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) {
  1096. break;
  1097. }
  1098. fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR);
  1099. }
  1100. fseek(ff, length_offset, SEEK_SET);
  1101. if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
  1102. ast_log(LOG_ERROR, "Short read?!!\n");
  1103. fclose(ff);
  1104. return -1;
  1105. }
  1106. fseek(ff, offset_offset, SEEK_SET);
  1107. if (fwrite(value, 1, vlength, ff) < vlength) {
  1108. ast_log(LOG_ERROR, "Short write?!!\n");
  1109. fclose(ff);
  1110. return -1;
  1111. } else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
  1112. ast_log(LOG_ERROR, "Short write?!!\n");
  1113. fclose(ff);
  1114. return -1;
  1115. } else {
  1116. off_t curpos = ftello(ff);
  1117. foplen = lastwritten - curpos;
  1118. if (fwrite(fbuf, 1, foplen, ff) < foplen) {
  1119. ast_log(LOG_ERROR, "Short write?!!\n");
  1120. }
  1121. }
  1122. fclose(ff);
  1123. }
  1124. }
  1125. }
  1126. return 0;
  1127. }
  1128. static struct ast_custom_function env_function = {
  1129. .name = "ENV",
  1130. .read = env_read,
  1131. .write = env_write
  1132. };
  1133. static struct ast_custom_function stat_function = {
  1134. .name = "STAT",
  1135. .read = stat_read,
  1136. .read_max = 12,
  1137. };
  1138. static struct ast_custom_function file_function = {
  1139. .name = "FILE",
  1140. .read2 = file_read,
  1141. .write = file_write,
  1142. };
  1143. static struct ast_custom_function file_count_line_function = {
  1144. .name = "FILE_COUNT_LINE",
  1145. .read2 = file_count_line,
  1146. .read_max = 12,
  1147. };
  1148. static struct ast_custom_function file_format_function = {
  1149. .name = "FILE_FORMAT",
  1150. .read2 = file_format,
  1151. .read_max = 2,
  1152. };
  1153. static int unload_module(void)
  1154. {
  1155. int res = 0;
  1156. res |= ast_custom_function_unregister(&env_function);
  1157. res |= ast_custom_function_unregister(&stat_function);
  1158. res |= ast_custom_function_unregister(&file_function);
  1159. res |= ast_custom_function_unregister(&file_count_line_function);
  1160. res |= ast_custom_function_unregister(&file_format_function);
  1161. return res;
  1162. }
  1163. static int load_module(void)
  1164. {
  1165. int res = 0;
  1166. res |= ast_custom_function_register(&env_function);
  1167. res |= ast_custom_function_register_escalating(&stat_function, AST_CFE_READ);
  1168. res |= ast_custom_function_register_escalating(&file_function, AST_CFE_BOTH);
  1169. res |= ast_custom_function_register_escalating(&file_count_line_function, AST_CFE_READ);
  1170. res |= ast_custom_function_register_escalating(&file_format_function, AST_CFE_READ);
  1171. return res;
  1172. }
  1173. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");