file_open.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /**
  2. * @file file_open.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * file_open(string filename, string mode [, map options])
  33. *
  34. * Variables:
  35. * string is_error - "true" if the file_open object is in error state, "false"
  36. * otherwise
  37. *
  38. * Options:
  39. * "read_size" - the maximum number of bytes that can be read by a single
  40. * read() call. Must be greater than zero. Greater values may improve
  41. * performance, but will increase memory usage. Default: 8192.
  42. *
  43. * Description:
  44. * Opens a file for subsequent reading or writing. The 'mode' argument must
  45. * be one of: "r", "w", "a", "r+", "w+", "a+"; it corresponds to the mode string
  46. * that will be passed to the fopen() function.
  47. * When the file_open() statement goes up, the error state is set depending on
  48. * whether opening succeeded or failed. The 'is_error' variable should be used
  49. * to check the error state.
  50. * If an error occurs afterward within read(), write() or seek(), the error state
  51. * is set, and the file_open() statement is toggled down and back up. This way,
  52. * the same piece of user code can handle all file errors.
  53. *
  54. * Synopsis:
  55. * file_open::read()
  56. *
  57. * Variables:
  58. * string (empty) - the data which was read, or an empty string if EOF was reached
  59. * string not_eof - "false" if EOF was reached, "true" if not
  60. *
  61. * Description:
  62. * Reads data from an opened file. The file must not be in error state.
  63. * If reading fails, this statement will never go up, the error state of the
  64. * file_open() statement will be set, and the file_open() statement will trigger
  65. * backtracking (go down and up).
  66. *
  67. * Synopsis:
  68. * file_open::write(string data)
  69. *
  70. * Description:
  71. * Writes data to an opened file. The file must not be in error state.
  72. * If writing fails, this statement will never go up, the error state of the
  73. * file_open() statement will be set, and the file_open() statement will trigger
  74. * backtracking (go down and up).
  75. *
  76. * Synopsis:
  77. * file_open::seek(string position, string whence)
  78. *
  79. * Description:
  80. * Sets the file position indicator. The 'position' argument must be a possibly
  81. * negative decimal number, and is interpreted relative to 'whence'. Here, 'whence'
  82. * may be one of:
  83. * - "set", meaning beginning of file,
  84. * - "cur", meaning the current position, and
  85. * - "end", meaning the end of file.
  86. * Errors are handled as in read() and write(). Note that if the position argument
  87. * is too small or too large to convert to off_t, this is not a seek error, and only
  88. * the seek command will fail.
  89. *
  90. * Synopsis:
  91. * file_open::close()
  92. *
  93. * Description:
  94. * Closes the file. The file must not be in error state.
  95. * Errors are handled as handled as in read() and write(), i.e. the process is
  96. * backtracked to file_open() with the error state set.
  97. * On success, the error state of the file is set (but without backtracking), and
  98. * the close() statement goes up .
  99. */
  100. #include <stddef.h>
  101. #include <stdio.h>
  102. #include <stdint.h>
  103. #include <limits.h>
  104. #include <misc/debug.h>
  105. #include <misc/balloc.h>
  106. #include <misc/parse_number.h>
  107. #include <ncd/NCDModule.h>
  108. #include <ncd/static_strings.h>
  109. #include <ncd/extra/value_utils.h>
  110. #include <ncd/extra/NCDBuf.h>
  111. #include <generated/blog_channel_ncd_file_open.h>
  112. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  113. #define READ_BUF_SIZE 8192
  114. struct open_instance {
  115. NCDModuleInst *i;
  116. FILE *fh;
  117. NCDBufStore store;
  118. };
  119. struct read_instance {
  120. NCDModuleInst *i;
  121. NCDBuf *buf;
  122. size_t length;
  123. };
  124. static int parse_mode (const char *data, size_t mode_len, char *out)
  125. {
  126. if (mode_len == 0) {
  127. return 0;
  128. }
  129. switch (*data) {
  130. case 'r':
  131. case 'w':
  132. case 'a':
  133. *out++ = *data++;
  134. mode_len--;
  135. break;
  136. default:
  137. return 0;
  138. }
  139. if (mode_len == 0) {
  140. goto finish;
  141. }
  142. switch (*data) {
  143. case '+':
  144. *out++ = *data++;
  145. mode_len--;
  146. break;
  147. default:
  148. return 0;
  149. }
  150. if (mode_len == 0) {
  151. goto finish;
  152. }
  153. return 0;
  154. finish:
  155. *out = '\0';
  156. return 1;
  157. }
  158. static void trigger_error (struct open_instance *o)
  159. {
  160. if (o->fh) {
  161. // close file
  162. if (fclose(o->fh) != 0) {
  163. ModuleLog(o->i, BLOG_ERROR, "fclose failed");
  164. }
  165. // set no file, indicating error
  166. o->fh = NULL;
  167. }
  168. // go down and up
  169. NCDModuleInst_Backend_Down(o->i);
  170. NCDModuleInst_Backend_Up(o->i);
  171. }
  172. static void open_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  173. {
  174. struct open_instance *o = vo;
  175. o->i = i;
  176. // check arguments
  177. NCDValRef filename_arg;
  178. NCDValRef mode_arg;
  179. NCDValRef options_arg = NCDVal_NewInvalid();
  180. if (!NCDVal_ListRead(params->args, 2, &filename_arg, &mode_arg) &&
  181. !NCDVal_ListRead(params->args, 3, &filename_arg, &mode_arg, &options_arg)
  182. ) {
  183. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  184. goto fail0;
  185. }
  186. if (!NCDVal_IsStringNoNulls(filename_arg) || !NCDVal_IsString(mode_arg) ||
  187. (!NCDVal_IsInvalid(options_arg) && !NCDVal_IsMap(options_arg))
  188. ) {
  189. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  190. goto fail0;
  191. }
  192. // check mode
  193. char mode[5];
  194. if (!parse_mode(NCDVal_StringData(mode_arg), NCDVal_StringLength(mode_arg), mode)) {
  195. ModuleLog(o->i, BLOG_ERROR, "wrong mode");
  196. goto fail0;
  197. }
  198. size_t read_size_opt = READ_BUF_SIZE;
  199. // parse options
  200. if (!NCDVal_IsInvalid(options_arg)) {
  201. int num_recognized = 0;
  202. NCDValRef value;
  203. if (!NCDVal_IsInvalid(value = NCDVal_MapGetValue(options_arg, "read_size"))) {
  204. uintmax_t read_size;
  205. if (!NCDVal_IsString(value) || !ncd_read_uintmax(value, &read_size) || read_size > SIZE_MAX || read_size == 0) {
  206. ModuleLog(o->i, BLOG_ERROR, "wrong read_size");
  207. goto fail0;
  208. }
  209. num_recognized++;
  210. read_size_opt = read_size;
  211. }
  212. if (NCDVal_MapCount(options_arg) > num_recognized) {
  213. ModuleLog(o->i, BLOG_ERROR, "unrecognized options present");
  214. goto fail0;
  215. }
  216. }
  217. // init store
  218. NCDBufStore_Init(&o->store, read_size_opt);
  219. // null terminate filename
  220. NCDValNullTermString filename_nts;
  221. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  222. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  223. goto fail1;
  224. }
  225. // open file
  226. o->fh = fopen(filename_nts.data, mode);
  227. NCDValNullTermString_Free(&filename_nts);
  228. if (!o->fh) {
  229. ModuleLog(o->i, BLOG_ERROR, "fopen failed");
  230. }
  231. // go up
  232. NCDModuleInst_Backend_Up(i);
  233. return;
  234. fail1:
  235. NCDBufStore_Free(&o->store);
  236. fail0:
  237. NCDModuleInst_Backend_DeadError(i);
  238. }
  239. static void open_func_die (void *vo)
  240. {
  241. struct open_instance *o = vo;
  242. // close file
  243. if (o->fh) {
  244. if (fclose(o->fh) != 0) {
  245. ModuleLog(o->i, BLOG_ERROR, "fclose failed");
  246. }
  247. }
  248. // free store
  249. NCDBufStore_Free(&o->store);
  250. NCDModuleInst_Backend_Dead(o->i);
  251. }
  252. static int open_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  253. {
  254. struct open_instance *o = vo;
  255. if (name == NCD_STRING_IS_ERROR) {
  256. *out = ncd_make_boolean(mem, !o->fh, o->i->params->iparams->string_index);
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. static void read_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  262. {
  263. struct read_instance *o = vo;
  264. o->i = i;
  265. // check arguments
  266. if (!NCDVal_ListRead(params->args, 0)) {
  267. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  268. goto fail0;
  269. }
  270. // get open instance
  271. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  272. // make sure it's not in error
  273. if (!open_inst->fh) {
  274. ModuleLog(o->i, BLOG_ERROR, "open instance is in error");
  275. goto fail0;
  276. }
  277. // get buffer
  278. o->buf = NCDBufStore_GetBuf(&open_inst->store);
  279. if (!o->buf) {
  280. ModuleLog(o->i, BLOG_ERROR, "NCDBufStore_GetBuf failed");
  281. goto fail0;
  282. }
  283. // starting with empty buffer
  284. char *data = NCDBuf_Data(o->buf);
  285. size_t buf_size = NCDBufStore_BufSize(&open_inst->store);
  286. o->length = 0;
  287. while (o->length < buf_size) {
  288. // read
  289. size_t readed = fread(data + o->length, 1, buf_size - o->length, open_inst->fh);
  290. if (readed == 0) {
  291. break;
  292. }
  293. ASSERT(readed <= buf_size - o->length)
  294. // increment length
  295. o->length += readed;
  296. }
  297. // if we couldn't read anything due to an error, trigger
  298. // error in the open instance, and don't go up
  299. if (o->length == 0 && !feof(open_inst->fh)) {
  300. ModuleLog(o->i, BLOG_ERROR, "fread failed");
  301. trigger_error(open_inst);
  302. return;
  303. }
  304. // go up
  305. NCDModuleInst_Backend_Up(i);
  306. return;
  307. fail0:
  308. NCDModuleInst_Backend_DeadError(i);
  309. }
  310. static void read_func_die (void *vo)
  311. {
  312. struct read_instance *o = vo;
  313. // release buffer
  314. NCDRefTarget_Deref(NCDBuf_RefTarget(o->buf));
  315. NCDModuleInst_Backend_Dead(o->i);
  316. }
  317. static int read_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  318. {
  319. struct read_instance *o = vo;
  320. if (name == NCD_STRING_EMPTY) {
  321. *out = NCDVal_NewExternalString(mem, NCDBuf_Data(o->buf), o->length, NCDBuf_RefTarget(o->buf));
  322. return 1;
  323. }
  324. if (name == NCD_STRING_NOT_EOF) {
  325. *out = ncd_make_boolean(mem, (o->length != 0), o->i->params->iparams->string_index);
  326. return 1;
  327. }
  328. return 0;
  329. }
  330. static void write_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  331. {
  332. // check arguments
  333. NCDValRef data_arg;
  334. if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
  335. ModuleLog(i, BLOG_ERROR, "wrong arity");
  336. goto fail0;
  337. }
  338. if (!NCDVal_IsString(data_arg)) {
  339. ModuleLog(i, BLOG_ERROR, "wrong type");
  340. goto fail0;
  341. }
  342. // get open instance
  343. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  344. // make sure it's not in error
  345. if (!open_inst->fh) {
  346. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  347. goto fail0;
  348. }
  349. // get data pointer and length
  350. const char *data = NCDVal_StringData(data_arg);
  351. size_t length = NCDVal_StringLength(data_arg);
  352. while (length > 0) {
  353. // write
  354. size_t written = fwrite(data, 1, length, open_inst->fh);
  355. if (written == 0) {
  356. ModuleLog(i, BLOG_ERROR, "fwrite failed");
  357. trigger_error(open_inst);
  358. return;
  359. }
  360. ASSERT(written <= length)
  361. // update writing state
  362. data += written;
  363. length -= written;
  364. }
  365. // go up
  366. NCDModuleInst_Backend_Up(i);
  367. return;
  368. fail0:
  369. NCDModuleInst_Backend_DeadError(i);
  370. }
  371. static void seek_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  372. {
  373. // check arguments
  374. NCDValRef position_arg;
  375. NCDValRef whence_arg;
  376. if (!NCDVal_ListRead(params->args, 2, &position_arg, &whence_arg)) {
  377. ModuleLog(i, BLOG_ERROR, "wrong arity");
  378. goto fail0;
  379. }
  380. if (!NCDVal_IsString(position_arg) || !NCDVal_IsString(whence_arg)) {
  381. ModuleLog(i, BLOG_ERROR, "wrong type");
  382. goto fail0;
  383. }
  384. // parse position
  385. int position_sign;
  386. uintmax_t position_mag;
  387. if (!parse_signmag_integer_bin(NCDVal_StringData(position_arg), NCDVal_StringLength(position_arg), &position_sign, &position_mag)) {
  388. ModuleLog(i, BLOG_ERROR, "wrong position");
  389. goto fail0;
  390. }
  391. // parse whence
  392. int whence;
  393. if (NCDVal_StringEquals(whence_arg, "set")) {
  394. whence = SEEK_SET;
  395. }
  396. else if (NCDVal_StringEquals(whence_arg, "cur")) {
  397. whence = SEEK_CUR;
  398. }
  399. else if (NCDVal_StringEquals(whence_arg, "end")) {
  400. whence = SEEK_END;
  401. }
  402. else {
  403. ModuleLog(i, BLOG_ERROR, "wrong whence");
  404. goto fail0;
  405. }
  406. // determine min/max values of off_t (non-portable hack)
  407. off_t off_t_min = (sizeof(off_t) == 8 ? INT64_MIN : INT32_MIN);
  408. off_t off_t_max = (sizeof(off_t) == 8 ? INT64_MAX : INT32_MAX);
  409. // compute position as off_t
  410. off_t position;
  411. if (position_sign < 0 && position_mag > 0) {
  412. if (position_mag - 1 > -(off_t_min + 1)) {
  413. ModuleLog(i, BLOG_ERROR, "position underflow");
  414. goto fail0;
  415. }
  416. position = -(off_t)(position_mag - 1) - 1;
  417. } else {
  418. if (position_mag > off_t_max) {
  419. ModuleLog(i, BLOG_ERROR, "position overflow");
  420. goto fail0;
  421. }
  422. position = position_mag;
  423. }
  424. // get open instance
  425. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  426. // make sure it's not in error
  427. if (!open_inst->fh) {
  428. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  429. goto fail0;
  430. }
  431. // seek
  432. if (fseeko(open_inst->fh, position, whence) < 0) {
  433. ModuleLog(i, BLOG_ERROR, "fseeko failed");
  434. trigger_error(open_inst);
  435. return;
  436. }
  437. // go up
  438. NCDModuleInst_Backend_Up(i);
  439. return;
  440. fail0:
  441. NCDModuleInst_Backend_DeadError(i);
  442. }
  443. static void close_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  444. {
  445. // check arguments
  446. if (!NCDVal_ListRead(params->args, 0)) {
  447. ModuleLog(i, BLOG_ERROR, "wrong arity");
  448. goto fail0;
  449. }
  450. // get open instance
  451. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  452. // make sure it's not in error
  453. if (!open_inst->fh) {
  454. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  455. goto fail0;
  456. }
  457. // close
  458. int res = fclose(open_inst->fh);
  459. open_inst->fh = NULL;
  460. if (res != 0) {
  461. ModuleLog(i, BLOG_ERROR, "fclose failed");
  462. trigger_error(open_inst);
  463. return;
  464. }
  465. // go up
  466. NCDModuleInst_Backend_Up(i);
  467. return;
  468. fail0:
  469. NCDModuleInst_Backend_DeadError(i);
  470. }
  471. static struct NCDModule modules[] = {
  472. {
  473. .type = "file_open",
  474. .func_new2 = open_func_new,
  475. .func_die = open_func_die,
  476. .func_getvar2 = open_func_getvar,
  477. .alloc_size = sizeof(struct open_instance)
  478. }, {
  479. .type = "file_open::read",
  480. .func_new2 = read_func_new,
  481. .func_die = read_func_die,
  482. .func_getvar2 = read_func_getvar,
  483. .alloc_size = sizeof(struct read_instance)
  484. }, {
  485. .type = "file_open::write",
  486. .func_new2 = write_func_new,
  487. }, {
  488. .type = "file_open::seek",
  489. .func_new2 = seek_func_new,
  490. }, {
  491. .type = "file_open::close",
  492. .func_new2 = close_func_new,
  493. }, {
  494. .type = NULL
  495. }
  496. };
  497. const struct NCDModuleGroup ncdmodule_file_open = {
  498. .modules = modules
  499. };