Forum Rule: Always post complete source code & details to reproduce any issue!
Page 38 of 38 FirstFirst ... 28 36 37 38
Results 926 to 933 of 933

Thread: LittleFS port to Teensy/SPIFlash

  1. #926
    Senior Member wwatson's Avatar
    Join Date
    Aug 2017
    Posts
    443
    That's what I saw. Time to create a simple method to detect the existence of a directory I think if possible. Maybe check to see if object is a directory and if it is then compare the object name to the name we are trying to create. If the name matches then it exists else if it not a directory then create the directory?

    My days are usually weekends

  2. #927
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Odd situation - since it isn't working the obvious way in process - it exists in source media - and likely not in the dest media.

    With recursion the processing may be going funny - but what works in SD ... if I have symmetric code ... isn't working on LFS.

    ... will poke around a bit ... doesn't seem I'm missing anything obvious ...

  3. #928
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Figured it out - can't ask LFS mkdir to make a dir with a trailing "/" - works on SD but not LFS.

    This change makes it work BOTH to SD and to LFS !!!!

    BAD:
    Code:
    			strcat( szNewDir, entry.name());
    			strcat( szNewDir, "/");
    			if ( destMedia == destLFS ) myfs.mkdir( szNewDir );
    			else SD.mkdir( szNewDir );
    			Serial.print("\n");
    			errCnt += mediaTransfer(entry, szNewDir, destMedia);
    WORKS:
    Code:
    			strcat( szNewDir, entry.name());
    			if ( destMedia == destLFS ) myfs.mkdir( szNewDir );
    			else SD.mkdir( szNewDir );
    			Serial.print("\n");
    			strcat( szNewDir, "/");
    			errCnt += mediaTransfer(entry, szNewDir, destMedia);
    Will post usable code in a bit ...

  4. #929
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439

    LittleFS media data Save or Restore from SD card

    Using one of these two commented xferSD() calls:
    Code:
    #define destLFS 1
    #define destSD 2
    	// xferSD( destSD ); // do MediaTransfer LFS TO SD
    	// xferSD( destLFS ); // do MediaTransfer SD TO LFS
    NOTE: In Any LittleFS/examples/Integrity place it in setup() before this line to prevent errors reported in filecount:
    Code:
    	filecount = printDirectoryFilecount( myfs.open("/") );  // Set base value of filecount for disk
    And this piece of INO code { has to be INO to share the global "myfs" - unless it is made Media specific and extern in user code if not visibly global}.

    Above placed in any of the LittleFS/examples/Integrity using this sketch code will 'recursively' copy as indicated between LFS and SD Media - all files and directories. Tested on PSRAM.ino and PROG.ino.
    Removed most debug spew, only two cases of error displayed.
    Example will use SD directory: "\LFS_CPY". SD Media of GB bigger than any LFS Media's MB's, so makes no sense to store/copy all of SD root. This SD directory name can be tested and changed programmatically {with change in xferSD()} to allow saving multiple runs of data if desired.

    NOTE: This code has a DEBUG copy of makeRootDirsTest() that adds extra files and dirs to prove function when sending LFS data to SD. It is not needed in use.

    For a quick test in Integrity examples put in setup - Fill the LFS Media using '3' to make some test files {perhaps "S" or "B"}, then restart to push to SD with destSD.
    Then do a 'w' to Wipe all LFS Media files, and rebuild with destLFS and the target SD card that should hold the files. It will then restore to any LFS media - though it does not check for SIZE issues if the contents won't restore to Media in use.

    Code:
    #include <SD.h>
    
    void xferSD( int copyType ) { // do MediaTransfer with SD
    #define destLFS 1
    #define destSD 2
    
    	static bool initSD = true;
    	Serial.print("Initializing SD card...");
    	if ( initSD && !SD.begin(BUILTIN_SDCARD)) { // see if the card is present and can be initialized:
    		Serial.println("\n\n SD Card failed, or not present - Cannot do Xfer\n");
    	}
    	else {
    		initSD = false;
    		Serial.println("card initialized.\n\n");
    		if ( copyType == destSD ) {
    			// char szSDdir[] = "/"; // COPY to SD ROOT
    			char szSDdir[] = "LFS_CPY/"; // COPY to SD subdirectory
    			if ( '/' != szSDdir[0] )
    				SD.mkdir( szSDdir );
    			makeRootDirsTest(); // BUGBUG DEBUG make extra subdirs and files to show function
    			Serial.println("\n STARTING :: LittleFS copy to SD card XFER ...\n\n");
    			mediaTransfer( myfs.open("/"), szSDdir, destSD ); // TOO SD
    			Serial.println("\n LittleFS copy to SD card XFER COMPLETE.\n\n");
    		}
    		else {
    			char szLFSdir[] = "/";
    			char szSDdir[] = "LFS_CPY";
    			Serial.println("\n STARTING :: SD card copy to LittleFS XFER ...\n\n");
    			mediaTransfer( SD.open(szSDdir), szLFSdir, destLFS );	// FROM SD
    			Serial.println("\n SD card copy to LittleFS XFER COMPLETE.\n\n");
    		}
    	}
    }
    
    void mediaTransfer(File dir, char* szDir, int destMedia) {
    	char szNewDir[36];
    	while (true) {
    		File entry =  dir.openNextFile();
    		if (! entry) {
    			break;
    		}
    		if (entry.isDirectory()) {
    			strcpy( szNewDir, szDir);
    			if ( destMedia == destLFS )
    				myfs.mkdir( szNewDir );
    			else
    				SD.mkdir( szNewDir );
    			strcat( szNewDir, entry.name());
    			if ( destMedia == destLFS ) myfs.mkdir( szNewDir );
    			else SD.mkdir( szNewDir );
    			strcat( szNewDir, "/");
    			mediaTransfer(entry, szNewDir, destMedia);
    		} else {
    			uint64_t fileSize, sizeCnt = 0, xfSize = 1;
    			char mm[512];
    			strcpy( szNewDir, szDir);
    			strcat( szNewDir, entry.name() );
    			File dataFile;
    			if ( destMedia == destLFS ) {
    				dataFile = myfs.open(szNewDir, FILE_WRITE_BEGIN);
    			}
    			else {
    				dataFile = SD.open(szNewDir, FILE_WRITE_BEGIN);
    			}
    			if ( !dataFile )
    				Serial.print("\td_FILE: NOT open\n");
    			fileSize = entry.size();
    			while ( entry.available() ) {
    				if ( fileSize < sizeCnt ) break;
    				if ( fileSize - sizeCnt >= sizeof(mm) ) xfSize = sizeof(mm);
    				else xfSize = fileSize - sizeCnt;
    				entry.read( &mm , xfSize );
    				dataFile.write( &mm , xfSize );
    				sizeCnt += xfSize;
    			}
    			if (fileSize != sizeCnt ) {
    				Serial.print("\n File Size Error:: ");
    				Serial.println( entry.name() );
    			}
    			dataFile.close();
    		}
    		entry.close();
    	}
    }
    
    void makeRootDirsTest() {  // TEST DEBUG CODE
    	char szDir[36];
    	for ( uint32_t ii = 1; ii <= NUMDIRS; ii++ ) {
    		sprintf( szDir, "/%lu_dir", ii );
    		myfs.mkdir( szDir );
    		sprintf( szDir, "/%lu_dir/aFile.txt", ii ); // BUGBUG DEBUG
    		file3 = myfs.open(szDir, FILE_WRITE);
    		file3.write( szDir , 12 );
    		file3.close();
    
    		sprintf( szDir, "/%lu_dir/TEST", ii ); // BUGBUG DEBUG
    		myfs.mkdir( szDir ); // BUGBUG DEBUG
    		sprintf( szDir, "/%lu_dir/TEST/bFile.txt", ii ); // BUGBUG DEBUG
    		file3 = myfs.open(szDir, FILE_WRITE);
    		file3.write( szDir , 12 );
    		file3.close();
    	}
    	filecount = printDirectoryFilecount( myfs.open("/") );  // Set base value of filecount for disk
    }
    This function does all the work when properly called as desired: void mediaTransfer(File dir, char* szDir, int destMedia)

  5. #930
    Senior Member wwatson's Avatar
    Join Date
    Aug 2017
    Posts
    443
    Just tried it out. It works great I ran into that same problem with the forward slashes in my project. I kept forgetting which functions were in control of them.

  6. #931
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Quote Originally Posted by wwatson View Post
    Just tried it out. It works great I ran into that same problem with the forward slashes in my project. I kept forgetting which functions were in control of them.
    Awesome- thx for testing/confirming!

    ... on returning to cleanup/test/upload I must have hit UNDO ... the "/" line went back ... unseen ... and I pulled out all the extra print()'s and .mkdir and .exist ... then it didn't work --- Arrgggh.

  7. #932
    Senior Member wwatson's Avatar
    Join Date
    Aug 2017
    Posts
    443
    Do you need a copy of your last working version? If you do here it is:
    Code:
    #include <SD.h>
    
    void xferSD( int copyType ) { // do MediaTransfer with SD
    #define destLFS 1
    #define destSD 2
    
    	static bool initSD = true;
    	Serial.print("Initializing SD card...");
    	if ( initSD && !SD.begin(BUILTIN_SDCARD)) { // see if the card is present and can be initialized:
    		Serial.println("\n\n SD Card failed, or not present - Cannot do Xfer\n");
    	}
    	else {
    		initSD = false;
    		Serial.println("card initialized.\n\n");
    		if ( copyType == destSD ) {
    			// char szSDdir[] = "/"; // COPY to SD ROOT
    			char szSDdir[] = "LFS_CPY/"; // COPY to SD subdirectory
    			if ( '/' != szSDdir[0] )
    				SD.mkdir( szSDdir );
    			makeRootDirsTest(); // BUGBUG DEBUG make extra subdirs and files to show function
    			Serial.println("\n STARTING :: LittleFS copy to SD card XFER ...\n\n");
    			mediaTransfer( myfs.open("/"), szSDdir, destSD ); // TOO SD
    			Serial.println("\n LittleFS copy to SD card XFER COMPLETE.\n\n");
    		}
    		else {
    			char szLFSdir[] = "/";
    			char szSDdir[] = "LFS_CPY";
    			Serial.println("\n STARTING :: SD card copy to LittleFS XFER ...\n\n");
    			mediaTransfer( SD.open(szSDdir), szLFSdir, destLFS );	// FROM SD
    			Serial.println("\n SD card copy to LittleFS XFER COMPLETE.\n\n");
    		}
    	}
    }
    
    void mediaTransfer(File dir, char* szDir, int destMedia) {
    	char szNewDir[36];
    	while (true) {
    		File entry =  dir.openNextFile();
    		if (! entry) {
    			break;
    		}
    		if (entry.isDirectory()) {
    			strcpy( szNewDir, szDir);
    			if ( destMedia == destLFS )
    				myfs.mkdir( szNewDir );
    			else
    				SD.mkdir( szNewDir );
    			strcat( szNewDir, entry.name());
    			if ( destMedia == destLFS ) myfs.mkdir( szNewDir );
    			else SD.mkdir( szNewDir );
    			strcat( szNewDir, "/");
    			mediaTransfer(entry, szNewDir, destMedia);
    		} else {
    			uint64_t fileSize, sizeCnt = 0, xfSize = 1;
    			char mm[512];
    			strcpy( szNewDir, szDir);
    			strcat( szNewDir, entry.name() );
    			File dataFile;
    			if ( destMedia == destLFS ) {
    				dataFile = myfs.open(szNewDir, FILE_WRITE_BEGIN);
    			}
    			else {
    				dataFile = SD.open(szNewDir, FILE_WRITE_BEGIN);
    			}
    			if ( !dataFile )
    				Serial.print("\td_FILE: NOT open\n");
    			fileSize = entry.size();
    			while ( entry.available() ) {
    				if ( fileSize < sizeCnt ) break;
    				if ( fileSize - sizeCnt >= sizeof(mm) ) xfSize = sizeof(mm);
    				else xfSize = fileSize - sizeCnt;
    				entry.read( &mm , xfSize );
    				dataFile.write( &mm , xfSize );
    				sizeCnt += xfSize;
    			}
    			if (fileSize != sizeCnt ) {
    				Serial.print("\n File Size Error:: ");
    				Serial.println( entry.name() );
    			}
    			dataFile.close();
    		}
    		entry.close();
    	}
    }
    
    void makeRootDirsTest() {  // TEST DEBUG CODE
    	char szDir[36];
    	for ( uint32_t ii = 1; ii <= NUMDIRS; ii++ ) {
    		sprintf( szDir, "/%lu_dir", ii );
    		myfs.mkdir( szDir );
    		sprintf( szDir, "/%lu_dir/aFile.txt", ii ); // BUGBUG DEBUG
    		file3 = myfs.open(szDir, FILE_WRITE);
    		file3.write( szDir , 12 );
    		file3.close();
    
    		sprintf( szDir, "/%lu_dir/TEST", ii ); // BUGBUG DEBUG
    		myfs.mkdir( szDir ); // BUGBUG DEBUG
    		sprintf( szDir, "/%lu_dir/TEST/bFile.txt", ii ); // BUGBUG DEBUG
    		file3 = myfs.open(szDir, FILE_WRITE);
    		file3.write( szDir , 12 );
    		file3.close();
    	}
    	filecount = printDirectoryFilecount( myfs.open("/") );  // Set base value of filecount for disk
    }

  8. #933
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Quote Originally Posted by wwatson View Post
    Do you need a copy of your last working version? If you do here it is:
    ...
    No, Thanks, p#929 is after I got it sorted and that posted code was tested to work - just took a bit of extra time ... undoing all the 'cleanup' only then to see the misplaced "/" ... and redo all the cleanup and then test ...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •