Skip to content

Commit ad5a203

Browse files
committed
GCode M30: Properly handle absolute paths and relative paths
1 parent b91eeda commit ad5a203

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

src/marlin_stubs/sdcard/M20-M30_M32-M34.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <dirent.h>
2+
#include <string.h>
23

34
#include "../../lib/Marlin/Marlin/src/gcode/gcode.h"
45
#include "../src/common/print_utils.hpp"
@@ -199,14 +200,50 @@ void GcodeSuite::M29() {
199200
* M30 [filename]
200201
*/
201202
void GcodeSuite::M30() {
203+
const char *filename = parser.string_arg;
204+
205+
if (!filename) {
206+
SERIAL_ECHOLNPGM("Deletion failed: No filename provided");
207+
return;
208+
}
209+
210+
// Skip leading whitespace
211+
while (*filename == ' ') {
212+
filename++;
213+
}
214+
215+
// Truncate at first space: the gcode serial protocol has no escape mechanism for
216+
// spaces, so filenames with spaces cannot be expressed. Hosts like Simplify3D also
217+
// append the file size after a space (e.g. "M30 filename.gcode 12345"), matching M23.
218+
for (char *fn = const_cast<char *>(filename); *fn; ++fn) {
219+
if (*fn == ' ') {
220+
*fn = '\0';
221+
break;
222+
}
223+
}
224+
225+
if (!*filename) {
226+
SERIAL_ECHOLNPGM("Deletion failed: No filename provided");
227+
return;
228+
}
229+
202230
ArrayStringBuilder<FF_MAX_LFN> filepath;
203-
filepath.append_printf("/usb/%s", parser.string_arg);
204-
DeleteResult result = DeleteResult::GeneralError;
205-
if (filepath.is_ok()) {
206-
result = remove_file(filepath.str());
231+
232+
// Handle paths that may already have /usb/ or /sd/ prefix
233+
if (strncmp(filename, "/usb/", 5) == 0 || strncmp(filename, "/sd/", 4) == 0) {
234+
filepath.append_string(filename);
235+
} else {
236+
filepath.append_printf("/usb/%s", filename);
237+
}
238+
239+
if (!filepath.is_ok()) {
240+
SERIAL_ECHOLNPGM("Deletion failed: Path too long");
241+
return;
207242
}
243+
244+
DeleteResult result = remove_file(filepath.str());
208245
SERIAL_ECHOPGM(result == DeleteResult::Success ? "File deleted:" : "Deletion failed:");
209-
SERIAL_ECHO(parser.string_arg);
246+
SERIAL_ECHO(filename);
210247
SERIAL_ECHOLN(".");
211248
}
212249

0 commit comments

Comments
 (0)