Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-io/src/file.c

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