Skip to content

Commit

Permalink
Restructure for RTOS use, getName mods, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
greiman committed Nov 24, 2021
1 parent fb74151 commit bab0061
Show file tree
Hide file tree
Showing 60 changed files with 678 additions and 283 deletions.
Binary file modified doc/html.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=SdFat
version=2.1.1
version=2.1.2
license=MIT
author=Bill Greiman <fat16lib@sbcglobal.net>
maintainer=Bill Greiman <fat16lib@sbcglobal.net>
sentence=FAT16/FAT32/exFAT file system.
paragraph=FAT16/FAT32/exFAT file system.
sentence=Provides access to SD memory cards.
paragraph=The SdFat library supports FAT16, FAT32, and exFAT file systems on Standard SD, SDHC, and SDXC cards.
category=Data Storage
url=https://github.com/greiman/SdFat
repository=https://github.com/greiman/SdFat.git
Expand Down
6 changes: 3 additions & 3 deletions src/ExFatLib/ExFatConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#define ExFatConfig_h
#include "SdFatConfig.h"

#ifndef READ_ONLY
#define READ_ONLY 0
#endif // READ_ONLY
#ifndef EXFAT_READ_ONLY
#define EXFAT_READ_ONLY 0
#endif // EXFAT_READ_ONLY

