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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include "md4c-html.h"
#include "bindings.h"
#include "logging.h"
struct concat_buffer {
char *buf;
size_t size;
int index;
bool ok;
};
static void md_callback(const MD_CHAR *html, MD_SIZE size, void *data)
{
argent_log(TRACE, "begin md_callback()");
struct concat_buffer *d = data;
if (!d->ok)
return;
if (d->index + size >= d->size) {
char *new_buf = realloc(d->buf, d->size * 2);
if (new_buf == NULL) {
// failed to allocate memory, abort!
argent_log(WARN, "failed to properly allocate memory for html buffer!");
d->ok = false;
return;
}
d->buf = new_buf;
d->size *= 2;
}
memcpy((d->buf) + d->index, html, size);
d->index += size;
argent_log(TRACE, "finish md_callback()");
}
int markdown(lua_State *L)
{
argent_log(TRACE, "begin markdown parsing");
char *markdown_buffer = luaL_checkstring(L, 1);
size_t len = strlen(markdown_buffer);
argent_log(TRACE, "markdown input (%ld bytes): %s", len, markdown_buffer);
unsigned int md_flags = MD_FLAG_TABLES | MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE;
struct concat_buffer data;
data.buf = malloc(2048 * sizeof(char));
data.size = 2048 * sizeof(char);
data.index = 0;
data.ok = true;
argent_log(TRACE, "call md_html()");
// fill out the buffer
int error = md_html(markdown_buffer, len, md_callback, &data, md_flags, 0);
if (error)
luaL_error(L, "markdown parsing failed!");
if (data.ok == false)
luaL_error(L, "encountered error (re)allocating html buffer memory!");
data.buf[data.index] = 0;
argent_log(TRACE, "finished HTML buffer (%d bytes, %d chars): %s",
data.size, data.index, data.buf);
lua_pushstring(L, data.buf);
free(data.buf);
argent_log(TRACE, "finish markdown parsing");
return 1;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int current_working_directory(lua_State *L)
{
char *cwd = getcwd(NULL, 0);
lua_pushstring(L, cwd);
free(cwd);
return 1;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
static void throw_directory_error(lua_State *L, char *dir_name);
static struct dirent *read_dir(lua_State *L, DIR *directory);
int scan_directory(lua_State *L)
{
argent_log(TRACE, "begin scan_directory()");
char *dir_name = luaL_checkstring(L, 1);
DIR *directory = opendir(dir_name);
if (directory == NULL)
throw_directory_error(L, dir_name);
lua_newtable(L);
int directory_table = lua_gettop(L);
int directory_index = 1;
lua_newtable(L);
int file_table = lua_gettop(L);
int file_index = 1;
struct dirent *entry;
while ((entry = read_dir(L, directory)) != NULL) {
lua_pushstring(L, entry->d_name);
if (entry->d_type == DT_DIR) {
lua_rawseti(L, directory_table, directory_index);
directory_index += 1;
}
else {
lua_rawseti(L, file_table, file_index);
file_index += 1;
}
}
closedir(directory);
argent_log(TRACE, "end scan_directory()");
return 2;
}
static void throw_directory_error(lua_State *L, char *dir_name)
{
argent_log(ERROR, "failed to open directory: %d\n", errno);
switch(errno) {
case EACCES:
luaL_error(L, "read %s: permission denied", dir_name);
break;
case EMFILE:
luaL_error(L, "read %s: this process has too many open files", dir_name);
break;
case ENFILE:
luaL_error(L, "read %s: this file system cannot support any more open files", dir_name);
break;
case ENOMEM:
luaL_error(L, "read %s: Not enough memory", dir_name);
break;
default:
luaL_error(L, "read %s: unknown error", dir_name);
break;
}
}
static struct dirent *read_dir(lua_State *L, DIR *directory)
{
// can't tell if a NULL was EOF or error without this
errno = 0;
struct dirent *entry = readdir(directory);
if (entry == NULL && errno != 0) {
argent_log(ERROR, "attempted to read invalid dirstream");
closedir(directory);
luaL_error(L, "attempted to read invalid dirstream");
}
return entry;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
static const char* mkdir_error_string()
{
switch(errno) {
case EACCES:
return "Permission denied";
case EEXIST:
return "A file with that name already exists";
case EMLINK:
return "The parent directory is full";
case ENOSPC:
return "The file system is full";
case EROFS:
return "The parent directory is read-only";
default:
return "unknown error";
}
}
int create_directory(lua_State *L)
{
char *dir_name = luaL_checkstring(L, 1);
mode_t mode = S_IRWXU | S_IRWXG | (S_IROTH | S_IXOTH); // u+rwx, g+rwx, a+rx
int error = mkdir(dir_name, mode);
if (error) {
argent_log(ERROR, "failed to create directory '%s': %s",
dir_name, mkdir_error_string());
luaL_error(L, "failed to create directory '%s': %s",
dir_name, mkdir_error_string());
}
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
static const char* open_error_string()
{
switch (errno) {
case EACCES:
return "Permission denied";
case EBUSY:
return "Device busy";
case EDQUOT:
return "User cannot create new inodes";
case EEXIST:
return "File exists";
case EFAULT:
return "Pathname points outside your accessible address space";
case EINTR:
return "Operation interrupted";
case EINVAL:
return "Attempted an invalid operation";
case ELOOP:
return "Too many symbolic links encountered in resolving the pathname";
case EMFILE:
return "The process has too many open files";
case ENAMETOOLONG:
return "The pathname was too long";
case ENOMEM:
return "Insufficient kernel memory available";
case ENOSPC:
return "No space available on the device";
case EOVERFLOW:
return "File is too large to open";
case ETXTBSY:
return "File is busy";
default:
return "Other error";
}
}
int copy_file(lua_State *L)
{
char *source_name = luaL_checkstring(L, 1);
char *dest_name = luaL_checkstring(L, 2);
int source_fd, dest_fd;
source_fd = open(source_name, 0);
if (source_fd == -1) {
argent_log(ERROR, "failed to open file '%s': %s",
source_name, open_error_string());
luaL_error(L, "failed to open file '%s': %s",
source_name, open_error_string());
}
struct stat source_stat;
int error = fstat(source_fd, &source_stat);
if (error) {
close(source_fd);
argent_log(ERROR, "failed to stat '%s': %s",
source_name, open_error_string());
luaL_error(L, "failed to stat '%s': %s",
source_name, open_error_string());
}
mode_t dest_mode = source_stat.st_mode;
dest_fd = creat(dest_name, dest_mode);
if (dest_fd == -1) {
close(source_fd);
argent_log(ERROR, "failed to open file '%s': %s",
dest_name, open_error_string());
luaL_error(L, "failed to open file '%s': %s",
dest_name, open_error_string());
}
unsigned char buffer[256];
size_t buffer_size = sizeof(unsigned char) * 256;
ssize_t bytes_read;
while ( (bytes_read = read(source_fd, buffer, buffer_size)) == buffer_size ) {
write(dest_fd, buffer, buffer_size);
}
if (bytes_read == -1) {
argent_log(ERROR, "failed to write to file '%s'", dest_name);
close(source_fd);
close(dest_fd);
luaL_error(L, "failed to write to file '%s'", dest_name);
}
else {
write(dest_fd, buffer, bytes_read);
}
close(source_fd);
close(dest_fd);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int argent_log_lua(lua_State *L)
{
char *level_string = luaL_checkstring(L, 1);
char *message = luaL_checkstring(L, 2);
int level;
if(strcmp(level_string, "fatal") == 0)
level = FATAL;
else if (strcmp(level_string, "error") == 0)
level = ERROR;
else if (strcmp(level_string, "warn") == 0)
level = WARN;
else if (strcmp(level_string, "info") == 0)
level = INFO;
else if (strcmp(level_string, "debug") == 0)
level = DEBUG;
else if (strcmp(level_string, "trace") == 0)
level = TRACE;
else {
argent_log(WARN, "level '%s' is invalid; defaulting to 'info'", level_string);
level = INFO;
}
argent_log(level, message);
return 0;
}
|