Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for removing an empty folder using "rm -f" command. Lis…

…ting the contents of a folder lists the subfolders first and then the files in the folder.
  • Loading branch information...
commit f50cf7a516a570ef30132cf185e0488a24b99a6e 1 parent 6d52aaa
Paul Ring authored
Showing with 110 additions and 74 deletions.
  1. +81 −54 OpenLog_v2/OpenLog_v2.pde
  2. +25 −20 OpenLog_v2/SdFat.cpp
  3. +4 −0 OpenLog_v2/SdFat.h
View
135 OpenLog_v2/OpenLog_v2.pde
@@ -769,6 +769,60 @@ uint8_t append_file(char* file_name)
return(1); //Success!
}
+uint8_t gotoDir(char *dir)
+{
+ SdFile subDirectory;
+ uint8_t tmp_var = 0;
+
+ //Goto parent directory
+ //@NOTE: This is a fix to make this work. Should be replaced with
+ //proper handling. Limitation: FOLDER_TRACK_DEPTH subfolders
+ if (strncmp_P(dir, PSTR(".."), 2) == 0) {
+ tmp_var = 1;
+
+ //close file system
+ currentDirectory.close();
+
+ // open the root directory
+ if (!currentDirectory.openRoot(volume)) error("openRoot");
+ int8_t index = getNextFolderTreeIndex() - 1;
+ if (index >= 0)
+ {
+ for (int8_t iTemp = 0; iTemp < index; iTemp++)
+ {
+ if (!(tmp_var = subDirectory.open(currentDirectory, folderTree[iTemp], O_READ)))
+ break;
+
+ currentDirectory = subDirectory; //Point to new directory
+ subDirectory.close();
+ }
+ memset(folderTree[index], 0, 11);
+ }
+ if (((feedback_mode & EXTENDED_INFO) > 0) && (tmp_var == 0))
+ {
+ PgmPrint("cannot cd to parent directory: ");
+ Serial.println(dir);
+ }
+ }
+ else
+ {
+ if (!(tmp_var = subDirectory.open(currentDirectory, dir, O_READ))) {
+ if ((feedback_mode & EXTENDED_INFO) > 0)
+ {
+ PgmPrint("directory not found: ");
+ Serial.println(dir);
+ }
+ }
+ else
+ {
+ currentDirectory = subDirectory; //Point to new directory
+ int8_t index = getNextFolderTreeIndex();
+ if (index >= 0)
+ strncpy(folderTree[index], dir, 11);
+ }
+ }
+ return tmp_var;
+}
void command_shell(void)
{
//provide a simple shell
@@ -923,6 +977,29 @@ void command_shell(void)
if (too_many_arguments_error(3, command))
continue;
+ //Argument 2: Directory name
+ command_arg = get_cmd_arg(1);
+ if(command_arg == 0)
+ continue;
+
+ //Remove empty folder.
+ //@NOTE: Unfortunately -rf does not work due to a buggy rmRfStar() function.
+ //Calling rmRfStart() will result in OpenLog rebooting.
+ if (strncmp_P(command_arg, PSTR("-f"), 2) == 0)
+ {
+ if (file.open(currentDirectory, command_arg, O_READ))
+ file.close(); //There is a file called "-f"
+ else
+ {
+ //remove and goto parent directory
+ if (currentDirectory.rmDir()) gotoDir("..");
+#ifdef INCLUDE_SIMPLE_EMBEDDED
+ command_succedded = 1;
+#endif
+ continue;
+ }
+ }
+
//Argument 2: File name or file wildcard removal
uint32_t filesDeleted = currentDirectory.remove(&wildcmp, get_cmd_arg(1), &removeErrorCallback);
if ((feedback_mode & EXTENDED_INFO) > 0)
@@ -946,61 +1023,12 @@ void command_shell(void)
command_arg = get_cmd_arg(1);
if(command_arg == 0)
continue;
+ //open directory
+ tmp_var = gotoDir(command_arg);
- SdFile subDirectory;
-
- //Goto parent directory
- //@NOTE: This is a fix to make this work. Should be replaced with
- //proper handling. Limitation: FOLDER_TRACK_DEPTH subfolders
- if (strncmp_P(command_arg, PSTR(".."), 2) == 0) {
- tmp_var = 1;
-
- //close file system
- currentDirectory.close();
-
- // open the root directory
- if (!currentDirectory.openRoot(volume)) error("openRoot");
- int8_t index = getNextFolderTreeIndex() - 1;
- if (index >= 0)
- {
- for (int8_t iTemp = 0; iTemp < index; iTemp++)
- {
- if (!(tmp_var = subDirectory.open(currentDirectory, folderTree[iTemp], O_READ)))
- break;
-
- currentDirectory = subDirectory; //Point to new directory
- subDirectory.close();
- }
- memset(folderTree[index], 0, 11);
- }
#ifdef INCLUDE_SIMPLE_EMBEDDED
command_succedded = tmp_var;
#endif
- if (((feedback_mode & EXTENDED_INFO) > 0) && (tmp_var == 0))
- {
- PgmPrint("cannot cd to parent directory: ");
- Serial.println(command_arg);
- }
- continue;
- }
-
- if (!subDirectory.open(currentDirectory, (char *)command_arg, O_READ)) {
- if ((feedback_mode & EXTENDED_INFO) > 0)
- {
- PgmPrint("directory not found: ");
- Serial.println(command_arg);
- }
- }
- else
- {
- currentDirectory = subDirectory; //Point to new directory
- int8_t index = getNextFolderTreeIndex();
- if (index >= 0)
- strncpy(folderTree[index], command_arg, 11);
-#ifdef INCLUDE_SIMPLE_EMBEDDED
- command_succedded = 1;
-#endif
- }
}
else if(strncmp_P(command_arg, PSTR("read"), 4) == 0)
@@ -1227,7 +1255,7 @@ void command_shell(void)
//994816 = 1GB card
PgmPrintln(" KB");
#ifdef INCLUDE_SIMPLE_EMBEDDED
- command_succedded = 1;
+ command_succedded = 1;
#endif
}
else if(strcmp_P(command_arg, PSTR("sync")) == 0)
@@ -1237,7 +1265,7 @@ void command_shell(void)
file.sync();
currentDirectory.sync();
#ifdef INCLUDE_SIMPLE_EMBEDDED
- command_succedded = 1;
+ command_succedded = 1;
#endif
}
else if(strncmp_P(command_arg, PSTR("new"), 3) == 0)
@@ -2278,4 +2306,3 @@ uint8_t wildcmp(const char* wild, const char* string)
//End wildcard functions
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
View
45 OpenLog_v2/SdFat.cpp
@@ -233,25 +233,7 @@ uint32_t SdFile::fileInfo(uint8_t flags, uint32_t index, char* fileName)
return val;
}
-//------------------------------------------------------------------------------
-/** List directory contents to Serial.
- *
- * \param[in] flags The inclusive OR of
- *
- * LS_DATE - %Print file modification date
- *
- * LS_SIZE - %Print file size.
- *
- * LS_R - Recursive list of subdirectories.
- *
- * \param[in] indent Amount of space before file name. Used for recursive
- * list to indicate subdirectory level.
- * \param[in] compare String comparison function. The function shall
- * \param[in] wildcard String to compare.
- * return 0 if strings are equal.
- *
- */
-void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wild, const char* string), char *wildcard)
+void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wild, const char* string), char *wildcard, uint8_t (*dirType)(dir_t &dir))
{
dir_t *p;
@@ -265,7 +247,7 @@ void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wi
if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
// only list subdirectories and files
- if (!DIR_IS_FILE_OR_SUBDIR(*p)) continue;
+ if (!(*dirType)(*p)) continue;
// check if we are to do a wild card comparison of the file name.
// for example: "ls *h.txt". The "*compare" function is provided by the
@@ -306,6 +288,29 @@ void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wi
}
}
//------------------------------------------------------------------------------
+/** List directory contents to Serial.
+ *
+ * \param[in] flags The inclusive OR of
+ *
+ * LS_DATE - %Print file modification date
+ *
+ * LS_SIZE - %Print file size.
+ *
+ * LS_R - Recursive list of subdirectories.
+ *
+ * \param[in] indent Amount of space before file name. Used for recursive
+ * list to indicate subdirectory level.
+ * \param[in] compare String comparison function. The function shall
+ * \param[in] wildcard String to compare.
+ * return 0 if strings are equal.
+ *
+ */
+void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wild, const char* string), char *wildcard)
+{
+ ls(flags, indent, compare, wildcard, SdFile::isSubDir); // Show only directories
+ ls(flags, indent, compare, wildcard, SdFile::isFile); // Show only files
+}
+//------------------------------------------------------------------------------
// format directory name field from a 8.3 name string
uint8_t SdFile::make83Name(char *str, uint8_t *name)
{
View
4 OpenLog_v2/SdFat.h
@@ -305,6 +305,10 @@ class SdFile {
void write(const char *str);
void write_P(PGM_P str);
void writeln_P(PGM_P str);
+private:
+ void ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wild, const char* string), char *wildcard, uint8_t (*dirType)(dir_t &dir));
+ static uint8_t isFile(dir_t &dir) {return DIR_IS_FILE(dir);};
+ static uint8_t isSubDir(dir_t &dir) {return DIR_IS_SUBDIR(dir);};
};
//==============================================================================
// SdVolume class
Please sign in to comment.
Something went wrong with that request. Please try again.