Browse Source

Tweak readDir code

Scott Lahteine 6 years ago
parent
commit
82df656cc7
1 changed files with 11 additions and 8 deletions
  1. 11
    8
      Marlin/SdBaseFile.cpp

+ 11
- 8
Marlin/SdBaseFile.cpp View File

@@ -1061,7 +1061,7 @@ int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) {
1061 1061
   while (1) {
1062 1062
 
1063 1063
     n = read(dir, sizeof(dir_t));
1064
-    if (n != sizeof(dir_t)) return n == 0 ? 0 : -1;
1064
+    if (n != sizeof(dir_t)) return n ? -1 : 0;
1065 1065
 
1066 1066
     // last entry if DIR_NAME_FREE
1067 1067
     if (dir->name[0] == DIR_NAME_FREE) return 0;
@@ -1074,13 +1074,16 @@ int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) {
1074 1074
     if (longFilename != NULL && DIR_IS_LONG_NAME(dir)) {
1075 1075
       vfat_t* VFAT = (vfat_t*)dir;
1076 1076
       // Sanity-check the VFAT entry. The first cluster is always set to zero. And the sequence number should be higher than 0
1077
-      if (VFAT->firstClusterLow == 0 && (VFAT->sequenceNumber & 0x1F) > 0 && (VFAT->sequenceNumber & 0x1F) <= MAX_VFAT_ENTRIES) {
1078
-        // TODO: Store the filename checksum to verify if a long-filename-unaware system modified the file table.
1079
-        n = ((VFAT->sequenceNumber & 0x1F) - 1) * (FILENAME_LENGTH);
1080
-        for (uint8_t i = 0; i < FILENAME_LENGTH; i++)
1081
-          longFilename[n + i] = (i < 5) ? VFAT->name1[i] : (i < 11) ? VFAT->name2[i - 5] : VFAT->name3[i - 11];
1082
-        // If this VFAT entry is the last one, add a NUL terminator at the end of the string
1083
-        if (VFAT->sequenceNumber & 0x40) longFilename[n + FILENAME_LENGTH] = '\0';
1077
+      if (VFAT->firstClusterLow == 0) {
1078
+        const uint8_t seq = VFAT->sequenceNumber & 0x1F;
1079
+        if (WITHIN(seq, 1, MAX_VFAT_ENTRIES)) {
1080
+          // TODO: Store the filename checksum to verify if a long-filename-unaware system modified the file table.
1081
+          n = (seq - 1) * (FILENAME_LENGTH);
1082
+          for (uint8_t i = 0; i < FILENAME_LENGTH; i++)
1083
+            longFilename[n + i] = (i < 5) ? VFAT->name1[i] : (i < 11) ? VFAT->name2[i - 5] : VFAT->name3[i - 11];
1084
+          // If this VFAT entry is the last one, add a NUL terminator at the end of the string
1085
+          if (VFAT->sequenceNumber & 0x40) longFilename[n + FILENAME_LENGTH] = '\0';
1086
+        }
1084 1087
       }
1085 1088
     }
1086 1089
     // Return if normal file or subdirectory

Loading…
Cancel
Save