Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By perryc
#56056 I'm using the ESP8266/SPIFFS as a datalogger that writes data to a file every couple of seconds. On a couple test units probably because the power was removed before doing a fclose I have some files that can be read but SPIFFS.remove() fails. I see in the c api there is a spiffs_check.c but I don't think it's exposed in the SPIFFS obj model. Bit over my head here but right now I think my only recourse at the moment is to reformat the spiffs, is a exposing the check() API a sane thing to add to SPIFFS?

Perry
User avatar
By Jindrich Sirucek
#77798 Hi, its seems resonable, why nobody reaplyied this thread? Im solving pretty much the same.
So far im using this for check - not best but at least something...
Code: Select allbool spiffsConsistencyCheck()
{
   //SPIFFS CONSISTENCY CHECK
   Serial.println(F("SPIFFS reading consistency check: "));

   // Check each file for reading firt line
   Dir dir = SPIFFS.openDir("/");
   while(dir.next())
   {
      yield();
      File f = dir.openFile("r");
      if(f.size() == 0)
      {
         Serial.println((String)f.name() + F(" - size: ") + f.size() + F(" - Deleting!!"));
         removeFileOrFormatSPIFSS(f);
         continue;
      }

      if(!f.available())
      {
         Serial.println((String)f.name() + F(" - size: ") + f.size() + F(" - NOT AVAILABLE - Deleting!!"));
         removeFileOrFormatSPIFSS(f);
         continue;
      }

      String loadedLine = String(f.readStringUntil('\n'));
      if(loadedLine.length() >= 1)
      {
        if(SPIFFS_DEBUG == false)
          loadedLine = "";
        Serial.println((String)f.name() + F(" - size: ") + f.size() + F(" - OK") + F(" (") + loadedLine + F(")"));
      }
      else
      {
        Serial.println((String)f.name() + F(" - size: ") + f.size() + F(" - LENGTH ZERO - Deleting!!"));
        removeFileOrFormatSPIFSS(f);
        continue;
     }
     f.close();
   }

   Serial.print(F("SPIFFS writing consistency check: "));
   File writeTestFile = SPIFFS.open("/testFile","w");
   if(writeTestFile)
   {
      String randomNumber = (String)random(1000000);
      writeTestFile.println(randomNumber);
      writeTestFile.close();

      SPIFFS.end();
      delay(100);
      SPIFFS.begin();

      writeTestFile = SPIFFS.open("/testFile","r");
      String readString = writeTestFile.readStringUntil('\n');

      Serial.print((String)F("(") + randomNumber + "&" + readString + F("): "));

      if(randomNumber.equals(readString) != 0 || SPIFFS.remove("/testFile") == false)
      {
         Serial.println(F("Problem!! Formating SPIFFS.."));
         SPIFFS.format();
      }
      else
        Serial.println(F("OK!"));

      return true;
   }
   else
   {
      Serial.println(F("Problem!! Formating SPIFFS.."));
      SPIFFS.format();
   }
   return false;
}


bool removeFileOrFormatSPIFSS(File f)
{
   String fileName = f.name();
   f.close();
   if(SPIFFS.remove(fileName))
     return true;
   else
   {
      SPIFFS.format();
   }
   return false;
}