Skip to content
This repository
Browse code

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
authored September 26, 2010
135  OpenLog_v2/OpenLog_v2.pde
@@ -769,6 +769,60 @@ uint8_t append_file(char* file_name)
769 769
   return(1); //Success!
770 770
 }
771 771
 
  772
+uint8_t gotoDir(char *dir)
  773
+{
  774
+  SdFile subDirectory;
  775
+  uint8_t tmp_var = 0;
  776
+
  777
+  //Goto parent directory
  778
+  //@NOTE: This is a fix to make this work. Should be replaced with
  779
+  //proper handling. Limitation: FOLDER_TRACK_DEPTH subfolders
  780
+  if (strncmp_P(dir, PSTR(".."), 2) == 0) {
  781
+    tmp_var = 1;
  782
+
  783
+    //close file system
  784
+    currentDirectory.close();
  785
+
  786
+    // open the root directory
  787
+    if (!currentDirectory.openRoot(volume)) error("openRoot");
  788
+    int8_t index = getNextFolderTreeIndex() - 1;
  789
+    if (index >= 0)
  790
+    {
  791
+      for (int8_t iTemp = 0; iTemp < index; iTemp++)
  792
+      {
  793
+        if (!(tmp_var = subDirectory.open(currentDirectory, folderTree[iTemp], O_READ)))
  794
+          break;
  795
+
  796
+        currentDirectory = subDirectory; //Point to new directory
  797
+        subDirectory.close();
  798
+      }
  799
+      memset(folderTree[index], 0, 11);
  800
+    }
  801
+    if (((feedback_mode & EXTENDED_INFO) > 0) && (tmp_var == 0))
  802
+    {
  803
+      PgmPrint("cannot cd to parent directory: ");
  804
+      Serial.println(dir);
  805
+    }
  806
+  }
  807
+  else
  808
+  {
  809
+    if (!(tmp_var = subDirectory.open(currentDirectory, dir, O_READ))) {
  810
+      if ((feedback_mode & EXTENDED_INFO) > 0)
  811
+      {
  812
+        PgmPrint("directory not found: ");
  813
+        Serial.println(dir);
  814
+      }
  815
+    }
  816
+    else
  817
+    {
  818
+      currentDirectory = subDirectory; //Point to new directory
  819
+      int8_t index = getNextFolderTreeIndex();
  820
+      if (index >= 0)
  821
+        strncpy(folderTree[index], dir, 11);
  822
+    }
  823
+  }
  824
+  return tmp_var;
  825
+}
772 826
 void command_shell(void)
773 827
 {
774 828
   //provide a simple shell
@@ -923,6 +977,29 @@ void command_shell(void)
923 977
       if (too_many_arguments_error(3, command))
924 978
         continue;
925 979
 
  980
+      //Argument 2: Directory name
  981
+      command_arg = get_cmd_arg(1);
  982
+      if(command_arg == 0)
  983
+        continue;
  984
+
  985
+      //Remove empty folder.
  986
+      //@NOTE: Unfortunately -rf does not work due to a buggy rmRfStar() function.
  987
+      //Calling rmRfStart() will result in OpenLog rebooting.
  988
+      if (strncmp_P(command_arg, PSTR("-f"), 2) == 0)
  989
+      {
  990
+        if (file.open(currentDirectory, command_arg, O_READ))
  991
+          file.close(); //There is a file called "-f"
  992
+        else
  993
+        {
  994
+          //remove and goto parent directory
  995
+          if (currentDirectory.rmDir()) gotoDir("..");
  996
+#ifdef INCLUDE_SIMPLE_EMBEDDED
  997
+          command_succedded = 1;
  998
+#endif
  999
+          continue;
  1000
+        }
  1001
+      }
  1002
+
926 1003
       //Argument 2: File name or file wildcard removal
927 1004
       uint32_t filesDeleted = currentDirectory.remove(&wildcmp, get_cmd_arg(1), &removeErrorCallback);
928 1005
       if ((feedback_mode & EXTENDED_INFO) > 0)
@@ -946,61 +1023,12 @@ void command_shell(void)
946 1023
       command_arg = get_cmd_arg(1);
947 1024
       if(command_arg == 0)
948 1025
         continue;
  1026
+      //open directory
  1027
+      tmp_var = gotoDir(command_arg);
949 1028
 
950  
-      SdFile subDirectory;
951  
-
952  
-      //Goto parent directory
953  
-      //@NOTE: This is a fix to make this work. Should be replaced with
954  
-      //proper handling. Limitation: FOLDER_TRACK_DEPTH subfolders
955  
-      if (strncmp_P(command_arg, PSTR(".."), 2) == 0) {
956  
-        tmp_var = 1;
957  
-
958  
-        //close file system
959  
-        currentDirectory.close();
960  
-
961  
-        // open the root directory
962  
-        if (!currentDirectory.openRoot(volume)) error("openRoot");
963  
-        int8_t index = getNextFolderTreeIndex() - 1;
964  
-        if (index >= 0)
965  
-        {
966  
-          for (int8_t iTemp = 0; iTemp < index; iTemp++)
967  
-          {
968  
-            if (!(tmp_var = subDirectory.open(currentDirectory, folderTree[iTemp], O_READ)))
969  
-              break;
970  
-
971  
-            currentDirectory = subDirectory; //Point to new directory
972  
-            subDirectory.close();
973  
-          }
974  
-          memset(folderTree[index], 0, 11);
975  
-        }
976 1029
 #ifdef INCLUDE_SIMPLE_EMBEDDED
977 1030
         command_succedded = tmp_var;
978 1031
 #endif
979  
-        if (((feedback_mode & EXTENDED_INFO) > 0) && (tmp_var == 0))
980  
-        {
981  
-          PgmPrint("cannot cd to parent directory: ");
982  
-          Serial.println(command_arg);
983  
-        }
984  
-        continue;
985  
-      }
986  
-
987  
-      if (!subDirectory.open(currentDirectory, (char *)command_arg, O_READ)) {
988  
-        if ((feedback_mode & EXTENDED_INFO) > 0)
989  
-        {
990  
-          PgmPrint("directory not found: ");
991  
-          Serial.println(command_arg);
992  
-        }
993  
-      }
994  
-      else
995  
-      {
996  
-        currentDirectory = subDirectory; //Point to new directory
997  
-        int8_t index = getNextFolderTreeIndex();
998  
-        if (index >= 0)
999  
-          strncpy(folderTree[index], command_arg, 11);
1000  
-#ifdef INCLUDE_SIMPLE_EMBEDDED
1001  
-        command_succedded = 1;
1002  
-#endif
1003  
-      }
1004 1032
     }
