1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825 |
-
-
- #include "Marlin.h"
- #ifdef SDSUPPORT
-
- #include "SdBaseFile.h"
-
-
- SdBaseFile* SdBaseFile::cwd_ = 0;
-
- void (*SdBaseFile::dateTime_)(uint16_t* date, uint16_t* time) = 0;
-
-
- bool SdBaseFile::addCluster() {
- if (!vol_->allocContiguous(1, &curCluster_)) goto fail;
-
-
- if (firstCluster_ == 0) {
- firstCluster_ = curCluster_;
- flags_ |= F_FILE_DIR_DIRTY;
- }
- return true;
-
- fail:
- return false;
- }
-
-
-
- bool SdBaseFile::addDirCluster() {
- uint32_t block;
-
- if (fileSize_/sizeof(dir_t) >= 0XFFFF) goto fail;
-
- if (!addCluster()) goto fail;
- if (!vol_->cacheFlush()) goto fail;
-
- 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)) goto fail;
- }
-
- fileSize_ += 512UL << vol_->clusterSizeShift_;
- return true;
-
- fail:
- return false;
- }
-
-
-
- dir_t* SdBaseFile::cacheDirEntry(uint8_t action) {
- if (!vol_->cacheRawBlock(dirBlock_, action)) goto fail;
- return vol_->cache()->dir + dirIndex_;
-
- fail:
- return 0;
- }
-
-
- 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) goto fail;
-
- for (uint32_t c = firstCluster_; ; c++) {
- uint32_t next;
- if (!vol_->fatGet(c, &next)) goto fail;
-
-
- if (next != (c + 1)) {
-
- if (!vol_->isEOC(next)) goto fail;
- *bgnBlock = vol_->clusterStartBlock(firstCluster_);
- *endBlock = vol_->clusterStartBlock(c)
- + vol_->blocksPerCluster_ - 1;
- return true;
- }
- }
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::createContiguous(SdBaseFile* dirFile,
- const char* path, uint32_t size) {
- uint32_t count;
-
- if (size == 0) goto fail;
- if (!open(dirFile, path, O_CREAT | O_EXCL | O_RDWR)) goto fail;
-
-
- count = ((size - 1) >> (vol_->clusterSizeShift_ + 9)) + 1;
-
-
- if (!vol_->allocContiguous(count, &firstCluster_)) {
- remove();
- goto fail;
- }
- fileSize_ = size;
-
-
- flags_ |= F_FILE_DIR_DIRTY;
-
- return sync();
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::dirEntry(dir_t* dir) {
- dir_t* p;
-
- if (!sync()) goto fail;
-
-
- p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
- if (!p) goto fail;
-
-
- memcpy(dir, p, sizeof(dir_t));
- return true;
-
- fail:
- return false;
- }
-
-
- 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* 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(fpos_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++) MYSERIAL.write(' ');
-
-
- for (uint8_t i = 0; i < 11; i++) {
- if (dir.name[i] == ' ')continue;
- if (i == 8) {
- MYSERIAL.write('.');
- w++;
- }
- MYSERIAL.write(dir.name[i]);
- w++;
- }
- if (DIR_IS_SUBDIR(&dir)) {
- MYSERIAL.write('/');
- w++;
- }
- if (flags & (LS_DATE | LS_SIZE)) {
- while (w++ < 14) MYSERIAL.write(' ');
- }
-
- if (flags & LS_DATE) {
- MYSERIAL.write(' ');
- printFatDate( dir.lastWriteDate);
- MYSERIAL.write(' ');
- printFatTime( dir.lastWriteTime);
- }
-
- if (!DIR_IS_SUBDIR(&dir) && (flags & LS_SIZE)) {
- MYSERIAL.write(' ');
- MYSERIAL.print(dir.fileSize);
- }
- MYSERIAL.println();
- return DIR_IS_FILE(&dir) ? 1 : 2;
- }
-
-
- bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) {
- uint8_t c;
- uint8_t n = 7;
- uint8_t i = 0;
-
- while (i < 11) name[i++] = ' ';
- i = 0;
- while (*str != '\0' && *str != '/') {
- c = *str++;
- if (c == '.') {
- if (n == 10) goto fail;
- n = 10;
- i = 8;
- } else {
-
- PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
- uint8_t b;
- while ((b = pgm_read_byte(p++))) if (b == c) goto fail;
-
- if (i > n || c < 0X21 || c > 0X7E)goto fail;
-
- name[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a'));
- }
- }
- *ptr = str;
-
- return name[0] != ' ';
-
- fail:
- return false;
- }
-
-
- 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()) goto fail;
-
- if (*path == '/') {
- while (*path == '/') path++;
- if (!parent->isRoot()) {
- if (!dir2.openRoot(parent->vol_)) goto fail;
- parent = &dir2;
- }
- }
- while (1) {
- if (!make83Name(path, dname, &path)) goto fail;
- while (*path == '/') path++;
- if (!*path) break;
- if (!sub->open(parent, dname, O_READ)) {
- if (!pFlag || !sub->mkdir(parent, dname)) {
- goto fail;
- }
- }
- if (parent != start) parent->close();
- parent = sub;
- sub = parent != &dir1 ? &dir1 : &dir2;
- }
- return mkdir(parent, dname);
-
- fail:
- return false;
- }
-
- bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) {
- uint32_t block;
- dir_t d;
- dir_t* p;
-
- if (!parent->isDir()) goto fail;
-
-
- if (!open(parent, dname, O_CREAT | O_EXCL | O_RDWR)) goto fail;
-
-
- flags_ = O_READ;
- type_ = FAT_FILE_TYPE_SUBDIR;
-
-
- if (!addDirCluster())goto fail;
-
-
- if (!sync()) goto fail;
-
-
- p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!p) goto fail;
-
-
- 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)) goto fail;
-
-
- 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();
-
- fail:
- return false;
- }
-
-
-
- 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;
- SdBaseFile *sub = &dir1;
-
- if (!dirFile) goto fail;
-
-
- if (isOpen()) goto fail;
-
- if (*path == '/') {
- while (*path == '/') path++;
- if (!dirFile->isRoot()) {
- if (!dir2.openRoot(dirFile->vol_)) goto fail;
- parent = &dir2;
- }
- }
- while (1) {
- if (!make83Name(path, dname, &path)) goto fail;
- while (*path == '/') path++;
- if (!*path) break;
- if (!sub->open(parent, dname, O_READ)) goto fail;
- if (parent != dirFile) parent->close();
- parent = sub;
- sub = parent != &dir1 ? &dir1 : &dir2;
- }
- return open(parent, dname, oflag);
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::open(SdBaseFile* dirFile,
- const uint8_t dname[11], uint8_t oflag) {
- bool emptyFound = false;
- bool 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) goto fail;
-
- 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) goto fail;
- } else {
-
- if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) goto fail;
- if (emptyFound) {
- index = dirIndex_;
- p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!p) goto fail;
- } else {
- if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) goto fail;
-
-
- if (!dirFile->addDirCluster()) goto fail;
-
-
- p = dirFile->vol_->cache()->dir;
- index = 0;
- }
-
- memset(p, 0, sizeof(dir_t));
- 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()) goto fail;
- }
-
- return openCachedEntry(index, oflag);
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) {
- dir_t* p;
-
- vol_ = dirFile->vol_;
-
-
- if (isOpen() || !dirFile) goto fail;
-
-
- if (oflag & O_EXCL) goto fail;
-
-
- if (!dirFile->seekSet(32 * index)) goto fail;
-
-
- p = dirFile->readDirCache();
- if (!p) goto fail;
-
-
- if (p->name[0] == DIR_NAME_FREE ||
- p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
- goto fail;
- }
-
- return openCachedEntry(index & 0XF, oflag);
-
- fail:
- return false;
- }
-
-
- 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) goto fail;
-
-
- if (isOpen()) goto fail;
-
- vol_ = dirFile->vol_;
-
- while (1) {
- index = 0XF & (dirFile->curPosition_ >> 5);
-
-
- p = dirFile->readDirCache();
- if (!p) goto fail;
-
-
- if (p->name[0] == DIR_NAME_FREE) goto fail;
-
-
- if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
- continue;
- }
-
- if (DIR_IS_FILE_OR_SUBDIR(p)) {
- return openCachedEntry(index, oflag);
- }
- }
-
- fail:
- return false;
- }
-
-
- 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()) goto fail;
- vol_ = dir->vol_;
-
- if (!dir->seekSet(32)) goto fail;
-
- if (dir->read(&entry, sizeof(entry)) != 32) goto fail;
-
- if (entry.name[0] != '.' || entry.name[1] != '.') goto fail;
-
- 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)) {
- goto fail;
- }
- p = &vol_->cacheBuffer_.dir[1];
-
- if (p->name[0] != '.' || p->name[1] != '.') goto fail;
-
- if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) {
- if (!file.openRoot(dir->volume())) goto fail;
- } else {
- if (!file.openCachedEntry(1, O_READ)) goto fail;
- }
-
- do {
- if (file.readDir(&entry, NULL) != 32) goto fail;
- c = entry.firstClusterLow;
- c |= (uint32_t)entry.firstClusterHigh << 16;
- } while (c != cluster);
-
- return open(&file, file.curPosition()/32 - 1, O_READ);
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::openRoot(SdVolume* vol) {
-
- if (isOpen()) goto fail;
-
- 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_)) goto fail;
- } else {
-
- return false;
- }
- vol_ = vol;
-
- flags_ = O_READ;
-
-
- curCluster_ = 0;
- curPosition_ = 0;
-
-
- dirBlock_ = 0;
- dirIndex_ = 0;
- return true;
-
- fail:
- return false;
- }
-
-
- int SdBaseFile::peek() {
- fpos_t pos;
- getpos(&pos);
- int c = read();
- if (c >= 0) setpos(&pos);
- return c;
- }
-
-
-
- void SdBaseFile::printDirName(const dir_t& dir,
- uint8_t width, bool printSlash) {
- uint8_t w = 0;
- for (uint8_t i = 0; i < 11; i++) {
- if (dir.name[i] == ' ')continue;
- if (i == 8) {
- MYSERIAL.write('.');
- w++;
- }
- MYSERIAL.write(dir.name[i]);
- w++;
- }
- if (DIR_IS_SUBDIR(&dir) && printSlash) {
- MYSERIAL.write('/');
- w++;
- }
- while (w < width) {
- MYSERIAL.write(' ');
- w++;
- }
- }
-
-
- static void print2u( uint8_t v) {
- if (v < 10) MYSERIAL.write('0');
- MYSERIAL.print(v, DEC);
- }
-
-
-
-
-
- void SdBaseFile::printFatDate(uint16_t fatDate) {
- MYSERIAL.print(FAT_YEAR(fatDate));
- MYSERIAL.write('-');
- print2u( FAT_MONTH(fatDate));
- MYSERIAL.write('-');
- print2u( FAT_DAY(fatDate));
- }
-
-
-
- void SdBaseFile::printFatTime( uint16_t fatTime) {
- print2u( FAT_HOUR(fatTime));
- MYSERIAL.write(':');
- print2u( FAT_MINUTE(fatTime));
- MYSERIAL.write(':');
- print2u( FAT_SECOND(fatTime));
- }
-
-
- bool SdBaseFile::printName() {
- char name[13];
- if (!getFilename(name)) return false;
- MYSERIAL.print(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;
- uint16_t toRead;
- uint32_t block;
-
-
- if (!isOpen() || !(flags_ & O_READ)) goto fail;
-
-
- if (nbyte >= (fileSize_ - curPosition_)) {
- 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_)) goto fail;
- }
- }
- block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
- }
- uint16_t n = toRead;
-
-
- if (n > (512 - offset)) n = 512 - offset;
-
-
- if (n == 512 && block != vol_->cacheBlockNumber()) {
- if (!vol_->readBlock(block, dst)) goto fail;
- } else {
-
- if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail;
- uint8_t* src = vol_->cache()->data + offset;
- memcpy(dst, src, n);
- }
- dst += n;
- curPosition_ += n;
- toRead -= n;
- }
- return nbyte;
-
- fail:
- return -1;
- }
-
-
- int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) {
- int16_t n;
-
- if (!isDir() || (0X1F & curPosition_)) return -1;
-
-
- if (longFilename != NULL)
- {
- longFilename[0] = '\0';
- }
-
- while (1) {
- n = read(dir, sizeof(dir_t));
- if (n != sizeof(dir_t)) return n == 0 ? 0 : -1;
-
- if (dir->name[0] == DIR_NAME_FREE) return 0;
-
- if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') continue;
-
-
- if (DIR_IS_LONG_NAME(dir) && longFilename != NULL)
- {
- vfat_t *VFAT = (vfat_t*)dir;
-
- if (VFAT->firstClusterLow == 0 && (VFAT->sequenceNumber & 0x1F) > 0 && (VFAT->sequenceNumber & 0x1F) <= MAX_VFAT_ENTRIES)
- {
-
- n = ((VFAT->sequenceNumber & 0x1F) - 1) * 13;
- longFilename[n+0] = VFAT->name1[0];
- longFilename[n+1] = VFAT->name1[1];
- longFilename[n+2] = VFAT->name1[2];
- longFilename[n+3] = VFAT->name1[3];
- longFilename[n+4] = VFAT->name1[4];
- longFilename[n+5] = VFAT->name2[0];
- longFilename[n+6] = VFAT->name2[1];
- longFilename[n+7] = VFAT->name2[2];
- longFilename[n+8] = VFAT->name2[3];
- longFilename[n+9] = VFAT->name2[4];
- longFilename[n+10] = VFAT->name2[5];
- longFilename[n+11] = VFAT->name3[0];
- longFilename[n+12] = VFAT->name3[1];
-
- if (VFAT->sequenceNumber & 0x40)
- longFilename[n+13] = '\0';
- }
- }
-
- if (DIR_IS_FILE_OR_SUBDIR(dir)) return n;
- }
- }
-
-
-
- dir_t* SdBaseFile::readDirCache() {
- uint8_t i;
-
- if (!isDir()) goto fail;
-
-
- i = (curPosition_ >> 5) & 0XF;
-
-
- if (read() < 0) goto fail;
-
-
- curPosition_ += 31;
-
-
- return vol_->cache()->dir + i;
-
- fail:
- return 0;
- }
-
-
- bool SdBaseFile::remove() {
- dir_t* d;
-
- if (!truncate(0)) goto fail;
-
-
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) goto fail;
-
-
- d->name[0] = DIR_NAME_DELETED;
-
-
- type_ = FAT_FILE_TYPE_CLOSED;
-
-
- return vol_->cacheFlush();
- return true;
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::remove(SdBaseFile* dirFile, const char* path) {
- SdBaseFile file;
- if (!file.open(dirFile, path, O_WRITE)) goto fail;
- return file.remove();
-
- fail:
-
- return false;
- }
-
-
- bool SdBaseFile::rename(SdBaseFile* dirFile, const char* newPath) {
- dir_t entry;
- uint32_t dirCluster = 0;
- SdBaseFile file;
- dir_t* d;
-
-
- if (!(isFile() || isSubDir())) goto fail;
-
-
- if (vol_ != dirFile->vol_) goto fail;
-
-
- sync();
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) goto fail;
-
-
- 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) goto fail;
-
-
- 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)) goto fail;
- memcpy(&entry, &vol_->cache()->dir[1], sizeof(entry));
-
-
- if (!vol_->freeChain(dirCluster)) goto fail;
-
-
- block = vol_->clusterStartBlock(firstCluster_);
- if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
- memcpy(&vol_->cache()->dir[1], &entry, sizeof(entry));
- }
- return vol_->cacheFlush();
-
- restore:
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) goto fail;
-
- d->name[0] = entry.name[0];
- vol_->cacheFlush();
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::rmdir() {
-
- if (!isSubDir()) goto fail;
-
- rewind();
-
-
- while (curPosition_ < fileSize_) {
- dir_t* p = readDirCache();
- if (!p) goto fail;
-
- 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)) goto fail;
- }
-
- type_ = FAT_FILE_TYPE_NORMAL;
- flags_ |= O_WRITE;
- return remove();
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::rmRfStar() {
- uint16_t index;
- SdBaseFile f;
- rewind();
- while (curPosition_ < fileSize_) {
-
- index = curPosition_/32;
-
- dir_t* p = readDirCache();
- if (!p) goto fail;
-
-
- 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)) goto fail;
- if (f.isSubDir()) {
-
- if (!f.rmRfStar()) goto fail;
- } else {
-
- f.flags_ |= O_WRITE;
- if (!f.remove()) goto fail;
- }
-
- if (curPosition_ != (32*(index + 1))) {
- if (!seekSet(32*(index + 1))) goto fail;
- }
- }
-
- if (!isRoot()) {
- if (!rmdir()) goto fail;
- }
- return true;
-
- fail:
- return false;
- }
-
-
- SdBaseFile::SdBaseFile(const char* path, uint8_t oflag) {
- type_ = FAT_FILE_TYPE_CLOSED;
- writeError = false;
- open(path, oflag);
- }
-
-
- bool SdBaseFile::seekSet(uint32_t pos) {
- uint32_t nCur;
- uint32_t nNew;
-
- if (!isOpen() || pos > fileSize_) goto fail;
-
- if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
- curPosition_ = pos;
- goto done;
- }
- if (pos == 0) {
-
- curCluster_ = 0;
- curPosition_ = 0;
- goto done;
- }
-
- 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_)) goto fail;
- }
- curPosition_ = pos;
-
- done:
- return true;
-
- fail:
- return false;
- }
-
- void SdBaseFile::setpos(fpos_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)) goto fail;
-
-
- if (!sync()) goto fail;
-
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) goto fail;
-
-
- 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();
-
- fail:
- return false;
- }
-
-
- 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;
- uint16_t dirTime;
- dir_t* d;
-
- if (!isOpen()
- || year < 1980
- || year > 2107
- || month < 1
- || month > 12
- || day < 1
- || day > 31
- || hour > 23
- || minute > 59
- || second > 59) {
- goto fail;
- }
-
- if (!sync()) goto fail;
-
- d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
- if (!d) goto fail;
-
- 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();
-
- fail:
- return false;
- }
-
-
- bool SdBaseFile::truncate(uint32_t length) {
- uint32_t newPos;
-
- if (!isFile() || !(flags_ & O_WRITE)) goto fail;
-
-
- if (length > fileSize_) goto fail;
-
-
- if (fileSize_ == 0) return true;
-
-
- newPos = curPosition_ > length ? length : curPosition_;
-
-
- if (!seekSet(length)) goto fail;
-
- if (length == 0) {
-
- if (!vol_->freeChain(firstCluster_)) goto fail;
- firstCluster_ = 0;
- } else {
- uint32_t toFree;
- if (!vol_->fatGet(curCluster_, &toFree)) goto fail;
-
- if (!vol_->isEOC(toFree)) {
-
- if (!vol_->freeChain(toFree)) goto fail;
-
-
- if (!vol_->fatPutEOC(curCluster_)) goto fail;
- }
- }
- fileSize_ = length;
-
-
- flags_ |= F_FILE_DIR_DIRTY;
-
- if (!sync()) goto fail;
-
-
- return seekSet(newPos);
-
- fail:
- return false;
- }
-
-
- 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;
-
-
- if (n > nToWrite) 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;
- }
-
-
- #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
- void (*SdBaseFile::oldDateTime_)(uint16_t& date, uint16_t& time) = 0;
- #endif
-
-
- #endif
|