#endif // ExFatConfig_h
12 changes: 6 additions & 6 deletions src/ExFatLib/ExFatFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,13 @@ bool ExFatFile::openPrivate(ExFatFile* dir, ExName_t* fname, oflag_t oflag) {
}
// Write, truncate, or at end is an error for a directory or read-only file.
if ((oflag & (O_TRUNC | O_AT_END)) || (m_flags & FILE_FLAG_WRITE)) {
if (isSubDir() || isReadOnly() || READ_ONLY) {
if (isSubDir() || isReadOnly() || EXFAT_READ_ONLY) {
DBG_FAIL_MACRO;
goto fail;
}
}

#if !READ_ONLY
#if !EXFAT_READ_ONLY
if (oflag & O_TRUNC) {
if (!(m_flags & FILE_FLAG_WRITE)) {
DBG_FAIL_MACRO;
Expand All @@ -381,14 +381,14 @@ bool ExFatFile::openPrivate(ExFatFile* dir, ExName_t* fname, oflag_t oflag) {
DBG_FAIL_MACRO;
goto fail;
}
#endif // !READ_ONLY
#endif // !EXFAT_READ_ONLY
return true;

create:
#if READ_ONLY
#if EXFAT_READ_ONLY
DBG_FAIL_MACRO;
goto fail;
#else // READ_ONLY
#else // EXFAT_READ_ONLY
// don't create unless O_CREAT and write
if (!(oflag & O_CREAT) || !(modeFlags & FILE_FLAG_WRITE) || !fname) {
DBG_WARN_MACRO;
Expand Down Expand Up @@ -471,7 +471,7 @@ bool ExFatFile::openPrivate(ExFatFile* dir, ExName_t* fname, oflag_t oflag) {
}
}
return sync();
#endif // READ_ONLY
#endif // EXFAT_READ_ONLY

fail:
// close file
Expand Down
2 changes: 1 addition & 1 deletion src/ExFatLib/ExFatFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class ExFatFile {
return isOpen() ? m_error & WRITE_ERROR : true;
}
/**
* Check for BlockDevice busy.
* Check for FsBlockDevice busy.
*
* \return true if busy else false.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/ExFatLib/ExFatFileWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "../common/DebugMacros.h"
#include "ExFatLib.h"
//==============================================================================
#if READ_ONLY
#if EXFAT_READ_ONLY
bool ExFatFile::mkdir(ExFatFile* parent, const char* path, bool pFlag) {
(void) parent;
(void)path;
Expand Down Expand Up @@ -58,7 +58,7 @@ size_t ExFatFile::write(const void* buf, size_t nbyte) {
return false;
}
//==============================================================================
#else // READ_ONLY
#else // EXFAT_READ_ONLY
//------------------------------------------------------------------------------
static uint16_t exFatDirChecksum(const uint8_t* data, uint16_t checksum) {
bool skip = data[0] == EXFAT_TYPE_FILE;
Expand Down Expand Up @@ -753,4 +753,4 @@ size_t ExFatFile::write(const void* buf, size_t nbyte) {
m_error |= WRITE_ERROR;
return 0;
}
#endif // READ_ONLY
#endif // EXFAT_READ_ONLY
2 changes: 1 addition & 1 deletion src/ExFatLib/ExFatFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const uint32_t ROOT_CLUSTER = 4;
#define writeMsg(pr, str) if (pr) pr->write(str)
#endif // PRINT_FORMAT_PROGRESS
//------------------------------------------------------------------------------
bool ExFatFormatter::format(BlockDevice* dev, uint8_t* secBuf, print_t* pr) {
bool ExFatFormatter::format(FsBlockDevice* dev, uint8_t* secBuf, print_t* pr) {
#if !PRINT_FORMAT_PROGRESS
(void)pr;
#endif // !PRINT_FORMAT_PROGRESS
Expand Down
6 changes: 3 additions & 3 deletions src/ExFatLib/ExFatFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
#ifndef ExFatFormatter_h
#define ExFatFormatter_h
#include "../common/BlockDevice.h"
#include "../common/FsBlockDevice.h"
/**
* \class ExFatFormatter
* \brief Format an exFAT volume.
Expand All @@ -40,7 +40,7 @@ class ExFatFormatter {
*
* \return true for success or false for failure.
*/
bool format(BlockDevice* dev, uint8_t* secBuf, print_t* pr = nullptr);
bool format(FsBlockDevice* dev, uint8_t* secBuf, print_t* pr = nullptr);
private:
bool syncUpcase();
bool writeUpcase(uint32_t sector);
Expand All @@ -49,7 +49,7 @@ class ExFatFormatter {
uint32_t m_upcaseSector;
uint32_t m_upcaseChecksum;
uint32_t m_upcaseSize;
BlockDevice* m_dev;
FsBlockDevice* m_dev;
uint8_t* m_secBuf;
};
#endif // ExFatFormatter_h
10 changes: 7 additions & 3 deletions src/ExFatLib/ExFatName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ size_t ExFatFile::getName7(char* name, size_t count) {
}
for (uint8_t in = 0; in < 15; in++) {
uint16_t c = getLe16(dn->unicode + 2*in);
if (c == 0 || (n + 1) >= count) {
if (c == 0) {
goto done;
}
if ((n + 1) >= count) {
DBG_FAIL_MACRO;
goto fail;
}
name[n++] = c < 0X7F ? c : '?';
}
}
Expand Down Expand Up @@ -140,8 +144,8 @@ size_t ExFatFile::getName8(char* name, size_t count) {
// Save space for zero byte.
ptr = FsUtf::cpToMb(cp, str, end - 1);
if (!ptr) {
// Truncate name. Could goto fail.
goto done;
DBG_FAIL_MACRO;
goto fail;
}
str = ptr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ExFatLib/ExFatPartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ uint32_t ExFatPartition::freeClusterCount() {
}
}
//------------------------------------------------------------------------------
bool ExFatPartition::init(BlockDevice* dev, uint8_t part) {
bool ExFatPartition::init(FsBlockDevice* dev, uint8_t part) {
uint32_t volStart = 0;
uint8_t* cache;
pbs_t* pbs;
Expand Down
22 changes: 16 additions & 6 deletions src/ExFatLib/ExFatPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
* \brief ExFatPartition include file.
*/
#include "../common/SysCall.h"
#include "../common/BlockDevice.h"
#include "../common/FsBlockDevice.h"
#include "../common/FsCache.h"
#include "../common/FsStructs.h"
#include "ExFatConfig.h"
/** Set EXFAT_READ_ONLY non-zero for read only */
#ifndef EXFAT_READ_ONLY
#define EXFAT_READ_ONLY 0
#endif // EXFAT_READ_ONLY
/** Type for exFAT partition */
const uint8_t FAT_TYPE_EXFAT = 64;

Expand Down Expand Up @@ -79,6 +82,13 @@ class ExFatPartition {
uint32_t clusterCount() const {return m_clusterCount;}
/** \return the cluster heap start sector. */
uint32_t clusterHeapStartSector() const {return m_clusterHeapStartSector;}
/** End access to volume
* \return pointer to sector size buffer for format.
*/
uint8_t* end() {
m_fatType = 0;
return cacheClear();
}
/** \return the FAT length in sectors */
uint32_t fatLength() const {return m_fatLength;}
/** \return the FAT start sector number. */
Expand All @@ -96,9 +106,9 @@ class ExFatPartition {
*
* \return true for success or false for failure.
*/
bool init(BlockDevice* dev, uint8_t part);
bool init(FsBlockDevice* dev, uint8_t part);
/**
* Check for BlockDevice busy.
* Check for device busy.
*
* \return true if busy else false.
*/
Expand Down Expand Up @@ -142,7 +152,7 @@ class ExFatPartition {
return m_dataCache.prepare(sector, option);
#endif // USE_EXFAT_BITMAP_CACHE
}
void cacheInit(BlockDevice* dev) {
void cacheInit(FsBlockDevice* dev) {
#if USE_EXFAT_BITMAP_CACHE
m_bitmapCache.init(dev);
#endif // USE_EXFAT_BITMAP_CACHE
Expand Down Expand Up @@ -213,7 +223,7 @@ class ExFatPartition {
uint32_t m_rootDirectoryCluster;
uint32_t m_clusterMask;
uint32_t m_bytesPerCluster;
BlockDevice* m_blockDev;
FsBlockDevice* m_blockDev;
uint8_t m_fatType = 0;
uint8_t m_sectorsPerClusterShift;
};
Expand Down
2 changes: 1 addition & 1 deletion src/ExFatLib/ExFatVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ExFatVolume : public ExFatPartition {
* \param[in] part partition to initialize.
* \return true for success or false for failure.
*/
bool begin(BlockDevice* dev, bool setCwv = true, uint8_t part = 1) {
bool begin(FsBlockDevice* dev, bool setCwv = true, uint8_t part = 1) {
if (!init(dev, part)) {
return false;
}
Expand Down
10 changes: 1 addition & 9 deletions src/FatLib/FatFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class FatFile {
return isOpen() ? m_error & WRITE_ERROR : true;
}
/**
* Check for BlockDevice busy.
* Check for device busy.
*
* \return true if busy else false.
*/
Expand Down Expand Up @@ -442,14 +442,6 @@ class FatFile {
* \return true for success or false for failure.
*/
bool mkdir(FatFile* dir, const char* path, bool pFlag = true);
/** No longer implemented due to Long File Names.
*
* Use getName(char* name, size_t size).
* \return a pointer to replacement suggestion.
*/
#ifndef DOXYGEN_SHOULD_SKIP_THIS
const char* __attribute__((error("use getName(name, size)"))) name();
#endif // DOXYGEN_SHOULD_SKIP_THIS
/** Open a file in the volume root directory.
*
* \param[in] vol Volume where the file is located.
Expand Down
4 changes: 2 additions & 2 deletions src/FatLib/FatFileLFN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ bool FatFile::makeUniqueSfn(FatLfn_t* fname) {
const uint8_t FIRST_HASH_SEQ = 2; // min value is 2
uint8_t pos = fname->seqPos;
DirFat_t* dir;
uint16_t hex;
uint16_t hex = 0;

DBG_HALT_IF(!(fname->flags & FNAME_FLAG_LOST_CHARS));
DBG_HALT_IF(fname->sfn[pos] != '~' && fname->sfn[pos + 1] != '1');
Expand All @@ -242,7 +242,7 @@ bool FatFile::makeUniqueSfn(FatLfn_t* fname) {
#ifdef USE_LFN_HASH
hex = Bernstein(fname->begin, fname->end, seq);
#else
hex = micros();
hex += millis();
#endif
if (pos > 3) {
// Make space in name for ~HHHH.
Expand Down
4 changes: 2 additions & 2 deletions src/FatLib/FatFileSFN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool FatFile::open(FatFile* dirFile, FatSfn_t* fname, oflag_t oflag) {
uint8_t checksum;
#endif // SFN_OPEN_USES_CHKSUM
uint8_t lfnOrd = 0;
uint16_t emptyIndex;
uint16_t emptyIndex = 0;
uint16_t index = 0;
DirFat_t* dir;
DirLfn_t* ldir;
Expand All @@ -53,7 +53,7 @@ bool FatFile::open(FatFile* dirFile, FatSfn_t* fname, oflag_t oflag) {
// At EOF if no error.
break;
}
if (dir->name[0] == FAT_NAME_FREE || dir->name[0] == FAT_NAME_FREE) {
if (dir->name[0] == FAT_NAME_DELETED || dir->name[0] == FAT_NAME_FREE) {
if (!emptyFound) {
emptyIndex = index;
emptyFound = true;
Expand Down
2 changes: 1 addition & 1 deletion src/FatLib/FatFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const uint16_t FAT16_ROOT_SECTOR_COUNT =
#define writeMsg(str) if (m_pr) m_pr->write(str)
#endif // PRINT_FORMAT_PROGRESS
//------------------------------------------------------------------------------
bool FatFormatter::format(BlockDevice* dev, uint8_t* secBuf, print_t* pr) {
bool FatFormatter::format(FsBlockDevice* dev, uint8_t* secBuf, print_t* pr) {
bool rtn;
m_dev = dev;
m_secBuf = secBuf;
Expand Down
6 changes: 3 additions & 3 deletions src/FatLib/FatFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef FatFormatter_h
#define FatFormatter_h
#include "../common/SysCall.h"
#include "../common/BlockDevice.h"
#include "../common/FsBlockDevice.h"
/**
* \class FatFormatter
* \brief Format a FAT volume.
Expand All @@ -41,7 +41,7 @@ class FatFormatter {
*
* \return true for success or false for failure.
*/
bool format(BlockDevice* dev, uint8_t* secBuffer, print_t* pr = nullptr);
bool format(FsBlockDevice* dev, uint8_t* secBuffer, print_t* pr = nullptr);

private:
bool initFatDir(uint8_t fatType, uint32_t sectorCount);
Expand All @@ -56,7 +56,7 @@ class FatFormatter {
uint32_t m_relativeSectors;
uint32_t m_sectorCount;
uint32_t m_totalSectors;
BlockDevice* m_dev;
FsBlockDevice* m_dev;
print_t* m_pr;
uint8_t* m_secBuf;
uint16_t m_reservedSectorCount;
Expand Down
24 changes: 14 additions & 10 deletions src/FatLib/FatName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ size_t FatFile::getName(char* name, size_t size) {
size_t FatFile::getName7(char* name, size_t size) {
FatFile dir;
DirLfn_t* ldir;
char* ptr = name;
char* end = ptr + size;
size_t n = 0;
if (!isOpen()) {
DBG_FAIL_MACRO;
goto fail;
Expand All @@ -78,15 +77,19 @@ size_t FatFile::getName7(char* name, size_t size) {
}
for (uint8_t i = 0; i < 13; i++) {
uint16_t c = getLfnChar(ldir, i);
if (c == 0 || (ptr + 1) == end) {
if (c == 0) {
goto done;
}
*ptr++ = c >= 0X7F ? '?' : c;
if ((n + 1) >= size) {
DBG_FAIL_MACRO;
goto fail;
}
name[n++] = c >= 0X7F ? '?' : c;
}
}
done:
*ptr = '\0';
return ptr - name;
name[n] = 0;
return n;

fail:
name[0] = '\0';
Expand Down Expand Up @@ -147,8 +150,8 @@ size_t FatFile::getName8(char* name, size_t size) {
// Save space for zero byte.
ptr = FsUtf::cpToMb(cp, str, end - 1);
if (!ptr) {
// Truncate name. Could goto fail.
goto done;
DBG_FAIL_MACRO;
goto fail;
}
str = ptr;
}
Expand Down Expand Up @@ -205,8 +208,9 @@ size_t FatFile::getSFN(char* name, size_t size) {
continue;
}
}
if ((j + 1u) == size) {
break;
if ((j + 1u) >= size) {
DBG_FAIL_MACRO;
goto fail;
}
name[j++] = c;
}
Expand Down
Loading

0 comments on commit bab0061

Please sign in to comment.