123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728 |
- #include "MarlinConfig.h"
- #if ENABLED(SDSUPPORT)
- #include "SdBaseFile.h"
- #include "Marlin.h"
- SdBaseFile* SdBaseFile::cwd_ = 0;
- void (*SdBaseFile::dateTime_)(uint16_t* date, uint16_t* time) = 0;
- bool SdBaseFile::addCluster() {
- if (!vol_->allocContiguous(1, &curCluster_)) return false;
-
- if (firstCluster_ == 0) {
- firstCluster_ = curCluster_;
- flags_ |= F_FILE_DIR_DIRTY;
- }
- return true;
- }
- bool SdBaseFile::addDirCluster() {
- uint32_t block;
-
- if (fileSize_ / sizeof(dir_t) >= 0xFFFF) return false;
- if (!addCluster()) return false;
- if (!vol_->cacheFlush()) return false;
- block = vol_->clusterStartBlock(curCluster_);
-
- vol_->cacheSetBlockNumber(block, true);
-
- memset(vol_->cacheBuffer_.data, 0, 512);
-
- for (uint8_t i = 1; i < vol_->blocksPerCluster_; i++) {
- if (!vol_->writeBlock(block + i, vol_->cacheBuffer_.data)) return false;
- }
-
- fileSize_ += 512UL << vol_->clusterSizeShift_;
- return true;
- }
- dir_t* SdBaseFile::cacheDirEntry(uint8_t action) {
- if (!vol_->cacheRawBlock(dirBlock_, action)) return NULL;
- return vol_->cache()->dir + dirIndex_;
- }
- bool SdBaseFile::close() {
- bool rtn = sync();
- type_ = FAT_FILE_TYPE_CLOSED;
- return rtn;
- }
- bool SdBaseFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) {
-
- if (firstCluster_ == 0) return false;
- for (uint32_t c = firstCluster_; ; c++) {
- uint32_t next;
- if (!vol_->fatGet(c, &next)) return false;
-
- if (next != (c + 1)) {
-
- if (!vol_->isEOC(next)) return false;
- *bgnBlock = vol_->clusterStartBlock(firstCluster_);
- *endBlock = vol_->clusterStartBlock(c)
- + vol_->blocksPerCluster_ - 1;
- return true;
- }
- }
- return false;
- }
- bool SdBaseFile::createContiguous(SdBaseFile* dirFile, const char* path, uint32_t size) {
- uint32_t count;
-
- if (size == 0) return false;
- if (!open(dirFile, path, O_CREAT | O_EXCL | O_RDWR)) return false;
-
- count = ((size - 1) >> (vol_->clusterSizeShift_ + 9)) + 1;
-
- if (!vol_->allocContiguous(count, &firstCluster_)) {
- remove();
- return false;
- }
- fileSize_ = size;
-
- flags_ |= F_FILE_DIR_DIRTY;
- return sync();
- }
- bool SdBaseFile::dirEntry(dir_t* dir) {
- dir_t* p;
-
- if (!sync()) return false;
-
- p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
- if (!p) return false;
-
- memcpy(dir, p, sizeof(dir_t));
- return true;
- }
- void SdBaseFile::dirName(const dir_t& dir, char* name) {
- uint8_t j = 0;
- for (uint8_t i = 0; i < 11; i++) {
- if (dir.name[i] == ' ')continue;
- if (i == 8) name[j++] = '.';
- name[j++] = dir.name[i];
- }
- name[j] = 0;
- }
- bool SdBaseFile::exists(const char* name) {
- SdBaseFile file;
- return file.open(this, name, O_READ);
- }
- int16_t SdBaseFile::fgets(char* str, int16_t num, char* delim) {
- char ch;
- int16_t n = 0;
- int16_t r = -1;
- while ((n + 1) < num && (r = read(&ch, 1)) == 1) {
-
- if (ch == '\r') continue;
- str[n++] = ch;
- if (!delim) {
- if (ch == '\n') break;
- }
- else {
- if (strchr(delim, ch)) break;
- }
- }
- if (r < 0) {
-
- return -1;
- }
- str[n] = '\0';
- return n;
- }
- bool SdBaseFile::getFilename(char * const name) {
- if (!isOpen()) return false;
- if (isRoot()) {
- name[0] = '/';
- name[1] = '\0';
- return true;
- }
-
- dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
- if (!p) return false;
-
- dirName(*p, name);
- return true;
- }
- void SdBaseFile::getpos(filepos_t* pos) {
- pos->position = curPosition_;
- pos->cluster = curCluster_;
- }
- void SdBaseFile::ls(uint8_t flags, uint8_t indent) {
- rewind();
- int8_t status;
- while ((status = lsPrintNext(flags, indent))) {
- if (status > 1 && (flags & LS_R)) {
- uint16_t index = curPosition() / 32 - 1;
- SdBaseFile s;
- if (s.open(this, index, O_READ)) s.ls(flags, indent + 2);
- seekSet(32 * (index + 1));
- }
- }
- }
- int8_t SdBaseFile::lsPrintNext(uint8_t flags, uint8_t indent) {
- dir_t dir;
- uint8_t w = 0;
- while (1) {
- if (read(&dir, sizeof(dir)) != sizeof(dir)) return 0;
- if (dir.name[0] == DIR_NAME_FREE) return 0;
-
- if (dir.name[0] != DIR_NAME_DELETED && dir.name[0] != '.'
- && DIR_IS_FILE_OR_SUBDIR(&dir)) break;
- }
-
- for (uint8_t i = 0; i < indent; i++) SERIAL_CHAR(' ');
-
- for (uint8_t i = 0; i < 11; i++) {
- if (dir.name[i] == ' ')continue;
- if (i == 8) {
- SERIAL_CHAR('.');
- w++;
- }
- SERIAL_CHAR(dir.name[i]);
- w++;
- }
- if (DIR_IS_SUBDIR(&dir)) {
- SERIAL_CHAR('/');
- w++;
- }
- if (flags & (LS_DATE | LS_SIZE)) {
- while (w++ < 14) SERIAL_CHAR(' ');
- }
-
- if (flags & LS_DATE) {
- SERIAL_CHAR(' ');
- printFatDate(dir.lastWriteDate);
- SERIAL_CHAR(' ');
- printFatTime(dir.lastWriteTime);
- }
-
- if (!DIR_IS_SUBDIR(&dir) && (flags & LS_SIZE)) {
- SERIAL_CHAR(' ');
- SERIAL_ECHO(dir.fileSize);
- }
- SERIAL_EOL();
- return DIR_IS_FILE(&dir) ? 1 : 2;
- }
- bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) {
- uint8_t n = 7,
- i = 11;
- while (i) name[--i] = ' ';
- while (*str && *str != '/') {
- uint8_t c = *str++;
- if (c == '.') {
- if (n == 10) return false;
- n = 10;
- i = 8;
- }
- else {
-
- PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
- while (uint8_t b = pgm_read_byte(p++)) if (b == c) return false;
- if (i > n || c < 0x21 || c == 0x7F) return false;
- name[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a'));
- }
- }
- *ptr = str;
- return name[0] != ' ';
- }
- bool SdBaseFile::mkdir(SdBaseFile* parent, const char* path, bool pFlag) {
- uint8_t dname[11];
- SdBaseFile dir1, dir2;
- SdBaseFile* sub = &dir1;
- SdBaseFile* start = parent;
- if (!parent || isOpen()) return false;
- if (*path == '/') {
- while (*path == '/') path++;
- if (!parent->isRoot()) {
- if (!dir2.openRoot(parent->vol_)) return false;
- parent = &dir2;
- }
- }
- while (1) {
- if (!make83Name(path, dname, &path)) return false;
- while (*path == '/') path++;
- if (!*path) break;
- if (!sub->open(parent, dname, O_READ)) {
- if (!pFlag || !sub->mkdir(parent, dname))
- return false;
- }
- if (parent != start) parent->close();
- parent = sub;
- sub = parent != &dir1 ? &dir1 : &dir2;
- }
- return mkdir(parent, dname);
- }
- bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) {
- uint32_t block;
- dir_t d;
- dir_t* p;
- if (!parent->isDir()) return false;
-
- if (!open(parent, dname, O_CREAT | O_EXCL | O_RDWR)) return false;
-
- flags_ = O_READ;
- type_ = FAT_FILE_TYPE_SUBDIR;
-
- if (!addDirCluster()) return false;
-
- if (!sync()) return false;
-
- p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!p) return false;
-
- p->attributes = DIR_ATT_DIRECTORY;
-
- memcpy(&d, p, sizeof(d));
- d.name[0] = '.';
- for (uint8_t i = 1; i < 11; i++) d.name[i] = ' ';
-
- block = vol_->clusterStartBlock(firstCluster_);
- if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) return false;
-
- memcpy(&vol_->cache()->dir[0], &d, sizeof(d));
-
- d.name[1] = '.';
- if (parent->isRoot()) {
- d.firstClusterLow = 0;
- d.firstClusterHigh = 0;
- }
- else {
- d.firstClusterLow = parent->firstCluster_ & 0xFFFF;
- d.firstClusterHigh = parent->firstCluster_ >> 16;
- }
-
- memcpy(&vol_->cache()->dir[1], &d, sizeof(d));
-
- return vol_->cacheFlush();
- }
- bool SdBaseFile::open(const char* path, uint8_t oflag) {
- return open(cwd_, path, oflag);
- }
- bool SdBaseFile::open(SdBaseFile* dirFile, const char* path, uint8_t oflag) {
- uint8_t dname[11];
- SdBaseFile dir1, dir2;
- SdBaseFile *parent = dirFile, *sub = &dir1;
- if (!dirFile || isOpen()) return false;
- if (*path == '/') {
- if (!dirFile->isRoot()) {
- if (!dir2.openRoot(dirFile->vol_)) return false;
- parent = &dir2;
- }
- while (*path == '/') path++;
- }
- for (;;) {
- if (!make83Name(path, dname, &path)) return false;
- while (*path == '/') path++;
- if (!*path) break;
- if (!sub->open(parent, dname, O_READ)) return false;
- if (parent != dirFile) parent->close();
- parent = sub;
- sub = parent != &dir1 ? &dir1 : &dir2;
- }
- return open(parent, dname, oflag);
- }
- bool SdBaseFile::open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag) {
- bool emptyFound = false, fileFound = false;
- uint8_t index;
- dir_t* p;
- vol_ = dirFile->vol_;
- dirFile->rewind();
-
- while (dirFile->curPosition_ < dirFile->fileSize_) {
- index = 0xF & (dirFile->curPosition_ >> 5);
- p = dirFile->readDirCache();
- if (!p) return false;
- if (p->name[0] == DIR_NAME_FREE || p->name[0] == DIR_NAME_DELETED) {
-
- if (!emptyFound) {
- dirBlock_ = dirFile->vol_->cacheBlockNumber();
- dirIndex_ = index;
- emptyFound = true;
- }
-
- if (p->name[0] == DIR_NAME_FREE) break;
- }
- else if (!memcmp(dname, p->name, 11)) {
- fileFound = true;
- break;
- }
- }
- if (fileFound) {
-
- if (oflag & O_EXCL) return false;
- }
- else {
-
- if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) return false;
- if (emptyFound) {
- index = dirIndex_;
- p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!p) return false;
- }
- else {
- if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) return false;
-
- if (!dirFile->addDirCluster()) return false;
-
- p = dirFile->vol_->cache()->dir;
- index = 0;
- }
-
- memset(p, 0, sizeof(*p));
- memcpy(p->name, dname, 11);
-
- if (dateTime_) {
-
- dateTime_(&p->creationDate, &p->creationTime);
- }
- else {
-
- p->creationDate = FAT_DEFAULT_DATE;
- p->creationTime = FAT_DEFAULT_TIME;
- }
- p->lastAccessDate = p->creationDate;
- p->lastWriteDate = p->creationDate;
- p->lastWriteTime = p->creationTime;
-
- if (!dirFile->vol_->cacheFlush()) return false;
- }
-
- return openCachedEntry(index, oflag);
- }
- bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) {
- dir_t* p;
- vol_ = dirFile->vol_;
-
- if (isOpen() || !dirFile) return false;
-
- if (oflag & O_EXCL) return false;
-
- if (!dirFile->seekSet(32 * index)) return false;
-
- p = dirFile->readDirCache();
- if (!p) return false;
-
- if (p->name[0] == DIR_NAME_FREE ||
- p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
- return false;
- }
-
- return openCachedEntry(index & 0xF, oflag);
- }
- bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) {
-
- dir_t* p = &vol_->cache()->dir[dirIndex];
-
- if (p->attributes & (DIR_ATT_READ_ONLY | DIR_ATT_DIRECTORY)) {
- if (oflag & (O_WRITE | O_TRUNC)) goto FAIL;
- }
-
- dirBlock_ = vol_->cacheBlockNumber();
- dirIndex_ = dirIndex;
-
- firstCluster_ = (uint32_t)p->firstClusterHigh << 16;
- firstCluster_ |= p->firstClusterLow;
-
- if (DIR_IS_FILE(p)) {
- fileSize_ = p->fileSize;
- type_ = FAT_FILE_TYPE_NORMAL;
- }
- else if (DIR_IS_SUBDIR(p)) {
- if (!vol_->chainSize(firstCluster_, &fileSize_)) goto FAIL;
- type_ = FAT_FILE_TYPE_SUBDIR;
- }
- else
- goto FAIL;
-
- flags_ = oflag & F_OFLAG;
-
- curCluster_ = 0;
- curPosition_ = 0;
- if ((oflag & O_TRUNC) && !truncate(0)) return false;
- return oflag & O_AT_END ? seekEnd(0) : true;
- FAIL:
- type_ = FAT_FILE_TYPE_CLOSED;
- return false;
- }
- bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) {
- dir_t* p;
- uint8_t index;
- if (!dirFile) return false;
-
- if (isOpen()) return false;
- vol_ = dirFile->vol_;
- while (1) {
- index = 0xF & (dirFile->curPosition_ >> 5);
-
- p = dirFile->readDirCache();
- if (!p) return false;
-
- if (p->name[0] == DIR_NAME_FREE) return false;
-
- if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
- continue;
- }
-
- if (DIR_IS_FILE_OR_SUBDIR(p)) {
- return openCachedEntry(index, oflag);
- }
- }
- return false;
- }
- #if 0
- bool SdBaseFile::openParent(SdBaseFile* dir) {
- dir_t entry;
- dir_t* p;
- SdBaseFile file;
- uint32_t c;
- uint32_t cluster;
- uint32_t lbn;
-
- if (isOpen() || !dir || dir->isRoot() || !dir->isDir()) return false;
- vol_ = dir->vol_;
-
- if (!dir->seekSet(32)) return false;
-
- if (dir->read(&entry, sizeof(entry)) != 32) return false;
-
- if (entry.name[0] != '.' || entry.name[1] != '.') return false;
-
- cluster = entry.firstClusterLow;
- cluster |= (uint32_t)entry.firstClusterHigh << 16;
- if (cluster == 0) return openRoot(vol_);
-
- lbn = vol_->clusterStartBlock(cluster);
-
- if (!vol_->cacheRawBlock(lbn, SdVolume::CACHE_FOR_READ)) return false;
- p = &vol_->cacheBuffer_.dir[1];
-
- if (p->name[0] != '.' || p->name[1] != '.') return false;
-
- if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) {
- if (!file.openRoot(dir->volume())) return false;
- }
- else if (!file.openCachedEntry(1, O_READ))
- return false;
-
- do {
- if (file.readDir(&entry, NULL) != 32) return false;
- c = entry.firstClusterLow;
- c |= (uint32_t)entry.firstClusterHigh << 16;
- } while (c != cluster);
-
- return open(&file, file.curPosition() / 32 - 1, O_READ);
- }
- #endif
- bool SdBaseFile::openRoot(SdVolume* vol) {
-
- if (isOpen()) return false;
- if (vol->fatType() == 16 || (FAT12_SUPPORT && vol->fatType() == 12)) {
- type_ = FAT_FILE_TYPE_ROOT_FIXED;
- firstCluster_ = 0;
- fileSize_ = 32 * vol->rootDirEntryCount();
- }
- else if (vol->fatType() == 32) {
- type_ = FAT_FILE_TYPE_ROOT32;
- firstCluster_ = vol->rootDirStart();
- if (!vol->chainSize(firstCluster_, &fileSize_)) return false;
- }
- else
- return false;
- vol_ = vol;
-
- flags_ = O_READ;
-
- curCluster_ = curPosition_ = 0;
-
- dirBlock_ = dirIndex_ = 0;
- return true;
- }
- int SdBaseFile::peek() {
- filepos_t pos;
- getpos(&pos);
- int c = read();
- if (c >= 0) setpos(&pos);
- return c;
- }
- static void print2u(const uint8_t v) {
- if (v < 10) SERIAL_CHAR('0');
- SERIAL_ECHO_F(v, DEC);
- }
- void SdBaseFile::printFatDate(uint16_t fatDate) {
- SERIAL_ECHO(FAT_YEAR(fatDate));
- SERIAL_CHAR('-');
- print2u(FAT_MONTH(fatDate));
- SERIAL_CHAR('-');
- print2u(FAT_DAY(fatDate));
- }
- void SdBaseFile::printFatTime(uint16_t fatTime) {
- print2u(FAT_HOUR(fatTime));
- SERIAL_CHAR(':');
- print2u(FAT_MINUTE(fatTime));
- SERIAL_CHAR(':');
- print2u(FAT_SECOND(fatTime));
- }
- bool SdBaseFile::printName() {
- char name[FILENAME_LENGTH];
- if (!getFilename(name)) return false;
- SERIAL_ECHO(name);
- return true;
- }
- int16_t SdBaseFile::read() {
- uint8_t b;
- return read(&b, 1) == 1 ? b : -1;
- }
- int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
- uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
- uint16_t offset, toRead;
- uint32_t block;
-
- if (!isOpen() || !(flags_ & O_READ)) return -1;
-
- NOMORE(nbyte, fileSize_ - curPosition_);
-
- toRead = nbyte;
- while (toRead > 0) {
- offset = curPosition_ & 0x1FF;
- if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
- block = vol_->rootDirStart() + (curPosition_ >> 9);
- }
- else {
- uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
- if (offset == 0 && blockOfCluster == 0) {
-
- if (curPosition_ == 0)
- curCluster_ = firstCluster_;
- else if (!vol_->fatGet(curCluster_, &curCluster_))
- return -1;
- }
- block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
- }
- uint16_t n = toRead;
-
- NOMORE(n, 512 - offset);
-
- if (n == 512 && block != vol_->cacheBlockNumber()) {
- if (!vol_->readBlock(block, dst)) return -1;
- }
- else {
-
- if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) return -1;
- uint8_t* src = vol_->cache()->data + offset;
- memcpy(dst, src, n);
- }
- dst += n;
- curPosition_ += n;
- toRead -= n;
- }
- return nbyte;
- }
- int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) {
- int16_t n;
-
- if (!isDir() || (0x1F & curPosition_)) return -1;
-
-
- if (longFilename) longFilename[0] = '\0';
- while (1) {
- n = read(dir, sizeof(dir_t));
- if (n != sizeof(dir_t)) return n ? -1 : 0;
-
- if (dir->name[0] == DIR_NAME_FREE) return 0;
-
- if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') {
- if (longFilename) longFilename[0] = '\0';
- continue;
- }
-
-
- if (longFilename && DIR_IS_LONG_NAME(dir)) {
- vfat_t* VFAT = (vfat_t*)dir;
-
- if (VFAT->firstClusterLow == 0) {
- const uint8_t seq = VFAT->sequenceNumber & 0x1F;
- if (WITHIN(seq, 1, MAX_VFAT_ENTRIES)) {
-
- n = (seq - 1) * (FILENAME_LENGTH);
- for (uint8_t i = 0; i < FILENAME_LENGTH; i++)
- longFilename[n + i] = (i < 5) ? VFAT->name1[i] : (i < 11) ? VFAT->name2[i - 5] : VFAT->name3[i - 11];
-
- if (VFAT->sequenceNumber & 0x40) longFilename[n + FILENAME_LENGTH] = '\0';
- }
- }
- }
-
- if (DIR_IS_FILE_OR_SUBDIR(dir)) return n;
- }
- }
- dir_t* SdBaseFile::readDirCache() {
- uint8_t i;
-
- if (!isDir()) return 0;
-
- i = (curPosition_ >> 5) & 0xF;
-
- if (read() < 0) return 0;
-
- curPosition_ += 31;
-
- return vol_->cache()->dir + i;
- }
- bool SdBaseFile::remove() {
- dir_t* d;
-
- if (!truncate(0)) return false;
-
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) return false;
-
- d->name[0] = DIR_NAME_DELETED;
-
- type_ = FAT_FILE_TYPE_CLOSED;
-
- return vol_->cacheFlush();
- return true;
- }
- bool SdBaseFile::remove(SdBaseFile* dirFile, const char* path) {
- SdBaseFile file;
- return file.open(dirFile, path, O_WRITE) ? file.remove() : false;
- }
- bool SdBaseFile::rename(SdBaseFile* dirFile, const char* newPath) {
- dir_t entry;
- uint32_t dirCluster = 0;
- SdBaseFile file;
- dir_t* d;
-
- if (!(isFile() || isSubDir())) return false;
-
- if (vol_ != dirFile->vol_) return false;
-
- sync();
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) return false;
-
- memcpy(&entry, d, sizeof(entry));
-
- d->name[0] = DIR_NAME_DELETED;
-
- if (isFile()) {
- if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) {
- goto restore;
- }
- }
- else {
-
- if (!file.mkdir(dirFile, newPath, false)) {
- goto restore;
- }
-
- dirCluster = file.firstCluster_;
- }
-
- dirBlock_ = file.dirBlock_;
- dirIndex_ = file.dirIndex_;
-
- file.type_ = FAT_FILE_TYPE_CLOSED;
-
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) return false;
-
- memcpy(&d->attributes, &entry.attributes, sizeof(entry) - sizeof(d->name));
-
- if (dirCluster) {
-
- uint32_t block = vol_->clusterStartBlock(dirCluster);
- if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) return false;
- memcpy(&entry, &vol_->cache()->dir[1], sizeof(entry));
-
- if (!vol_->freeChain(dirCluster)) return false;
-
- block = vol_->clusterStartBlock(firstCluster_);
- if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) return false;
- memcpy(&vol_->cache()->dir[1], &entry, sizeof(entry));
- }
- return vol_->cacheFlush();
- restore:
- if ((d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE))) {
-
- d->name[0] = entry.name[0];
- vol_->cacheFlush();
- }
- return false;
- }
- bool SdBaseFile::rmdir() {
-
- if (!isSubDir()) return false;
- rewind();
-
- while (curPosition_ < fileSize_) {
- dir_t* p = readDirCache();
- if (!p) return false;
-
- if (p->name[0] == DIR_NAME_FREE) break;
-
- if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
-
- if (DIR_IS_FILE_OR_SUBDIR(p)) return false;
- }
-
- type_ = FAT_FILE_TYPE_NORMAL;
- flags_ |= O_WRITE;
- return remove();
- }
- bool SdBaseFile::rmRfStar() {
- uint32_t index;
- SdBaseFile f;
- rewind();
- while (curPosition_ < fileSize_) {
-
- index = curPosition_ / 32;
- dir_t* p = readDirCache();
- if (!p) return false;
-
- if (p->name[0] == DIR_NAME_FREE) break;
-
- if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
-
- if (!DIR_IS_FILE_OR_SUBDIR(p)) continue;
- if (!f.open(this, index, O_READ)) return false;
- if (f.isSubDir()) {
-
- if (!f.rmRfStar()) return false;
- }
- else {
-
- f.flags_ |= O_WRITE;
- if (!f.remove()) return false;
- }
-
- if (curPosition_ != (32 * (index + 1))) {
- if (!seekSet(32 * (index + 1))) return false;
- }
- }
-
- if (!isRoot()) {
- if (!rmdir()) return false;
- }
- return true;
- }
- SdBaseFile::SdBaseFile(const char* path, uint8_t oflag) {
- type_ = FAT_FILE_TYPE_CLOSED;
- writeError = false;
- open(path, oflag);
- }
- bool SdBaseFile::seekSet(const uint32_t pos) {
- uint32_t nCur, nNew;
-
- if (!isOpen() || pos > fileSize_) return false;
- if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
- curPosition_ = pos;
- return true;
- }
- if (pos == 0) {
- curCluster_ = curPosition_ = 0;
- return true;
- }
-
- nCur = (curPosition_ - 1) >> (vol_->clusterSizeShift_ + 9);
- nNew = (pos - 1) >> (vol_->clusterSizeShift_ + 9);
- if (nNew < nCur || curPosition_ == 0)
- curCluster_ = firstCluster_;
- else
- nNew -= nCur;
- while (nNew--)
- if (!vol_->fatGet(curCluster_, &curCluster_)) return false;
- curPosition_ = pos;
- return true;
- }
- void SdBaseFile::setpos(filepos_t* pos) {
- curPosition_ = pos->position;
- curCluster_ = pos->cluster;
- }
- bool SdBaseFile::sync() {
-
- if (!isOpen()) goto FAIL;
- if (flags_ & F_FILE_DIR_DIRTY) {
- dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
-
- if (!d || d->name[0] == DIR_NAME_DELETED) goto FAIL;
-
- if (!isDir()) d->fileSize = fileSize_;
-
- d->firstClusterLow = firstCluster_ & 0xFFFF;
- d->firstClusterHigh = firstCluster_ >> 16;
-
- if (dateTime_) {
- dateTime_(&d->lastWriteDate, &d->lastWriteTime);
- d->lastAccessDate = d->lastWriteDate;
- }
-
- flags_ &= ~F_FILE_DIR_DIRTY;
- }
- return vol_->cacheFlush();
- FAIL:
- writeError = true;
- return false;
- }
- bool SdBaseFile::timestamp(SdBaseFile* file) {
- dir_t* d;
- dir_t dir;
-
- if (!file->dirEntry(&dir)) return false;
-
- if (!sync()) return false;
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) return false;
-
- d->lastAccessDate = dir.lastAccessDate;
- d->creationDate = dir.creationDate;
- d->creationTime = dir.creationTime;
- d->creationTimeTenths = dir.creationTimeTenths;
- d->lastWriteDate = dir.lastWriteDate;
- d->lastWriteTime = dir.lastWriteTime;
-
- return vol_->cacheFlush();
- }
- bool SdBaseFile::timestamp(uint8_t flags, uint16_t year, uint8_t month,
- uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
- uint16_t dirDate, dirTime;
- dir_t* d;
- if (!isOpen()
- || year < 1980
- || year > 2107
- || month < 1
- || month > 12
- || day < 1
- || day > 31
- || hour > 23
- || minute > 59
- || second > 59) {
- return false;
- }
-
- if (!sync()) return false;
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) return false;
- dirDate = FAT_DATE(year, month, day);
- dirTime = FAT_TIME(hour, minute, second);
- if (flags & T_ACCESS) {
- d->lastAccessDate = dirDate;
- }
- if (flags & T_CREATE) {
- d->creationDate = dirDate;
- d->creationTime = dirTime;
-
- d->creationTimeTenths = second & 1 ? 100 : 0;
- }
- if (flags & T_WRITE) {
- d->lastWriteDate = dirDate;
- d->lastWriteTime = dirTime;
- }
- return vol_->cacheFlush();
- }
- bool SdBaseFile::truncate(uint32_t length) {
- uint32_t newPos;
-
- if (!isFile() || !(flags_ & O_WRITE)) return false;
-
- if (length > fileSize_) return false;
-
- if (fileSize_ == 0) return true;
-
- newPos = curPosition_ > length ? length : curPosition_;
-
- if (!seekSet(length)) return false;
- if (length == 0) {
-
- if (!vol_->freeChain(firstCluster_)) return false;
- firstCluster_ = 0;
- }
- else {
- uint32_t toFree;
- if (!vol_->fatGet(curCluster_, &toFree)) return false;
- if (!vol_->isEOC(toFree)) {
-
- if (!vol_->freeChain(toFree)) return false;
-
- if (!vol_->fatPutEOC(curCluster_)) return false;
- }
- }
- fileSize_ = length;
-
- flags_ |= F_FILE_DIR_DIRTY;
- if (!sync()) return false;
-
- return seekSet(newPos);
- }
- int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
-
- const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
-
- uint16_t nToWrite = nbyte;
-
- if (!isFile() || !(flags_ & O_WRITE)) goto FAIL;
-
- if ((flags_ & O_APPEND) && curPosition_ != fileSize_) {
- if (!seekEnd()) goto FAIL;
- }
- while (nToWrite > 0) {
- uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
- uint16_t blockOffset = curPosition_ & 0x1FF;
- if (blockOfCluster == 0 && blockOffset == 0) {
-
- if (curCluster_ == 0) {
- if (firstCluster_ == 0) {
-
- if (!addCluster()) goto FAIL;
- }
- else {
- curCluster_ = firstCluster_;
- }
- }
- else {
- uint32_t next;
- if (!vol_->fatGet(curCluster_, &next)) goto FAIL;
- if (vol_->isEOC(next)) {
-
- if (!addCluster()) goto FAIL;
- }
- else {
- curCluster_ = next;
- }
- }
- }
-
- uint16_t n = 512 - blockOffset;
-
- NOMORE(n, nToWrite);
-
- uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
- if (n == 512) {
-
- if (vol_->cacheBlockNumber() == block) {
-
- vol_->cacheSetBlockNumber(0xFFFFFFFF, false);
- }
- if (!vol_->writeBlock(block, src)) goto FAIL;
- }
- else {
- if (blockOffset == 0 && curPosition_ >= fileSize_) {
-
- if (!vol_->cacheFlush()) goto FAIL;
-
- vol_->cacheSetBlockNumber(block, true);
- }
- else {
-
- if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto FAIL;
- }
- uint8_t* dst = vol_->cache()->data + blockOffset;
- memcpy(dst, src, n);
- }
- curPosition_ += n;
- src += n;
- nToWrite -= n;
- }
- if (curPosition_ > fileSize_) {
-
- fileSize_ = curPosition_;
- flags_ |= F_FILE_DIR_DIRTY;
- }
- else if (dateTime_ && nbyte) {
-
- flags_ |= F_FILE_DIR_DIRTY;
- }
- if (flags_ & O_SYNC) {
- if (!sync()) goto FAIL;
- }
- return nbyte;
- FAIL:
-
- writeError = true;
- return -1;
- }
- #endif
|