file_open.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 (b_cstring cstr, char *out)
  125. {
  126. size_t pos = 0;
  127. size_t left = cstr.length;
  128. if (left == 0) {
  129. return 0;
  130. }
  131. switch (b_cstring_at(cstr, pos)) {
  132. case 'r':
  133. case 'w':
  134. case 'a':
  135. *out++ = b_cstring_at(cstr, pos);
  136. pos++;
  137. left--;
  138. break;
  139. default:
  140. return 0;
  141. }
  142. if (left == 0) {
  143. goto finish;
  144. }
  145. switch (b_cstring_at(cstr, pos)) {
  146. case '+':
  147. *out++ = b_cstring_at(cstr, pos);
  148. pos++;
  149. left--;
  150. break;
  151. default:
  152. return 0;
  153. }
  154. if (left == 0) {
  155. goto finish;
  156. }
  157. return 0;
  158. finish:
  159. *out = '\0';
  160. return 1;
  161. }
  162. static void trigger_error (struct open_instance *o)
  163. {
  164. if (o->fh) {
  165. // close file
  166. if (fclose(o->fh) != 0) {
  167. ModuleLog(o->i, BLOG_ERROR, "fclose failed");
  168. }
  169. // set no file, indicating error
  170. o->fh = NULL;
  171. }
  172. // go down and up
  173. NCDModuleInst_Backend_Down(o->i);
  174. NCDModuleInst_Backend_Up(o->i);
  175. }
  176. static void open_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  177. {
  178. struct open_instance *o = vo;
  179. o->i = i;
  180. // check arguments
  181. NCDValRef filename_arg;
  182. NCDValRef mode_arg;
  183. NCDValRef options_arg = NCDVal_NewInvalid();
  184. if (!NCDVal_ListRead(params->args, 2, &filename_arg, &mode_arg) &&
  185. !NCDVal_ListRead(params->args, 3, &filename_arg, &mode_arg, &options_arg)
  186. ) {
  187. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  188. goto fail0;
  189. }
  190. if (!NCDVal_IsStringNoNulls(filename_arg) || !NCDVal_IsString(mode_arg) ||
  191. (!NCDVal_IsInvalid(options_arg) && !NCDVal_IsMap(options_arg))
  192. ) {
  193. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  194. goto fail0;
  195. }
  196. // check mode
  197. char mode[5];
  198. if (!parse_mode(NCDVal_StringCstring(mode_arg), mode)) {
  199. ModuleLog(o->i, BLOG_ERROR, "wrong mode");
  200. goto fail0;
  201. }
  202. size_t read_size_opt = READ_BUF_SIZE;
  203. // parse options
  204. if (!NCDVal_IsInvalid(options_arg)) {
  205. int num_recognized = 0;
  206. NCDValRef value;
  207. if (!NCDVal_IsInvalid(value = NCDVal_MapGetValue(options_arg, "read_size"))) {
  208. uintmax_t read_size;
  209. if (!NCDVal_IsString(value) || !ncd_read_uintmax(value, &read_size) || read_size > SIZE_MAX || read_size == 0) {
  210. ModuleLog(o->i, BLOG_ERROR, "wrong read_size");
  211. goto fail0;
  212. }
  213. num_recognized++;
  214. read_size_opt = read_size;
  215. }
  216. if (NCDVal_MapCount(options_arg) > num_recognized) {
  217. ModuleLog(o->i, BLOG_ERROR, "unrecognized options present");
  218. goto fail0;
  219. }
  220. }
  221. // init store
  222. NCDBufStore_Init(&o->store, read_size_opt);
  223. // null terminate filename
  224. NCDValNullTermString filename_nts;
  225. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  226. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  227. goto fail1;
  228. }
  229. // open file
  230. o->fh = fopen(filename_nts.data, mode);
  231. NCDValNullTermString_Free(&filename_nts);
  232. if (!o->fh) {
  233. ModuleLog(o->i, BLOG_ERROR, "fopen failed");
  234. }
  235. // go up
  236. NCDModuleInst_Backend_Up(i);
  237. return;
  238. fail1:
  239. NCDBufStore_Free(&o->store);
  240. fail0:
  241. NCDModuleInst_Backend_DeadError(i);
  242. }
  243. static void open_func_die (void *vo)
  244. {
  245. struct open_instance *o = vo;
  246. // close file
  247. if (o->fh) {
  248. if (fclose(o->fh) != 0) {
  249. ModuleLog(o->i, BLOG_ERROR, "fclose failed");
  250. }
  251. }
  252. // free store
  253. NCDBufStore_Free(&o->store);
  254. NCDModuleInst_Backend_Dead(o->i);
  255. }
  256. static int open_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  257. {
  258. struct open_instance *o = vo;
  259. if (name == NCD_STRING_IS_ERROR) {
  260. *out = ncd_make_boolean(mem, !o->fh, o->i->params->iparams->string_index);
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. static void read_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  266. {
  267. struct read_instance *o = vo;
  268. o->i = i;
  269. // check arguments
  270. if (!NCDVal_ListRead(params->args, 0)) {
  271. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  272. goto fail0;
  273. }
  274. // get open instance
  275. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  276. // make sure it's not in error
  277. if (!open_inst->fh) {
  278. ModuleLog(o->i, BLOG_ERROR, "open instance is in error");
  279. goto fail0;
  280. }
  281. // get buffer
  282. o->buf = NCDBufStore_GetBuf(&open_inst->store);
  283. if (!o->buf) {
  284. ModuleLog(o->i, BLOG_ERROR, "NCDBufStore_GetBuf failed");
  285. goto fail0;
  286. }
  287. // starting with empty buffer
  288. char *data = NCDBuf_Data(o->buf);
  289. size_t buf_size = NCDBufStore_BufSize(&open_inst->store);
  290. o->length = 0;
  291. while (o->length < buf_size) {
  292. // read
  293. size_t readed = fread(data + o->length, 1, buf_size - o->length, open_inst->fh);
  294. if (readed == 0) {
  295. break;
  296. }
  297. ASSERT(readed <= buf_size - o->length)
  298. // increment length
  299. o->length += readed;
  300. }
  301. // if we couldn't read anything due to an error, trigger
  302. // error in the open instance, and don't go up
  303. if (o->length == 0 && !feof(open_inst->fh)) {
  304. ModuleLog(o->i, BLOG_ERROR, "fread failed");
  305. trigger_error(open_inst);
  306. return;
  307. }
  308. // go up
  309. NCDModuleInst_Backend_Up(i);
  310. return;
  311. fail0:
  312. NCDModuleInst_Backend_DeadError(i);
  313. }
  314. static void read_func_die (void *vo)
  315. {
  316. struct read_instance *o = vo;
  317. // release buffer
  318. NCDRefTarget_Deref(NCDBuf_RefTarget(o->buf));
  319. NCDModuleInst_Backend_Dead(o->i);
  320. }
  321. static int read_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  322. {
  323. struct read_instance *o = vo;
  324. if (name == NCD_STRING_EMPTY) {
  325. *out = NCDVal_NewExternalString(mem, NCDBuf_Data(o->buf), o->length, NCDBuf_RefTarget(o->buf));
  326. return 1;
  327. }
  328. if (name == NCD_STRING_NOT_EOF) {
  329. *out = ncd_make_boolean(mem, (o->length != 0), o->i->params->iparams->string_index);
  330. return 1;
  331. }
  332. return 0;
  333. }
  334. static void write_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  335. {
  336. // check arguments
  337. NCDValRef data_arg;
  338. if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
  339. ModuleLog(i, BLOG_ERROR, "wrong arity");
  340. goto fail0;
  341. }
  342. if (!NCDVal_IsString(data_arg)) {
  343. ModuleLog(i, BLOG_ERROR, "wrong type");
  344. goto fail0;
  345. }
  346. // get open instance
  347. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  348. // make sure it's not in error
  349. if (!open_inst->fh) {
  350. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  351. goto fail0;
  352. }
  353. // write all the data
  354. b_cstring data_cstr = NCDVal_StringCstring(data_arg);
  355. B_CSTRING_LOOP(data_cstr, pos, chunk_data, chunk_length, {
  356. size_t chunk_pos = 0;
  357. while (chunk_pos < chunk_length) {
  358. size_t written = fwrite(chunk_data + chunk_pos, 1, chunk_length - chunk_pos, open_inst->fh);
  359. if (written == 0) {
  360. ModuleLog(i, BLOG_ERROR, "fwrite failed");
  361. trigger_error(open_inst);
  362. return;
  363. }
  364. ASSERT(written < chunk_length - chunk_pos)
  365. chunk_pos += written;
  366. }
  367. })
  368. // go up
  369. NCDModuleInst_Backend_Up(i);
  370. return;
  371. fail0:
  372. NCDModuleInst_Backend_DeadError(i);
  373. }
  374. static void seek_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  375. {
  376. // check arguments
  377. NCDValRef position_arg;
  378. NCDValRef whence_arg;
  379. if (!NCDVal_ListRead(params->args, 2, &position_arg, &whence_arg)) {
  380. ModuleLog(i, BLOG_ERROR, "wrong arity");
  381. goto fail0;
  382. }
  383. if (!NCDVal_IsString(position_arg) || !NCDVal_IsString(whence_arg)) {
  384. ModuleLog(i, BLOG_ERROR, "wrong type");
  385. goto fail0;
  386. }
  387. // parse position
  388. int position_sign;
  389. uintmax_t position_mag;
  390. b_cstring position_cstr = NCDVal_StringCstring(position_arg);
  391. if (!parse_signmag_integer_cstr(position_cstr, 0, position_cstr.length, &position_sign, &position_mag)) {
  392. ModuleLog(i, BLOG_ERROR, "wrong position");
  393. goto fail0;
  394. }
  395. // parse whence
  396. int whence;
  397. if (NCDVal_StringEquals(whence_arg, "set")) {
  398. whence = SEEK_SET;
  399. }
  400. else if (NCDVal_StringEquals(whence_arg, "cur")) {
  401. whence = SEEK_CUR;
  402. }
  403. else if (NCDVal_StringEquals(whence_arg, "end")) {
  404. whence = SEEK_END;
  405. }
  406. else {
  407. ModuleLog(i, BLOG_ERROR, "wrong whence");
  408. goto fail0;
  409. }
  410. // determine min/max values of off_t (non-portable hack)
  411. off_t off_t_min = (sizeof(off_t) == 8 ? INT64_MIN : INT32_MIN);
  412. off_t off_t_max = (sizeof(off_t) == 8 ? INT64_MAX : INT32_MAX);
  413. // compute position as off_t
  414. off_t position;
  415. if (position_sign < 0 && position_mag > 0) {
  416. if (position_mag - 1 > -(off_t_min + 1)) {
  417. ModuleLog(i, BLOG_ERROR, "position underflow");
  418. goto fail0;
  419. }
  420. position = -(off_t)(position_mag - 1) - 1;
  421. } else {
  422. if (position_mag > off_t_max) {
  423. ModuleLog(i, BLOG_ERROR, "position overflow");
  424. goto fail0;
  425. }
  426. position = position_mag;
  427. }
  428. // get open instance
  429. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  430. // make sure it's not in error
  431. if (!open_inst->fh) {
  432. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  433. goto fail0;
  434. }
  435. // seek
  436. if (fseeko(open_inst->fh, position, whence) < 0) {
  437. ModuleLog(i, BLOG_ERROR, "fseeko failed");
  438. trigger_error(open_inst);
  439. return;
  440. }
  441. // go up
  442. NCDModuleInst_Backend_Up(i);
  443. return;
  444. fail0:
  445. NCDModuleInst_Backend_DeadError(i);
  446. }
  447. static void close_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  448. {
  449. // check arguments
  450. if (!NCDVal_ListRead(params->args, 0)) {
  451. ModuleLog(i, BLOG_ERROR, "wrong arity");
  452. goto fail0;
  453. }
  454. // get open instance
  455. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  456. // make sure it's not in error
  457. if (!open_inst->fh) {
  458. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  459. goto fail0;
  460. }
  461. // close
  462. int res = fclose(open_inst->fh);
  463. open_inst->fh = NULL;
  464. if (res != 0) {
  465. ModuleLog(i, BLOG_ERROR, "fclose failed");
  466. trigger_error(open_inst);
  467. return;
  468. }
  469. // go up
  470. NCDModuleInst_Backend_Up(i);
  471. return;
  472. fail0:
  473. NCDModuleInst_Backend_DeadError(i);
  474. }
  475. static struct NCDModule modules[] = {
  476. {
  477. .type = "file_open",
  478. .func_new2 = open_func_new,
  479. .func_die = open_func_die,
  480. .func_getvar2 = open_func_getvar,
  481. .alloc_size = sizeof(struct open_instance),
  482. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  483. }, {
  484. .type = "file_open::read",
  485. .func_new2 = read_func_new,
  486. .func_die = read_func_die,
  487. .func_getvar2 = read_func_getvar,
  488. .alloc_size = sizeof(struct read_instance),
  489. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  490. }, {
  491. .type = "file_open::write",
  492. .func_new2 = write_func_new,
  493. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  494. }, {
  495. .type = "file_open::seek",
  496. .func_new2 = seek_func_new,
  497. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  498. }, {
  499. .type = "file_open::close",
  500. .func_new2 = close_func_new,
  501. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  502. }, {
  503. .type = NULL
  504. }
  505. };
  506. const struct NCDModuleGroup ncdmodule_file_open = {
  507. .modules = modules
  508. };