1 /*
2 ** file.c - File class
3 */
4 
5 #include "mruby.h"
6 #include "mruby/class.h"
7 #include "mruby/data.h"
8 #include "mruby/string.h"
9 #include "mruby/ext/io.h"
10 #include "mruby/error.h"
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 
15 #include <fcntl.h>
16 #include <limits.h>
17 
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #if defined(_WIN32) || defined(_WIN64)
22   #include <windows.h>
23   #include <io.h>
24   #define NULL_FILE "NUL"
25   #define UNLINK _unlink
26   #define GETCWD _getcwd
27   #define CHMOD(a, b) 0
28   #define MAXPATHLEN 1024
29  #if !defined(PATH_MAX)
30   #define PATH_MAX _MAX_PATH
31  #endif
32   #define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
33   #include <direct.h>
34 #else
35   #define NULL_FILE "/dev/null"
36   #include <unistd.h>
37   #define UNLINK unlink
38   #define GETCWD getcwd
39   #define CHMOD(a, b) chmod(a,b)
40   #include <sys/file.h>
41   #include <libgen.h>
42   #include <sys/param.h>
43   #include <pwd.h>
44 #endif
45 
46 #define FILE_SEPARATOR "/"
47 
48 #if defined(_WIN32) || defined(_WIN64)
49   #define PATH_SEPARATOR ";"
50   #define FILE_ALT_SEPARATOR "\\"
51   #define VOLUME_SEPARATOR ":"
52 #else
53   #define PATH_SEPARATOR ":"
54 #endif
55 
56 #ifndef LOCK_SH
57 #define LOCK_SH 1
58 #endif
59 #ifndef LOCK_EX
60 #define LOCK_EX 2
61 #endif
62 #ifndef LOCK_NB
63 #define LOCK_NB 4
64 #endif
65 #ifndef LOCK_UN
66 #define LOCK_UN 8
67 #endif
68 
69 #ifndef _WIN32
70 typedef struct stat         mrb_stat;
71 # define mrb_stat(path, sb) stat(path, sb)
72 # define mrb_fstat(fd, sb)  fstat(fd, sb)
73 #else
74 typedef struct __stat64     mrb_stat;
75 # define mrb_stat(path, sb) _stat64(path, sb)
76 # define mrb_fstat(fd, sb)  _fstat64(fd, sb)
77 #endif
78 
79 #ifdef _WIN32
80 static int
flock(int fd,int operation)81 flock(int fd, int operation) {
82   OVERLAPPED ov;
83   HANDLE h = (HANDLE)_get_osfhandle(fd);
84   DWORD flags;
85   flags = ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0)
86           | ((operation & LOCK_SH) ? LOCKFILE_EXCLUSIVE_LOCK : 0);
87   memset(&ov, 0, sizeof(ov));
88   return LockFileEx(h, flags, 0, 0xffffffff, 0xffffffff, &ov) ? 0 : -1;
89 }
90 #endif
91 
92 static mrb_value
mrb_file_s_umask(mrb_state * mrb,mrb_value klass)93 mrb_file_s_umask(mrb_state *mrb, mrb_value klass)
94 {
95 #if defined(_WIN32) || defined(_WIN64)
96   /* nothing to do on windows */
97   return mrb_fixnum_value(0);
98 
99 #else
100   mrb_int mask, omask;
101   if (mrb_get_args(mrb, "|i", &mask) == 0) {
102     omask = umask(0);
103     umask(omask);
104   } else {
105     omask = umask(mask);
106   }
107   return mrb_fixnum_value(omask);
108 #endif
109 }
110 
111 static mrb_value
mrb_file_s_unlink(mrb_state * mrb,mrb_value obj)112 mrb_file_s_unlink(mrb_state *mrb, mrb_value obj)
113 {
114   mrb_value *argv;
115   mrb_value pathv;
116   mrb_int argc, i;
117   char *path;
118 
119   mrb_get_args(mrb, "*", &argv, &argc);
120   for (i = 0; i < argc; i++) {
121     const char *utf8_path;
122     pathv = mrb_ensure_string_type(mrb, argv[i]);
123     utf8_path = RSTRING_CSTR(mrb, pathv);
124     path = mrb_locale_from_utf8(utf8_path, -1);
125     if (UNLINK(path) < 0) {
126       mrb_locale_free(path);
127       mrb_sys_fail(mrb, utf8_path);
128     }
129     mrb_locale_free(path);
130   }
131   return mrb_fixnum_value(argc);
132 }
133 
134 static mrb_value
mrb_file_s_rename(mrb_state * mrb,mrb_value obj)135 mrb_file_s_rename(mrb_state *mrb, mrb_value obj)
136 {
137   mrb_value from, to;
138   char *src, *dst;
139 
140   mrb_get_args(mrb, "SS", &from, &to);
141   src = mrb_locale_from_utf8(RSTRING_CSTR(mrb, from), -1);
142   dst = mrb_locale_from_utf8(RSTRING_CSTR(mrb, to), -1);
143   if (rename(src, dst) < 0) {
144 #if defined(_WIN32) || defined(_WIN64)
145     if (CHMOD(dst, 0666) == 0 && UNLINK(dst) == 0 && rename(src, dst) == 0) {
146       mrb_locale_free(src);
147       mrb_locale_free(dst);
148       return mrb_fixnum_value(0);
149     }
150 #endif
151     mrb_locale_free(src);
152     mrb_locale_free(dst);
153     mrb_sys_fail(mrb, RSTRING_CSTR(mrb, mrb_format(mrb, "(%v, %v)", from, to)));
154     return mrb_fixnum_value(-1); /* not reached */
155   }
156   mrb_locale_free(src);
157   mrb_locale_free(dst);
158   return mrb_fixnum_value(0);
159 }
160 
161 static mrb_value
mrb_file_dirname(mrb_state * mrb,mrb_value klass)162 mrb_file_dirname(mrb_state *mrb, mrb_value klass)
163 {
164 #if defined(_WIN32) || defined(_WIN64)
165   char dname[_MAX_DIR], vname[_MAX_DRIVE];
166   char buffer[_MAX_DRIVE + _MAX_DIR];
167   const char *utf8_path;
168   char *path;
169   size_t ridx;
170   mrb_get_args(mrb, "z", &utf8_path);
171   path = mrb_locale_from_utf8(utf8_path, -1);
172   _splitpath(path, vname, dname, NULL, NULL);
173   snprintf(buffer, _MAX_DRIVE + _MAX_DIR, "%s%s", vname, dname);
174   mrb_locale_free(path);
175   ridx = strlen(buffer);
176   if (ridx == 0) {
177     strncpy(buffer, ".", 2);  /* null terminated */
178   } else if (ridx > 1) {
179     ridx--;
180     while (ridx > 0 && (buffer[ridx] == '/' || buffer[ridx] == '\\')) {
181       buffer[ridx] = '\0';  /* remove last char */
182       ridx--;
183     }
184   }
185   return mrb_str_new_cstr(mrb, buffer);
186 #else
187   char *dname, *path;
188   mrb_value s;
189   mrb_get_args(mrb, "S", &s);
190   path = mrb_locale_from_utf8(mrb_str_to_cstr(mrb, s), -1);
191 
192   if ((dname = dirname(path)) == NULL) {
193     mrb_locale_free(path);
194     mrb_sys_fail(mrb, "dirname");
195   }
196   mrb_locale_free(path);
197   return mrb_str_new_cstr(mrb, dname);
198 #endif
199 }
200 
201 static mrb_value
mrb_file_basename(mrb_state * mrb,mrb_value klass)202 mrb_file_basename(mrb_state *mrb, mrb_value klass)
203 {
204   // NOTE: Do not use mrb_locale_from_utf8 here
205 #if defined(_WIN32) || defined(_WIN64)
206   char bname[_MAX_DIR];
207   char extname[_MAX_EXT];
208   char *path;
209   size_t ridx;
210   char buffer[_MAX_DIR + _MAX_EXT];
211   mrb_value s;
212 
213   mrb_get_args(mrb, "S", &s);
214   path = mrb_str_to_cstr(mrb, s);
215   ridx = strlen(path);
216   if (ridx > 0) {
217     ridx--;
218     while (ridx > 0 && (path[ridx] == '/' || path[ridx] == '\\')) {
219       path[ridx] = '\0';
220       ridx--;
221     }
222     if (strncmp(path, "/", 2) == 0) {
223       return mrb_str_new_cstr(mrb, path);
224     }
225   }
226   _splitpath((const char*)path, NULL, NULL, bname, extname);
227   snprintf(buffer, _MAX_DIR + _MAX_EXT, "%s%s", bname, extname);
228   return mrb_str_new_cstr(mrb, buffer);
229 #else
230   char *bname, *path;
231   mrb_value s;
232   mrb_get_args(mrb, "S", &s);
233   path = mrb_str_to_cstr(mrb, s);
234   if ((bname = basename(path)) == NULL) {
235     mrb_sys_fail(mrb, "basename");
236   }
237   if (strncmp(bname, "//", 3) == 0) bname[1] = '\0';  /* patch for Cygwin */
238   return mrb_str_new_cstr(mrb, bname);
239 #endif
240 }
241 
242 static mrb_value
mrb_file_realpath(mrb_state * mrb,mrb_value klass)243 mrb_file_realpath(mrb_state *mrb, mrb_value klass)
244 {
245   mrb_value pathname, dir_string, s, result;
246   mrb_int argc;
247   char *cpath;
248 
249   argc = mrb_get_args(mrb, "S|S", &pathname, &dir_string);
250   if (argc == 2) {
251     s = mrb_str_dup(mrb, dir_string);
252     s = mrb_str_append(mrb, s, mrb_str_new_cstr(mrb, FILE_SEPARATOR));
253     s = mrb_str_append(mrb, s, pathname);
254     pathname = s;
255   }
256   cpath = mrb_locale_from_utf8(RSTRING_CSTR(mrb, pathname), -1);
257   result = mrb_str_buf_new(mrb, PATH_MAX);
258   if (realpath(cpath, RSTRING_PTR(result)) == NULL) {
259     mrb_locale_free(cpath);
260     mrb_sys_fail(mrb, cpath);
261     return result;              /* not reached */
262   }
263   mrb_locale_free(cpath);
264   mrb_str_resize(mrb, result, strlen(RSTRING_PTR(result)));
265   return result;
266 }
267 
268 static mrb_value
mrb_file__getwd(mrb_state * mrb,mrb_value klass)269 mrb_file__getwd(mrb_state *mrb, mrb_value klass)
270 {
271   mrb_value path;
272   char buf[MAXPATHLEN], *utf8;
273 
274   if (GETCWD(buf, MAXPATHLEN) == NULL) {
275     mrb_sys_fail(mrb, "getcwd(2)");
276   }
277   utf8 = mrb_utf8_from_locale(buf, -1);
278   path = mrb_str_new_cstr(mrb, utf8);
279   mrb_utf8_free(utf8);
280   return path;
281 }
282 
283 #ifdef _WIN32
284 #define IS_FILESEP(x) (x == (*(char*)(FILE_SEPARATOR)) || x == (*(char*)(FILE_ALT_SEPARATOR)))
285 #define IS_VOLSEP(x) (x == (*(char*)(VOLUME_SEPARATOR)))
286 #define IS_DEVICEID(x) (x == '.' || x == '?')
287 #define CHECK_UNCDEV_PATH (IS_FILESEP(path[0]) && IS_FILESEP(path[1]))
288 
289 static int
is_absolute_traditional_path(const char * path,size_t len)290 is_absolute_traditional_path(const char *path, size_t len)
291 {
292   if (len < 3) return 0;
293   return (ISALPHA(path[0]) && IS_VOLSEP(path[1]) && IS_FILESEP(path[2]));
294 }
295 
296 static int
is_aboslute_unc_path(const char * path,size_t len)297 is_aboslute_unc_path(const char *path, size_t len) {
298   if (len < 2) return 0;
299   return (CHECK_UNCDEV_PATH && !IS_DEVICEID(path[2]));
300 }
301 
302 static int
is_absolute_device_path(const char * path,size_t len)303 is_absolute_device_path(const char *path, size_t len) {
304   if (len < 4) return 0;
305   return (CHECK_UNCDEV_PATH && IS_DEVICEID(path[2]) && IS_FILESEP(path[3]));
306 }
307 
308 static int
mrb_file_is_absolute_path(const char * path)309 mrb_file_is_absolute_path(const char *path)
310 {
311   size_t len = strlen(path);
312   if (IS_FILESEP(path[0])) return 1;
313   if (len > 0)
314     return (
315       is_absolute_traditional_path(path, len) ||
316       is_aboslute_unc_path(path, len) ||
317       is_absolute_device_path(path, len)
318       );
319   else
320     return 0;
321 }
322 
323 #undef IS_FILESEP
324 #undef IS_VOLSEP
325 #undef IS_DEVICEID
326 #undef CHECK_UNCDEV_PATH
327 
328 #else
329 static int
mrb_file_is_absolute_path(const char * path)330 mrb_file_is_absolute_path(const char *path)
331 {
332   return (path[0] == *(char*)(FILE_SEPARATOR));
333 }
334 #endif
335 
336 static mrb_value
mrb_file__gethome(mrb_state * mrb,mrb_value klass)337 mrb_file__gethome(mrb_state *mrb, mrb_value klass)
338 {
339   mrb_int argc;
340   char *home;
341   mrb_value path;
342 
343 #ifndef _WIN32
344   mrb_value username;
345 
346   argc = mrb_get_args(mrb, "|S", &username);
347   if (argc == 0) {
348     home = getenv("HOME");
349     if (home == NULL) {
350       return mrb_nil_value();
351     }
352     if (!mrb_file_is_absolute_path(home)) {
353       mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
354     }
355   } else {
356     const char *cuser = RSTRING_CSTR(mrb, username);
357     struct passwd *pwd = getpwnam(cuser);
358     if (pwd == NULL) {
359       return mrb_nil_value();
360     }
361     home = pwd->pw_dir;
362     if (!mrb_file_is_absolute_path(home)) {
363       mrb_raisef(mrb, E_ARGUMENT_ERROR, "non-absolute home of ~%v", username);
364     }
365   }
366   home = mrb_locale_from_utf8(home, -1);
367   path = mrb_str_new_cstr(mrb, home);
368   mrb_locale_free(home);
369   return path;
370 #else  /* _WIN32 */
371   argc = mrb_get_argc(mrb);
372   if (argc == 0) {
373     home = getenv("USERPROFILE");
374     if (home == NULL) {
375       return mrb_nil_value();
376     }
377     if (!mrb_file_is_absolute_path(home)) {
378       mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
379     }
380   } else {
381     return mrb_nil_value();
382   }
383   home = mrb_locale_from_utf8(home, -1);
384   path = mrb_str_new_cstr(mrb, home);
385   mrb_locale_free(home);
386   return path;
387 #endif
388 }
389 
390 static mrb_value
mrb_file_mtime(mrb_state * mrb,mrb_value self)391 mrb_file_mtime(mrb_state *mrb, mrb_value self)
392 {
393   mrb_value obj;
394   struct stat st;
395   int fd;
396 
397   obj = mrb_obj_value(mrb_class_get(mrb, "Time"));
398   fd = mrb_io_fileno(mrb, self);
399   if (fstat(fd, &st) == -1)
400     return mrb_false_value();
401   return mrb_funcall(mrb, obj, "at", 1, mrb_fixnum_value(st.st_mtime));
402 }
403 
404 static mrb_value
mrb_file_flock(mrb_state * mrb,mrb_value self)405 mrb_file_flock(mrb_state *mrb, mrb_value self)
406 {
407 #if defined(sun)
408   mrb_raise(mrb, E_NOTIMP_ERROR, "flock is not supported on Illumos/Solaris/Windows");
409 #else
410   mrb_int operation;
411   int fd;
412 
413   mrb_get_args(mrb, "i", &operation);
414   fd = mrb_io_fileno(mrb, self);
415 
416   while (flock(fd, (int)operation) == -1) {
417     switch (errno) {
418       case EINTR:
419         /* retry */
420         break;
421       case EAGAIN:      /* NetBSD */
422 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
423       case EWOULDBLOCK: /* FreeBSD OpenBSD Linux */
424 #endif
425         if (operation & LOCK_NB) {
426           return mrb_false_value();
427         }
428         /* FALLTHRU - should not happen */
429       default:
430         mrb_sys_fail(mrb, "flock failed");
431         break;
432     }
433   }
434 #endif
435   return mrb_fixnum_value(0);
436 }
437 
438 static mrb_value
mrb_file_size(mrb_state * mrb,mrb_value self)439 mrb_file_size(mrb_state *mrb, mrb_value self)
440 {
441   mrb_stat st;
442   int fd;
443 
444   fd = mrb_io_fileno(mrb, self);
445   if (mrb_fstat(fd, &st) == -1) {
446     mrb_raise(mrb, E_RUNTIME_ERROR, "fstat failed");
447   }
448 
449   if (st.st_size > MRB_INT_MAX) {
450 #ifdef MRB_WITHOUT_FLOAT
451     mrb_raise(mrb, E_RUNTIME_ERROR, "File#size too large for MRB_WITHOUT_FLOAT");
452 #else
453     return mrb_float_value(mrb, (mrb_float)st.st_size);
454 #endif
455   }
456 
457   return mrb_fixnum_value((mrb_int)st.st_size);
458 }
459 
460 static int
mrb_ftruncate(int fd,mrb_int length)461 mrb_ftruncate(int fd, mrb_int length)
462 {
463 #ifndef _WIN32
464   return ftruncate(fd, (off_t)length);
465 #else
466   HANDLE file;
467   __int64 cur;
468 
469   file = (HANDLE)_get_osfhandle(fd);
470   if (file == INVALID_HANDLE_VALUE) {
471     return -1;
472   }
473 
474   cur = _lseeki64(fd, 0, SEEK_CUR);
475   if (cur == -1) return -1;
476 
477   if (_lseeki64(fd, (__int64)length, SEEK_SET) == -1) return -1;
478 
479   if (!SetEndOfFile(file)) {
480     errno = EINVAL; /* TODO: GetLastError to errno */
481     return -1;
482   }
483 
484   if (_lseeki64(fd, cur, SEEK_SET) == -1) return -1;
485 
486   return 0;
487 #endif /* _WIN32 */
488 }
489 
490 static mrb_value
mrb_file_truncate(mrb_state * mrb,mrb_value self)491 mrb_file_truncate(mrb_state *mrb, mrb_value self)
492 {
493   int fd;
494   mrb_int length;
495   mrb_value lenv = mrb_get_arg1(mrb);
496 
497   fd = mrb_io_fileno(mrb, self);
498   length = mrb_int(mrb, lenv);
499   if (mrb_ftruncate(fd, length) != 0) {
500     mrb_raise(mrb, E_IO_ERROR, "ftruncate failed");
501   }
502 
503   return mrb_fixnum_value(0);
504 }
505 
506 static mrb_value
mrb_file_s_symlink(mrb_state * mrb,mrb_value klass)507 mrb_file_s_symlink(mrb_state *mrb, mrb_value klass)
508 {
509 #if defined(_WIN32) || defined(_WIN64)
510   mrb_raise(mrb, E_NOTIMP_ERROR, "symlink is not supported on this platform");
511 #else
512   mrb_value from, to;
513   const char *src, *dst;
514   int ai = mrb_gc_arena_save(mrb);
515 
516   mrb_get_args(mrb, "SS", &from, &to);
517   src = mrb_locale_from_utf8(RSTRING_CSTR(mrb, from), -1);
518   dst = mrb_locale_from_utf8(RSTRING_CSTR(mrb, to), -1);
519   if (symlink(src, dst) == -1) {
520     mrb_locale_free(src);
521     mrb_locale_free(dst);
522     mrb_sys_fail(mrb, RSTRING_CSTR(mrb, mrb_format(mrb, "(%v, %v)", from, to)));
523   }
524   mrb_locale_free(src);
525   mrb_locale_free(dst);
526   mrb_gc_arena_restore(mrb, ai);
527 #endif
528   return mrb_fixnum_value(0);
529 }
530 
531 static mrb_value
mrb_file_s_chmod(mrb_state * mrb,mrb_value klass)532 mrb_file_s_chmod(mrb_state *mrb, mrb_value klass) {
533   mrb_int mode;
534   mrb_int argc, i;
535   mrb_value *filenames;
536   int ai = mrb_gc_arena_save(mrb);
537 
538   mrb_get_args(mrb, "i*", &mode, &filenames, &argc);
539   for (i = 0; i < argc; i++) {
540     const char *utf8_path = RSTRING_CSTR(mrb, filenames[i]);
541     char *path = mrb_locale_from_utf8(utf8_path, -1);
542     if (CHMOD(path, mode) == -1) {
543       mrb_locale_free(path);
544       mrb_sys_fail(mrb, utf8_path);
545     }
546     mrb_locale_free(path);
547     mrb_gc_arena_restore(mrb, ai);
548   }
549 
550   return mrb_fixnum_value(argc);
551 }
552 
553 static mrb_value
mrb_file_s_readlink(mrb_state * mrb,mrb_value klass)554 mrb_file_s_readlink(mrb_state *mrb, mrb_value klass) {
555 #if defined(_WIN32) || defined(_WIN64)
556   mrb_raise(mrb, E_NOTIMP_ERROR, "readlink is not supported on this platform");
557   return mrb_nil_value(); // unreachable
558 #else
559   char *path, *buf, *tmp;
560   size_t bufsize = 100;
561   ssize_t rc;
562   mrb_value ret;
563   int ai = mrb_gc_arena_save(mrb);
564 
565   mrb_get_args(mrb, "z", &path);
566   tmp = mrb_locale_from_utf8(path, -1);
567 
568   buf = (char *)mrb_malloc(mrb, bufsize);
569   while ((rc = readlink(tmp, buf, bufsize)) == (ssize_t)bufsize && rc != -1) {
570     bufsize *= 2;
571     buf = (char *)mrb_realloc(mrb, buf, bufsize);
572   }
573   mrb_locale_free(tmp);
574   if (rc == -1) {
575     mrb_free(mrb, buf);
576     mrb_sys_fail(mrb, path);
577   }
578   tmp = mrb_utf8_from_locale(buf, -1);
579   ret = mrb_str_new(mrb, tmp, rc);
580   mrb_locale_free(tmp);
581   mrb_free(mrb, buf);
582 
583   mrb_gc_arena_restore(mrb, ai);
584   return ret;
585 #endif
586 }
587 
588 void
mrb_init_file(mrb_state * mrb)589 mrb_init_file(mrb_state *mrb)
590 {
591   struct RClass *io, *file, *cnst;
592 
593   io   = mrb_class_get(mrb, "IO");
594   file = mrb_define_class(mrb, "File", io);
595   MRB_SET_INSTANCE_TT(file, MRB_TT_DATA);
596   mrb_define_class_method(mrb, file, "umask",  mrb_file_s_umask, MRB_ARGS_OPT(1));
597   mrb_define_class_method(mrb, file, "delete", mrb_file_s_unlink, MRB_ARGS_ANY());
598   mrb_define_class_method(mrb, file, "unlink", mrb_file_s_unlink, MRB_ARGS_ANY());
599   mrb_define_class_method(mrb, file, "rename", mrb_file_s_rename, MRB_ARGS_REQ(2));
600   mrb_define_class_method(mrb, file, "symlink", mrb_file_s_symlink, MRB_ARGS_REQ(2));
601   mrb_define_class_method(mrb, file, "chmod", mrb_file_s_chmod, MRB_ARGS_REQ(1) | MRB_ARGS_REST());
602   mrb_define_class_method(mrb, file, "readlink", mrb_file_s_readlink, MRB_ARGS_REQ(1));
603 
604   mrb_define_class_method(mrb, file, "dirname",   mrb_file_dirname,    MRB_ARGS_REQ(1));
605   mrb_define_class_method(mrb, file, "basename",  mrb_file_basename,   MRB_ARGS_REQ(1));
606   mrb_define_class_method(mrb, file, "realpath",  mrb_file_realpath,   MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
607   mrb_define_class_method(mrb, file, "_getwd",    mrb_file__getwd,     MRB_ARGS_NONE());
608   mrb_define_class_method(mrb, file, "_gethome",  mrb_file__gethome,   MRB_ARGS_OPT(1));
609 
610   mrb_define_method(mrb, file, "flock", mrb_file_flock, MRB_ARGS_REQ(1));
611   mrb_define_method(mrb, file, "mtime", mrb_file_mtime, MRB_ARGS_NONE());
612   mrb_define_method(mrb, file, "size", mrb_file_size, MRB_ARGS_NONE());
613   mrb_define_method(mrb, file, "truncate", mrb_file_truncate, MRB_ARGS_REQ(1));
614 
615   cnst = mrb_define_module_under(mrb, file, "Constants");
616   mrb_define_const(mrb, cnst, "LOCK_SH", mrb_fixnum_value(LOCK_SH));
617   mrb_define_const(mrb, cnst, "LOCK_EX", mrb_fixnum_value(LOCK_EX));
618   mrb_define_const(mrb, cnst, "LOCK_UN", mrb_fixnum_value(LOCK_UN));
619   mrb_define_const(mrb, cnst, "LOCK_NB", mrb_fixnum_value(LOCK_NB));
620   mrb_define_const(mrb, cnst, "SEPARATOR", mrb_str_new_cstr(mrb, FILE_SEPARATOR));
621   mrb_define_const(mrb, cnst, "PATH_SEPARATOR", mrb_str_new_cstr(mrb, PATH_SEPARATOR));
622 #if defined(_WIN32) || defined(_WIN64)
623   mrb_define_const(mrb, cnst, "ALT_SEPARATOR", mrb_str_new_cstr(mrb, FILE_ALT_SEPARATOR));
624 #else
625   mrb_define_const(mrb, cnst, "ALT_SEPARATOR", mrb_nil_value());
626 #endif
627   mrb_define_const(mrb, cnst, "NULL", mrb_str_new_cstr(mrb, NULL_FILE));
628 
629   mrb_define_const(mrb, cnst, "RDONLY", mrb_fixnum_value(MRB_O_RDONLY));
630   mrb_define_const(mrb, cnst, "WRONLY", mrb_fixnum_value(MRB_O_WRONLY));
631   mrb_define_const(mrb, cnst, "RDWR", mrb_fixnum_value(MRB_O_RDWR));
632   mrb_define_const(mrb, cnst, "APPEND", mrb_fixnum_value(MRB_O_APPEND));
633   mrb_define_const(mrb, cnst, "CREAT", mrb_fixnum_value(MRB_O_CREAT));
634   mrb_define_const(mrb, cnst, "EXCL", mrb_fixnum_value(MRB_O_EXCL));
635   mrb_define_const(mrb, cnst, "TRUNC", mrb_fixnum_value(MRB_O_TRUNC));
636   mrb_define_const(mrb, cnst, "NONBLOCK", mrb_fixnum_value(MRB_O_NONBLOCK));
637   mrb_define_const(mrb, cnst, "NOCTTY", mrb_fixnum_value(MRB_O_NOCTTY));
638   mrb_define_const(mrb, cnst, "BINARY", mrb_fixnum_value(MRB_O_BINARY));
639   mrb_define_const(mrb, cnst, "SHARE_DELETE", mrb_fixnum_value(MRB_O_SHARE_DELETE));
640   mrb_define_const(mrb, cnst, "SYNC", mrb_fixnum_value(MRB_O_SYNC));
641   mrb_define_const(mrb, cnst, "DSYNC", mrb_fixnum_value(MRB_O_DSYNC));
642   mrb_define_const(mrb, cnst, "RSYNC", mrb_fixnum_value(MRB_O_RSYNC));
643   mrb_define_const(mrb, cnst, "NOFOLLOW", mrb_fixnum_value(MRB_O_NOFOLLOW));
644   mrb_define_const(mrb, cnst, "NOATIME", mrb_fixnum_value(MRB_O_NOATIME));
645   mrb_define_const(mrb, cnst, "DIRECT", mrb_fixnum_value(MRB_O_DIRECT));
646   mrb_define_const(mrb, cnst, "TMPFILE", mrb_fixnum_value(MRB_O_TMPFILE));
647 }
648