file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/NCDModule.h>
  88. #include <ncd/static_strings.h>
  89. #include <ncd/extra/value_utils.h>
  90. #include <generated/blog_channel_ncd_file.h>
  91. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  92. struct read_instance {
  93. NCDModuleInst *i;
  94. uint8_t *file_data;
  95. size_t file_len;
  96. };
  97. struct stat_instance {
  98. NCDModuleInst *i;
  99. int succeeded;
  100. struct stat result;
  101. };
  102. static void read_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  103. {
  104. struct read_instance *o = vo;
  105. o->i = i;
  106. // read arguments
  107. NCDValRef filename_arg;
  108. if (!NCDVal_ListRead(params->args, 1, &filename_arg)) {
  109. ModuleLog(i, BLOG_ERROR, "wrong arity");
  110. goto fail0;
  111. }
  112. if (!NCDVal_IsStringNoNulls(filename_arg)) {
  113. ModuleLog(i, BLOG_ERROR, "wrong type");
  114. goto fail0;
  115. }
  116. // get null terminated name
  117. NCDValNullTermString filename_nts;
  118. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  119. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  120. goto fail0;
  121. }
  122. // read file
  123. int res = read_file(filename_nts.data, &o->file_data, &o->file_len);
  124. NCDValNullTermString_Free(&filename_nts);
  125. if (!res) {
  126. ModuleLog(i, BLOG_ERROR, "failed to read file");
  127. goto fail0;
  128. }
  129. // signal up
  130. NCDModuleInst_Backend_Up(i);
  131. return;
  132. fail0:
  133. NCDModuleInst_Backend_DeadError(i);
  134. }
  135. static void read_func_die (void *vo)
  136. {
  137. struct read_instance *o = vo;
  138. // free data
  139. free(o->file_data);
  140. NCDModuleInst_Backend_Dead(o->i);
  141. }
  142. static int read_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  143. {
  144. struct read_instance *o = vo;
  145. if (name == NCD_STRING_EMPTY) {
  146. *out = NCDVal_NewStringBin(mem, o->file_data, o->file_len);
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. static void write_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  152. {
  153. // read arguments
  154. NCDValRef filename_arg;
  155. NCDValRef contents_arg;
  156. if (!NCDVal_ListRead(params->args, 2, &filename_arg, &contents_arg)) {
  157. ModuleLog(i, BLOG_ERROR, "wrong arity");
  158. goto fail0;
  159. }
  160. if (!NCDVal_IsStringNoNulls(filename_arg) || !NCDVal_IsString(contents_arg)) {
  161. ModuleLog(i, BLOG_ERROR, "wrong type");
  162. goto fail0;
  163. }
  164. // get null terminated name
  165. NCDValNullTermString filename_nts;
  166. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  167. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  168. goto fail0;
  169. }
  170. // write file
  171. int res = write_file(filename_nts.data, (const uint8_t *)NCDVal_StringData(contents_arg), NCDVal_StringLength(contents_arg));
  172. NCDValNullTermString_Free(&filename_nts);
  173. if (!res) {
  174. ModuleLog(i, BLOG_ERROR, "failed to write file");
  175. goto fail0;
  176. }
  177. // signal up
  178. NCDModuleInst_Backend_Up(i);
  179. return;
  180. fail0:
  181. NCDModuleInst_Backend_DeadError(i);
  182. }
  183. static void stat_func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_lstat)
  184. {
  185. struct stat_instance *o = vo;
  186. o->i = i;
  187. NCDValRef filename_arg;
  188. if (!NCDVal_ListRead(params->args, 1, &filename_arg)) {
  189. ModuleLog(i, BLOG_ERROR, "wrong arity");
  190. goto fail0;
  191. }
  192. if (!NCDVal_IsString(filename_arg)) {
  193. ModuleLog(i, BLOG_ERROR, "wrong type");
  194. goto fail0;
  195. }
  196. o->succeeded = 0;
  197. if (!NCDVal_IsStringNoNulls(filename_arg)) {
  198. goto out;
  199. }
  200. // null terminate filename
  201. NCDValNullTermString filename_nts;
  202. if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) {
  203. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  204. goto fail0;
  205. }
  206. int res;
  207. if (is_lstat) {
  208. res = lstat(filename_nts.data, &o->result);
  209. } else {
  210. res = stat(filename_nts.data, &o->result);
  211. }
  212. NCDValNullTermString_Free(&filename_nts);
  213. if (res < 0) {
  214. goto out;
  215. }
  216. o->succeeded = 1;
  217. out:
  218. NCDModuleInst_Backend_Up(i);
  219. return;
  220. fail0:
  221. NCDModuleInst_Backend_DeadError(i);
  222. }
  223. static void stat_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  224. {
  225. stat_func_new_common(vo, i, params, 0);
  226. }
  227. static void lstat_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  228. {
  229. stat_func_new_common(vo, i, params, 1);
  230. }
  231. static int stat_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  232. {
  233. struct stat_instance *o = vo;
  234. if (name == NCD_STRING_SUCCEEDED) {
  235. *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
  236. return 1;
  237. }
  238. if (name == NCD_STRING_TYPE) {
  239. const char *str;
  240. if (!o->succeeded) {
  241. str = "failed";
  242. } else if (S_ISREG(o->result.st_mode)) {
  243. str = "file";
  244. } else if (S_ISDIR(o->result.st_mode)) {
  245. str = "dir";
  246. } else if (S_ISCHR(o->result.st_mode)) {
  247. str = "chr";
  248. } else if (S_ISBLK(o->result.st_mode)) {
  249. str = "blk";
  250. } else if (S_ISFIFO(o->result.st_mode)) {
  251. str = "fifo";
  252. } else if (S_ISLNK(o->result.st_mode)) {
  253. str = "link";
  254. } else if (S_ISSOCK(o->result.st_mode)) {
  255. str = "socket";
  256. } else {
  257. str = "other";
  258. }
  259. *out = NCDVal_NewString(mem, str);
  260. return 1;
  261. }
  262. if (name == NCD_STRING_SIZE) {
  263. char str[50];
  264. if (!o->succeeded) {
  265. strcpy(str, "failed");
  266. } else {
  267. generate_decimal_repr_string((uintmax_t)o->result.st_size, str);
  268. }
  269. *out = NCDVal_NewString(mem, str);
  270. return 1;
  271. }
  272. return 0;
  273. }
  274. static struct NCDModule modules[] = {
  275. {
  276. .type = "file_read",
  277. .func_new2 = read_func_new,
  278. .func_die = read_func_die,
  279. .func_getvar2 = read_func_getvar2,
  280. .alloc_size = sizeof(struct read_instance)
  281. }, {
  282. .type = "file_write",
  283. .func_new2 = write_func_new
  284. }, {
  285. .type = "file_stat",
  286. .func_new2 = stat_func_new,
  287. .func_getvar2 = stat_func_getvar2,
  288. .alloc_size = sizeof(struct stat_instance)
  289. }, {
  290. .type = "file_lstat",
  291. .func_new2 = lstat_func_new,
  292. .func_getvar2 = stat_func_getvar2,
  293. .alloc_size = sizeof(struct stat_instance)
  294. }, {
  295. .type = NULL
  296. }
  297. };
  298. const struct NCDModuleGroup ncdmodule_file = {
  299. .modules = modules
  300. };