📄 fatlite.c
字号:
if (lfnStatus == LFN_SHORT_NAME)
reqSpace = sizeof(DirectoryEntry);
else
reqSpace = sizeof(DirectoryEntry) *
(LFN_ENTRIES_NUM(remainedLength) + 1);
}
}
if (lfnSearch.neverUsed)
cont = FALSE; /* path not found */
}
} /* while() */
} /* for () */
#ifndef FL_READ_ONLY
if( (status == flFileNotFound && (file->flags & FL_FILE_MUST_OPEN) &&
file->directorySector == 0) && reqSpace ) {
FLDword offsetInCluster;
FLSDword savePosition = scanFile.currentPosition;
/* There is not enough space for the new file name. Find more space in the next */
/* sectors in current cluster or in the sectors of new allocated cluster */
#ifdef SUB_DIRECTORY
offsetInCluster = (FLDword)scanFile.currentPosition & (pVol->bytesPerCluster - 1);
if ((offsetInCluster == 0) || ((offsetInCluster + reqSpace - 1) >= pVol->bytesPerCluster)) {
/* Allocate new cluster */
if (offsetInCluster != 0)
scanFile.currentPosition += (pVol->bytesPerCluster - offsetInCluster);
if(!(scanFile.flags & FL_FILE_IS_ROOT_DIR))
scanFile.fileSize = scanFile.currentPosition;
checkStatus(extendDirectory(&scanFile,(unsigned) file->ownerDirCluster));
}
#else
status = flRootDirectoryFull;
#endif
scanFile.currentPosition = savePosition + reqSpace - 1;
checkStatus( getSectorAndOffset( &scanFile, &file->directorySector, &offsetInSector));
file->directoryIndex = offsetInSector / sizeof(DirectoryEntry);
}
#endif /* FL_READ_ONLY */
return status;
}
/*----------------------------------------------------------------------*/
/* r e a d M u l t i S e c t o r */
/* */
/* Checks if file was written on consequent sectors. */
/* Parameters: */
/* file : File to check */
/* stillToRead : Number of bytes to read. If the read extends */
/* beyond the end-of-file, the read is truncated */
/* at the end-of-file. */
/* Returns: */
/* FLStatus : 0 on success, otherwise failed */
/* sectors : Number of consequent sectors */
/*----------------------------------------------------------------------*/
static FLStatus readMultiSector(Volume vol,File *file,
FLDword stillToRead,
SectorNo* sectors)
{
SectorNo sectorCount = 1;
unsigned offsetInCluster = (unsigned)((file->currentPosition & (vol.bytesPerCluster - 1))+512);
while(stillToRead>=((sectorCount+1)<<FL_SECTOR_SIZE_BITS))
{
if(offsetInCluster>=vol.bytesPerCluster) {
unsigned nextCluster;
nextCluster = file->currentCluster;
checkStatus(getFATentry(&vol,&nextCluster));
if( IsValidCluster( nextCluster ) == FALSE )
/* We have a bad file size, or the FAT is bad */
return flInvalidFATchain;
if(nextCluster!=file->currentCluster+1)
break;
file->currentCluster = nextCluster;
offsetInCluster = 0;
}
offsetInCluster+=FL_SECTOR_SIZE;
sectorCount++;
}
*sectors = sectorCount;
return flOK;
}
/*----------------------------------------------------------------------*/
/* r e a d F i l e */
/* */
/* Reads from the current position in the file to the user-buffer. */
/* Parameters: */
/* file : File to read. */
/* ioreq->irData : Address of user buffer */
/* ioreq->irLength : Number of bytes to read. If the read extends */
/* beyond the end-of-file, the read is truncated */
/* at the end-of-file. */
/* */
/* Returns: */
/* FLStatus : 0 on success, otherwise failed */
/* ioreq->irLength : Actual number of bytes read */
/*----------------------------------------------------------------------*/
FLStatus readFile(File *file,IOreq FAR2 *ioreq)
{
Volume vol = file->fileVol;
FLByte FAR1 *userData = (FLByte FAR1 *) ioreq->irData; /* user buffer address */
FLDword stillToRead = ioreq->irLength;
FLDword remainingInFile = file->fileSize - file->currentPosition;
ioreq->irLength = 0; /* read so far */
/* Should we return an end of file status ? */
if (stillToRead > remainingInFile)
stillToRead = (unsigned) remainingInFile;
while (stillToRead > 0) {
SectorNo sectorToRead;
unsigned offsetInSector;
FLDword readThisTime;
const FLByte FAR0 *sector;
checkStatus(getSectorAndOffset(file,§orToRead,&offsetInSector));
if (stillToRead < FL_SECTOR_SIZE || offsetInSector > 0 || vol.tl.readSectors==NULL) {
sector = (const FLByte FAR0 *) findSector(&vol,sectorToRead);
if(sector==NULL)
{
DBG_PRINT_ERR(FLZONE_TL,"readFile : sector was not found\r\n");
return flSectorNotFound;
}
if(sector==(const FLByte FAR0 *)dataErrorToken)
return flDataError;
readThisTime = FL_SECTOR_SIZE - offsetInSector;
if (readThisTime > stillToRead)
readThisTime = (unsigned) stillToRead;
if (sector)
tffscpy(userData,sector + offsetInSector,(FLWord)readThisTime);
else
return flSectorNotFound; /* Sector does not exist */
}
else {
SectorNo sectorCount;
checkStatus(readMultiSector(&vol,file,stillToRead,§orCount));
checkStatus(vol.tl.readSectors(vol.tl.rec,sectorToRead,userData,sectorCount));
readThisTime = (sectorCount<<FL_SECTOR_SIZE_BITS);
}
stillToRead -= readThisTime;
ioreq->irLength += readThisTime;
userData = (FLByte FAR1 *)addToFarPointer(userData,readThisTime);
file->currentPosition += readThisTime;
}
return flOK;
}
/*-------------------------------------------------------------------------------------------
* f i n d N e x t F i l e
* For complete description see findFirstFile().
* Input: ioreq->irHandle - File handle returned by flFindFirstFile
* ioreq->irPath - not relevant
*------------------------------------------------------------------------------------------*/
FLStatus findNextFile(File *file, IOreq FAR2 *ioreq)
{
return
#ifdef FL_NO_LONG_FILENAMES_SUPPORT
simpleFindNextFile(file, ioreq);
#else
findNextFileHdlr(file, ioreq);
#endif
}
#ifdef FL_NO_LONG_FILENAMES_SUPPORT
/*-------------------------------------------------------------------------------------------
* s i m p l e F i n d N e x t F i l e
* For complete description see findFirstFile().
*------------------------------------------------------------------------------------------*/
static FLStatus simpleFindNextFile(File *file, IOreq FAR2 *ioreq)
{
LfnParam lfn;
DirectoryEntry *entryBuff = (DirectoryEntry *)ioreq->irData;
FLStatus ret;
lfn.name = nameBuff;
lfn.bufSize = sizeof(nameBuff);
ioreq->irData = (void *)&lfn;
ret = findNextFileHdlr( file, ioreq);
if (ret == flOK)
*entryBuff = lfn.dirEntry;
else
entryBuff->name[0] = 0;
ioreq->irData = entryBuff;
return ret;
}
#endif
#ifdef FL_LONG_FILENAMES_ASCII_SUPPORT
/*-------------------------------------------------------------------------------------------
* a s c i i F i n d N e x t F i l e
* For complete description see asciiFindFirstFile().
*------------------------------------------------------------------------------------------*/
static FLStatus asciiFindNextFile(File *file, IOreq FAR2 *ioreq)
{
LfnParam *lfnSrc = ioreq->irData, lfn;
FLStatus ret;
lfn.name = nameBuff;
lfn.length = sizeof(nameBuff);
ioreq->irData = &lfn;
ret = findNextFileHdlr( file, ioreq);
lfnSrc->dirEntry = lfn.dirEntry;
if (ret == flOK)
ret = unicodeToAscii( lfnSrc->name, lfn.name, lfnSrc->length);
ioreq->irData = lfnSrc;
return ret;
}
#endif
/*--------------------------------------------------------------------------
* f i n d N e x t F i l e H d l r
* See the description of findFirstFile()
* Input: ioreq->irHandle - File handle returned by flFindFirstFile
* ioreq->irPath - not relevant
*---------------------------------------------------------------------------*/
static FLStatus findNextFileHdlr(File *file, IOreq FAR2 *ioreq)
{
LfnParam *lfn = (LfnParam *) ioreq->irData;
FLStatus res = flOK;
const DirectoryEntry FAR0 *entry;
FLLfnSearch search;
FLPathSegment path;
File dir;
FLBoolean cont;
DirectoryEntry FAR0 *foundEntry;
SectorNo sectorOfDir;
unsigned offsetInSector;
FLSDword delta;
FLDword startIdx;
/* Do we have a directory ? */
if (!(file->flags & FL_FILE_IS_DIRECTORY))
return flNotADirectory;
/* Obtain a directory entry corresponding to current position */
if (!file->currentPosition) {
entry = getDirEntry(file);
if (entry == NULL)
return flSectorNotFound;
if(entry==(const DirectoryEntry FAR0 *)dataErrorToken)
return flDataError;
dir.currentPosition = sizeof(DirectoryEntry) - 1;
dir.currentCluster = LE2(entry->startingCluster);
}
else {
dir.currentPosition = file->currentPosition;
dir.currentCluster = file->currentCluster;
}
dir.fileVol = file->fileVol;
dir.fileSize = 0x7fffffffl;
dir.flags = file->flags;
path.buffSize = lfn->bufSize / sizeof(FLWchar);
path.name = (FLWchar *)lfn->name;
path.get = TRUE;
search.verifiedChars = 0;
cont = TRUE;
while ((res == flOK) && cont) {
res = getSectorAndOffset( &dir, §orOfDir, &offsetInSector);
if (res != flOK)
continue;
startIdx = search.idx = offsetInSector / sizeof(DirectoryEntry);
entry = (DirectoryEntry FAR0 *)findSector( file->fileVol, sectorOfDir);
if (entry == NULL) {
res = flSectorNotFound;
continue;
}
else if(entry==(const DirectoryEntry FAR0 *)dataErrorToken) {
res = flDataError;
continue;
}
foundEntry = getFirstFileName( &path, entry, &search);
if (foundEntry) {
lfn->dirEntry = *foundEntry;
if (path.name[0] == 0)
res = flBufferTooShort;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -