source: EcnlProtoTool/trunk/mrbgems/mruby-io/src/file.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 10.9 KB
Line 
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
11#if MRUBY_RELEASE_NO < 10000
12#include "error.h"
13#else
14#include "mruby/error.h"
15#endif
16
17#include <sys/types.h>
18#include <sys/stat.h>
19
20#include <fcntl.h>
21#include <limits.h>
22
23#include <errno.h>
24#include <stdlib.h>
25#include <string.h>
26#if defined(_WIN32) || defined(_WIN64)
27 #define NULL_FILE "NUL"
28 #define UNLINK _unlink
29 #define GETCWD _getcwd
30 #define CHMOD(a, b) 0
31 #define MAXPATHLEN 1024
32 #if !defined(PATH_MAX)
33 #define PATH_MAX _MAX_PATH
34 #endif
35 #define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
36 #include <direct.h>
37#else
38 #define NULL_FILE "/dev/null"
39 #include <unistd.h>
40 #define UNLINK unlink
41 #define GETCWD getcwd
42 #define CHMOD(a, b) chmod(a,b)
43 #include <sys/file.h>
44 #include <libgen.h>
45 #include <sys/param.h>
46 #include <pwd.h>
47#endif
48
49#define FILE_SEPARATOR "/"
50
51#if defined(_WIN32) || defined(_WIN64)
52 #define PATH_SEPARATOR ";"
53 #define FILE_ALT_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#define STAT(p, s) stat(p, s)
72
73
74mrb_value
75mrb_file_s_umask(mrb_state *mrb, mrb_value klass)
76{
77#if defined(_WIN32) || defined(_WIN64)
78 /* nothing to do on windows */
79 return mrb_fixnum_value(0);
80
81#else
82 mrb_int mask, omask;
83 if (mrb_get_args(mrb, "|i", &mask) == 0) {
84 omask = umask(0);
85 umask(omask);
86 } else {
87 omask = umask(mask);
88 }
89 return mrb_fixnum_value(omask);
90#endif
91}
92
93static mrb_value
94mrb_file_s_unlink(mrb_state *mrb, mrb_value obj)
95{
96 mrb_value *argv;
97 mrb_value pathv;
98 mrb_int argc, i;
99 const char *path;
100
101 mrb_get_args(mrb, "*", &argv, &argc);
102 for (i = 0; i < argc; i++) {
103 pathv = mrb_convert_type(mrb, argv[i], MRB_TT_STRING, "String", "to_str");
104 path = mrb_string_value_cstr(mrb, &pathv);
105 if (UNLINK(path) < 0) {
106 mrb_sys_fail(mrb, path);
107 }
108 }
109 return mrb_fixnum_value(argc);
110}
111
112static mrb_value
113mrb_file_s_rename(mrb_state *mrb, mrb_value obj)
114{
115 mrb_value from, to;
116 const char *src, *dst;
117
118 mrb_get_args(mrb, "SS", &from, &to);
119 src = mrb_string_value_cstr(mrb, &from);
120 dst = mrb_string_value_cstr(mrb, &to);
121 if (rename(src, dst) < 0) {
122#if defined(_WIN32) || defined(_WIN64)
123 if (CHMOD(dst, 0666) == 0 && UNLINK(dst) == 0 && rename(src, dst) == 0) {
124 return mrb_fixnum_value(0);
125 }
126#endif
127 mrb_sys_fail(mrb, mrb_str_to_cstr(mrb, mrb_format(mrb, "(%S, %S)", from, to)));
128 }
129 return mrb_fixnum_value(0);
130}
131
132static mrb_value
133mrb_file_dirname(mrb_state *mrb, mrb_value klass)
134{
135#if defined(_WIN32) || defined(_WIN64)
136 char dname[_MAX_DIR], vname[_MAX_DRIVE];
137 char buffer[_MAX_DRIVE + _MAX_DIR];
138 char *path;
139 size_t ridx;
140 mrb_value s;
141 mrb_get_args(mrb, "S", &s);
142 path = mrb_str_to_cstr(mrb, s);
143 _splitpath((const char*)path, vname, dname, NULL, NULL);
144 snprintf(buffer, _MAX_DRIVE + _MAX_DIR, "%s%s", vname, dname);
145 ridx = strlen(buffer);
146 if (ridx == 0) {
147 strncpy(buffer, ".", 2); /* null terminated */
148 } else if (ridx > 1) {
149 ridx--;
150 while (ridx > 0 && (buffer[ridx] == '/' || buffer[ridx] == '\\')) {
151 buffer[ridx] = '\0'; /* remove last char */
152 ridx--;
153 }
154 }
155 return mrb_str_new_cstr(mrb, buffer);
156#else
157 char *dname, *path;
158 mrb_value s;
159 mrb_get_args(mrb, "S", &s);
160 path = mrb_str_to_cstr(mrb, s);
161
162 if ((dname = dirname(path)) == NULL) {
163 mrb_sys_fail(mrb, "dirname");
164 }
165#endif
166 return mrb_str_new_cstr(mrb, dname);
167}
168
169static mrb_value
170mrb_file_basename(mrb_state *mrb, mrb_value klass)
171{
172#if defined(_WIN32) || defined(_WIN64)
173 char bname[_MAX_DIR];
174 char extname[_MAX_EXT];
175 char *path;
176 size_t ridx;
177 char buffer[_MAX_DIR + _MAX_EXT];
178 mrb_value s;
179 mrb_get_args(mrb, "S", &s);
180 path = mrb_str_to_cstr(mrb, s);
181 ridx = strlen(path);
182 if (ridx > 0) {
183 ridx--;
184 while (ridx > 0 && (path[ridx] == '/' || path[ridx] == '\\')) {
185 path[ridx] = '\0';
186 ridx--;
187 }
188 if (strncmp(path, "/", 2) == 0) {
189 return mrb_str_new_cstr(mrb, path);
190 }
191 }
192 _splitpath((const char*)path, NULL, NULL, bname, extname);
193 snprintf(buffer, _MAX_DIR + _MAX_EXT, "%s%s", bname, extname);
194 return mrb_str_new_cstr(mrb, buffer);
195#else
196 char *bname, *path;
197 mrb_value s;
198 mrb_get_args(mrb, "S", &s);
199 path = mrb_str_to_cstr(mrb, s);
200 if ((bname = basename(path)) == NULL) {
201 mrb_sys_fail(mrb, "basename");
202 }
203 return mrb_str_new_cstr(mrb, bname);
204#endif
205}
206
207static mrb_value
208mrb_file_realpath(mrb_state *mrb, mrb_value klass)
209{
210 mrb_value pathname, dir_string, s, result;
211 int argc;
212 char *cpath;
213
214 argc = mrb_get_args(mrb, "S|S", &pathname, &dir_string);
215 if (argc == 2) {
216 s = mrb_str_dup(mrb, dir_string);
217 s = mrb_str_append(mrb, s, mrb_str_new_cstr(mrb, FILE_SEPARATOR));
218 s = mrb_str_append(mrb, s, pathname);
219 pathname = s;
220 }
221 cpath = mrb_str_to_cstr(mrb, pathname);
222 result = mrb_str_buf_new(mrb, PATH_MAX);
223 if (realpath(cpath, RSTRING_PTR(result)) == NULL)
224 mrb_sys_fail(mrb, cpath);
225 mrb_str_resize(mrb, result, strlen(RSTRING_PTR(result)));
226 return result;
227}
228
229mrb_value
230mrb_file__getwd(mrb_state *mrb, mrb_value klass)
231{
232 mrb_value path;
233
234 path = mrb_str_buf_new(mrb, MAXPATHLEN);
235 if (GETCWD(RSTRING_PTR(path), MAXPATHLEN) == NULL) {
236 mrb_sys_fail(mrb, "getcwd(2)");
237 }
238 mrb_str_resize(mrb, path, strlen(RSTRING_PTR(path)));
239 return path;
240}
241
242static int
243mrb_file_is_absolute_path(const char *path)
244{
245 return (path[0] == '/');
246}
247
248static mrb_value
249mrb_file__gethome(mrb_state *mrb, mrb_value klass)
250{
251#ifndef _WIN32
252 mrb_value username;
253 int argc;
254 char *home;
255
256 argc = mrb_get_args(mrb, "|S", &username);
257 if (argc == 0) {
258 home = getenv("HOME");
259 if (home == NULL) {
260 return mrb_nil_value();
261 }
262 if (!mrb_file_is_absolute_path(home)) {
263 mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
264 }
265 } else {
266 const char *cuser = mrb_str_to_cstr(mrb, username);
267 struct passwd *pwd = getpwnam(cuser);
268 if (pwd == NULL) {
269 return mrb_nil_value();
270 }
271 home = pwd->pw_dir;
272 if (!mrb_file_is_absolute_path(home)) {
273 mrb_raisef(mrb, E_ARGUMENT_ERROR, "non-absolute home of ~%S", username);
274 }
275 }
276 return mrb_str_new_cstr(mrb, home);
277#else
278
279 return mrb_nil_value();
280#endif
281}
282
283mrb_value
284mrb_file_flock(mrb_state *mrb, mrb_value self)
285{
286#if defined(_WIN32) || defined(_WIN64) || defined(sun)
287 mrb_raise(mrb, E_NOTIMP_ERROR, "flock is not supported on Illumos/Solaris/Windows");
288#else
289 mrb_int operation;
290 int fd;
291
292 mrb_get_args(mrb, "i", &operation);
293 fd = mrb_fixnum(mrb_io_fileno(mrb, self));
294
295 while (flock(fd, operation) == -1) {
296 switch (errno) {
297 case EINTR:
298 /* retry */
299 break;
300 case EAGAIN: /* NetBSD */
301#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
302 case EWOULDBLOCK: /* FreeBSD OpenBSD Linux */
303#endif
304 if (operation & LOCK_NB) {
305 return mrb_false_value();
306 }
307 /* FALLTHRU - should not happen */
308 default:
309 mrb_sys_fail(mrb, "flock failed");
310 break;
311 }
312 }
313#endif
314 return mrb_fixnum_value(0);
315}
316
317static mrb_value
318mrb_file_s_symlink(mrb_state *mrb, mrb_value klass)
319{
320#if defined(_WIN32) || defined(_WIN64)
321 mrb_raise(mrb, E_NOTIMP_ERROR, "symlink is not supported on this platform");
322#else
323 mrb_value from, to;
324 const char *src, *dst;
325 int ai = mrb_gc_arena_save(mrb);
326
327 mrb_get_args(mrb, "SS", &from, &to);
328 src = mrb_str_to_cstr(mrb, from);
329 dst = mrb_str_to_cstr(mrb, to);
330
331 if (symlink(src, dst) == -1) {
332 mrb_sys_fail(mrb, mrb_str_to_cstr(mrb, mrb_format(mrb, "(%S, %S)", from, to)));
333 }
334 mrb_gc_arena_restore(mrb, ai);
335#endif
336 return mrb_fixnum_value(0);
337}
338
339static mrb_value
340mrb_file_s_chmod(mrb_state *mrb, mrb_value klass) {
341 mrb_int mode;
342 mrb_int argc, i;
343 mrb_value *filenames;
344 int ai = mrb_gc_arena_save(mrb);
345
346 mrb_get_args(mrb, "i*", &mode, &filenames, &argc);
347 for (i = 0; i < argc; i++) {
348 char *path = mrb_str_to_cstr(mrb, filenames[i]);
349 if (CHMOD(path, mode) == -1) {
350 mrb_sys_fail(mrb, path);
351 }
352 }
353
354 mrb_gc_arena_restore(mrb, ai);
355 return mrb_fixnum_value(argc);
356}
357
358static mrb_value
359mrb_file_s_readlink(mrb_state *mrb, mrb_value klass) {
360#if defined(_WIN32) || defined(_WIN64)
361 mrb_raise(mrb, E_NOTIMP_ERROR, "readlink is not supported on this platform");
362 return mrb_nil_value(); // unreachable
363#else
364 char *path, *buf;
365 size_t bufsize = 100;
366 ssize_t rc;
367 mrb_value ret;
368 int ai = mrb_gc_arena_save(mrb);
369
370 mrb_get_args(mrb, "z", &path);
371
372 buf = (char *)mrb_malloc(mrb, bufsize);
373 while ((rc = readlink(path, buf, bufsize)) == bufsize && rc != -1) {
374 bufsize *= 2;
375 buf = (char *)mrb_realloc(mrb, buf, bufsize);
376 }
377 if (rc == -1) {
378 mrb_free(mrb, buf);
379 mrb_sys_fail(mrb, path);
380 }
381 ret = mrb_str_new(mrb, buf, rc);
382 mrb_free(mrb, buf);
383
384 mrb_gc_arena_restore(mrb, ai);
385 return ret;
386#endif
387}
388
389void
390mrb_init_file(mrb_state *mrb)
391{
392 struct RClass *io, *file, *cnst;
393
394 io = mrb_class_get(mrb, "IO");
395 file = mrb_define_class(mrb, "File", io);
396 MRB_SET_INSTANCE_TT(file, MRB_TT_DATA);
397 mrb_define_class_method(mrb, file, "umask", mrb_file_s_umask, MRB_ARGS_REQ(1));
398 mrb_define_class_method(mrb, file, "delete", mrb_file_s_unlink, MRB_ARGS_ANY());
399 mrb_define_class_method(mrb, file, "unlink", mrb_file_s_unlink, MRB_ARGS_ANY());
400 mrb_define_class_method(mrb, file, "rename", mrb_file_s_rename, MRB_ARGS_REQ(2));
401 mrb_define_class_method(mrb, file, "symlink", mrb_file_s_symlink, MRB_ARGS_REQ(2));
402 mrb_define_class_method(mrb, file, "chmod", mrb_file_s_chmod, MRB_ARGS_REQ(1) | MRB_ARGS_REST());
403 mrb_define_class_method(mrb, file, "readlink", mrb_file_s_readlink, MRB_ARGS_REQ(1));
404
405 mrb_define_class_method(mrb, file, "dirname", mrb_file_dirname, MRB_ARGS_REQ(1));
406 mrb_define_class_method(mrb, file, "basename", mrb_file_basename, MRB_ARGS_REQ(1));
407 mrb_define_class_method(mrb, file, "realpath", mrb_file_realpath, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
408 mrb_define_class_method(mrb, file, "_getwd", mrb_file__getwd, MRB_ARGS_NONE());
409 mrb_define_class_method(mrb, file, "_gethome", mrb_file__gethome, MRB_ARGS_OPT(1));
410
411 mrb_define_method(mrb, file, "flock", mrb_file_flock, MRB_ARGS_REQ(1));
412
413 cnst = mrb_define_module_under(mrb, file, "Constants");
414 mrb_define_const(mrb, cnst, "LOCK_SH", mrb_fixnum_value(LOCK_SH));
415 mrb_define_const(mrb, cnst, "LOCK_EX", mrb_fixnum_value(LOCK_EX));
416 mrb_define_const(mrb, cnst, "LOCK_UN", mrb_fixnum_value(LOCK_UN));
417 mrb_define_const(mrb, cnst, "LOCK_NB", mrb_fixnum_value(LOCK_NB));
418 mrb_define_const(mrb, cnst, "SEPARATOR", mrb_str_new_cstr(mrb, FILE_SEPARATOR));
419 mrb_define_const(mrb, cnst, "PATH_SEPARATOR", mrb_str_new_cstr(mrb, PATH_SEPARATOR));
420#if defined(_WIN32) || defined(_WIN64)
421 mrb_define_const(mrb, cnst, "ALT_SEPARATOR", mrb_str_new_cstr(mrb, FILE_ALT_SEPARATOR));
422#else
423 mrb_define_const(mrb, cnst, "ALT_SEPARATOR", mrb_nil_value());
424#endif
425 mrb_define_const(mrb, cnst, "NULL", mrb_str_new_cstr(mrb, NULL_FILE));
426
427}
Note: See TracBrowser for help on using the repository browser.