@@ -647,14 +647,26 @@ bool jsfCompact(bool showMessage) {
647
647
return compacted ;
648
648
}
649
649
650
+ static bool jsvIsDriveNameExplicit (JsfFileName * name ) {
651
+ return name -> c [1 ]== ':' ;
652
+ }
653
+
654
+ static bool jsvIsDriveC (char drive ) {
655
+ #ifdef JSF_BANK2_START_ADDRESS
656
+ return (drive & (~0x20 )) == 'C' ; // make drive case insensitive
657
+ #else
658
+ return false;
659
+ #endif
660
+ }
661
+
650
662
/* If we have a filename like "C:foo", take the 'C:' bit
651
663
* off it and return the drive. If explicitOnly==false,
652
664
* we also return the drive name if we think a file should
653
665
* go somewhere (eg it's *.js or .boot0 it should go in internal flash)
654
666
*/
655
- char jsfStripDriveFromName (JsfFileName * name , bool explicitOnly ){
667
+ static char jsfStripDriveFromName (JsfFileName * name , bool explicitOnly ){
656
668
#ifndef SAVE_ON_FLASH
657
- if (name -> c [ 1 ] == ':' ) { // if a 'drive' is specified like "C:foobar.js"
669
+ if (jsvIsDriveNameExplicit ( name ) ) { // if a 'drive' is specified like "C:foobar.js"
658
670
char drive = name -> c [0 ];
659
671
memmove (name -> c , name -> c + 2 , sizeof (JsfFileName )- 2 ); // shift back and clear the rest
660
672
name -> c [sizeof (JsfFileName )- 2 ]= 0 ;name -> c [sizeof (JsfFileName )- 1 ]= 0 ;
@@ -675,7 +687,7 @@ char jsfStripDriveFromName(JsfFileName *name, bool explicitOnly){
675
687
void jsfGetDriveBankAddress (char drive , uint32_t * bankStartAddr , uint32_t * bankEndAddr ){
676
688
#ifdef JSF_BANK2_START_ADDRESS
677
689
if (drive ){
678
- if ((drive & (~ 0x20 )) == 'C' ){ // make drive case insensitive
690
+ if (jsvIsDriveC (drive )){
679
691
* bankStartAddr = JSF_START_ADDRESS ;
680
692
* bankEndAddr = JSF_END_ADDRESS ;
681
693
} else {
@@ -688,10 +700,11 @@ void jsfGetDriveBankAddress(char drive, uint32_t *bankStartAddr, uint32_t *bankE
688
700
* bankStartAddr = JSF_DEFAULT_START_ADDRESS ;
689
701
* bankEndAddr = JSF_DEFAULT_END_ADDRESS ;
690
702
}
691
- /// Create a new 'file' in the memory store - DOES NOT remove existing files with same name. Return the address of data start, or 0 on error
692
- static uint32_t jsfCreateFile ( JsfFileName name , uint32_t size , JsfFileFlags flags , JsfFileHeader * returnedHeader ) {
703
+ static uint32_t _jsfCreateFile ( JsfFileName name , uint32_t size , JsfFileFlags flags , JsfFileHeader * returnedHeader , bool explicitOnly ) {
704
+ // explicitOnly -> only put in the non-default storage while filename explicitly starts with 'C:' (default=false)
693
705
jsDebug (DBG_INFO ,"CreateFile (%d bytes)\n" , size );
694
- char drive = jsfStripDriveFromName (& name , false/* ensure .js/etc go in C */ );
706
+ bool explicitDriveName = jsvIsDriveNameExplicit (& name );
707
+ char drive = jsfStripDriveFromName (& name , explicitOnly /* ensure .js/etc go in C */ );
695
708
jsfCacheClearFile (name );
696
709
uint32_t bankStartAddress ,bankEndAddress ;
697
710
jsfGetDriveBankAddress (drive ,& bankStartAddress ,& bankEndAddress );
@@ -724,6 +737,14 @@ static uint32_t jsfCreateFile(JsfFileName name, uint32_t size, JsfFileFlags flag
724
737
} while (addr && !freeAddr );
725
738
// If we don't have space, compact
726
739
if (!freeAddr ) {
740
+ #ifdef JSF_BANK2_START_ADDRESS
741
+ if (!explicitOnly && !explicitDriveName && bankStartAddress != JSF_DEFAULT_START_ADDRESS ) {
742
+ /* Drive name wasn't explicit but we're not in the default area (eg maybe file ends in .js)
743
+ so let's try again with explicitOnly=true to force file into the default area where maybe
744
+ there's space */
745
+ return _jsfCreateFile (name , size , flags , returnedHeader , true);
746
+ }
747
+ #endif
727
748
//jsiConsolePrintf("%d Free, %d Trash -> need %d\n", freeSpace, trashSpace, requiredSize);
728
749
if (!compacted && (requiredSize < (freeSpace + trashSpace ))) {
729
750
// only try and compact if we're sure there would be enough space - it's better to fail fast!
@@ -756,6 +777,10 @@ static uint32_t jsfCreateFile(JsfFileName name, uint32_t size, JsfFileFlags flag
756
777
jsfCachePut (& header , addr );
757
778
return addr ;
758
779
}
780
+ /** Create a new 'file' in the memory store - DOES NOT remove existing files with same name. Return the address of data start, or 0 on error */
781
+ static uint32_t jsfCreateFile (JsfFileName name , uint32_t size , JsfFileFlags flags , JsfFileHeader * returnedHeader ) {
782
+ return _jsfCreateFile (name , size , flags , returnedHeader , false);
783
+ }
759
784
760
785
static uint32_t jsfBankFindFile (uint32_t bankAddress , uint32_t bankEndAddress , JsfFileName name , JsfFileHeader * returnedHeader ) {
761
786
uint32_t addr = bankAddress ;
@@ -1066,7 +1091,7 @@ bool jsfWriteFile(JsfFileName name, JsVar *data, JsfFileFlags flags, JsVarInt of
1066
1091
JsfFileHeader header ;
1067
1092
uint32_t addr = jsfFindFile (name , & header );
1068
1093
#ifdef JSF_BANK2_START_ADDRESS
1069
- if (!addr && name . c [ 1 ] == ':' ){
1094
+ if (!addr && jsvIsDriveNameExplicit ( & name ) ){
1070
1095
// if not found where it should be, try another bank to not end with two files
1071
1096
JsfFileName shortname = name ;
1072
1097
jsfStripDriveFromName (& shortname , true/* we're not using the return value - we just want the ':' bit stripped off */ );
0 commit comments