From aac0ca92a873706abd4643e9a055b3ad48a6920d Mon Sep 17 00:00:00 2001 From: isaacgravenortechnologies <83920636+isaacgravenortechnologies@users.noreply.github.com> Date: Sun, 9 Mar 2025 17:32:23 +0000 Subject: [PATCH 1/4] Add cp and mv commands This commit adds 'cp' and 'mv', where 'mv' uses the same code as 'cp' but attempts to delete the file afterwards. Command-line fkags (such as -r ), or moving/copying folders isn't supported by this code (yet!). --- src/shell.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/shell.c b/src/shell.c index ad2e8dc..bcd2937 100644 --- a/src/shell.c +++ b/src/shell.c @@ -903,6 +903,71 @@ void ShellExecuteCommand(char* p, bool* pbExit) { KePrintMemoryMapInfo(); } + else if (strcmp (token, "cp") == 0) + { + char* fileName = Tokenize (&state, NULL, " "); + + char* fileName2 = Tokenize (&state, NULL, " "); + int fd = FiOpen(fileName, O_RDONLY); + if(fd<0) { + LogMsg("File not found: %s", fileName); + FiClose(fd); + return; + } + else { + + FiSeek(fd, 0, SEEK_END); + + unsigned int sz; + sz = FiTell(fd); + FiSeek(fd, 0, SEEK_SET); + char* data = (char*)MmAllocate(sz+1); + FiRead(fd, data, sz); + FiClose (fd); + + fd = FiOpen(fileName2, O_CREAT | O_RDWR); + FiWrite(fd, data, sz); + FiClose (fd); + LogMsg("Done."); + return; + } + } + + else if (strcmp (token, "mv") == 0) + { + char* fileName = Tokenize (&state, NULL, " "); + char* fileName2 = Tokenize (&state, NULL, " "); + int fd = FiOpen(fileName, O_RDONLY); + if(fd<0) { + LogMsg("File not found: %s", fileName); + FiClose(fd); + return; + } + else { + + FiSeek(fd, 0, SEEK_END); + + unsigned int sz; + sz = FiTell(fd); + FiSeek(fd, 0, SEEK_SET); + char* data = (char*)MmAllocate(sz+1); + FiRead(fd, data, sz); + FiClose (fd); + + fd = FiOpen(fileName2, O_CREAT | O_RDWR); + FiWrite(fd, data, sz); + FiClose (fd); + // Get rid of the file. + int io = FiUnlinkFile (fileName); + if (io < 0) + { + LogMsg("mv: couldn't remove %s: %s", fileName, GetErrNoString(io)); + return; + } + + LogMsg("Done"); + } + } else if (strcmp (token, "ls") == 0) { enum From 933dc4ba2618ca9eea071d34f9c4f85786d2db97 Mon Sep 17 00:00:00 2001 From: isaacgravenortechnologies <83920636+isaacgravenortechnologies@users.noreply.github.com> Date: Mon, 10 Mar 2025 22:11:33 +0000 Subject: [PATCH 2/4] Added filename check + fixed code style Yet to do: block reading, FiRenameFile --- src/shell.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/shell.c b/src/shell.c index bcd2937..b96a3b0 100644 --- a/src/shell.c +++ b/src/shell.c @@ -906,21 +906,41 @@ void ShellExecuteCommand(char* p, bool* pbExit) else if (strcmp (token, "cp") == 0) { char* fileName = Tokenize (&state, NULL, " "); - + if (!fileName) + { + LogMsg("Expected filename"); + return; + } + else if (*fileName == 0) + { + LogMsg("Expected filename"); + return; + } char* fileName2 = Tokenize (&state, NULL, " "); + if (!fileName2) + { + LogMsg("Expected filename"); + return; + } + else if (*fileName2 == 0) + { + LogMsg("Expected filename"); + return; + } int fd = FiOpen(fileName, O_RDONLY); if(fd<0) { LogMsg("File not found: %s", fileName); FiClose(fd); return; } - else { - + else + { FiSeek(fd, 0, SEEK_END); unsigned int sz; sz = FiTell(fd); FiSeek(fd, 0, SEEK_SET); + //TODO: allocate/r+w in blocks char* data = (char*)MmAllocate(sz+1); FiRead(fd, data, sz); FiClose (fd); @@ -936,20 +956,42 @@ void ShellExecuteCommand(char* p, bool* pbExit) else if (strcmp (token, "mv") == 0) { char* fileName = Tokenize (&state, NULL, " "); + if (!fileName) + { + LogMsg("Expected filename"); + return; + } + else if (*fileName == 0) + { + LogMsg("Expected filename"); + return; + } char* fileName2 = Tokenize (&state, NULL, " "); + if (!fileName2) + { + LogMsg("Expected filename"); + return; + } + else if (*fileName2 == 0) + { + LogMsg("Expected filename"); + return; + } + //TODO: try using FiRenameFile here? int fd = FiOpen(fileName, O_RDONLY); if(fd<0) { LogMsg("File not found: %s", fileName); FiClose(fd); return; } - else { - + else + { FiSeek(fd, 0, SEEK_END); unsigned int sz; sz = FiTell(fd); FiSeek(fd, 0, SEEK_SET); + //TODO: allocate/r+w in blocks char* data = (char*)MmAllocate(sz+1); FiRead(fd, data, sz); FiClose (fd); From 1bfd007027b171682bb70a54bda1554ef86bc98b Mon Sep 17 00:00:00 2001 From: isaacgravenortechnologies <83920636+isaacgravenortechnologies@users.noreply.github.com> Date: Mon, 10 Mar 2025 22:57:23 +0000 Subject: [PATCH 3/4] Various changes Used movedata code for cp (and for the second attempt of mv, where it copies/deletes), reusing movedata_precision for cp and mv by defining it up top. --- src/shell.c | 229 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 155 insertions(+), 74 deletions(-) diff --git a/src/shell.c b/src/shell.c index b96a3b0..0bfccf8 100644 --- a/src/shell.c +++ b/src/shell.c @@ -29,7 +29,7 @@ #include #include #include - +#define MOVEDATA_PRECISION 4096 char g_lastCommandExecuted[256] = {0}; void FsPipeTest(); @@ -720,7 +720,7 @@ void ShellExecuteCommand(char* p, bool* pbExit) LogMsg("Progress..."); - #define MOVEDATA_PRECISION 4096 + uint8_t chunk_of_data[MOVEDATA_PRECISION]; size_t sz = FiTellSize(fd_in); @@ -764,9 +764,8 @@ void ShellExecuteCommand(char* p, bool* pbExit) FiClose(fd_out); FiClose(fd_in); + fail_movedata:; LogMsg("Done"); - - fail_movedata:; } else if (strcmp (token, "rmdir") == 0) { @@ -905,109 +904,191 @@ void ShellExecuteCommand(char* p, bool* pbExit) } else if (strcmp (token, "cp") == 0) { - char* fileName = Tokenize (&state, NULL, " "); - if (!fileName) + LogMsg("Starting to copy..."); + char* fileNameIn = Tokenize (&state, NULL, " "); + if (!fileNameIn) { - LogMsg("Expected filename"); + LogMsg("Usage: cp "); return; } - else if (*fileName == 0) + if (*fileNameIn == 0) { - LogMsg("Expected filename"); + LogMsg("Usage: cp "); return; } - char* fileName2 = Tokenize (&state, NULL, " "); - if (!fileName2) + + char* fileNameOut = Tokenize (&state, NULL, " "); + if (!fileNameOut) { - LogMsg("Expected filename"); + LogMsg("Usage: cp "); return; } - else if (*fileName2 == 0) + if (*fileNameOut == 0) { - LogMsg("Expected filename"); + LogMsg("Usage: cp "); return; } - int fd = FiOpen(fileName, O_RDONLY); - if(fd<0) { - LogMsg("File not found: %s", fileName); - FiClose(fd); + + int fd_in = FiOpen (fileNameIn, O_RDONLY); + if (fd_in < 0) + { + LogMsg("cp: Could not open %s for reading: %s", fileNameIn, GetErrNoString(fd_in)); return; } - else + + int fd_out = FiOpen (fileNameOut, O_WRONLY | O_CREAT | O_TRUNC); + if (fd_out < 0) { - FiSeek(fd, 0, SEEK_END); + LogMsg("cp: Could not open %s for writing: %s", fileNameOut, GetErrNoString(fd_out)); + FiClose(fd_in); + return; + } - unsigned int sz; - sz = FiTell(fd); - FiSeek(fd, 0, SEEK_SET); - //TODO: allocate/r+w in blocks - char* data = (char*)MmAllocate(sz+1); - FiRead(fd, data, sz); - FiClose (fd); - - fd = FiOpen(fileName2, O_CREAT | O_RDWR); - FiWrite(fd, data, sz); - FiClose (fd); - LogMsg("Done."); - return; + LogMsg("Progress..."); + + + + uint8_t chunk_of_data[MOVEDATA_PRECISION]; + size_t sz = FiTellSize(fd_in); + + //write 512 byte blocks + for (size_t i = 0; i < sz; i += MOVEDATA_PRECISION) + { + size_t read_in = FiRead(fd_in, chunk_of_data, MOVEDATA_PRECISION); + if ((int)read_in < 0) + { + LogMsg("cp: Could not write all %d bytes to %s - only wrote %d: %s", MOVEDATA_PRECISION, fileNameOut, read_in, GetErrNoString((int)read_in)); + FiClose(fd_in); + FiClose(fd_out); + return; + } + + FiWrite(fd_out, chunk_of_data, read_in); + + LogMsgNoCr("\rProgress: %d/%d", i, sz); } + + // write the last few bytes + int last_x_block_size = (sz % MOVEDATA_PRECISION); + for (int i = 0; i < last_x_block_size; i++) + { + size_t read_in = FiRead(fd_in, chunk_of_data, 1); + if ((int)read_in < 0) + { + LogMsg("cp: Could not write 1 byte to %s - only wrote %d: %s", fileNameOut, read_in, GetErrNoString((int)read_in)); + FiClose(fd_in); + FiClose(fd_out); + return; + } + + FiWrite(fd_out, chunk_of_data, read_in); + + LogMsgNoCr("\rProgress: %d/%d", i, sz); + } + + LogMsg(""); + + FiClose(fd_out); + FiClose(fd_in); + LogMsg("Done"); } else if (strcmp (token, "mv") == 0) { - char* fileName = Tokenize (&state, NULL, " "); - if (!fileName) - { - LogMsg("Expected filename"); - return; - } - else if (*fileName == 0) + char* fileNameIn = Tokenize (&state, NULL, " "); + if (!fileNameIn) { - LogMsg("Expected filename"); + LogMsg("Usage: mv "); return; } - char* fileName2 = Tokenize (&state, NULL, " "); - if (!fileName2) + if (*fileNameIn == 0) { - LogMsg("Expected filename"); + LogMsg("Usage: mv "); return; } - else if (*fileName2 == 0) + + char* fileNameOut = Tokenize (&state, NULL, " "); + if (!fileNameOut) { - LogMsg("Expected filename"); + LogMsg("Usage: mv "); return; } - //TODO: try using FiRenameFile here? - int fd = FiOpen(fileName, O_RDONLY); - if(fd<0) { - LogMsg("File not found: %s", fileName); - FiClose(fd); - return; - } - else + if (*fileNameOut == 0) { - FiSeek(fd, 0, SEEK_END); - - unsigned int sz; - sz = FiTell(fd); - FiSeek(fd, 0, SEEK_SET); - //TODO: allocate/r+w in blocks - char* data = (char*)MmAllocate(sz+1); - FiRead(fd, data, sz); - FiClose (fd); - - fd = FiOpen(fileName2, O_CREAT | O_RDWR); - FiWrite(fd, data, sz); - FiClose (fd); - // Get rid of the file. - int io = FiUnlinkFile (fileName); - if (io < 0) - { - LogMsg("mv: couldn't remove %s: %s", fileName, GetErrNoString(io)); + LogMsg("Usage: mv "); return; } - - LogMsg("Done"); + int result = FiRename(fileNameIn, fileNameOut); + if(result == -ENXIO) { + LogMsg("FiRename failed, trying move+delete"); + // Get rid of the file. + int fd_in = FiOpen (fileNameIn, O_RDONLY); + if (fd_in < 0) + { + LogMsg("mv: Could not open %s for reading: %s", fileNameIn, GetErrNoString(fd_in)); + return; + } + + int fd_out = FiOpen (fileNameOut, O_WRONLY | O_CREAT | O_TRUNC); + if (fd_out < 0) + { + LogMsg("mv: Could not open %s for writing: %s", fileNameOut, GetErrNoString(fd_out)); + FiClose(fd_in); + return; + } + + LogMsg("Progress..."); + + + + uint8_t chunk_of_data[MOVEDATA_PRECISION]; + size_t sz = FiTellSize(fd_in); + + //write 512 byte blocks + for (size_t i = 0; i < sz; i += MOVEDATA_PRECISION) + { + size_t read_in = FiRead(fd_in, chunk_of_data, MOVEDATA_PRECISION); + if ((int)read_in < 0) + { + LogMsg("mv: Could not write all %d bytes to %s - only wrote %d: %s", MOVEDATA_PRECISION, fileNameOut, read_in, GetErrNoString((int)read_in)); + FiClose(fd_in); + FiClose(fd_out); + return; + } + + FiWrite(fd_out, chunk_of_data, read_in); + + LogMsgNoCr("\rProgress: %d/%d", i, sz); + } + + // write the last few bytes + int last_x_block_size = (sz % MOVEDATA_PRECISION); + for (int i = 0; i < last_x_block_size; i++) + { + size_t read_in = FiRead(fd_in, chunk_of_data, 1); + if ((int)read_in < 0) + { + LogMsg("mv: Could not write 1 byte to %s - only wrote %d: %s", fileNameOut, read_in, GetErrNoString((int)read_in)); + FiClose(fd_in); + FiClose(fd_out); + return; + } + + FiWrite(fd_out, chunk_of_data, read_in); + + LogMsgNoCr("\rProgress: %d/%d", i, sz); + } + + LogMsg(""); + + FiClose(fd_out); + FiClose(fd_in); + int io = FiUnlinkFile (fileNameIn); + if (io < 0) + { + LogMsg("mv: couldn't remove %s: %s", fileNameOut, GetErrNoString(io)); + return; + } } } else if (strcmp (token, "ls") == 0) From 58d9ac4be60eba04c08b724b0f8d6640849b08f5 Mon Sep 17 00:00:00 2001 From: isaacgravenortechnologies <83920636+isaacgravenortechnologies@users.noreply.github.com> Date: Fri, 6 Jun 2025 15:55:21 +0100 Subject: [PATCH 4/4] Various requested changes The code styles were all sorted out, and after having looked into other source files, size_t seems to be pointless here so int has been used instead, with casts removed where appropriate. Also, '||' has been added when checking filenames, as suggested. --- src/shell.c | 55 ++++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/src/shell.c b/src/shell.c index 0bfccf8..0685688 100644 --- a/src/shell.c +++ b/src/shell.c @@ -906,24 +906,14 @@ void ShellExecuteCommand(char* p, bool* pbExit) { LogMsg("Starting to copy..."); char* fileNameIn = Tokenize (&state, NULL, " "); - if (!fileNameIn) - { - LogMsg("Usage: cp "); - return; - } - if (*fileNameIn == 0) + if (!fileNameIn || (*fileNameIn == 0)) { LogMsg("Usage: cp "); return; } char* fileNameOut = Tokenize (&state, NULL, " "); - if (!fileNameOut) - { - LogMsg("Usage: cp "); - return; - } - if (*fileNameOut == 0) + if (!fileNameOut || (*fileNameOut == 0)) { LogMsg("Usage: cp "); return; @@ -954,10 +944,10 @@ void ShellExecuteCommand(char* p, bool* pbExit) //write 512 byte blocks for (size_t i = 0; i < sz; i += MOVEDATA_PRECISION) { - size_t read_in = FiRead(fd_in, chunk_of_data, MOVEDATA_PRECISION); - if ((int)read_in < 0) + int read_in = FiRead(fd_in, chunk_of_data, MOVEDATA_PRECISION); + if (read_in < 0) { - LogMsg("cp: Could not write all %d bytes to %s - only wrote %d: %s", MOVEDATA_PRECISION, fileNameOut, read_in, GetErrNoString((int)read_in)); + LogMsg("cp: Could not write all %d bytes to %s - only wrote %d: %s", MOVEDATA_PRECISION, fileNameOut, read_in, GetErrNoString(read_in)); FiClose(fd_in); FiClose(fd_out); return; @@ -972,10 +962,10 @@ void ShellExecuteCommand(char* p, bool* pbExit) int last_x_block_size = (sz % MOVEDATA_PRECISION); for (int i = 0; i < last_x_block_size; i++) { - size_t read_in = FiRead(fd_in, chunk_of_data, 1); - if ((int)read_in < 0) + int read_in = FiRead(fd_in, chunk_of_data, 1); + if (read_in < 0) { - LogMsg("cp: Could not write 1 byte to %s - only wrote %d: %s", fileNameOut, read_in, GetErrNoString((int)read_in)); + LogMsg("cp: Could not write 1 byte to %s - only wrote %d: %s", fileNameOut, read_in, GetErrNoString(read_in)); FiClose(fd_in); FiClose(fd_out); return; @@ -996,33 +986,24 @@ void ShellExecuteCommand(char* p, bool* pbExit) else if (strcmp (token, "mv") == 0) { char* fileNameIn = Tokenize (&state, NULL, " "); - if (!fileNameIn) - { - LogMsg("Usage: mv "); - return; - } - if (*fileNameIn == 0) + if (!fileNameIn || (*fileNameIn == 0)) { LogMsg("Usage: mv "); return; } char* fileNameOut = Tokenize (&state, NULL, " "); - if (!fileNameOut) - { - LogMsg("Usage: mv "); - return; - } - if (*fileNameOut == 0) + if (!fileNameOut || (*fileNameOut == 0)) { LogMsg("Usage: mv "); return; } int result = FiRename(fileNameIn, fileNameOut); - if(result == -ENXIO) { + if (result == -ENXIO) + { LogMsg("FiRename failed, trying move+delete"); // Get rid of the file. - int fd_in = FiOpen (fileNameIn, O_RDONLY); + int fd_in = FiOpen (fileNameIn, O_RDONLY); if (fd_in < 0) { LogMsg("mv: Could not open %s for reading: %s", fileNameIn, GetErrNoString(fd_in)); @@ -1047,10 +1028,10 @@ void ShellExecuteCommand(char* p, bool* pbExit) //write 512 byte blocks for (size_t i = 0; i < sz; i += MOVEDATA_PRECISION) { - size_t read_in = FiRead(fd_in, chunk_of_data, MOVEDATA_PRECISION); - if ((int)read_in < 0) + int read_in = FiRead(fd_in, chunk_of_data, MOVEDATA_PRECISION); + if (read_in < 0) { - LogMsg("mv: Could not write all %d bytes to %s - only wrote %d: %s", MOVEDATA_PRECISION, fileNameOut, read_in, GetErrNoString((int)read_in)); + LogMsg("mv: Could not write all %d bytes to %s - only wrote %d: %s", MOVEDATA_PRECISION, fileNameOut, read_in, GetErrNoString(read_in)); FiClose(fd_in); FiClose(fd_out); return; @@ -1065,8 +1046,8 @@ void ShellExecuteCommand(char* p, bool* pbExit) int last_x_block_size = (sz % MOVEDATA_PRECISION); for (int i = 0; i < last_x_block_size; i++) { - size_t read_in = FiRead(fd_in, chunk_of_data, 1); - if ((int)read_in < 0) + int read_in = FiRead(fd_in, chunk_of_data, 1); + if (read_in < 0) { LogMsg("mv: Could not write 1 byte to %s - only wrote %d: %s", fileNameOut, read_in, GetErrNoString((int)read_in)); FiClose(fd_in);