Skip to content

Commit 9a5f00a

Browse files
committed
Remove upload button and fix mkdir parents
1 parent 3493be7 commit 9a5f00a

File tree

6 files changed

+80
-94
lines changed

6 files changed

+80
-94
lines changed

supervisor/shared/bluetooth/file_transfer.c

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
#include "supervisor/shared/reload.h"
4848
#include "supervisor/shared/bluetooth/file_transfer.h"
4949
#include "supervisor/shared/bluetooth/file_transfer_protocol.h"
50+
#include "supervisor/shared/workflow.h"
5051
#include "supervisor/shared/tick.h"
5152
#include "supervisor/usb.h"
5253

5354
#include "py/mpstate.h"
54-
#include "py/stackctrl.h"
5555

5656
STATIC bleio_service_obj_t supervisor_ble_service;
5757
STATIC bleio_uuid_obj_t supervisor_ble_service_uuid;
@@ -387,39 +387,6 @@ STATIC uint8_t _process_write_data(const uint8_t *raw_buf, size_t command_len) {
387387
return WRITE_DATA;
388388
}
389389

390-
STATIC FRESULT _delete_directory_contents(FATFS *fs, const TCHAR *path) {
391-
FF_DIR dir;
392-
FRESULT res = f_opendir(fs, &dir, path);
393-
FILINFO file_info;
394-
// Check the stack since we're putting paths on it.
395-
if (mp_stack_usage() >= MP_STATE_THREAD(stack_limit)) {
396-
return FR_INT_ERR;
397-
}
398-
while (res == FR_OK) {
399-
res = f_readdir(&dir, &file_info);
400-
if (res != FR_OK || file_info.fname[0] == '\0') {
401-
break;
402-
}
403-
size_t pathlen = strlen(path);
404-
size_t fnlen = strlen(file_info.fname);
405-
TCHAR full_path[pathlen + 1 + fnlen];
406-
memcpy(full_path, path, pathlen);
407-
full_path[pathlen] = '/';
408-
size_t full_pathlen = pathlen + 1 + fnlen;
409-
memcpy(full_path + pathlen + 1, file_info.fname, fnlen);
410-
full_path[full_pathlen] = '\0';
411-
if ((file_info.fattrib & AM_DIR) != 0) {
412-
res = _delete_directory_contents(fs, full_path);
413-
}
414-
if (res != FR_OK) {
415-
break;
416-
}
417-
res = f_unlink(fs, full_path);
418-
}
419-
f_closedir(&dir);
420-
return res;
421-
}
422-
423390
STATIC uint8_t _process_delete(const uint8_t *raw_buf, size_t command_len) {
424391
const struct delete_command *command = (struct delete_command *)raw_buf;
425392
size_t header_size = sizeof(struct delete_command);
@@ -446,7 +413,7 @@ STATIC uint8_t _process_delete(const uint8_t *raw_buf, size_t command_len) {
446413
FRESULT result = f_stat(fs, path, &file);
447414
if (result == FR_OK) {
448415
if ((file.fattrib & AM_DIR) != 0) {
449-
result = _delete_directory_contents(fs, path);
416+
result = supervisor_workflow_delete_directory_contents(fs, path);
450417
}
451418
if (result == FR_OK) {
452419
result = f_unlink(fs, path);
@@ -503,7 +470,7 @@ STATIC uint8_t _process_mkdir(const uint8_t *raw_buf, size_t command_len) {
503470
DWORD fattime;
504471
response.truncated_time = truncate_time(command->modification_time, &fattime);
505472
override_fattime(fattime);
506-
FRESULT result = f_mkdir(fs, path);
473+
FRESULT result = supervisor_workflow_mkdir_parents(fs, path);
507474
override_fattime(0);
508475
#if CIRCUITPY_USB_MSC
509476
usb_msc_unlock();

supervisor/shared/web_workflow/static/directory.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ <h1><a href="/"><img src="/favicon.ico"/></a>&nbsp;<span id="path"></span></h1>
1818
<hr>
1919
<label>📄 <input type="file" id="files" multiple></label>
2020
<label for="dirs">📁 <input type="file" id="dirs" multiple webkitdirectory></label>
21-
<button type="submit" id="upload">Upload</button>
2221
<label>Upload progress:<progress value="0"></progress></label>
2322
<hr>
2423
+📁&nbsp;<input type="text" id="name"><button type="submit" id="mkdir">Create Directory</button>

supervisor/shared/web_workflow/static/directory.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ var url_base = window.location;
66
var current_path;
77
var editable = undefined;
88

9+
function set_upload_enabled(enabled) {
10+
files.disabled = !enabled;
11+
dirs.disabled = !enabled;
12+
}
13+
914
async function refresh_list() {
1015

1116
function compareValues(a, b) {
@@ -46,7 +51,7 @@ async function refresh_list() {
4651
);
4752
editable = status.headers.get("Access-Control-Allow-Methods").includes("DELETE");
4853
new_directory_name.disabled = !editable;
49-
files.disabled = !editable;
54+
set_upload_enabled(editable);
5055
if (!editable) {
5156
let usbwarning = document.querySelector("#usbwarning");
5257
usbwarning.style.display = "block";
@@ -149,6 +154,7 @@ async function mkdir(e) {
149154
}
150155

151156
async function upload(e) {
157+
set_upload_enabled(false);
152158
let progress = document.querySelector("progress");
153159
let made_dirs = new Set();
154160
progress.max = files.files.length + dirs.files.length;
@@ -185,7 +191,7 @@ async function upload(e) {
185191
files.value = "";
186192
dirs.value = "";
187193
progress.value = 0;
188-
upload_button.disabled = true;
194+
set_upload_enabled(true);
189195
}
190196

191197
async function del(e) {
@@ -213,18 +219,8 @@ find_devices();
213219
let mkdir_button = document.getElementById("mkdir");
214220
mkdir_button.onclick = mkdir;
215221

216-
let upload_button = document.getElementById("upload");
217-
upload_button.onclick = upload;
218-
219-
upload_button.disabled = files.files.length == 0 && dirs.files.length == 0;
220-
221-
files.onchange = () => {
222-
upload_button.disabled = files.files.length == 0 && dirs.files.length == 0;
223-
}
224-
225-
dirs.onchange = () => {
226-
upload_button.disabled = files.files.length == 0 && dirs.files.length == 0;
227-
}
222+
files.onchange = upload;
223+
dirs.onchange = upload;
228224

229225
mkdir_button.disabled = new_directory_name.value.length == 0;
230226

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "supervisor/shared/translate/translate.h"
4343
#include "supervisor/shared/web_workflow/web_workflow.h"
4444
#include "supervisor/shared/web_workflow/websocket.h"
45+
#include "supervisor/shared/workflow.h"
4546
#include "supervisor/usb.h"
4647

4748
#include "shared-bindings/hashlib/__init__.h"
@@ -763,47 +764,6 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request *
763764
_send_chunk(socket, "");
764765
}
765766

766-
// Copied from ble file_transfer.c. We should share it.
767-
STATIC FRESULT _delete_directory_contents(FATFS *fs, const TCHAR *path) {
768-
FF_DIR dir;
769-
FILINFO file_info;
770-
// Check the stack since we're putting paths on it.
771-
if (mp_stack_usage() >= MP_STATE_THREAD(stack_limit)) {
772-
return FR_INT_ERR;
773-
}
774-
FRESULT res = FR_OK;
775-
while (res == FR_OK) {
776-
res = f_opendir(fs, &dir, path);
777-
if (res != FR_OK) {
778-
break;
779-
}
780-
res = f_readdir(&dir, &file_info);
781-
// We close and reopen the directory every time since we're deleting
782-
// entries and it may invalidate the directory handle.
783-
f_closedir(&dir);
784-
if (res != FR_OK || file_info.fname[0] == '\0') {
785-
break;
786-
}
787-
size_t pathlen = strlen(path);
788-
size_t fnlen = strlen(file_info.fname);
789-
TCHAR full_path[pathlen + 1 + fnlen];
790-
memcpy(full_path, path, pathlen);
791-
full_path[pathlen] = '/';
792-
size_t full_pathlen = pathlen + 1 + fnlen;
793-
memcpy(full_path + pathlen + 1, file_info.fname, fnlen);
794-
full_path[full_pathlen] = '\0';
795-
if ((file_info.fattrib & AM_DIR) != 0) {
796-
res = _delete_directory_contents(fs, full_path);
797-
}
798-
if (res != FR_OK) {
799-
break;
800-
}
801-
res = f_unlink(fs, full_path);
802-
}
803-
f_closedir(&dir);
804-
return res;
805-
}
806-
807767
// FATFS has a two second timestamp resolution but the BLE API allows for nanosecond resolution.
808768
// This function truncates the time the time to a resolution storable by FATFS and fills in the
809769
// FATFS encoded version into fattime.
@@ -1054,7 +1014,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
10541014
FRESULT result = f_stat(fs, path, &file);
10551015
if (result == FR_OK) {
10561016
if ((file.fattrib & AM_DIR) != 0) {
1057-
result = _delete_directory_contents(fs, path);
1017+
result = supervisor_workflow_delete_directory_contents(fs, path);
10581018
}
10591019
if (result == FR_OK) {
10601020
result = f_unlink(fs, path);
@@ -1105,7 +1065,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
11051065
truncate_time(request->timestamp_ms * 1000000, &fattime);
11061066
override_fattime(fattime);
11071067
}
1108-
FRESULT result = f_mkdir(fs, path);
1068+
FRESULT result = supervisor_workflow_mkdir_parents(fs, path);
11091069
override_fattime(0);
11101070
#if CIRCUITPY_USB_MSC
11111071
usb_msc_unlock();

supervisor/shared/workflow.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdbool.h>
2828
#include "py/mpconfig.h"
29+
#include "py/stackctrl.h"
2930
#include "supervisor/background_callback.h"
3031
#include "supervisor/workflow.h"
3132
#include "supervisor/serial.h"
@@ -115,3 +116,60 @@ void supervisor_workflow_start(void) {
115116
supervisor_start_web_workflow();
116117
#endif
117118
}
119+
120+
FRESULT supervisor_workflow_mkdir_parents(FATFS *fs, char *path) {
121+
FRESULT result = FR_OK;
122+
// Make parent directories.
123+
for (size_t j = 1; j < strlen(path); j++) {
124+
if (path[j] == '/') {
125+
path[j] = '\0';
126+
result = f_mkdir(fs, path);
127+
path[j] = '/';
128+
if (result != FR_OK && result != FR_EXIST) {
129+
return result;
130+
}
131+
}
132+
}
133+
// Make the target directory.
134+
return f_mkdir(fs, path);
135+
}
136+
137+
FRESULT supervisor_workflow_delete_directory_contents(FATFS *fs, const TCHAR *path) {
138+
FF_DIR dir;
139+
FILINFO file_info;
140+
// Check the stack since we're putting paths on it.
141+
if (mp_stack_usage() >= MP_STATE_THREAD(stack_limit)) {
142+
return FR_INT_ERR;
143+
}
144+
FRESULT res = FR_OK;
145+
while (res == FR_OK) {
146+
res = f_opendir(fs, &dir, path);
147+
if (res != FR_OK) {
148+
break;
149+
}
150+
res = f_readdir(&dir, &file_info);
151+
// We close and reopen the directory every time since we're deleting
152+
// entries and it may invalidate the directory handle.
153+
f_closedir(&dir);
154+
if (res != FR_OK || file_info.fname[0] == '\0') {
155+
break;
156+
}
157+
size_t pathlen = strlen(path);
158+
size_t fnlen = strlen(file_info.fname);
159+
TCHAR full_path[pathlen + 1 + fnlen];
160+
memcpy(full_path, path, pathlen);
161+
full_path[pathlen] = '/';
162+
size_t full_pathlen = pathlen + 1 + fnlen;
163+
memcpy(full_path + pathlen + 1, file_info.fname, fnlen);
164+
full_path[full_pathlen] = '\0';
165+
if ((file_info.fattrib & AM_DIR) != 0) {
166+
res = supervisor_workflow_delete_directory_contents(fs, full_path);
167+
}
168+
if (res != FR_OK) {
169+
break;
170+
}
171+
res = f_unlink(fs, full_path);
172+
}
173+
f_closedir(&dir);
174+
return res;
175+
}

supervisor/shared/workflow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@
2626

2727
#pragma once
2828

29+
#include "lib/oofatfs/ff.h"
30+
2931
extern bool supervisor_workflow_connecting(void);
32+
33+
// File system helpers for workflow code.
34+
FRESULT supervisor_workflow_mkdir_parents(FATFS *fs, char *path);
35+
FRESULT supervisor_workflow_delete_directory_contents(FATFS *fs, const TCHAR *path);

0 commit comments

Comments
 (0)