-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplugins.c
More file actions
244 lines (217 loc) · 5.52 KB
/
plugins.c
File metadata and controls
244 lines (217 loc) · 5.52 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
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
#include "../build.h"
#include "../util.h"
#include "../file.h"
#include "../strs.h"
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
static h_err* build_plugin(
const char* dirpath,
const char* outdirphp,
const char* outdirmisc,
h_build_outfiles outfiles,
const h_conf* conf)
{
char* confjsonpath = h_util_path_join(dirpath, H_FILE_PLUGIN_CONF);
if (confjsonpath == NULL)
return h_err_create(H_ERR_ALLOC, NULL);
errno = 0;
char* confjson = h_util_file_read(confjsonpath);
if (confjson == NULL && errno)
{
h_err* err = h_err_from_errno(errno, confjsonpath);
free(confjsonpath);
return err;
}
free(confjsonpath);
if (confjson == NULL)
return h_err_create(H_ERR_ALLOC, NULL);
int jsonlen = strlen(confjson);
int rootlen = strlen(conf->root);
char* jspath = h_util_path_join(dirpath, H_FILE_PLUGIN_JS);
if (jspath == NULL)
{
free(confjson);
return h_err_create(H_ERR_ALLOC, NULL);
}
char* phppath = h_util_path_join(dirpath, H_FILE_PLUGIN_PHP);
if (phppath == NULL)
{
free(confjson);
free(jspath);
return h_err_create(H_ERR_ALLOC, NULL);
}
char* miscpath = h_util_path_join(dirpath, H_FILE_PLUGIN_MISC);
if (miscpath == NULL)
{
free(confjson);
free(jspath);
free(phppath);
return h_err_create(H_ERR_ALLOC, NULL);
}
//Copy things to script.js
if (h_util_file_err(jspath) != ENOENT)
{
char* starttemplate = H_STRS_JS_START;
int len = sizeof(char) * (strlen(starttemplate) + jsonlen + rootlen);
char* start = malloc(len);
snprintf(start, len, starttemplate, confjson, conf->root);
char* end = H_STRS_JS_END;
h_util_cp_dir_to_file_se(jspath, outfiles.js, start, end);
free(start);
}
//Copy PHP files
if (h_util_file_err(phppath) != ENOENT)
{
const char* starttemplate = H_STRS_PHP_START;
int len = sizeof(char) * (strlen(starttemplate) + jsonlen) + rootlen;
char* start = malloc(len);
snprintf(start, len, starttemplate, confjson, conf->root);
//Make sure dirs are okay
DIR* d1 = opendir(phppath);
if (d1 == NULL)
return h_err_from_errno(errno, phppath);
closedir(d1);
if (mkdir(outdirphp, 0777) == -1 && errno != EEXIST)
return h_err_from_errno(errno, outdirphp);
h_util_cp_dir_se(phppath, outdirphp, start, "");
free(start);
}
//Copy misc files
if (h_util_file_err(miscpath) != ENOENT)
{
//Make sure dirs are okay
DIR* d1 = opendir(miscpath);
if (d1 == NULL)
return h_err_from_errno(errno, miscpath);
closedir(d1);
if (mkdir(outdirmisc, 0777) == -1 && errno != EEXIST)
return h_err_from_errno(errno, outdirmisc);
h_util_cp_dir(miscpath, outdirmisc);
}
free(confjson);
free(phppath);
free(jspath);
free(miscpath);
return NULL;
}
h_err* h_build_plugins(const char* rootdir, h_build_outfiles outfiles, const h_conf* conf)
{
char* pluginsdir = h_util_path_join(rootdir, H_FILE_PLUGINS);
if (pluginsdir == NULL)
return h_err_create(H_ERR_ALLOC, NULL);
char* outpluginsdirphp = h_util_path_join(
rootdir,
H_FILE_OUTPUT "/" H_FILE_OUT_META "/" H_FILE_OUT_PHP
);
if (outpluginsdirphp == NULL)
{
free(pluginsdir);
return h_err_create(H_ERR_ALLOC, NULL);
}
char* outpluginsdirmisc = h_util_path_join(
rootdir,
H_FILE_OUTPUT "/" H_FILE_OUT_META "/" H_FILE_OUT_MISC
);
if (outpluginsdirmisc == NULL)
{
free(pluginsdir);
free(outpluginsdirphp);
return h_err_create(H_ERR_ALLOC, NULL);
}
//Check status of rootdir/plugins, returning if it doesn't exist
{
int err = h_util_file_err(pluginsdir);
if (err == ENOENT)
{
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return NULL;
}
if (err && err != EEXIST)
{
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return h_err_from_errno(err, pluginsdir);
}
}
//Create dirs if they don't exist
if (mkdir(outpluginsdirphp, 0777) == -1 && errno != EEXIST) {
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return h_err_from_errno(errno, outpluginsdirphp);
}
if (mkdir(outpluginsdirmisc, 0777) == -1 && errno != EEXIST) {
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return h_err_from_errno(errno, outpluginsdirmisc);
}
//Loop through plugins, building them
struct dirent** namelist;
int n = scandir(pluginsdir, &namelist, NULL, alphasort);
int i;
for (i = 0; i < n; ++i)
{
struct dirent* ent = namelist[i];
if (ent->d_name[0] == '.')
{
free(ent);
continue;
}
char* dirpath = h_util_path_join(pluginsdir, ent->d_name);
if (dirpath == NULL)
{
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return h_err_create(H_ERR_ALLOC, NULL);
}
char* outdirphp = h_util_path_join(outpluginsdirphp, ent->d_name);
if (outdirphp == NULL)
{
free(dirpath);
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return h_err_create(H_ERR_ALLOC, NULL);
}
char* outdirmisc = h_util_path_join(outpluginsdirmisc, ent->d_name);
if (outdirmisc == NULL)
{
free(dirpath);
free(outdirphp);
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return h_err_create(H_ERR_ALLOC, NULL);
}
h_err* err;
err = build_plugin(dirpath, outdirphp, outdirmisc, outfiles, conf);
if (err)
{
free(dirpath);
free(outdirphp);
free(outdirmisc);
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(pluginsdir);
return err;
}
free(dirpath);
free(outdirphp);
free(outdirmisc);
free(ent);
}
free(pluginsdir);
free(outpluginsdirphp);
free(outpluginsdirmisc);
free(namelist);
return NULL;
}