file.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <stdlib.h>
  63. #include <string.h>
  64. #include <stdint.h>
  65. #include <misc/read_file.h>
  66. #include <misc/write_file.h>
  67. #include <ncd/NCDModule.h>
  68. #include <generated/blog_channel_ncd_file.h>
  69. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  70. struct read_instance {
  71. NCDModuleInst *i;
  72. uint8_t *file_data;
  73. size_t file_len;
  74. };
  75. static void read_func_new (NCDModuleInst *i)
  76. {
  77. // allocate instance
  78. struct read_instance *o = malloc(sizeof(*o));
  79. if (!o) {
  80. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  81. goto fail0;
  82. }
  83. o->i = i;
  84. NCDModuleInst_Backend_SetUser(i, o);
  85. // read arguments
  86. NCDValue *filename_arg;
  87. if (!NCDValue_ListRead(i->args, 1, &filename_arg)) {
  88. ModuleLog(i, BLOG_ERROR, "wrong arity");
  89. goto fail1;
  90. }
  91. if (!NCDValue_IsStringNoNulls(filename_arg)) {
  92. ModuleLog(i, BLOG_ERROR, "wrong type");
  93. goto fail1;
  94. }
  95. // read file
  96. if (!read_file(NCDValue_StringValue(filename_arg), &o->file_data, &o->file_len)) {
  97. ModuleLog(i, BLOG_ERROR, "failed to read file");
  98. goto fail1;
  99. }
  100. // signal up
  101. NCDModuleInst_Backend_Up(i);
  102. return;
  103. fail1:
  104. free(o);
  105. fail0:
  106. NCDModuleInst_Backend_SetError(i);
  107. NCDModuleInst_Backend_Dead(i);
  108. }
  109. static void read_func_die (void *vo)
  110. {
  111. struct read_instance *o = vo;
  112. NCDModuleInst *i = o->i;
  113. // free data
  114. free(o->file_data);
  115. // free instance
  116. free(o);
  117. NCDModuleInst_Backend_Dead(i);
  118. }
  119. static int read_func_getvar (void *vo, const char *name, NCDValue *out_value)
  120. {
  121. struct read_instance *o = vo;
  122. if (!strcmp(name, "")) {
  123. if (!NCDValue_InitStringBin(out_value, o->file_data, o->file_len)) {
  124. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitStringBin failed");
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. static void write_func_new (NCDModuleInst *i)
  132. {
  133. // read arguments
  134. NCDValue *filename_arg;
  135. NCDValue *contents_arg;
  136. if (!NCDValue_ListRead(i->args, 2, &filename_arg, &contents_arg)) {
  137. ModuleLog(i, BLOG_ERROR, "wrong arity");
  138. goto fail0;
  139. }
  140. if (!NCDValue_IsStringNoNulls(filename_arg) || !NCDValue_IsString(contents_arg)) {
  141. ModuleLog(i, BLOG_ERROR, "wrong type");
  142. goto fail0;
  143. }
  144. // write file
  145. if (!write_file(NCDValue_StringValue(filename_arg), (const uint8_t *)NCDValue_StringValue(contents_arg), NCDValue_StringLength(contents_arg))) {
  146. ModuleLog(i, BLOG_ERROR, "failed to write file");
  147. goto fail0;
  148. }
  149. // signal up
  150. NCDModuleInst_Backend_Up(i);
  151. return;
  152. fail0:
  153. NCDModuleInst_Backend_SetError(i);
  154. NCDModuleInst_Backend_Dead(i);
  155. }
  156. static const struct NCDModule modules[] = {
  157. {
  158. .type = "file_read",
  159. .func_new = read_func_new,
  160. .func_die = read_func_die,
  161. .func_getvar = read_func_getvar
  162. }, {
  163. .type = "file_write",
  164. .func_new = write_func_new
  165. }, {
  166. .type = NULL
  167. }
  168. };
  169. const struct NCDModuleGroup ncdmodule_file = {
  170. .modules = modules
  171. };