|
1 | 1 | #include <dirent.h> |
| 2 | +#include <string.h> |
2 | 3 |
|
3 | 4 | #include "../../lib/Marlin/Marlin/src/gcode/gcode.h" |
4 | 5 | #include "../src/common/print_utils.hpp" |
@@ -199,14 +200,50 @@ void GcodeSuite::M29() { |
199 | 200 | * M30 [filename] |
200 | 201 | */ |
201 | 202 | 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 | + |
202 | 230 | 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; |
207 | 242 | } |
| 243 | + |
| 244 | + DeleteResult result = remove_file(filepath.str()); |
208 | 245 | SERIAL_ECHOPGM(result == DeleteResult::Success ? "File deleted:" : "Deletion failed:"); |
209 | | - SERIAL_ECHO(parser.string_arg); |
| 246 | + SERIAL_ECHO(filename); |
210 | 247 | SERIAL_ECHOLN("."); |
211 | 248 | } |
212 | 249 |
|
|
0 commit comments