/** * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction: * LS_Count - Add +1 to nrFiles for every file within the parent * LS_GetFilename - Get the filename of the file indexed by nrFiles */ void CardReader::lsDive(SdBaseFile parent, const char* const match/*=NULL*/) { dir_t* p; uint8_t cnt = 0; // Read the next entry from a directory while ((p = parent.getLongFilename(p, fullName, 0, NULL)) != NULL) { char pn0 = p->name[0]; if (pn0 == DIR_NAME_FREE) break; if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue; if (fullName[0] == '.') continue; if (!DIR_IS_FILE_OR_SUBDIR(p)) continue; filenameIsDir = DIR_IS_SUBDIR(p); if (!filenameIsDir && (p->name[8] != 'G' || p->name[9] == '~')) continue; switch (lsAction) { case LS_Count: nrFiles++; break; case LS_GetFilename: if (match != NULL) { if (strcasecmp(match, fullName) == 0) return; } else if (cnt == nrFiles) return; cnt++; break; } } // while readDir }
// allows you to recurse into a directory File File::openNextFile(uint8_t mode) { dir_t p; //Serial.print("\t\treading dir..."); while (_file->readDir(&p) > 0) { // done if past last used entry if (p.name[0] == DIR_NAME_FREE) { //Serial.println("end"); return File(); } // skip deleted entry and entries for . and .. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') { //Serial.println("dots"); continue; } // only list subdirectories and files if (!DIR_IS_FILE_OR_SUBDIR(&p)) { //Serial.println("notafile"); continue; } // print file name with possible blank fill SdFile f; char name[13]; _file->dirName(p, name); //Serial.print("try to open file "); //Serial.println(name); if (f.open(_file, name, mode)) { //Serial.println("OK!"); return File(f, name); } else { //Serial.println("ugh"); return File(); } } //Serial.println("nothing"); return File(); }
void PBStorage::ls(uint8_t flags) { SoftwareSerial btSerial(2, 4); btSerial.begin(38400); if (!mFile.openRoot(&mVolume)) btSerial.println("openRoot failed"); //mFile.ls(flags); dir_t p; mFile.rewind(); while (mFile.readDir(p) > 0) { // done if past last used entry if (p.name[0] == DIR_NAME_FREE) break; // skip deleted entry and entries for . and .. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue; // only list subdirectories and files if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; // print file name with possible blank fill //root.printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0); for (uint8_t i = 0; i < 11; i++) { if (p.name[i] == ' ') continue; if (i == 8) { btSerial.print('.'); } btSerial.print( char(p.name[i]) ); } if (DIR_IS_SUBDIR(&p)) { btSerial.print('/'); } // print modify date/time if requested if (flags & LS_DATE) { mFile.printFatDate(p.lastWriteDate); btSerial.print(' '); mFile.printFatTime(p.lastWriteTime); } // print size if requested if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) { btSerial.print(' '); btSerial.print(p.fileSize); } btSerial.println(""); } }
void ListFiles(Client client, uint8_t flags) { // This code is just copied from SdFile.cpp in the SDFat library // and tweaked to print to the client output in html! dir_t p; root.rewind(); while (root.readDir(p) > 0) { // done if past last used entry if (p.name[0] == DIR_NAME_FREE) break; // skip deleted entry and entries for . and .. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue; // only list subdirectories and files if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; // print file name with possible blank fill //root.printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0); for (uint8_t i = 0; i < 11; i++) { if (p.name[i] == ' ') continue; if (i == 8) { client.print('.'); } client.print(p.name[i]); } if (DIR_IS_SUBDIR(&p)) { client.print('/'); } // print modify date/time if requested if (flags & LS_DATE) { root.printFatDate(p.lastWriteDate); client.print(' '); root.printFatTime(p.lastWriteTime); } // print size if requested if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) { client.print(' '); client.print(p.fileSize); } client.println("<br>"); } }
SdFile DigiSDClass::openNextFile(SdFile *curDir, char *fileName, uint8_t mode) { dir_t p; SdFile f; //Serial.print("\t\treading dir..."); while (curDir->readDir(&p) > 0) { // done if past last used entry if (p.name[0] == DIR_NAME_FREE) { //Serial.println("end"); return f; } // skip deleted entry and entries for . and .. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') { //Serial.println("dots"); continue; } // only list subdirectories and files if (!DIR_IS_FILE_OR_SUBDIR(&p)) { //Serial.println("notafile"); continue; } curDir->dirName(p, fileName); //Serial.print("try to open file "); //Serial.println(fileName); if (f.open(curDir, fileName, mode)) { //Serial.println("OK!"); return f; } else { //Serial.println("ugh"); return f; } } //Serial.println("nothing"); return f; }
void CardReader::lsDive(const char *prepend,SdFile parent) { dir_t p; uint8_t cnt=0; while (parent.readDir(p, longFilename) > 0) { if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint { char path[13*2]; char lfilename[13]; createFilename(lfilename,p); path[0]=0; if(strlen(prepend)==0) //avoid leading / if already in prepend { strcat(path,"/"); } strcat(path,prepend); strcat(path,lfilename); strcat(path,"/"); //Serial.print(path); SdFile dir; if(!dir.open(parent,lfilename, O_READ)) { if(lsAction==LS_SerialPrint) { SERIAL_ECHO_START; SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR); SERIAL_ECHOLN(lfilename); } } lsDive(path,dir); //close done automatically by destructor of SdFile } else { if (p.name[0] == DIR_NAME_FREE) break; if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue; if ( p.name[0] == '.') { if ( p.name[1] != '.') continue; } if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; filenameIsDir=DIR_IS_SUBDIR(&p); if(!filenameIsDir) { if(p.name[8]!='G') continue; if(p.name[9]=='~') continue; } //if(cnt++!=nr) continue; createFilename(filename,p); if(lsAction==LS_SerialPrint) { SERIAL_PROTOCOL(prepend); SERIAL_PROTOCOLLN(filename); } else if(lsAction==LS_Count) { nrFiles++; } else if(lsAction==LS_GetFilename) { if(cnt==nrFiles) return; cnt++; } } } }
void listFiles(const char* path, SdBaseFile* file, Client* client, uint8_t flags) { // This code is just copied from SdFile.cpp in the SDFat library // and tweaked to print to the client output in html! dir_t p; if(strcmp(path, "/") != 0) { int idx = strrchr( path, '/' ) - path; char parent[idx + 1]; strncpy(parent, path, idx); parent[idx] = 0; client->print(F("<a href=\"")); client->print(F("http://")); client->print(Ethernet.localIP()); client->print(parent); client->println(F("\">[To Parent Directory]</a>\n")); } file->rewind(); while (file->readDir(&p) > 0) { // done if past last used entry if (p.name[0] == DIR_NAME_FREE) break; // skip deleted entry and entries for . and .. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue; // only list subdirectories and files if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; // print modify date/time if requested if (flags & LS_DATE) { printFatDate(client, p.lastWriteDate); client->print(' '); printFatTime(client, p.lastWriteTime); } // print size if requested if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) { client->print(' '); client->print(p.fileSize); } if (DIR_IS_SUBDIR(&p)) { client->print(F(" <dir>")); } // print file name with possible blank fill char name[13]; file->dirName(p, name); client->print(F(" <a href=\"")); client->print(F("http://")); client->print(Ethernet.localIP()); client->print(path); if(strcmp(path, "/") != 0) client->print('/'); client->print(name); client->print("\">"); client->print(name); client->println("</a>"); } }
/** * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction: * LS_Count - Add +1 to nrFiles for every file within the parent * LS_GetFilename - Get the filename of the file indexed by nrFiles * LS_SerialPrint - Print the full path of each file to serial output */ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) { dir_t p; uint8_t cnt = 0; // Read the next entry from a directory while (parent.readDir(p, longFilename) > 0) { // If the entry is a directory and the action is LS_SerialPrint if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // Get the short name for the item, which we know is a folder char lfilename[FILENAME_LENGTH]; createFilename(lfilename, p); // Allocate enough stack space for the full path to a folder, trailing slash, and nul boolean prepend_is_empty = (prepend[0] == '\0'); int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1; char path[len]; // Append the FOLDERNAME12/ to the passed string. // It contains the full path to the "parent" argument. // We now have the full path to the item in this folder. strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum strcat(path, "/"); // 1 character // Serial.print(path); // Get a new directory object using the full path // and dive recursively into it. SdFile dir; if (!dir.open(parent, lfilename, O_READ)) { if (lsAction == LS_SerialPrint) { ECHO_LMV(ER, MSG_SD_CANT_OPEN_SUBDIR, lfilename); } } lsDive(path, dir); // close() is done automatically by destructor of SdFile } else { char pn0 = p.name[0]; if (pn0 == DIR_NAME_FREE) break; if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue; if (longFilename[0] == '.') continue; if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; filenameIsDir = DIR_IS_SUBDIR(&p); if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue; switch (lsAction) { case LS_Count: nrFiles++; break; case LS_SerialPrint: createFilename(filename, p); ECHO_V(prepend); ECHO_EV(filename); break; case LS_GetFilename: createFilename(filename, p); if (match != NULL) { if (strcasecmp(match, filename) == 0) return; } else if (cnt == nrFiles) return; cnt++; break; } } } // while readDir }
bool FatFile::open(FatFile* dirFile, fname_t* fname, uint8_t oflag) { bool emptyFound = false; #if SFN_OPEN_USES_CHKSUM uint8_t chksum; #endif uint8_t lfnOrd = 0; uint16_t emptyIndex; uint16_t index = 0; dir_t* dir; ldir_t* ldir; dirFile->rewind(); while (1) { if (!emptyFound) { emptyIndex = index; } dir = dirFile->readDirCache(true); if (!dir) { if (dirFile->getError()) { DBG_FAIL_MACRO; goto fail; } // At EOF if no error. break; } if (dir->name[0] == DIR_NAME_FREE) { emptyFound = true; break; } if (dir->name[0] == DIR_NAME_DELETED) { lfnOrd = 0; emptyFound = true; } else if (DIR_IS_FILE_OR_SUBDIR(dir)) { if (!memcmp(fname->sfn, dir->name, 11)) { // don't open existing file if O_EXCL if (oflag & O_EXCL) { DBG_FAIL_MACRO; goto fail; } #if SFN_OPEN_USES_CHKSUM if (lfnOrd && chksum != lfnChecksum(dir->name)) { DBG_FAIL_MACRO; goto fail; } #endif // SFN_OPEN_USES_CHKSUM if (!openCachedEntry(dirFile, index, oflag, lfnOrd)) { DBG_FAIL_MACRO; goto fail; } return true; } else { lfnOrd = 0; } } else if (DIR_IS_LONG_NAME(dir)) { ldir = reinterpret_cast<ldir_t*>(dir); if (ldir->ord & LDIR_ORD_LAST_LONG_ENTRY) { lfnOrd = ldir->ord & 0X1F; #if SFN_OPEN_USES_CHKSUM chksum = ldir->chksum; #endif // SFN_OPEN_USES_CHKSUM } } else { lfnOrd = 0; } index++; } // don't create unless O_CREAT and O_WRITE if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) { DBG_FAIL_MACRO; goto fail; } if (emptyFound) { index = emptyIndex; } else { if (!dirFile->addDirCluster()) { DBG_FAIL_MACRO; goto fail; } } if (!dirFile->seekSet(32UL * index)) { DBG_FAIL_MACRO; goto fail; } dir = dirFile->readDirCache(); if (!dir) { DBG_FAIL_MACRO; goto fail; } // initialize as empty file memset(dir, 0, sizeof(dir_t)); memcpy(dir->name, fname->sfn, 11); // Set base-name and extension lower case bits. dir->reservedNT = (DIR_NT_LC_BASE | DIR_NT_LC_EXT) & fname->flags; // set timestamps if (m_dateTime) { // call user date/time function m_dateTime(&dir->creationDate, &dir->creationTime); } else { // use default date/time dir->creationDate = FAT_DEFAULT_DATE; dir->creationTime = FAT_DEFAULT_TIME; } dir->lastAccessDate = dir->creationDate; dir->lastWriteDate = dir->creationDate; dir->lastWriteTime = dir->creationTime; // Force write of entry to device. dirFile->m_vol->cacheDirty(); // open entry in cache. return openCachedEntry(dirFile, index, oflag, 0); fail: return false; }