1005 1033
 
1006 1034
     else if(strncmp_P(command_arg, PSTR("read"), 4) == 0)
@@ -1227,7 +1255,7 @@ void command_shell(void)
1227 1255
       //994816 = 1GB card
1228 1256
       PgmPrintln(" KB");
1229 1257
 #ifdef INCLUDE_SIMPLE_EMBEDDED
1230  
-        command_succedded = 1;
  1258
+      command_succedded = 1;
1231 1259
 #endif
1232 1260
     }
1233 1261
     else if(strcmp_P(command_arg, PSTR("sync")) == 0)
@@ -1237,7 +1265,7 @@ void command_shell(void)
1237 1265
       file.sync();
1238 1266
       currentDirectory.sync();
1239 1267
 #ifdef INCLUDE_SIMPLE_EMBEDDED
1240  
-        command_succedded = 1;
  1268
+      command_succedded = 1;
1241 1269
 #endif
1242 1270
     }
1243 1271
     else if(strncmp_P(command_arg, PSTR("new"), 3) == 0)
@@ -2278,4 +2306,3 @@ uint8_t wildcmp(const char* wild, const char* string)
2278 2306
 
2279 2307
 //End wildcard functions
2280 2308
 //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
2281  
-
45  OpenLog_v2/SdFat.cpp
@@ -233,25 +233,7 @@ uint32_t SdFile::fileInfo(uint8_t flags, uint32_t index, char* fileName)
233 233
 
234 234
   return val;
235 235
 }
236  
-//------------------------------------------------------------------------------
237  
-/** List directory contents to Serial.
238  
- *
239  
- * \param[in] flags The inclusive OR of
240  
- *
241  
- * LS_DATE - %Print file modification date
242  
- *
243  
- * LS_SIZE - %Print file size.
244  
- *
245  
- * LS_R - Recursive list of subdirectories.
246  
- *
247  
- * \param[in] indent Amount of space before file name. Used for recursive
248  
- * list to indicate subdirectory level.
249  
- * \param[in] compare String comparison function. The function shall
250  
- * \param[in] wildcard String to compare.
251  
- * return 0 if strings are equal.
252  
- *
253  
- */
254  
-void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wild, const char* string), char *wildcard)
  236
+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))
255 237
 {
256 238
   dir_t *p;
257 239
 
@@ -265,7 +247,7 @@ void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wi
265 247
     if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
266 248
 
267 249
     // only list subdirectories and files
268  
-    if (!DIR_IS_FILE_OR_SUBDIR(*p)) continue;
  250
+    if (!(*dirType)(*p)) continue;
269 251
 
270 252
     // check if we are to do a wild card comparison of the file name.
271 253
     // 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
306 288
   }
307 289
 }
308 290
 //------------------------------------------------------------------------------
  291
+/** List directory contents to Serial.
  292
+ *
  293
+ * \param[in] flags The inclusive OR of
  294
+ *
  295
+ * LS_DATE - %Print file modification date
  296
+ *
  297
+ * LS_SIZE - %Print file size.
  298
+ *
  299
+ * LS_R - Recursive list of subdirectories.
  300
+ *
  301
+ * \param[in] indent Amount of space before file name. Used for recursive
  302
+ * list to indicate subdirectory level.
  303
+ * \param[in] compare String comparison function. The function shall
  304
+ * \param[in] wildcard String to compare.
  305
+ * return 0 if strings are equal.
  306
+ *
  307
+ */
  308
+void SdFile::ls(uint8_t flags, uint8_t indent, uint8_t (*compare)(const char* wild, const char* string), char *wildcard)
  309
+{
  310
+  ls(flags, indent, compare, wildcard, SdFile::isSubDir);  // Show only directories
  311
+  ls(flags, indent, compare, wildcard, SdFile::isFile);    // Show only files
  312
+}
  313
+//------------------------------------------------------------------------------
309 314
 // format directory name field from a 8.3 name string
310 315
 uint8_t SdFile::make83Name(char *str, uint8_t *name)
311 316
 {
4  OpenLog_v2/SdFat.h
@@ -305,6 +305,10 @@ class SdFile {
305 305
   void write(const char *str);
306 306
   void write_P(PGM_P str);
307 307
   void writeln_P(PGM_P str);
  308
+private:
  309
+  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));
  310
+  static uint8_t isFile(dir_t &dir) {return DIR_IS_FILE(dir);};
  311
+  static uint8_t isSubDir(dir_t &dir) {return DIR_IS_SUBDIR(dir);};
308 312
 };
309 313
 //==============================================================================
310 314
 // SdVolume class

0 notes on commit f50cf7a

Please sign in to comment.
Something went wrong with that request. Please try again.