file.c 10.0 KB

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