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"
12 #include <sys/types.h>
21 #if defined(_WIN32) || defined(_WIN64)
24 #define NULL_FILE "NUL"
25 #define UNLINK _unlink
26 #define GETCWD _getcwd
28 #define MAXPATHLEN 1024
29 #if !defined(PATH_MAX)
30 #define PATH_MAX _MAX_PATH
32 #define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
35 #define NULL_FILE "/dev/null"
39 #define CHMOD(a, b) chmod(a,b)
42 #include <sys/param.h>
46 #define FILE_SEPARATOR "/"
48 #if defined(_WIN32) || defined(_WIN64)
49 #define PATH_SEPARATOR ";"
50 #define FILE_ALT_SEPARATOR "\\"
51 #define VOLUME_SEPARATOR ":"
53 #define PATH_SEPARATOR ":"
70 typedef struct stat mrb_stat;
71 # define mrb_stat(path, sb) stat(path, sb)
72 # define mrb_fstat(fd, sb) fstat(fd, sb)
74 typedef struct __stat64 mrb_stat;
75 # define mrb_stat(path, sb) _stat64(path, sb)
76 # define mrb_fstat(fd, sb) _fstat64(fd, sb)
81 flock(int fd, int operation) {
83 HANDLE h = (HANDLE)_get_osfhandle(fd);
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;
93 mrb_file_s_umask(mrb_state *mrb, mrb_value klass)
95 #if defined(_WIN32) || defined(_WIN64)
96 /* nothing to do on windows */
97 return mrb_fixnum_value(0);
101 if (mrb_get_args(mrb, "|i", &mask) == 0) {
107 return mrb_fixnum_value(omask);
112 mrb_file_s_unlink(mrb_state *mrb, mrb_value obj)
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);
129 mrb_locale_free(path);
131 return mrb_fixnum_value(argc);
135 mrb_file_s_rename(mrb_state *mrb, mrb_value obj)
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);
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 */
156 mrb_locale_free(src);
157 mrb_locale_free(dst);
158 return mrb_fixnum_value(0);
162 mrb_file_dirname(mrb_state *mrb, mrb_value klass)
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;
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);
177 strncpy(buffer, ".", 2); /* null terminated */
178 } else if (ridx > 1) {
180 while (ridx > 0 && (buffer[ridx] == '/' || buffer[ridx] == '\\')) {
181 buffer[ridx] = '\0'; /* remove last char */
185 return mrb_str_new_cstr(mrb, buffer);
189 mrb_get_args(mrb, "S", &s);
190 path = mrb_locale_from_utf8(mrb_str_to_cstr(mrb, s), -1);
192 if ((dname = dirname(path)) == NULL) {
193 mrb_locale_free(path);
194 mrb_sys_fail(mrb, "dirname");
196 mrb_locale_free(path);
197 return mrb_str_new_cstr(mrb, dname);
202 mrb_file_basename(mrb_state *mrb, mrb_value klass)
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];
210 char buffer[_MAX_DIR + _MAX_EXT];
213 mrb_get_args(mrb, "S", &s);
214 path = mrb_str_to_cstr(mrb, s);
218 while (ridx > 0 && (path[ridx] == '/' || path[ridx] == '\\')) {
222 if (strncmp(path, "/", 2) == 0) {
223 return mrb_str_new_cstr(mrb, path);
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);
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");
237 if (strncmp(bname, "//", 3) == 0) bname[1] = '\0'; /* patch for Cygwin */
238 return mrb_str_new_cstr(mrb, bname);
243 mrb_file_realpath(mrb_state *mrb, mrb_value klass)
245 mrb_value pathname, dir_string, s, result;
249 argc = mrb_get_args(mrb, "S|S", &pathname, &dir_string);
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);
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 */
263 mrb_locale_free(cpath);
264 mrb_str_resize(mrb, result, strlen(RSTRING_PTR(result)));
269 mrb_file__getwd(mrb_state *mrb, mrb_value klass)
272 char buf[MAXPATHLEN], *utf8;
274 if (GETCWD(buf, MAXPATHLEN) == NULL) {
275 mrb_sys_fail(mrb, "getcwd(2)");
277 utf8 = mrb_utf8_from_locale(buf, -1);
278 path = mrb_str_new_cstr(mrb, utf8);
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]))
290 is_absolute_traditional_path(const char *path, size_t len)
292 if (len < 3) return 0;
293 return (ISALPHA(path[0]) && IS_VOLSEP(path[1]) && IS_FILESEP(path[2]));
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]));
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]));
309 mrb_file_is_absolute_path(const char *path)
311 size_t len = strlen(path);
312 if (IS_FILESEP(path[0])) return 1;
315 is_absolute_traditional_path(path, len) ||
316 is_aboslute_unc_path(path, len) ||
317 is_absolute_device_path(path, len)
326 #undef CHECK_UNCDEV_PATH
330 mrb_file_is_absolute_path(const char *path)
332 return (path[0] == *(char*)(FILE_SEPARATOR));
337 mrb_file__gethome(mrb_state *mrb, mrb_value klass)
346 argc = mrb_get_args(mrb, "|S", &username);
348 home = getenv("HOME");
350 return mrb_nil_value();
352 if (!mrb_file_is_absolute_path(home)) {
353 mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
356 const char *cuser = RSTRING_CSTR(mrb, username);
357 struct passwd *pwd = getpwnam(cuser);
359 return mrb_nil_value();
362 if (!mrb_file_is_absolute_path(home)) {
363 mrb_raisef(mrb, E_ARGUMENT_ERROR, "non-absolute home of ~%v", username);
366 home = mrb_locale_from_utf8(home, -1);
367 path = mrb_str_new_cstr(mrb, home);
368 mrb_locale_free(home);
371 argc = mrb_get_argc(mrb);
373 home = getenv("USERPROFILE");
375 return mrb_nil_value();
377 if (!mrb_file_is_absolute_path(home)) {
378 mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
381 return mrb_nil_value();
383 home = mrb_locale_from_utf8(home, -1);
384 path = mrb_str_new_cstr(mrb, home);
385 mrb_locale_free(home);
391 mrb_file_mtime(mrb_state *mrb, mrb_value self)
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));
405 mrb_file_flock(mrb_state *mrb, mrb_value self)
408 mrb_raise(mrb, E_NOTIMP_ERROR, "flock is not supported on Illumos/Solaris/Windows");
413 mrb_get_args(mrb, "i", &operation);
414 fd = mrb_io_fileno(mrb, self);
416 while (flock(fd, (int)operation) == -1) {
421 case EAGAIN: /* NetBSD */
422 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
423 case EWOULDBLOCK: /* FreeBSD OpenBSD Linux */
425 if (operation & LOCK_NB) {
426 return mrb_false_value();
428 /* FALLTHRU - should not happen */
430 mrb_sys_fail(mrb, "flock failed");
435 return mrb_fixnum_value(0);
439 mrb_file_size(mrb_state *mrb, mrb_value self)
444 fd = mrb_io_fileno(mrb, self);
445 if (mrb_fstat(fd, &st) == -1) {
446 mrb_raise(mrb, E_RUNTIME_ERROR, "fstat failed");
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");
453 return mrb_float_value(mrb, (mrb_float)st.st_size);
457 return mrb_fixnum_value((mrb_int)st.st_size);
461 mrb_ftruncate(int fd, mrb_int length)
464 return ftruncate(fd, (off_t)length);
469 file = (HANDLE)_get_osfhandle(fd);
470 if (file == INVALID_HANDLE_VALUE) {
474 cur = _lseeki64(fd, 0, SEEK_CUR);
475 if (cur == -1) return -1;
477 if (_lseeki64(fd, (__int64)length, SEEK_SET) == -1) return -1;
479 if (!SetEndOfFile(file)) {
480 errno = EINVAL; /* TODO: GetLastError to errno */
484 if (_lseeki64(fd, cur, SEEK_SET) == -1) return -1;
491 mrb_file_truncate(mrb_state *mrb, mrb_value self)
495 mrb_value lenv = mrb_get_arg1(mrb);
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");
503 return mrb_fixnum_value(0);
507 mrb_file_s_symlink(mrb_state *mrb, mrb_value klass)
509 #if defined(_WIN32) || defined(_WIN64)
510 mrb_raise(mrb, E_NOTIMP_ERROR, "symlink is not supported on this platform");
513 const char *src, *dst;
514 int ai = mrb_gc_arena_save(mrb);
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)));
524 mrb_locale_free(src);
525 mrb_locale_free(dst);
526 mrb_gc_arena_restore(mrb, ai);
528 return mrb_fixnum_value(0);
532 mrb_file_s_chmod(mrb_state *mrb, mrb_value klass) {
535 mrb_value *filenames;
536 int ai = mrb_gc_arena_save(mrb);
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);
546 mrb_locale_free(path);
547 mrb_gc_arena_restore(mrb, ai);
550 return mrb_fixnum_value(argc);
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
559 char *path, *buf, *tmp;
560 size_t bufsize = 100;
563 int ai = mrb_gc_arena_save(mrb);
565 mrb_get_args(mrb, "z", &path);
566 tmp = mrb_locale_from_utf8(path, -1);
568 buf = (char *)mrb_malloc(mrb, bufsize);
569 while ((rc = readlink(tmp, buf, bufsize)) == (ssize_t)bufsize && rc != -1) {
571 buf = (char *)mrb_realloc(mrb, buf, bufsize);
573 mrb_locale_free(tmp);
576 mrb_sys_fail(mrb, path);
578 tmp = mrb_utf8_from_locale(buf, -1);
579 ret = mrb_str_new(mrb, tmp, rc);
580 mrb_locale_free(tmp);
583 mrb_gc_arena_restore(mrb, ai);
589 mrb_init_file(mrb_state *mrb)
591 struct RClass *io, *file, *cnst;
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));
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));
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));
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));
625 mrb_define_const(mrb, cnst, "ALT_SEPARATOR", mrb_nil_value());
627 mrb_define_const(mrb, cnst, "NULL", mrb_str_new_cstr(mrb, NULL_FILE));
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));