-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_handlers.c
512 lines (391 loc) · 13.5 KB
/
fs_handlers.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
fs_handlers.c - An embedded CNC Controller with rs274/ngc (g-code) support
WebUI backend - file handling
Part of grblHAL
Copyright (c) 2020-2024 Terje Io
Some parts of the code is based on test code by francoiscolas
https://github.com/francoiscolas/multipart-parser/blob/master/tests.cpp
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef ARDUINO
#include "../driver.h"
#else
#include "driver.h"
#endif
#if WEBUI_ENABLE
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "../networking/httpd.h"
#include "../networking/http_upload.h"
#include "../networking/utils.h"
#include "../networking/strutils.h"
#include "../networking/cJSON.h"
#include "grbl/vfs.h"
static bool file_is_json = false;
static void data_is_json (void)
{
file_is_json = true;
}
// add file to the JSON response array
static bool add_file (cJSON *files, char *path, vfs_dirent_t *file, time_t *mtime)
{
bool ok;
cJSON *fileinfo;
if((ok = (fileinfo = cJSON_CreateObject()) != NULL)) {
ok = !!cJSON_AddStringToObject(fileinfo, "name", file->name);
#if WEBUI_ENABLE != 2
if(mtime)
ok = ok & !!cJSON_AddStringToObject(fileinfo, "time", strtoisodt(gmtime(mtime)));
#else
ok = ok & !!cJSON_AddStringToObject(fileinfo, "datetime", "");
ok = ok & !!cJSON_AddStringToObject(fileinfo, "shortname", file->name);
#endif
if(file->st_mode.directory)
ok = ok & !!cJSON_AddNumberToObject(fileinfo, "size", -1.0);
else
ok = ok & !!cJSON_AddStringToObject(fileinfo, "size", btoa(file->size));
if(!ok)
cJSON_Delete(fileinfo);
}
return ok && cJSON_AddItemToArray(files, fileinfo);
}
static char *get_fullpath (char *path, char *filename)
{
static char fullpath[255];
return strcat(strcat(strcpy(fullpath, vfs_fixpath(path)), "/"), filename);
}
static int sd_scan_dir (cJSON *files, char *path, uint_fast8_t depth)
{
int res = 0;
bool scan = true, subdirs = false;
vfs_dir_t *dir;
vfs_dirent_t *dirent;
#if WEBUI_ENABLE != 2
vfs_stat_t st;
#endif
if((dir = vfs_opendir(path)) == NULL)
return vfs_errno;
// Pass 1: Scan files
while(scan) {
if((dirent = vfs_readdir(dir)) == NULL)
break;
subdirs |= dirent->st_mode.directory;
if(!dirent->st_mode.directory) {
#if WEBUI_ENABLE != 2
int res = vfs_stat(get_fullpath(path, dirent->name), &st);
#if ESP_PLATFORM
scan = add_file(files, path, dirent, res == 0 ? &st.st_mtim : NULL);
#else
scan = add_file(files, path, dirent, res == 0 ? &st.st_mtime : NULL);
#endif
#else
scan = add_file(files, path, dirent, NULL);
#endif
}
}
vfs_closedir(dir);
dir = NULL;
if((subdirs = (scan && subdirs && depth)))
subdirs = (dir = vfs_opendir(path)) != NULL;
// Pass 2: Scan directories
while(subdirs) {
if((dirent = vfs_readdir(dir)) == NULL)
break;
if(dirent->st_mode.directory) {
size_t pathlen = strlen(path);
// if(pathlen + strlen(get_name(&fno)) > (MAX_PATHLEN - 1))
//break;
subdirs = add_file(files, path, dirent, NULL);
if(depth > 1) {
sprintf(&path[pathlen], "/%s", dirent->name);
if((res = sd_scan_dir(files, path, depth - 1)) != 0)
break;
path[pathlen] = '\0';
}
}
}
if(dir)
vfs_closedir(dir);
return res;
}
bool fs_ls (cJSON *root, char *path, char *status, vfs_drive_t *drive)
{
bool ok;
cJSON *files = NULL;
if((ok = (root && (files = cJSON_AddArrayToObject(root, "files"))))) {
vfs_fixpath(path);
if((ok = sd_scan_dir(files, path, 1) == 0 && !!cJSON_AddStringToObject(root, "path", path))) {
vfs_free_t *mount;
if(drive)
mount = vfs_drive_getfree(drive);
else
mount = vfs_fgetfree(path);
if(mount) {
uint32_t pct_used = (mount->used * 100) / mount->size;
ok = ok & !!cJSON_AddStringToObject(root, "total", btoa(mount->size));
ok = ok & !!cJSON_AddStringToObject(root, "used", btoa(mount->used));
ok = ok & !!cJSON_AddStringToObject(root, "occupation", uitoa(pct_used == 0 ? 1 : pct_used));
}
if(status) {
ok = ok & !!cJSON_AddStringToObject(root, "mode", "direct");
ok = ok & !!cJSON_AddStringToObject(root, "status", status);
}
}
}
return ok;
}
static bool _fs_ls (void *request, char *path, char *status, vfs_file_t *file, vfs_drive_t *drive)
{
bool ok = false;
cJSON *root = cJSON_CreateObject();
if(root && fs_ls(root, path, status, drive)) {
char *resp = cJSON_PrintUnformatted(root);
if((ok = !!resp)) {
cJSON_Delete(root);
data_is_json();
vfs_puts(resp, file);
cJSON_free(resp);
}
http_set_response_header(request, "Cache-Control", "no-cache");
}
if(root && !ok)
cJSON_Delete(root);
return ok;
}
const char *fs_action_handler (http_request_t *request, vfs_drive_t *drive)
{
bool ok = false;
char path[100], filename[100], *fullname;
char action[20], status[sizeof(filename) + 50], quiet[4];
uint_fast16_t pathlen;
if(drive == NULL) {
http_set_response_status(request, "400 Bad Request");
return NULL;
}
// if(!is_authorized(request, WebUIAuth_User))
// return ESP_OK;
*status = *path = '\0';
http_get_param_value(request, "path", filename, sizeof(filename));
if(strncmp(filename, drive->path, strlen(drive->path)))
strcpy(path, drive->path);
else
strcpy(path, "/");
strcat(path, *filename == '/' ? filename + 1 : filename);
http_get_param_value(request, "filename", filename, sizeof(filename));
http_get_param_value(request, "action", action, sizeof(action));
http_get_param_value(request, "quiet", quiet, sizeof(quiet));
pathlen = strlen(path);
if(pathlen > 1 && path[pathlen - 1] != '/') {
path[pathlen] = '/';
path[pathlen + 1] = '\0';
}
vfs_file_t *file = vfs_open("/ram/qry.json", "w");
if(*action && *filename) {
vfs_stat_t file;
// char *fullname = ((file_server_data_t *)req->user_ctx)->scratch;
fullname = get_fullpath(path, filename);
/*
if(strlen(drive->path) > 1 || *drive->path != '/')
strcat(strcat(strcpy(fullname, drive->path), "/"), filename);
else
strcat(strcpy(fullname, path), filename);
*/
switch(strlookup(action, "delete,createdir,deletedir,list", ',')) {
case 0: // delete
if(vfs_stat(fullname, &file) == 0) {
if(!(file.st_mode.directory) && vfs_unlink(fullname) == 0)
sprintf(status, "%s deleted", filename);
else
sprintf(status, "Cannot delete %s!", filename);
} else
sprintf(status, "%s does not exist!", filename);
break;
case 1: // createdir
if(vfs_stat(fullname, &file) != 0) {
if(vfs_mkdir(fullname) == 0)
sprintf(status, "%s created", filename);
else
sprintf(status, "Cannot create %s!", filename);
} else
sprintf(status, "%s already exists!", filename);
break;
case 2: // deletedir
if(strlen(fullname) == 1)
strcpy(status, "Cannot delete root directory!");
else if(vfs_stat(fullname, &file) == 0) {
if(vfs_rmdir(fullname) == 0)
sprintf(status, "%s deleted", filename);
else
sprintf(status, "Error deleting %s!", filename);
} else
sprintf(status, "%s does not exist!", filename);
break;
case 3: // list - do nothing here
break;
default:
sprintf(status, "Invalid action \"%s\" for %s!", action, filename);
break;
}
}
if(*path == '\0')
strcpy(path, "/");
if(*status == '\0')
strcat(status, "ok");
ok = *quiet == 'y' || _fs_ls(request, path, status, file, drive);
if(!ok) {
http_set_response_status(request, "500 Internal Server Error");
vfs_puts("Failed to generate response", file);
}
#if LWIP_HTTPD_FILE_STATE
char *fname = (char *)file->state;
fs_close(file);
return fname;
#else
vfs_close(file);
return "/ram/qry.json";
#endif
}
const char *fs_download_handler (http_request_t *request, vfs_drive_t *drive)
{
static char path[256] = "";
if(drive == NULL) {
http_set_response_status(request, "404 Not Found");
return NULL;
}
if(strlen(drive->path) > 1 || *drive->path != '/')
strcat(strcpy(path, drive->path), "/");
return strcat(path, http_get_uri(request));
}
static err_t fs_post_receive_data (http_request_t *request, struct pbuf *p)
{
struct pbuf *q = p;
http_upload_chunk(request, p->payload, p->len);
while((q = q->next))
http_upload_chunk(request, q->payload, q->len);
httpd_free_pbuf(request, p);
return ERR_OK;
}
static void fs_post_finished (http_request_t *request, char *response_uri, u16_t response_uri_len)
{
vfs_drive_t *drive = NULL;
vfs_file_t *file = vfs_open("/ram/qry.json", "w");
file_upload_t *upload = (file_upload_t *)request->private_data;
if(upload) {
strncpy(response_uri, *upload->path ? upload->path : "/", response_uri_len);
char *s;
if((s = strrchr(upload->filename, '/')))
*(++s) = '\0';
drive = vfs_get_drive(upload->filename);
}
if(*response_uri == '\0')
strcpy(response_uri, "/");
_fs_ls(request, upload ? upload->filename : response_uri, "ok", file, drive);
vfs_close(file);
if(request->on_request_completed) {
request->on_request_completed(request->private_data);
request->private_data = NULL;
request->on_request_completed = NULL;
}
strcpy(response_uri, "/ram/qry.json");
}
static void fs_on_upload_name_parsed (char *name, void *data)
{
char *drive_path = (char *)data;
size_t len = strlen(name), plen = strlen(drive_path);
if(!strncmp(name, drive_path, plen))
plen = 0;
else if(*name == '/')
plen--;
if(plen && len + plen <= HTTP_UPLOAD_MAX_PATHLENGTH) {
memmove(name + plen, name, len + 1);
memcpy(name, drive_path, plen);
}
}
const char *fs_upload_handler (http_request_t *request, vfs_drive_t *drive)
{
static char drive_path[32];
int len;
char ct[200], *boundary;
file_upload_t *upload = NULL;
if((len = http_get_header_value_len (request, "Content-Type")) >= 0) {
http_get_header_value (request, "Content-Type", ct, len);
if((boundary = strstr(ct, "boundary="))) {
boundary += strlen("boundary=");
upload = http_upload_start(request, boundary, true);
if(upload && drive) {
strcpy(drive_path, drive->path);
http_upload_on_filename_parsed(upload, fs_on_upload_name_parsed, drive_path);
}
}
}
if(upload) {
request->post_receive_data = fs_post_receive_data;
request->post_finished = fs_post_finished;
} else
http_set_response_status(request, "400 Bad Request");
return NULL;
}
vfs_drive_t *fs_get_root_drive (void)
{
static vfs_drive_t root;
vfs_drives_t *dh;
vfs_drive_t *drive = NULL;
if((dh = vfs_drives_open()))
{
while((drive = vfs_drives_read(dh, true))) {
if(!strcmp(drive->name, "/")) {
memcpy(&root, drive, sizeof(vfs_drive_t));
break;
}
}
vfs_drives_close(dh);
}
return drive ? &root : NULL;
}
vfs_drive_t *fs_get_sd_drive (void)
{
static vfs_drive_t sd;
vfs_drives_t *dh;
vfs_drive_t *drive = NULL;
if((dh = vfs_drives_open()))
{
while((drive = vfs_drives_read(dh, false))) {
if(drive->removable) {
memcpy(&sd, drive, sizeof(vfs_drive_t));
break;
}
}
vfs_drives_close(dh);
}
return drive ? &sd : NULL;
}
vfs_drive_t *fs_get_flash_drive (bool add_hidden)
{
static vfs_drive_t flash;
vfs_drives_t *dh;
vfs_drive_t *drive = NULL;
if((dh = vfs_drives_open()))
{
while((drive = vfs_drives_read(dh, add_hidden))) {
if(!(drive->removable || drive->mode.read_only)) {
memcpy(&flash, drive, sizeof(vfs_drive_t));
break;
} else
drive = NULL;
}
vfs_drives_close(dh);
}
return drive ? &flash : NULL;
}
#endif // WEBUI_ENABLE