-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdir.c
More file actions
202 lines (187 loc) · 5.82 KB
/
Copy pathdir.c
File metadata and controls
202 lines (187 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <dirent.h>
#include <glob.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "object.h"
#include "vm.h"
#include "runtime.h"
#include "table.h"
#include "memory.h"
ObjClass *lxDirClass = NULL;
typedef struct LxDir {
DIR *dir;
bool open;
} LxDir;
static inline LxDir *dirGetHidden(Value dir) {
return (LxDir*)AS_INSTANCE(dir)->internal->data;
}
static void freeInternalDir(Obj *internalObj) {
ASSERT(internalObj->type == OBJ_T_INTERNAL);
ObjInternal *internal = (ObjInternal*)internalObj;
LxDir *ldir = (LxDir*)internal->data;
if (ldir->open) {
closedir(ldir->dir);
}
}
static Value lxDirInit(int argCount, Value *args) {
CHECK_ARITY("Dir#init", 2, 2, argCount);
ObjInstance *selfObj = AS_INSTANCE(*args);
Value name = args[1];
CHECK_ARG_IS_A(name, lxStringClass, 1);
int last = errno;
const char *dirStr = AS_STRING(name)->chars;
DIR *d = opendir(dirStr);
if (!d) {
int err = errno;
errno = last;
throwArgErrorFmt("Given directory '%s' could not be opened: %s",
dirStr, strerror(err));
}
ObjInternal *internalObj = newInternalObject(false, NULL, sizeof(LxDir), NULL, freeInternalDir, NEWOBJ_FLAG_NONE);
selfObj->internal = internalObj;
LxDir *ldir = ALLOCATE(LxDir, 1);
ldir->dir = d;
ldir->open = true;
internalObj->data = ldir;
return *args;
}
static Value lxDirIterNext(int argCount, Value *args) {
LxDir *dir = dirGetHidden(*args);
struct dirent *de;
if ((de = readdir(dir->dir))) {
return OBJ_VAL(copyString(de->d_name, strlen(de->d_name), NEWOBJ_FLAG_NONE));
} else {
return NIL_VAL;
}
}
static Value lxDirClose(int argCount, Value *args) {
CHECK_ARITY("Dir#close", 1, 1, argCount);
LxDir *ldir = dirGetHidden(*args);
if (!ldir->open) {
return FALSE_VAL;
}
int res = closedir(ldir->dir); // TODO: Should we throw here?
ldir->open = false;
return BOOL_VAL(res == 0);
}
static Value lxDirRewind(int argCount, Value *args) {
CHECK_ARITY("Dir#rewind", 1, 1, argCount);
LxDir *ldir = dirGetHidden(*args);
if (!ldir->open) {
return FALSE_VAL; // TODO: should we throw here?
}
rewinddir(ldir->dir);
return TRUE_VAL;
}
static Value lxDirPwdStatic(int argCount, Value *args) {
CHECK_ARITY("Dir.pwd", 1, 1, argCount);
char buf[4096];
int last = errno;
char *res = getcwd(buf, 4096);
if (!res) {
int err = errno;
errno = last;
throwErrorFmt(sysErrClass(err), "Couldn't get current directory: %s", strerror(err));
}
return OBJ_VAL(copyString(buf, strlen(buf), NEWOBJ_FLAG_NONE));
}
static Value lxDirMkdirStatic(int argCount, Value *args) {
CHECK_ARITY("Dir.mkdir", 2, 3, argCount);
Value nameStrVal = args[1];
int mode = 0777; // umask is applied
CHECK_ARG_IS_A(nameStrVal, lxStringClass, 1);
if (argCount == 3) {
Value modeVal = args[2];
CHECK_ARG_BUILTIN_TYPE(modeVal, IS_NUMBER_FUNC, "number", 3);
mode = AS_NUMBER(modeVal);
}
char *nameCStr = AS_STRING(nameStrVal)->chars;
releaseGVL(THREAD_STOPPED);
int res = mkdir(nameCStr, mode);
acquireGVL();
int last = errno;
if (res != 0) {
int err = errno;
errno = last;
throwErrorFmt(sysErrClass(err), "Couldn't create directory %s: %s", nameCStr, strerror(err));
}
return TRUE_VAL;
}
static Value lxDirGlobStatic(int argCount, Value *args) {
CHECK_ARITY("Dir.glob", 2, 2, argCount);
Value globStrVal = args[1];
CHECK_ARG_IS_A(globStrVal, lxStringClass, 1);
char *globCStr = AS_STRING(globStrVal)->chars;
glob_t globBuf;
Value ary = newArray();
releaseGVL(THREAD_STOPPED);
int res = glob((const char *)globCStr, GLOB_BRACE, NULL, &globBuf);
acquireGVL();
if (res == GLOB_NOMATCH) {
globfree(&globBuf);
return ary;
} else if (res != 0) {
globfree(&globBuf);
// TODO: raise?
return ary;
}
int i = 0;
char *path = NULL;
while ((path = globBuf.gl_pathv[i])) {
Value pathVal = OBJ_VAL(copyString(path, strlen(path), NEWOBJ_FLAG_NONE));
arrayPush(ary, pathVal);
i++;
}
globfree(&globBuf);
return ary;
}
static Value lxDirChdirStatic(int argCount, Value *args) {
CHECK_ARITY("Dir.chdir", 2, 2, argCount);
Value newDir = args[1];
CHECK_ARG_IS_A(newDir, lxStringClass, 1);
char *oldDir = NULL;
char buf[4096];
oldDir = getcwd(buf, 4096);
char *dirStr = AS_STRING(newDir)->chars;
int last = errno;
int res = chdir(dirStr);
if (res != 0) {
int err = errno;
errno = last;
throwErrorFmt(sysErrClass(err), "Couldn't change directory to '%s': %s",
dirStr,
strerror(err));
}
Value ret = newDir;
if (blockGiven()) {
Value err = NIL_VAL;
ret = yieldBlockCatch(0, NULL, &err);
int res = 0;
if (oldDir) {
res = chdir(oldDir);
}
if (res != 0) {
fprintf(stderr, "[Warning]: Couldn't change back to old directory: '%s'\n",
oldDir);
}
if (!IS_NIL(err)) {
throwError(err);
}
}
return ret;
}
void Init_DirClass() {
lxDirClass = addGlobalClass("Dir", lxObjClass);
addNativeMethod(lxDirClass, "init", lxDirInit);
addNativeMethod(lxDirClass, "close", lxDirClose);
addNativeMethod(lxDirClass, "rewind", lxDirRewind);
addNativeMethod(lxDirClass, "iterNext", lxDirIterNext);
ObjClass *dirStatic = classSingletonClass(lxDirClass);
addNativeMethod(dirStatic, "mkdir", lxDirMkdirStatic);
addNativeMethod(dirStatic, "pwd", lxDirPwdStatic);
addNativeMethod(dirStatic, "chdir", lxDirChdirStatic);
addNativeMethod(dirStatic, "glob", lxDirGlobStatic);
}