| 1 |
#include <sys/types.h> |
| 2 |
#include <sys/stat.h> |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#if defined(_WIN32) || defined(_WIN64) |
| 9 |
#ifdef __cplusplus |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
typedef struct DIR DIR; |
| 13 |
struct dirent |
| 14 |
{ |
| 15 |
char *d_name; |
| 16 |
}; |
| 17 |
DIR *opendir(const char *name); |
| 18 |
struct dirent *readdir(DIR *dir); |
| 19 |
int closedir(DIR *dir); |
| 20 |
#ifdef __cplusplus |
| 21 |
} |
| 22 |
#endif |
| 23 |
#include <io.h> |
| 24 |
#include <direct.h> |
| 25 |
#define rmdir(path) _rmdir(path) |
| 26 |
#define getcwd(path,len) _getcwd(path,len) |
| 27 |
#define mkdir(path,mode) _mkdir(path) |
| 28 |
#define chdir(path) _chdir(path) |
| 29 |
#else |
| 30 |
#include <unistd.h> |
| 31 |
#include <dirent.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "mruby.h" |
| 35 |
#include "mruby/string.h" |
| 36 |
#include "mruby/variable.h" |
| 37 |
|
| 38 |
static void |
| 39 |
make_dir(mrb_state *mrb, const char *name, const char *up) |
| 40 |
{ |
| 41 |
if (mkdir(name, 0) == -1) { |
| 42 |
if (chdir("..") == 0) { |
| 43 |
rmdir(up); |
| 44 |
} |
| 45 |
mrb_raisef(mrb, E_RUNTIME_ERROR, "mkdir(%s) failed", mrb_str_new_cstr(mrb, name)); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
mrb_value |
| 50 |
mrb_dirtest_setup(mrb_state *mrb, mrb_value klass) |
| 51 |
{ |
| 52 |
char buf[1024]; |
| 53 |
const char *aname = "a"; |
| 54 |
const char *bname = "b"; |
| 55 |
|
| 56 |
/* save current working directory */ |
| 57 |
if (getcwd(buf, sizeof(buf)) == NULL) { |
| 58 |
mrb_raise(mrb, E_RUNTIME_ERROR, "getcwd() failed"); |
| 59 |
} |
| 60 |
mrb_cv_set(mrb, klass, mrb_intern_cstr(mrb, "pwd"), mrb_str_new_cstr(mrb, buf)); |
| 61 |
|
| 62 |
/* create sandbox */ |
| 63 |
#if defined(_WIN32) || defined(_WIN64) |
| 64 |
snprintf(buf, sizeof(buf), "%s\\mruby-dir-test.XXXXXX", _getcwd(NULL,0)); |
| 65 |
if ((_mktemp(buf) == NULL) || mkdir(buf,0) != 0) { |
| 66 |
mrb_raisef(mrb, E_RUNTIME_ERROR, "mkdtemp(%s) failed", buf); |
| 67 |
} |
| 68 |
#else |
| 69 |
snprintf(buf, sizeof(buf), "%s/mruby-dir-test.XXXXXX", P_tmpdir); |
| 70 |
if (mkdtemp(buf) == NULL) { |
| 71 |
mrb_raisef(mrb, E_RUNTIME_ERROR, "mkdtemp(%s) failed", buf); |
| 72 |
} |
| 73 |
#endif |
| 74 |
mrb_cv_set(mrb, klass, mrb_intern_cstr(mrb, "sandbox"), mrb_str_new_cstr(mrb, buf)); |
| 75 |
|
| 76 |
/* go to sandbox */ |
| 77 |
if (chdir(buf) == -1) { |
| 78 |
rmdir(buf); |
| 79 |
mrb_raisef(mrb, E_RUNTIME_ERROR, "chdir(%s) failed", buf); |
| 80 |
} |
| 81 |
|
| 82 |
/* make some directories in the sandbox */ |
| 83 |
make_dir(mrb, aname, buf); |
| 84 |
make_dir(mrb, bname, buf); |
| 85 |
|
| 86 |
return mrb_true_value(); |
| 87 |
} |
| 88 |
|
| 89 |
mrb_value |
| 90 |
mrb_dirtest_teardown(mrb_state *mrb, mrb_value klass) |
| 91 |
{ |
| 92 |
mrb_value d, sandbox; |
| 93 |
DIR *dirp; |
| 94 |
struct dirent *dp; |
| 95 |
const char *path; |
| 96 |
|
| 97 |
/* cleanup sandbox */ |
| 98 |
sandbox = mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "sandbox")); |
| 99 |
path = mrb_str_to_cstr(mrb, sandbox); |
| 100 |
|
| 101 |
dirp = opendir(path); |
| 102 |
while ((dp = readdir(dirp)) != NULL) { |
| 103 |
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) |
| 104 |
continue; |
| 105 |
if (rmdir(dp->d_name) == -1) { |
| 106 |
mrb_raisef(mrb, E_RUNTIME_ERROR, "rmdir(%s) failed", dp->d_name); |
| 107 |
} |
| 108 |
} |
| 109 |
closedir(dirp); |
| 110 |
|
| 111 |
/* back to original pwd */ |
| 112 |
d = mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "pwd")); |
| 113 |
path = mrb_str_to_cstr(mrb, d); |
| 114 |
if (chdir(path) == -1) { |
| 115 |
mrb_raisef(mrb, E_RUNTIME_ERROR, "chdir(%s) failed", path); |
| 116 |
} |
| 117 |
|
| 118 |
/* remove sandbox directory */ |
| 119 |
sandbox = mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "sandbox")); |
| 120 |
path = mrb_str_to_cstr(mrb, sandbox); |
| 121 |
if (rmdir(path) == -1) { |
| 122 |
mrb_raisef(mrb, E_RUNTIME_ERROR, "rmdir(%s) failed", path); |
| 123 |
} |
| 124 |
|
| 125 |
return mrb_true_value(); |
| 126 |
} |
| 127 |
|
| 128 |
mrb_value |
| 129 |
mrb_dirtest_sandbox(mrb_state *mrb, mrb_value klass) |
| 130 |
{ |
| 131 |
return mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "sandbox")); |
| 132 |
} |
| 133 |
|
| 134 |
void |
| 135 |
mrb_mruby_dir_gem_test(mrb_state *mrb) |
| 136 |
{ |
| 137 |
struct RClass *c = mrb_define_module(mrb, "DirTest"); |
| 138 |
|
| 139 |
mrb_define_class_method(mrb, c, "sandbox", mrb_dirtest_sandbox, MRB_ARGS_NONE()); |
| 140 |
mrb_define_class_method(mrb, c, "setup", mrb_dirtest_setup, MRB_ARGS_NONE()); |
| 141 |
mrb_define_class_method(mrb, c, "teardown", mrb_dirtest_teardown, MRB_ARGS_NONE()); |
| 142 |
} |