Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support extended ASCII filenames, handle hidden/system files #83

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ namespace SDLib {
//call this when a card is removed. It will allow you to insert and initialise a new card.
void SDClass::end() {
root.close();
card.done();
}

// this little helper is used to traverse paths
Expand Down Expand Up @@ -615,7 +616,8 @@ namespace SDLib {
//Serial.print("try to open file ");
//Serial.println(name);

if (f.open(_file, name, mode)) {
uint16_t index = _file->curPosition() / sizeof(dir_t) - 1;
if (f.open(_file, index, mode)) {
//Serial.println("OK!");
return File(f, name);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/utility/FatStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,6 @@ static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
}
/** Directory entry is for a file or subdirectory */
static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
return (dir->attributes & (DIR_ATT_VOLUME_ID | DIR_ATT_SYSTEM | DIR_ATT_HIDDEN)) == 0;
}
#endif // FatStructs_h
26 changes: 26 additions & 0 deletions src/utility/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,32 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
return false;
}
//------------------------------------------------------------------------------
/**
Return initialized pins to floating and dereference SPI lib.
*/
void Sd2Card::done() {
pinMode(chipSelectPin_, INPUT);
digitalWrite(chipSelectPin_, LOW);

#ifndef USE_SPI_LIB
pinMode(SPI_MISO_PIN, INPUT);
digitalWrite(SPI_MISO_PIN, LOW);
pinMode(SPI_MOSI_PIN, INPUT);
digitalWrite(SPI_MOSI_PIN, LOW);
pinMode(SPI_SCK_PIN, INPUT);
digitalWrite(SPI_SCK_PIN, LOW);
#endif

#ifndef SOFTWARE_SPI
#ifndef USE_SPI_LIB
pinMode(SS_PIN, INPUT);
digitalWrite(SS_PIN, LOW);
#else // USE_SPI_LIB
SDCARD_SPI.end();
#endif // USE_SPI_LIB
#endif // SOFTWARE_SPI
}
//------------------------------------------------------------------------------
/**
Enable or disable partial block reads.

Expand Down
1 change: 1 addition & 0 deletions src/utility/Sd2Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class Sd2Card {
return init(sckRateID, SD_CHIP_SELECT_PIN);
}
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
void done();
void partialBlockRead(uint8_t value);
/** Returns the current value, true or false, for partial block read. */
uint8_t partialBlockRead(void) const {
Expand Down
2 changes: 1 addition & 1 deletion src/utility/SdFat.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class SdFile : public Print {

// private data
uint8_t flags_; // See above for definition of flags_ bits
uint8_t type_; // type of file see above for values
uint8_t volatile type_; // type of file see above for values
uint32_t curCluster_; // cluster for current file position
uint32_t curPosition_; // current file position in bytes from beginning
uint32_t dirBlock_; // SD block that contains directory entry for file
Expand Down
50 changes: 27 additions & 23 deletions src/utility/SdFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,20 @@ uint8_t SdFile::dirEntry(dir_t* dir) {
\param[out] name A 13 byte char array for the formatted name.
*/
void SdFile::dirName(const dir_t& dir, char* name) {
uint8_t j = 0;
for (uint8_t i = 0; i < 11; i++) {
if (dir.name[i] == ' ') {
continue;
}
uint8_t i = 0, j = 0;
// unescape first character
if ((uint8_t) dir.name[i] == DIR_NAME_0XE5) {
name[j++] = 0xE5;
i++;
}
for (; i < 11; i++) {
if (i == 8) {
while (j > 0 && name[j-1] == ' ') j--;
name[j++] = '.';
}
name[j++] = dir.name[i];
}
while (name[j-1] == ' ') j--;
name[j] = 0;
}
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -288,10 +292,12 @@ uint8_t SdFile::make83Name(const char* str, uint8_t* name) {
uint8_t n = 7; // max index for part before dot
uint8_t i = 0;
// blank fill name and extension
while (i < 11) {
name[i++] = ' ';
memset(name, ' ', 11);
// escape first character
if ((uint8_t) *str == 0xE5) {
name[i++] = DIR_NAME_0XE5;
str++;
}
i = 0;
while ((c = *str++) != '\0') {
if (c == '.') {
if (n == 10) {
Expand All @@ -301,29 +307,25 @@ uint8_t SdFile::make83Name(const char* str, uint8_t* name) {
i = 8; // place for extension
} else {
// illegal FAT characters
uint8_t b;
#if defined(__AVR__)
PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
while ((b = pgm_read_byte(p++))) if (b == c) {
if (strchr_P(PSTR("|<>:+=?/[];,*\"\\"), c)) {
return false;
}
#elif defined(__arm__)
const uint8_t valid[] = "|<>^+=?/[];,*\"\\";
const uint8_t *p = valid;
while ((b = *p++)) if (b == c) {
#else
if (strchr("|<>:+=?/[];,*\"\\", c)) {
return false;
}
#endif
// check size and only allow ASCII printable characters
if (i > n || c < 0X21 || c > 0X7E) {
if (i > n || c < 0x20 || c == 0x7F) {
return false;
}
// only upper case allowed in 8.3 names - convert lower to upper
name[i++] = c < 'a' || c > 'z' ? c : c + ('A' - 'a');
}
}
// must have a file name, extension is optional
return name[0] != ' ';
return i > 0;
}
//------------------------------------------------------------------------------
/** Make a new directory.
Expand Down Expand Up @@ -690,11 +692,13 @@ uint8_t SdFile::openRoot(SdVolume* vol) {
\param[in] width Blank fill name if length is less than \a width.
*/
void SdFile::printDirName(const dir_t& dir, uint8_t width) {
uint8_t w = 0;
for (uint8_t i = 0; i < 11; i++) {
if (dir.name[i] == ' ') {
continue;
}
uint8_t i = 0, w = 0;
// unescape first character
if ((uint8_t) dir.name[i] == DIR_NAME_0XE5) {
Serial.write(0xE5);
i++; w++;
}
for (; i < 11; i++) {
if (i == 8) {
Serial.print('.');
w++;
Expand Down Expand Up @@ -1041,7 +1045,7 @@ uint8_t SdFile::rmRfStar(void) {
}

// skip if part of long file name or volume label in root
if (!DIR_IS_FILE_OR_SUBDIR(p)) {
if (p->attributes & DIR_ATT_VOLUME_ID) {
continue;
}

Expand Down
Loading