file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * @file file.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. * File I/O module.
  32. *
  33. * Synopsis:
  34. * file_read(string filename)
  35. *
  36. * Variables:
  37. * string (empty) - file contents
  38. *
  39. * Description:
  40. * Reads the contents of a file. Reports an error if something goes wrong.
  41. * WARNING: this uses fopen/fread/fclose, blocking the entire interpreter while
  42. * the file is being read. For this reason, you should only use this
  43. * to read small local files which will be read quickly, and especially
  44. * not files on network mounts.
  45. *
  46. * Synopsis:
  47. * file_write(string filename, string contents)
  48. *
  49. * Description:
  50. * Writes a file, possibly overwriting an existing one. Reports an error if something
  51. * goes wrong.
  52. * WARNING: this is not an atomic operation; other programs may see the file in an
  53. * inconsistent state while it is being written. Similarly, if writing
  54. * fails, the file may remain in an inconsistent state indefinitely.
  55. * If this is a problem, you should write the new contents to a temporary
  56. * file and rename this temporary file to the live file.
  57. * WARNING: this uses fopen/fwrite/fclose, blocking the entire interpreter while
  58. * the file is being written. For this reason, you should only use this
  59. * to write small local files which will be written quickly, and especially
  60. * not files on network mounts.
  61. *
  62. * Synopsis:
  63. * file_stat(string filename)
  64. * file_lstat(string filename)
  65. *
  66. * Description:
  67. * Retrieves information about a file.
  68. * file_stat() follows symlinks; file_lstat() does not and allows retrieving information
  69. * about a symlink.
  70. * WARNING: this blocks the interpreter
  71. *
  72. * Variables:
  73. * succeeded - whether the stat operation succeeded (true/false). If false, all other
  74. * variables obtain the value "failed".
  75. * type - file, dir, chr, blk, fifo, link, socket, other, failed
  76. * size - size of the file, or failed
  77. */
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <stdint.h>
  81. #include <sys/types.h>
  82. #include <sys/stat.h>
  83. #include <unistd.h>
  84. #include <misc/read_file.h>
  85. #include <misc/write_file.h>
  86. #include <misc/parse_number.h>
  87. #include <ncd/module_common.h>
  88. #include <generated/blog_channel_ncd_file.h>
  89. struct read_instance {
  90. NCDModuleInst *i;
  91. uint8_t *file_data;
  92. size_t file_len;
  93. };
  94. struct stat_instance {
  95. NCDModuleInst *i;
  96. int succeeded;
  97. struct stat result;
  98. };
  99. static void read_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  100. {
  101. struct read_instance *o = vo;
  102. o->i = i;
  103. // read arguments
  104. NCDValRef filename_arg;
  105. if (!NCDVal_ListRead(params->args, 1, &filename_arg)) {
  106. ModuleLog(i, BLOG_ERROR, "wrong arity");
  107. goto fail0;
  108. }
  109. if (!NCDVal_IsStringNoNulls(filename_arg)) {
  110. ModuleLog(i, BLOG_ERROR, "wrong type");
  111. goto fail0;
  112. }
  113. // get null terminated name
  114. NCDValNullTermString filename_nts;
  115. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  116. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  117. goto fail0;
  118. }
  119. // read file
  120. int res = read_file(filename_nts.data, &o->file_data, &o->file_len);
  121. NCDValNullTermString_Free(&filename_nts);
  122. if (!res) {
  123. ModuleLog(i, BLOG_ERROR, "failed to read file");
  124. goto fail0;
  125. }
  126. // signal up
  127. NCDModuleInst_Backend_Up(i);
  128. return;
  129. fail0:
  130. NCDModuleInst_Backend_DeadError(i);
  131. }
  132. static void read_func_die (void *vo)
  133. {
  134. struct read_instance *o = vo;
  135. // free data
  136. free(o->file_data);
  137. NCDModuleInst_Backend_Dead(o->i);
  138. }
  139. static int read_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  140. {
  141. struct read_instance *o = vo;
  142. if (name == NCD_STRING_EMPTY) {
  143. *out = NCDVal_NewStringBin(mem, o->file_data, o->file_len);
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. static void write_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  149. {
  150. // read arguments
  151. NCDValRef filename_arg;
  152. NCDValRef contents_arg;
  153. if (!NCDVal_ListRead(params->args, 2, &filename_arg, &contents_arg)) {
  154. ModuleLog(i, BLOG_ERROR, "wrong arity");
  155. goto fail0;
  156. }
  157. if (!NCDVal_IsStringNoNulls(filename_arg) || !NCDVal_IsString(contents_arg)) {
  158. ModuleLog(i, BLOG_ERROR, "wrong type");
  159. goto fail0;
  160. }
  161. // get null terminated name
  162. NCDValNullTermString filename_nts;
  163. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  164. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  165. goto fail0;
  166. }
  167. // write file
  168. MemRef contents_mr = NCDVal_StringMemRef(contents_arg);
  169. int res = write_file(filename_nts.data, (uint8_t const *)contents_mr.ptr, contents_mr.len);
  170. NCDValNullTermString_Free(&filename_nts);
  171. if (!res) {
  172. ModuleLog(i, BLOG_ERROR, "failed to write file");
  173. goto fail0;
  174. }
  175. // signal up
  176. NCDModuleInst_Backend_Up(i);
  177. return;
  178. fail0:
  179. NCDModuleInst_Backend_DeadError(i);
  180. }
  181. static void stat_func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_lstat)
  182. {
  183. struct stat_instance *o = vo;
  184. o->i = i;
  185. NCDValRef filename_arg;
  186. if (!NCDVal_ListRead(params->args, 1, &filename_arg)) {
  187. ModuleLog(i, BLOG_ERROR, "wrong arity");
  188. goto fail0;
  189. }
  190. if (!NCDVal_IsString(filename_arg)) {
  191. ModuleLog(i, BLOG_ERROR, "wrong type");
  192. goto fail0;
  193. }
  194. o->succeeded = 0;
  195. if (!NCDVal_IsStringNoNulls(filename_arg)) {
  196. goto out;
  197. }
  198. // null terminate filename
  199. NCDValNullTermString filename_nts;
  200. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  201. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  202. goto fail0;
  203. }
  204. int res;
  205. if (is_lstat) {
  206. res = lstat(filename_nts.data, &o->result);
  207. } else {
  208. res = stat(filename_nts.data, &o->result);
  209. }
  210. NCDValNullTermString_Free(&filename_nts);
  211. if (res < 0) {
  212. goto out;
  213. }
  214. o->succeeded = 1;
  215. out:
  216. NCDModuleInst_Backend_Up(i);
  217. return;
  218. fail0:
  219. NCDModuleInst_Backend_DeadError(i);
  220. }
  221. static void stat_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  222. {
  223. stat_func_new_common(vo, i, params, 0);
  224. }
  225. static void lstat_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  226. {
  227. stat_func_new_common(vo, i, params, 1);
  228. }
  229. static int stat_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  230. {
  231. struct stat_instance *o = vo;
  232. if (name == NCD_STRING_SUCCEEDED) {
  233. *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
  234. return 1;
  235. }
  236. if (name == NCD_STRING_TYPE) {
  237. const char *str;
  238. if (!o->succeeded) {
  239. str = "failed";
  240. } else if (S_ISREG(o->result.st_mode)) {
  241. str = "file";
  242. } else if (S_ISDIR(o->result.st_mode)) {
  243. str = "dir";
  244. } else if (S_ISCHR(o->result.st_mode)) {
  245. str = "chr";
  246. } else if (S_ISBLK(o->result.st_mode)) {
  247. str = "blk";
  248. } else if (S_ISFIFO(o->result.st_mode)) {
  249. str = "fifo";
  250. } else if (S_ISLNK(o->result.st_mode)) {
  251. str = "link";
  252. } else if (S_ISSOCK(o->result.st_mode)) {
  253. str = "socket";
  254. } else {
  255. str = "other";
  256. }
  257. *out = NCDVal_NewString(mem, str);
  258. return 1;
  259. }
  260. if (name == NCD_STRING_SIZE) {
  261. char str[50];
  262. if (!o->succeeded) {
  263. strcpy(str, "failed");
  264. } else {
  265. generate_decimal_repr_string((uintmax_t)o->result.st_size, str);
  266. }
  267. *out = NCDVal_NewString(mem, str);
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. static struct NCDModule modules[] = {
  273. {
  274. .type = "file_read",
  275. .func_new2 = read_func_new,
  276. .func_die = read_func_die,
  277. .func_getvar2 = read_func_getvar2,
  278. .alloc_size = sizeof(struct read_instance)
  279. }, {
  280. .type = "file_write",
  281. .func_new2 = write_func_new
  282. }, {
  283. .type = "file_stat",
  284. .func_new2 = stat_func_new,
  285. .func_getvar2 = stat_func_getvar2,
  286. .alloc_size = sizeof(struct stat_instance)
  287. }, {
  288. .type = "file_lstat",
  289. .func_new2 = lstat_func_new,
  290. .func_getvar2 = stat_func_getvar2,
  291. .alloc_size = sizeof(struct stat_instance)
  292. }, {
  293. .type = NULL
  294. }
  295. };
  296. const struct NCDModuleGroup ncdmodule_file = {
  297. .modules = modules
  298. };