func_env.c 40 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, "%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", (unsigned 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. 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 = MIN(offset + length - off_i, flength - 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; 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; 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");