tr_file.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include "esh.h"
  10. #include "toruby.h"
  11. #include "tr_macro.h"
  12. #include "monolog.h"
  13. #include "rh.h"
  14. #include "state.h"
  15. #include "server.h"
  16. #include "tr_file.h"
  17. struct tr_file {
  18. FILE * file;
  19. struct woe_config * cfg;
  20. };
  21. typedef struct tr_file tr_file;
  22. static void file_close(tr_file * file) {
  23. if (!file) return;
  24. if (!file->file) return;
  25. fclose(file->file);
  26. file->file = NULL;
  27. }
  28. static void tr_file_free(mrb_state *mrb, void *ptr) {
  29. struct tr_file * file = ptr;
  30. file_close(file);
  31. mrb_free(mrb, file);
  32. }
  33. static FILE * file_fopen(mrb_state * mrb, char * filename, char * mode)
  34. {
  35. struct woe_config * cfg;
  36. struct woesb buf = { 0 };
  37. FILE * me = NULL;
  38. if (!mrb) return NULL;
  39. cfg = MRB_WOE_CONFIG(mrb);
  40. if (!cfg) return NULL;
  41. if (!woesb_new_join(&buf, cfg->data_dir, "/var/", filename, NULL)) {
  42. LOG_ERROR("Cannot allocate space for file name.\n");
  43. return NULL;
  44. }
  45. if (strstr(buf.text, "..")) {
  46. mrb_free(mrb, me);
  47. woesb_free(&buf);
  48. LOG_ERROR("Path may not contain '..' \n");
  49. return NULL;
  50. }
  51. me = fopen(buf.text, mode);
  52. if (!me) {
  53. LOG_ERROR("Cannot open file %s.\n", filename);
  54. woesb_free(&buf);
  55. return NULL;
  56. }
  57. woesb_free(&buf);
  58. return me;
  59. }
  60. static tr_file * file_open(mrb_state * mrb, char * filename, char * mode)
  61. {
  62. struct woe_config * cfg;
  63. tr_file * me = NULL;
  64. if (!mrb) return NULL;
  65. cfg = MRB_WOE_CONFIG(mrb);
  66. if (!cfg) return NULL;
  67. me = mrb_malloc(mrb, sizeof(struct tr_file));
  68. if (!me) return NULL;
  69. me->file = file_fopen(mrb, filename, mode);
  70. if (!me->file) {
  71. mrb_free(mrb, me);
  72. return NULL;
  73. }
  74. me->cfg = cfg;
  75. return me;
  76. }
  77. int woe_mkdir(struct woe_config * cfg, char * filename) {
  78. int res;
  79. DIR * dir;
  80. struct woesb buf = { 0 };
  81. if (!cfg) return -3;
  82. if (!woesb_new_join(&buf, cfg->data_dir, "/var/", filename, NULL)) {
  83. LOG_ERROR("Cannot allocate space for file name.\n");
  84. return -1;
  85. }
  86. if (strstr(buf.text, "..")) {
  87. woesb_free(&buf);
  88. LOG_ERROR("Path may not contain '..' \n");
  89. return -2;
  90. }
  91. dir = opendir(buf.text);
  92. if (dir) {
  93. LOG_DEBUG("Dir %s already exists\n");
  94. /* Directory already exists */
  95. closedir(dir);
  96. woesb_free(&buf);
  97. return 0;
  98. }
  99. errno = 0;
  100. res = mkdir(buf.text , 0770);
  101. if (res < 0) {
  102. LOG_ERROR("Cannot make dir %s.\n", filename);
  103. woesb_free(&buf);
  104. return errno;
  105. }
  106. woesb_free(&buf);
  107. return 0;
  108. }
  109. int woe_link(struct woe_config * cfg, char * old_filename, char * new_filename) {
  110. int res;
  111. struct woesb old_buf = { 0 };
  112. struct woesb new_buf = { 0 };
  113. if (!cfg) return -3;
  114. if (!woesb_new_join(&old_buf, cfg->data_dir, "/var/", old_filename, NULL)) {
  115. LOG_ERROR("Cannot allocate space for file name.\n");
  116. return -1;
  117. }
  118. if (!woesb_new_join(&new_buf, cfg->data_dir, "/var/", new_filename, NULL)) {
  119. woesb_free(&old_buf);
  120. LOG_ERROR("Cannot allocate space for file name.\n");
  121. return -1;
  122. }
  123. if (strstr(old_buf.text, "..") || strstr(new_buf.text, "..")) {
  124. woesb_free(&new_buf);
  125. woesb_free(&old_buf);
  126. LOG_ERROR("Path may not contain '..' \n");
  127. return -2;
  128. }
  129. errno = 0;
  130. res = link(old_buf.text, new_buf.text);
  131. if (res < 0) {
  132. LOG_ERROR("Cannot link %s to %s.\n", old_filename, new_filename);
  133. woesb_free(&new_buf);
  134. woesb_free(&old_buf);
  135. return errno;
  136. }
  137. woesb_free(&new_buf);
  138. woesb_free(&old_buf);
  139. return 0;
  140. }
  141. struct mrb_data_type tr_file_type = { "File", tr_file_free };
  142. static mrb_value tr_file_wrap(mrb_state *mrb, struct RClass *tc, tr_file * file) {
  143. return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &tr_file_type, file));
  144. }
  145. tr_file * tr_file_unwrap(mrb_state *mrb, mrb_value val) {
  146. return DATA_CHECK_GET_PTR(mrb, val, &tr_file_type, tr_file);
  147. }
  148. static mrb_value tr_file_open(mrb_state * mrb, mrb_value self) {
  149. int res;
  150. char * name, * mode;
  151. tr_file * file;
  152. mrb_get_args(mrb, "zz", &name, &mode);
  153. file = file_open(mrb, name, mode);
  154. if (!file) return mrb_nil_value();
  155. return tr_file_wrap(mrb, mrb_class_get(mrb, "File"), file);
  156. }
  157. static mrb_value tr_file_close(mrb_state * mrb, mrb_value self) {
  158. file_close(tr_file_unwrap(mrb, self));
  159. return mrb_nil_value();
  160. }
  161. static mrb_value tr_file_write(mrb_state * mrb, mrb_value self) {
  162. mrb_int res, size;
  163. tr_file * file;
  164. char * buf;
  165. file = tr_file_unwrap(mrb, self);
  166. mrb_get_args(mrb, "s", &buf, &size);
  167. res = fwrite(buf, size, 1, file->file);
  168. return mrb_fixnum_value(res);
  169. }
  170. static mrb_value tr_file_read(mrb_state * mrb, mrb_value self) {
  171. mrb_int res, size;
  172. tr_file * file;
  173. char * mem;
  174. mrb_value buf;
  175. file = tr_file_unwrap(mrb, self);
  176. mrb_get_args(mrb, "i", &size);
  177. mem = calloc(size, 1);
  178. res = fread(mem, 1, size, file->file);
  179. if (res > 0) {
  180. buf = mrb_str_new(mrb, mem, size);
  181. free(mem);
  182. return buf;
  183. }
  184. if (res == 0) {
  185. free(mem);
  186. return mrb_nil_value();
  187. }
  188. // if (res < 0)
  189. free(mem);
  190. LOG_ERROR("Failed to read from file.\n");
  191. return mrb_nil_value();
  192. }
  193. static mrb_value tr_file_readall(mrb_state * mrb, mrb_value self) {
  194. #define READALL_BUFSIZE 1024
  195. mrb_int res = 0, size = 0;
  196. FILE * file;
  197. char * mem = NULL, * aid = NULL;
  198. char * filename = NULL;
  199. mrb_value buf;
  200. (void) self;
  201. mrb_get_args(mrb, "z", &filename);
  202. if (!filename) {
  203. return mrb_nil_value();
  204. }
  205. file = file_fopen(mrb, filename, "rb");
  206. if (!file) {
  207. return mrb_nil_value();
  208. }
  209. while (!feof(file)) {
  210. size += READALL_BUFSIZE;
  211. aid = realloc(mem, size);
  212. if (!aid) {
  213. buf = mrb_nil_value();
  214. goto done;
  215. }
  216. mem = aid;
  217. res = fread(mem + size - READALL_BUFSIZE, 1, READALL_BUFSIZE, file);
  218. if (res < READALL_BUFSIZE) {
  219. size = size - READALL_BUFSIZE + res;
  220. break;
  221. }
  222. }
  223. buf = mrb_str_new(mrb, mem, size);
  224. done:
  225. free(mem);
  226. fclose(file);
  227. return buf;
  228. }
  229. static mrb_value tr_file_puts(mrb_state * mrb, mrb_value self) {
  230. mrb_int res, size;
  231. tr_file * file;
  232. char * buf;
  233. file = tr_file_unwrap(mrb, self);
  234. mrb_get_args(mrb, "z", &buf);
  235. res = fputs(buf, file->file);
  236. return mrb_fixnum_value(res);
  237. }
  238. static mrb_value tr_file_gets(mrb_state * mrb, mrb_value self) {
  239. mrb_int size;
  240. tr_file * file;
  241. char * buf;
  242. char * check;
  243. mrb_value res;
  244. file = tr_file_unwrap(mrb, self);
  245. mrb_get_args(mrb, "i", &size);
  246. buf = calloc(size, 1);
  247. if (!buf) {
  248. return mrb_nil_value();
  249. }
  250. check = fgets(buf, size, file->file);
  251. if (check) {
  252. res = mrb_str_new_cstr(mrb, buf);
  253. free(buf);
  254. return res;
  255. }
  256. free(buf);
  257. return mrb_nil_value();
  258. }
  259. static mrb_value tr_file_putc(mrb_state * mrb, mrb_value self) {
  260. mrb_int res, chara = 0;
  261. tr_file * file;
  262. file = tr_file_unwrap(mrb, self);
  263. mrb_get_args(mrb, "i", &chara);
  264. res = fputc(chara, file->file);
  265. return mrb_fixnum_value(res);
  266. }
  267. static mrb_value tr_file_getc(mrb_state * mrb, mrb_value self) {
  268. mrb_int res;
  269. tr_file * file;
  270. file = tr_file_unwrap(mrb, self);
  271. res = fgetc(file->file);
  272. return mrb_fixnum_value(res);
  273. }
  274. static mrb_value tr_file_eof(mrb_state * mrb, mrb_value self) {
  275. tr_file * file;
  276. file = tr_file_unwrap(mrb, self);
  277. return rh_bool_value(feof(file->file));
  278. }
  279. static mrb_value tr_dir_mkdir(mrb_state * mrb, mrb_value self) {
  280. tr_file * file;
  281. char * name;
  282. struct woe_config * cfg;
  283. cfg = MRB_WOE_CONFIG(mrb);
  284. file = tr_file_unwrap(mrb, self);
  285. mrb_get_args(mrb, "z", &name);
  286. return mrb_fixnum_value(woe_mkdir(cfg, name));
  287. }
  288. static mrb_value tr_file_link(mrb_state * mrb, mrb_value self) {
  289. tr_file * file;
  290. char * old_name, * new_name;
  291. struct woe_config * cfg;
  292. cfg = MRB_WOE_CONFIG(mrb);
  293. file = tr_file_unwrap(mrb, self);
  294. mrb_get_args(mrb, "zz", &old_name, &new_name);
  295. return mrb_fixnum_value(woe_link(cfg, old_name, new_name));
  296. }
  297. int tr_init_file(mrb_state * mrb) {
  298. struct RClass *krn;
  299. struct RClass *dir;
  300. struct RClass *fil;
  301. fil = mrb_define_class(mrb, "File" , mrb_class_get(mrb, "Object"));
  302. dir = mrb_define_class(mrb, "Dir" , mrb_class_get(mrb, "Object"));
  303. TR_CLASS_METHOD_ARGC(mrb, fil, "read" , tr_file_readall, 1);
  304. TR_CLASS_METHOD_ARGC(mrb, fil, "open" , tr_file_open, 2);
  305. TR_CLASS_METHOD_ARGC(mrb, fil, "link" , tr_file_link, 2);
  306. TR_CLASS_METHOD_ARGC(mrb, dir, "mkdir", tr_dir_mkdir, 1);
  307. TR_METHOD_NOARG(mrb, fil, "close" , tr_file_close);
  308. TR_METHOD_NOARG(mrb, fil, "eof?" , tr_file_eof);
  309. TR_METHOD_ARGC(mrb, fil, "read" , tr_file_read, 1);
  310. TR_METHOD_ARGC(mrb, fil, "write" , tr_file_read, 2);
  311. TR_METHOD_ARGC(mrb, fil, "gets" , tr_file_gets, 1);
  312. TR_METHOD_ARGC(mrb, fil, "puts" , tr_file_puts, 1);
  313. TR_METHOD_ARGC(mrb, fil, "putc" , tr_file_putc, 1);
  314. TR_METHOD_ARGC(mrb, fil, "getc" , tr_file_getc, 1);
  315. return 0;
  316. }