file_open.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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)
  33. *
  34. * Variables:
  35. * string is_error - "true" if the file_open object is in error state, "false"
  36. * otherwise
  37. *
  38. * Description:
  39. * Opens a file for subsequent reading or writing. The 'mode' argument must
  40. * be one of: "r", "w", "a", "r+", "w+", "a+"; it corresponds to the mode string
  41. * that will be passed to the fopen() function.
  42. * When the file_open() statement goes up, the error state is set depending on
  43. * whether opening succeeded or failed. The 'is_error' variable should be used
  44. * to check the error state.
  45. * If an error occurs afterward within read(), write() or seek(), the error state
  46. * is set, and the file_open() statement is toggled down and back up. This way,
  47. * the same piece of user code can handle all file errors.
  48. *
  49. * Synopsis:
  50. * file_open::read()
  51. *
  52. * Variables:
  53. * string (empty) - the data which was read, or an empty string if EOF was reached
  54. * string is_eof - "true" if EOF was reached, "false" if not
  55. * string not_eof - "false" if EOF was reached, "true" if not
  56. *
  57. * Description:
  58. * Reads data from an opened file. The file must not be in error state.
  59. * If reading fails, this statement will never go up, the error state of the
  60. * file_open() statement will be set, and the file_open() statement will trigger
  61. * backtracking (go down and up).
  62. *
  63. * Synopsis:
  64. * file_open::write(string data)
  65. *
  66. * Description:
  67. * Writes data to an opened file. The file must not be in error state.
  68. * If writing fails, this statement will never go up, the error state of the
  69. * file_open() statement will be set, and the file_open() statement will trigger
  70. * backtracking (go down and up).
  71. *
  72. * Synopsis:
  73. * file_open::seek(string position, string whence)
  74. *
  75. * Description:
  76. * Sets the file position indicator. The 'position' argument must be a possibly
  77. * negative decimal number, and is interpreted relative to 'whence'. Here, 'whence'
  78. * may be one of:
  79. * - "set", meaning beginning of file,
  80. * - "cur", meaning the current position, and
  81. * - "end", meaning the end of file.
  82. * Errors are handled as in read() and write(). Note that if the position argument
  83. * is too small or too large to convert to off_t, this is not a seek error, and only
  84. * the seek command will fail.
  85. *
  86. * Synopsis:
  87. * file_open::close()
  88. *
  89. * Description:
  90. * Closes the file. The file must not be in error state.
  91. * Errors are handled as handled as in read() and write(), i.e. the process is
  92. * backtracked to file_open() with the error state set.
  93. * On success, the error state of the file is set (but without backtracking), and
  94. * the close() statement goes up .
  95. */
  96. #include <stddef.h>
  97. #include <stdio.h>
  98. #include <stdint.h>
  99. #include <limits.h>
  100. #include <misc/debug.h>
  101. #include <misc/balloc.h>
  102. #include <misc/parse_number.h>
  103. #include <ncd/NCDModule.h>
  104. #include <ncd/static_strings.h>
  105. #include <ncd/value_utils.h>
  106. #include <generated/blog_channel_ncd_file_open.h>
  107. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  108. #define START_READ_SIZE 512
  109. #define MAX_READ_SIZE 8192
  110. struct open_instance {
  111. NCDModuleInst *i;
  112. FILE *fh;
  113. };
  114. struct read_instance {
  115. NCDModuleInst *i;
  116. char *data;
  117. size_t length;
  118. };
  119. enum {STRING_IS_ERROR, STRING_IS_EOF, STRING_NOT_EOF};
  120. static struct NCD_string_request strings[] = {
  121. {"is_error"}, {"is_eof"}, {"not_eof"}, {NULL}
  122. };
  123. static int parse_mode (const char *data, char *out)
  124. {
  125. switch (*data) {
  126. case 'r':
  127. case 'w':
  128. case 'a':
  129. *out++ = *data++;
  130. break;
  131. default:
  132. return 0;
  133. }
  134. switch (*data) {
  135. case '\0':
  136. goto finish;
  137. case '+':
  138. *out++ = *data++;
  139. break;
  140. default:
  141. return 0;
  142. }
  143. switch (*data) {
  144. case '\0':
  145. goto finish;
  146. default:
  147. return 0;
  148. }
  149. finish:
  150. *out = '\0';
  151. return 1;
  152. }
  153. static void trigger_error (struct open_instance *o)
  154. {
  155. if (o->fh) {
  156. // close file
  157. if (fclose(o->fh) != 0) {
  158. ModuleLog(o->i, BLOG_ERROR, "fclose failed");
  159. }
  160. // set no file, indicating error
  161. o->fh = NULL;
  162. }
  163. // go down and up
  164. NCDModuleInst_Backend_Down(o->i);
  165. NCDModuleInst_Backend_Up(o->i);
  166. }
  167. static void open_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  168. {
  169. struct open_instance *o = vo;
  170. o->i = i;
  171. // check arguments
  172. NCDValRef filename_arg;
  173. NCDValRef mode_arg;
  174. if (!NCDVal_ListRead(params->args, 2, &filename_arg, &mode_arg)) {
  175. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  176. goto fail0;
  177. }
  178. if (!NCDVal_IsStringNoNulls(filename_arg) || !NCDVal_IsStringNoNulls(mode_arg)) {
  179. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  180. goto fail0;
  181. }
  182. // check mode
  183. char mode[5];
  184. if (!parse_mode(NCDVal_StringValue(mode_arg), mode)) {
  185. ModuleLog(o->i, BLOG_ERROR, "wrong mode");
  186. goto fail0;
  187. }
  188. // open file
  189. o->fh = fopen(NCDVal_StringValue(filename_arg), mode);
  190. if (!o->fh) {
  191. ModuleLog(o->i, BLOG_ERROR, "fopen failed");
  192. }
  193. // go up
  194. NCDModuleInst_Backend_Up(i);
  195. return;
  196. fail0:
  197. NCDModuleInst_Backend_SetError(i);
  198. NCDModuleInst_Backend_Dead(i);
  199. }
  200. static void open_func_die (void *vo)
  201. {
  202. struct open_instance *o = vo;
  203. // close file
  204. if (o->fh) {
  205. if (fclose(o->fh) != 0) {
  206. ModuleLog(o->i, BLOG_ERROR, "fclose failed");
  207. }
  208. }
  209. NCDModuleInst_Backend_Dead(o->i);
  210. }
  211. static int open_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  212. {
  213. struct open_instance *o = vo;
  214. if (name == strings[STRING_IS_ERROR].id) {
  215. *out = ncd_make_boolean(mem, !o->fh, o->i->params->iparams->string_index);
  216. if (NCDVal_IsInvalid(*out)) {
  217. ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
  218. }
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. static void read_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  224. {
  225. struct read_instance *o = vo;
  226. o->i = i;
  227. // check arguments
  228. if (!NCDVal_ListRead(params->args, 0)) {
  229. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  230. goto fail0;
  231. }
  232. // get open instance
  233. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  234. // make sure it's not in error
  235. if (!open_inst->fh) {
  236. ModuleLog(o->i, BLOG_ERROR, "open instance is in error");
  237. goto fail0;
  238. }
  239. // allocate buffer
  240. size_t capacity = START_READ_SIZE;
  241. o->data = BAlloc(capacity);
  242. if (!o->data) {
  243. ModuleLog(o->i, BLOG_ERROR, "BAlloc failed");
  244. goto fail0;
  245. }
  246. // starting with empty buffer
  247. o->length = 0;
  248. while (1) {
  249. // read
  250. size_t readed = fread(o->data + o->length, 1, capacity - o->length, open_inst->fh);
  251. if (readed == 0) {
  252. break;
  253. }
  254. ASSERT(readed <= capacity - o->length)
  255. // increment length
  256. o->length += readed;
  257. if (o->length == capacity) {
  258. // do not reallocate beyond limit
  259. if (capacity > MAX_READ_SIZE / 2) {
  260. break;
  261. }
  262. // reallocate buffer
  263. capacity *= 2;
  264. char *new_data = BRealloc(o->data, capacity);
  265. if (!new_data) {
  266. ModuleLog(o->i, BLOG_ERROR, "BRealloc failed");
  267. goto fail1;
  268. }
  269. o->data = new_data;
  270. }
  271. }
  272. if (o->length == 0) {
  273. // free buffer
  274. BFree(o->data);
  275. o->data = NULL;
  276. // if we couldn't read anything due to an error, trigger
  277. // error in the open instance, and don't go up
  278. if (!feof(open_inst->fh)) {
  279. ModuleLog(o->i, BLOG_ERROR, "fread failed");
  280. trigger_error(open_inst);
  281. return;
  282. }
  283. }
  284. // go up
  285. NCDModuleInst_Backend_Up(i);
  286. return;
  287. fail1:
  288. BFree(o->data);
  289. fail0:
  290. NCDModuleInst_Backend_SetError(i);
  291. NCDModuleInst_Backend_Dead(i);
  292. }
  293. static void read_func_die (void *vo)
  294. {
  295. struct read_instance *o = vo;
  296. // free buffer
  297. if (o->data) {
  298. BFree(o->data);
  299. }
  300. NCDModuleInst_Backend_Dead(o->i);
  301. }
  302. static int read_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  303. {
  304. struct read_instance *o = vo;
  305. if (name == NCD_STRING_EMPTY) {
  306. const char *data = (!o->data ? "" : o->data);
  307. *out = NCDVal_NewStringBin(mem, (const uint8_t *)data, o->length);
  308. if (NCDVal_IsInvalid(*out)) {
  309. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewStringBin failed");
  310. }
  311. return 1;
  312. }
  313. if (name == strings[STRING_IS_EOF].id) {
  314. *out = ncd_make_boolean(mem, (o->length == 0), o->i->params->iparams->string_index);
  315. if (NCDVal_IsInvalid(*out)) {
  316. ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
  317. }
  318. return 1;
  319. }
  320. if (name == strings[STRING_NOT_EOF].id) {
  321. *out = ncd_make_boolean(mem, (o->length != 0), o->i->params->iparams->string_index);
  322. if (NCDVal_IsInvalid(*out)) {
  323. ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
  324. }
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. static void write_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  330. {
  331. // check arguments
  332. NCDValRef data_arg;
  333. if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
  334. ModuleLog(i, BLOG_ERROR, "wrong arity");
  335. goto fail0;
  336. }
  337. if (!NCDVal_IsString(data_arg)) {
  338. ModuleLog(i, BLOG_ERROR, "wrong type");
  339. goto fail0;
  340. }
  341. // get open instance
  342. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  343. // make sure it's not in error
  344. if (!open_inst->fh) {
  345. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  346. goto fail0;
  347. }
  348. // get data pointer and length
  349. const char *data = NCDVal_StringValue(data_arg);
  350. size_t length = NCDVal_StringLength(data_arg);
  351. while (length > 0) {
  352. // write
  353. size_t written = fwrite(data, 1, length, open_inst->fh);
  354. if (written == 0) {
  355. ModuleLog(i, BLOG_ERROR, "fwrite failed");
  356. trigger_error(open_inst);
  357. return;
  358. }
  359. ASSERT(written <= length)
  360. // update writing state
  361. data += written;
  362. length -= written;
  363. }
  364. // go up
  365. NCDModuleInst_Backend_Up(i);
  366. return;
  367. fail0:
  368. NCDModuleInst_Backend_SetError(i);
  369. NCDModuleInst_Backend_Dead(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_StringValue(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_SetError(i);
  442. NCDModuleInst_Backend_Dead(i);
  443. }
  444. static void close_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  445. {
  446. // check arguments
  447. if (!NCDVal_ListRead(params->args, 0)) {
  448. ModuleLog(i, BLOG_ERROR, "wrong arity");
  449. goto fail0;
  450. }
  451. // get open instance
  452. struct open_instance *open_inst = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  453. // make sure it's not in error
  454. if (!open_inst->fh) {
  455. ModuleLog(i, BLOG_ERROR, "open instance is in error");
  456. goto fail0;
  457. }
  458. // close
  459. int res = fclose(open_inst->fh);
  460. open_inst->fh = NULL;
  461. if (res != 0) {
  462. ModuleLog(i, BLOG_ERROR, "fclose failed");
  463. trigger_error(open_inst);
  464. return;
  465. }
  466. // go up
  467. NCDModuleInst_Backend_Up(i);
  468. return;
  469. fail0:
  470. NCDModuleInst_Backend_SetError(i);
  471. NCDModuleInst_Backend_Dead(i);
  472. }
  473. static struct NCDModule modules[] = {
  474. {
  475. .type = "file_open",
  476. .func_new2 = open_func_new,
  477. .func_die = open_func_die,
  478. .func_getvar2 = open_func_getvar,
  479. .alloc_size = sizeof(struct open_instance)
  480. }, {
  481. .type = "file_open::read",
  482. .func_new2 = read_func_new,
  483. .func_die = read_func_die,
  484. .func_getvar2 = read_func_getvar,
  485. .alloc_size = sizeof(struct read_instance)
  486. }, {
  487. .type = "file_open::write",
  488. .func_new2 = write_func_new,
  489. }, {
  490. .type = "file_open::seek",
  491. .func_new2 = seek_func_new,
  492. }, {
  493. .type = "file_open::close",
  494. .func_new2 = close_func_new,
  495. }, {
  496. .type = NULL
  497. }
  498. };
  499. const struct NCDModuleGroup ncdmodule_file_open = {
  500. .modules = modules,
  501. .strings = strings
  502. };