Debian Codegraph @

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

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