Arduino IDE  0
Arduino IDE and compiler
SdFile Class Reference

Access FAT16 and FAT32 files on SD and SDHC cards. More...

#include <SdFat.h>

+ Inheritance diagram for SdFile:
+ Collaboration diagram for SdFile:

Public Member Functions

 SdFile (void)
 
void clearUnbufferedRead (void)
 
uint8_t close (void)
 
uint8_t contiguousRange (uint32_t *bgnBlock, uint32_t *endBlock)
 
uint8_t createContiguous (SdFile *dirFile, const char *fileName, uint32_t size)
 
uint32_t curCluster (void) const
 
uint32_t curPosition (void) const
 
uint32_t dirBlock (void) const
 
uint8_t dirEntry (dir_t *dir)
 
uint8_t dirIndex (void) const
 
uint32_t fileSize (void) const
 
uint32_t firstCluster (void) const
 
uint8_t isDir (void) const
 
uint8_t isFile (void) const
 
uint8_t isOpen (void) const
 
uint8_t isSubDir (void) const
 
uint8_t isRoot (void) const
 
void ls (uint8_t flags=0, uint8_t indent=0)
 
uint8_t makeDir (SdFile *dir, const char *dirName)
 
uint8_t open (SdFile *dirFile, uint16_t index, uint8_t oflag)
 
uint8_t open (SdFile *dirFile, const char *fileName, uint8_t oflag)
 
uint8_t openRoot (SdVolume *vol)
 
int16_t read (void)
 
int16_t read (void *buf, uint16_t nbyte)
 
int8_t readDir (dir_t *dir)
 
uint8_t remove (void)
 
void rewind (void)
 
uint8_t rmDir (void)
 
uint8_t rmRfStar (void)
 
uint8_t seekCur (uint32_t pos)
 
uint8_t seekEnd (void)
 
uint8_t seekSet (uint32_t pos)
 
void setUnbufferedRead (void)
 
uint8_t timestamp (uint8_t flag, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
 
uint8_t sync (void)
 
uint8_t type (void) const
 
uint8_t truncate (uint32_t size)
 
uint8_t unbufferedRead (void) const
 
SdVolumevolume (void) const
 
size_t write (uint8_t b)
 
size_t write (const void *buf, uint16_t nbyte)
 
size_t write (const char *str)
 
uint8_t contiguousRange (uint32_t &bgnBlock, uint32_t &endBlock)
 
uint8_t createContiguous (SdFile &dirFile, const char *fileName, uint32_t size)
 
uint8_t dirEntry (dir_t &dir)
 
uint8_t makeDir (SdFile &dir, const char *dirName)
 
uint8_t open (SdFile &dirFile, const char *fileName, uint8_t oflag)
 
uint8_t open (SdFile &dirFile, const char *fileName)
 
uint8_t open (SdFile &dirFile, uint16_t index, uint8_t oflag)
 
uint8_t openRoot (SdVolume &vol)
 
int8_t readDir (dir_t &dir)
 
 SdFile (void)
 
void clearUnbufferedRead (void)
 
uint8_t close (void)
 
uint8_t contiguousRange (uint32_t *bgnBlock, uint32_t *endBlock)
 
uint8_t createContiguous (SdFile *dirFile, const char *fileName, uint32_t size)
 
uint32_t curCluster (void) const
 
uint32_t curPosition (void) const
 
uint32_t dirBlock (void) const
 
uint8_t dirEntry (dir_t *dir)
 
uint8_t dirIndex (void) const
 
uint32_t fileSize (void) const
 
uint32_t firstCluster (void) const
 
uint8_t isDir (void) const
 
uint8_t isFile (void) const
 
uint8_t isOpen (void) const
 
uint8_t isSubDir (void) const
 
uint8_t isRoot (void) const
 
void ls (uint8_t flags=0, uint8_t indent=0)
 
uint8_t makeDir (SdFile *dir, const char *dirName)
 
uint8_t open (SdFile *dirFile, uint16_t index, uint8_t oflag)
 
uint8_t open (SdFile *dirFile, const char *fileName, uint8_t oflag)
 
uint8_t openRoot (SdVolume *vol)
 
int16_t read (void)
 
int16_t read (void *buf, uint16_t nbyte)
 
int8_t readDir (dir_t *dir)
 
uint8_t remove (void)
 
void rewind (void)
 
uint8_t rmDir (void)
 
uint8_t rmRfStar (void)
 
uint8_t seekCur (uint32_t pos)
 
uint8_t seekEnd (void)
 
uint8_t seekSet (uint32_t pos)
 
void setUnbufferedRead (void)
 
uint8_t timestamp (uint8_t flag, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
 
uint8_t sync (void)
 
uint8_t type (void) const
 
uint8_t truncate (uint32_t size)
 
uint8_t unbufferedRead (void) const
 
SdVolumevolume (void) const
 
size_t write (uint8_t b)
 
size_t write (const void *buf, uint16_t nbyte)
 
size_t write (const char *str)
 
uint8_t contiguousRange (uint32_t &bgnBlock, uint32_t &endBlock)
 
uint8_t createContiguous (SdFile &dirFile, const char *fileName, uint32_t size)
 
uint8_t dirEntry (dir_t &dir)
 
uint8_t makeDir (SdFile &dir, const char *dirName)
 
uint8_t open (SdFile &dirFile, const char *fileName, uint8_t oflag)
 
uint8_t open (SdFile &dirFile, const char *fileName)
 
uint8_t open (SdFile &dirFile, uint16_t index, uint8_t oflag)
 
uint8_t openRoot (SdVolume &vol)
 
int8_t readDir (dir_t &dir)
 

Static Public Member Functions

static void dateTimeCallback (void(*dateTime)(uint16_t *date, uint16_t *time))
 
static void dateTimeCallbackCancel (void)
 
static void dirName (const dir_t &dir, char *name)
 
static void printDirName (const dir_t &dir, uint8_t width)
 
static void printFatDate (uint16_t fatDate)
 
static void printFatTime (uint16_t fatTime)
 
static void printTwoDigits (uint8_t v)
 
static uint8_t remove (SdFile *dirFile, const char *fileName)
 
static void dateTimeCallback (void(*dateTime)(uint16_t &date, uint16_t &time))
 
static uint8_t remove (SdFile &dirFile, const char *fileName)
 
static void dateTimeCallback (void(*dateTime)(uint16_t *date, uint16_t *time))
 
static void dateTimeCallbackCancel (void)
 
static void dirName (const dir_t &dir, char *name)
 
static void printDirName (const dir_t &dir, uint8_t width)
 
static void printFatDate (uint16_t fatDate)
 
static void printFatTime (uint16_t fatTime)
 
static void printTwoDigits (uint8_t v)
 
static uint8_t remove (SdFile *dirFile, const char *fileName)
 
static void dateTimeCallback (void(*dateTime)(uint16_t &date, uint16_t &time))
 
static uint8_t remove (SdFile &dirFile, const char *fileName)
 

Detailed Description

Access FAT16 and FAT32 files on SD and SDHC cards.

Definition at line 137 of file SdFat.h.

Constructor & Destructor Documentation

◆ SdFile() [1/2]

SdFile::SdFile ( void  )

Create an instance of SdFile.

Definition at line 140 of file SdFat.h.

140 : type_(FAT_FILE_TYPE_CLOSED) {}
uint8_t const FAT_FILE_TYPE_CLOSED
Definition: SdFat.h:84

◆ SdFile() [2/2]

SdFile::SdFile ( void  )

Create an instance of SdFile.

Definition at line 140 of file SdFat.h.

140 : type_(FAT_FILE_TYPE_CLOSED) {}
uint8_t const FAT_FILE_TYPE_CLOSED
Definition: SdFat.h:84

Member Function Documentation

◆ clearUnbufferedRead() [1/2]

void SdFile::clearUnbufferedRead ( void  )

writeError is set to true if an error occurs during a write(). Set writeError to false before calling print() and/or write() and check for true after calls to print() and/or write(). Cancel unbuffered reads for this file. See setUnbufferedRead()

Definition at line 151 of file SdFat.h.

References close(), contiguousRange(), and createContiguous().

151  {
152  flags_ &= ~F_FILE_UNBUFFERED_READ;
153  }
+ Here is the call graph for this function:

◆ clearUnbufferedRead() [2/2]

void SdFile::clearUnbufferedRead ( void  )

writeError is set to true if an error occurs during a write(). Set writeError to false before calling print() and/or write() and check for true after calls to print() and/or write(). Cancel unbuffered reads for this file. See setUnbufferedRead()

Definition at line 151 of file SdFat.h.

References close(), contiguousRange(), and createContiguous().

151  {
152  flags_ &= ~F_FILE_UNBUFFERED_READ;
153  }
+ Here is the call graph for this function:

◆ close() [1/2]

uint8_t SdFile::close ( void  )

Close a file and force cached data and directory information to be written to the storage device.

Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include no file is open or an I/O error.

Definition at line 76 of file SdFile.cpp.

References FAT_FILE_TYPE_CLOSED, and sync().

Referenced by requests.sessions.Session::__exit__(), SDLib::SDClass::begin(), SDLib::callback_pathExists(), clearUnbufferedRead(), SDLib::File::close(), SDLib::SDClass::end(), SDLib::SDClass::open(), and SDLib::walkPath().

76  {
77  if (!sync())return false;
78  type_ = FAT_FILE_TYPE_CLOSED;
79  return true;
80 }
uint8_t sync(void)
Definition: SdFile.cpp:965
uint8_t const FAT_FILE_TYPE_CLOSED
Definition: SdFat.h:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ close() [2/2]

uint8_t SdFile::close ( void  )

Referenced by requests.sessions.Session::__exit__().

+ Here is the caller graph for this function:

◆ contiguousRange() [1/4]

uint8_t SdFile::contiguousRange ( uint32_t *  bgnBlock,
uint32_t *  endBlock 
)

Check for contiguous file and return its raw block range.

Parameters
[out]bgnBlockthe first block address for the file.
[out]endBlockthe last block address for the file.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include file is not contiguous, file has zero length or an I/O error occurred.

Definition at line 93 of file SdFile.cpp.

References requests.packages.urllib3.packages.six::next.

Referenced by clearUnbufferedRead(), and contiguousRange().

93  {
94  // error if no blocks
95  if (firstCluster_ == 0) return false;
96 
97  for (uint32_t c = firstCluster_; ; c++) {
98  uint32_t next;
99  if (!vol_->fatGet(c, &next)) return false;
100 
101  // check for contiguous
102  if (next != (c + 1)) {
103  // error if not end of chain
104  if (!vol_->isEOC(next)) return false;
105  *bgnBlock = vol_->clusterStartBlock(firstCluster_);
106  *endBlock = vol_->clusterStartBlock(c)
107  + vol_->blocksPerCluster_ - 1;
108  return true;
109  }
110  }
111 }
+ Here is the caller graph for this function:

◆ contiguousRange() [2/4]

uint8_t SdFile::contiguousRange ( uint32_t *  bgnBlock,
uint32_t *  endBlock 
)

◆ contiguousRange() [3/4]

uint8_t SdFile::contiguousRange ( uint32_t &  bgnBlock,
uint32_t &  endBlock 
)
Deprecated:
Use: uint8_t SdFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);

Definition at line 301 of file SdFat.h.

References contiguousRange().

301  { // NOLINT
302  return contiguousRange(&bgnBlock, &endBlock);
303  }
uint8_t contiguousRange(uint32_t *bgnBlock, uint32_t *endBlock)
Definition: SdFile.cpp:93
+ Here is the call graph for this function:

◆ contiguousRange() [4/4]

uint8_t SdFile::contiguousRange ( uint32_t &  bgnBlock,
uint32_t &  endBlock 
)
Deprecated:
Use: uint8_t SdFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);

Definition at line 301 of file SdFat.h.

References contiguousRange().

301  { // NOLINT
302  return contiguousRange(&bgnBlock, &endBlock);
303  }
uint8_t contiguousRange(uint32_t *bgnBlock, uint32_t *endBlock)
Definition: SdFile.cpp:93
+ Here is the call graph for this function:

◆ createContiguous() [1/4]

uint8_t SdFile::createContiguous ( SdFile dirFile,
const char *  fileName,
uint32_t  size 
)

Create and open a new contiguous file of a specified size.

Note
This function only supports short DOS 8.3 names. See open() for more information.
Parameters
[in]dirFileThe directory where the file will be created.
[in]fileNameA valid DOS 8.3 file name.
[in]sizeThe desired file size.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include fileName contains an invalid DOS 8.3 file name, the FAT volume has not been initialized, a file is already open, the file already exists, the root directory is full or an I/O error.

Definition at line 131 of file SdFile.cpp.

References O_CREAT, O_EXCL, O_RDWR, open(), and sync().

Referenced by clearUnbufferedRead(), and createContiguous().

132  {
133  // don't allow zero length file
134  if (size == 0) return false;
135  if (!open(dirFile, fileName, O_CREAT | O_EXCL | O_RDWR)) return false;
136 
137  // calculate number of clusters needed
138  uint32_t count = ((size - 1) >> (vol_->clusterSizeShift_ + 9)) + 1;
139 
140  // allocate clusters
141  if (!vol_->allocContiguous(count, &firstCluster_)) {
142  remove();
143  return false;
144  }
145  fileSize_ = size;
146 
147  // insure sync() will update dir entry
148  flags_ |= F_FILE_DIR_DIRTY;
149  return sync();
150 }
uint8_t const O_EXCL
Definition: SdFat.h:71
uint8_t const O_CREAT
Definition: SdFat.h:69
uint8_t sync(void)
Definition: SdFile.cpp:965
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
uint8_t const O_RDWR
Definition: SdFat.h:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createContiguous() [2/4]

uint8_t SdFile::createContiguous ( SdFile dirFile,
const char *  fileName,
uint32_t  size 
)

◆ createContiguous() [3/4]

uint8_t SdFile::createContiguous ( SdFile dirFile,
const char *  fileName,
uint32_t  size 
)
Deprecated:
Use: uint8_t SdFile::createContiguous(SdFile* dirFile, const char* fileName, uint32_t size)

Definition at line 308 of file SdFat.h.

References createContiguous().

309  {
310  return createContiguous(&dirFile, fileName, size);
311  }
uint8_t createContiguous(SdFile *dirFile, const char *fileName, uint32_t size)
Definition: SdFile.cpp:131
+ Here is the call graph for this function:

◆ createContiguous() [4/4]

uint8_t SdFile::createContiguous ( SdFile dirFile,
const char *  fileName,
uint32_t  size 
)
Deprecated:
Use: uint8_t SdFile::createContiguous(SdFile* dirFile, const char* fileName, uint32_t size)

Definition at line 308 of file SdFat.h.

References createContiguous().

309  {
310  return createContiguous(&dirFile, fileName, size);
311  }
uint8_t createContiguous(SdFile *dirFile, const char *fileName, uint32_t size)
Definition: SdFile.cpp:131
+ Here is the call graph for this function:

◆ curCluster() [1/2]

uint32_t SdFile::curCluster ( void  ) const
Returns
The current cluster number for a file or directory.

Definition at line 159 of file SdFat.h.

159 {return curCluster_;}

◆ curCluster() [2/2]

uint32_t SdFile::curCluster ( void  ) const
Returns
The current cluster number for a file or directory.

Definition at line 159 of file SdFat.h.

Referenced by SdVolume::init().

159 {return curCluster_;}
+ Here is the caller graph for this function:

◆ curPosition() [1/2]

uint32_t SdFile::curPosition ( void  ) const
Returns
The current position for a file or directory.

Definition at line 161 of file SdFat.h.

161 {return curPosition_;}

◆ curPosition() [2/2]

uint32_t SdFile::curPosition ( void  ) const
Returns
The current position for a file or directory.

Definition at line 161 of file SdFat.h.

Referenced by SDLib::File::position().

161 {return curPosition_;}
+ Here is the caller graph for this function:

◆ dateTimeCallback() [1/4]

static void SdFile::dateTimeCallback ( void(*)(uint16_t *date, uint16_t *time)  dateTime)
static

Set the date/time callback function

Parameters
[in]dateTimeThe user's call back function. The callback function is of the form:
void dateTime(uint16_t* date, uint16_t* time) {
uint16_t year;
uint8_t month, day, hour, minute, second;
// User gets date and time from GPS or real-time clock here
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(year, month, day);
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(hour, minute, second);
}

Sets the function that is called when a file is created or when a file's directory entry is modified by sync(). All timestamps, access, creation, and modify, are set when a file is created. sync() maintains the last access date and last modify date/time.

See the timestamp() function.

Definition at line 190 of file SdFat.h.

191  {
192  dateTime_ = dateTime;
193  }

◆ dateTimeCallback() [2/4]

static void SdFile::dateTimeCallback ( void(*)(uint16_t *date, uint16_t *time)  dateTime)
static

Set the date/time callback function

Parameters
[in]dateTimeThe user's call back function. The callback function is of the form:
void dateTime(uint16_t* date, uint16_t* time) {
uint16_t year;
uint8_t month, day, hour, minute, second;
// User gets date and time from GPS or real-time clock here
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(year, month, day);
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(hour, minute, second);
}

Sets the function that is called when a file is created or when a file's directory entry is modified by sync(). All timestamps, access, creation, and modify, are set when a file is created. sync() maintains the last access date and last modify date/time.

See the timestamp() function.

Definition at line 190 of file SdFat.h.

191  {
192  dateTime_ = dateTime;
193  }

◆ dateTimeCallback() [3/4]

static void SdFile::dateTimeCallback ( void(*)(uint16_t &date, uint16_t &time)  dateTime)
static
Deprecated:
Use: static void SdFile::dateTimeCallback( void (dateTime)(uint16_t date, uint16_t* time));

Definition at line 318 of file SdFat.h.

319  { // NOLINT
320  oldDateTime_ = dateTime;
321  dateTime_ = dateTime ? oldToNew : 0;
322  }

◆ dateTimeCallback() [4/4]

static void SdFile::dateTimeCallback ( void(*)(uint16_t &date, uint16_t &time)  dateTime)
static
Deprecated:
Use: static void SdFile::dateTimeCallback( void (dateTime)(uint16_t date, uint16_t* time));

Definition at line 318 of file SdFat.h.

319  { // NOLINT
320  oldDateTime_ = dateTime;
321  dateTime_ = dateTime ? oldToNew : 0;
322  }

◆ dateTimeCallbackCancel() [1/2]

static void SdFile::dateTimeCallbackCancel ( void  )
static

Cancel the date/time callback function.

Definition at line 197 of file SdFat.h.

197  {
198  // use explicit zero since NULL is not defined for Sanguino
199  dateTime_ = 0;
200  }

◆ dateTimeCallbackCancel() [2/2]

static void SdFile::dateTimeCallbackCancel ( void  )
static

Cancel the date/time callback function.

Definition at line 197 of file SdFat.h.

197  {
198  // use explicit zero since NULL is not defined for Sanguino
199  dateTime_ = 0;
200  }

◆ dirBlock() [1/2]

uint32_t SdFile::dirBlock ( void  ) const
Returns
Address of the block that contains this file's directory.

Definition at line 202 of file SdFat.h.

References dirEntry().

202 {return dirBlock_;}
+ Here is the call graph for this function:

◆ dirBlock() [2/2]

uint32_t SdFile::dirBlock ( void  ) const
Returns
Address of the block that contains this file's directory.

Definition at line 202 of file SdFat.h.

References dirEntry().

202 {return dirBlock_;}
+ Here is the call graph for this function:

◆ dirEntry() [1/4]

uint8_t SdFile::dirEntry ( dir_t dir)

◆ dirEntry() [2/4]

uint8_t SdFile::dirEntry ( dir_t dir)

Return a files directory entry

Parameters
[out]dirLocation for return of the files directory entry.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 160 of file SdFile.cpp.

References sync().

Referenced by dirBlock().

160  {
161  // make sure fields on SD are correct
162  if (!sync()) return false;
163 
164  // read entry
165  dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
166  if (!p) return false;
167 
168  // copy to caller's struct
169  memcpy(dir, p, sizeof(dir_t));
170  return true;
171 }
uint8_t sync(void)
Definition: SdFile.cpp:965
FAT short directory entry.
Definition: FatStructs.h:321
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dirEntry() [3/4]

uint8_t SdFile::dirEntry ( dir_t dir)
Deprecated:
Use: uint8_t SdFile::dirEntry(dir_t* dir);

Definition at line 324 of file SdFat.h.

References dirEntry().

Referenced by dirEntry().

324 {return dirEntry(&dir);} // NOLINT
uint8_t dirEntry(dir_t *dir)
Definition: SdFile.cpp:160
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dirEntry() [4/4]

uint8_t SdFile::dirEntry ( dir_t dir)
Deprecated:
Use: uint8_t SdFile::dirEntry(dir_t* dir);

Definition at line 324 of file SdFat.h.

References dirEntry().

Referenced by dirEntry().

324 {return dirEntry(&dir);} // NOLINT
uint8_t dirEntry(dir_t *dir)
Definition: SdFile.cpp:160
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dirIndex() [1/2]

uint8_t SdFile::dirIndex ( void  ) const
Returns
Index of this file's directory in the block dirBlock.

Definition at line 205 of file SdFat.h.

References dirName(), and name.

205 {return dirIndex_;}
+ Here is the call graph for this function:

◆ dirIndex() [2/2]

uint8_t SdFile::dirIndex ( void  ) const
Returns
Index of this file's directory in the block dirBlock.

Definition at line 205 of file SdFat.h.

References dirName(), and name.

Referenced by open().

205 {return dirIndex_;}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dirName() [1/2]

static void SdFile::dirName ( const dir_t dir,
char *  name 
)
static

◆ dirName() [2/2]

void SdFile::dirName ( const dir_t dir,
char *  name 
)
static

Format the name field of dir into the 13 byte array name in standard 8.3 short name format.

Parameters
[in]dirThe directory structure containing the name.
[out]nameA 13 byte char array for the formatted name.

Definition at line 180 of file SdFile.cpp.

References directoryEntry::name.

Referenced by dirIndex().

180  {
181  uint8_t j = 0;
182  for (uint8_t i = 0; i < 11; i++) {
183  if (dir.name[i] == ' ')continue;
184  if (i == 8) name[j++] = '.';
185  name[j++] = dir.name[i];
186  }
187  name[j] = 0;
188 }
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t name[11]
Definition: FatStructs.h:292
+ Here is the caller graph for this function:

◆ fileSize() [1/2]

uint32_t SdFile::fileSize ( void  ) const
Returns
The total number of bytes in a file or directory.

Definition at line 208 of file SdFat.h.

208 {return fileSize_;}

◆ fileSize() [2/2]

uint32_t SdFile::fileSize ( void  ) const
Returns
The total number of bytes in a file or directory.

Definition at line 208 of file SdFat.h.

Referenced by SDLib::SDClass::end(), SDLib::SDClass::open(), and SDLib::File::size().

208 {return fileSize_;}
+ Here is the caller graph for this function:

◆ firstCluster() [1/2]

uint32_t SdFile::firstCluster ( void  ) const
Returns
The first cluster number for a file or directory.

Definition at line 210 of file SdFat.h.

210 {return firstCluster_;}

◆ firstCluster() [2/2]

uint32_t SdFile::firstCluster ( void  ) const
Returns
The first cluster number for a file or directory.

Definition at line 210 of file SdFat.h.

210 {return firstCluster_;}

◆ isDir() [1/2]

uint8_t SdFile::isDir ( void  ) const
Returns
True if this is a SdFile for a directory else false.

Definition at line 212 of file SdFat.h.

References FAT_FILE_TYPE_MIN_DIR.

212 {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
uint8_t const FAT_FILE_TYPE_MIN_DIR
Definition: SdFat.h:94

◆ isDir() [2/2]

uint8_t SdFile::isDir ( void  ) const
Returns
True if this is a SdFile for a directory else false.

Definition at line 212 of file SdFat.h.

References FAT_FILE_TYPE_MIN_DIR.

Referenced by SDLib::File::isDirectory(), readDir(), and sync().

212 {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
uint8_t const FAT_FILE_TYPE_MIN_DIR
Definition: SdFat.h:94
+ Here is the caller graph for this function:

◆ isFile() [1/2]

uint8_t SdFile::isFile ( void  ) const
Returns
True if this is a SdFile for a file else false.

Definition at line 214 of file SdFat.h.

References FAT_FILE_TYPE_NORMAL.

Referenced by setUnbufferedRead(), truncate(), and write().

214 {return type_ == FAT_FILE_TYPE_NORMAL;}
uint8_t const FAT_FILE_TYPE_NORMAL
Definition: SdFat.h:86
+ Here is the caller graph for this function:

◆ isFile() [2/2]

uint8_t SdFile::isFile ( void  ) const
Returns
True if this is a SdFile for a file else false.

Definition at line 214 of file SdFat.h.

References FAT_FILE_TYPE_NORMAL.

214 {return type_ == FAT_FILE_TYPE_NORMAL;}
uint8_t const FAT_FILE_TYPE_NORMAL
Definition: SdFat.h:86

◆ isOpen() [1/2]

uint8_t SdFile::isOpen ( void  ) const
Returns
True if this is a SdFile for an open file/directory else false.

Definition at line 216 of file SdFat.h.

References FAT_FILE_TYPE_CLOSED.

Referenced by SDLib::SDClass::end(), SDLib::SDClass::open(), open(), openRoot(), SDLib::File::operator bool(), read(), seekSet(), sync(), and timestamp().

216 {return type_ != FAT_FILE_TYPE_CLOSED;}
uint8_t const FAT_FILE_TYPE_CLOSED
Definition: SdFat.h:84
+ Here is the caller graph for this function:

◆ isOpen() [2/2]

uint8_t SdFile::isOpen ( void  ) const
Returns
True if this is a SdFile for an open file/directory else false.

Definition at line 216 of file SdFat.h.

References FAT_FILE_TYPE_CLOSED.

216 {return type_ != FAT_FILE_TYPE_CLOSED;}
uint8_t const FAT_FILE_TYPE_CLOSED
Definition: SdFat.h:84

◆ isRoot() [1/2]

uint8_t SdFile::isRoot ( void  ) const
Returns
True if this is a SdFile for the root directory.

Definition at line 220 of file SdFat.h.

References FAT_FILE_TYPE_ROOT16, FAT_FILE_TYPE_ROOT32, ls(), makeDir(), open(), openRoot(), printDirName(), printFatDate(), printFatTime(), and printTwoDigits().

Referenced by SDLib::SDClass::open().

220  {
221  return type_ == FAT_FILE_TYPE_ROOT16 || type_ == FAT_FILE_TYPE_ROOT32;
222  }
uint8_t const FAT_FILE_TYPE_ROOT32
Definition: SdFat.h:90
uint8_t const FAT_FILE_TYPE_ROOT16
Definition: SdFat.h:88
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isRoot() [2/2]

uint8_t SdFile::isRoot ( void  ) const
Returns
True if this is a SdFile for the root directory.

Definition at line 220 of file SdFat.h.

References FAT_FILE_TYPE_ROOT16, FAT_FILE_TYPE_ROOT32, ls(), makeDir(), open(), openRoot(), printDirName(), printFatDate(), printFatTime(), and printTwoDigits().

220  {
221  return type_ == FAT_FILE_TYPE_ROOT16 || type_ == FAT_FILE_TYPE_ROOT32;
222  }
uint8_t const FAT_FILE_TYPE_ROOT32
Definition: SdFat.h:90
uint8_t const FAT_FILE_TYPE_ROOT16
Definition: SdFat.h:88
+ Here is the call graph for this function:

◆ isSubDir() [1/2]

uint8_t SdFile::isSubDir ( void  ) const
Returns
True if this is a SdFile for a subdirectory else false.

Definition at line 218 of file SdFat.h.

References FAT_FILE_TYPE_SUBDIR.

Referenced by rmDir().

218 {return type_ == FAT_FILE_TYPE_SUBDIR;}
uint8_t const FAT_FILE_TYPE_SUBDIR
Definition: SdFat.h:92
+ Here is the caller graph for this function:

◆ isSubDir() [2/2]

uint8_t SdFile::isSubDir ( void  ) const
Returns
True if this is a SdFile for a subdirectory else false.

Definition at line 218 of file SdFat.h.

References FAT_FILE_TYPE_SUBDIR.

218 {return type_ == FAT_FILE_TYPE_SUBDIR;}
uint8_t const FAT_FILE_TYPE_SUBDIR
Definition: SdFat.h:92

◆ ls() [1/2]

void SdFile::ls ( uint8_t  flags = 0,
uint8_t  indent = 0 
)

List directory contents to Serial.

Parameters
[in]flagsThe inclusive OR of

LS_DATE - Print file modification date

LS_SIZE - Print file size.

LS_R - Recursive list of subdirectories.

Parameters
[in]indentAmount of space before file name. Used for recursive list to indicate subdirectory level.

Definition at line 203 of file SdFile.cpp.

References DIR_NAME_DELETED, DIR_NAME_FREE, directoryEntry::name, and rewind().

Referenced by isRoot().

203  {
204  dir_t* p;
205 
206  rewind();
207  while ((p = readDirCache())) {
208  // done if past last used entry
209  if (p->name[0] == DIR_NAME_FREE) break;
210 
211  // skip deleted entry and entries for . and ..
212  if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
213 
214  // only list subdirectories and files
215  if (!DIR_IS_FILE_OR_SUBDIR(p)) continue;
216 
217  // print any indent spaces
218  for (int8_t i = 0; i < indent; i++) Serial.print(' ');
219 
220  // print file name with possible blank fill
221  printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0);
222 
223  // print modify date/time if requested
224  if (flags & LS_DATE) {
226  Serial.print(' ');
228  }
229  // print size if requested
230  if (!DIR_IS_SUBDIR(p) && (flags & LS_SIZE)) {
231  Serial.print(' ');
232  Serial.print(p->fileSize);
233  }
234  Serial.println();
235 
236  // list subdirectory content if requested
237  if ((flags & LS_R) && DIR_IS_SUBDIR(p)) {
238  uint16_t index = curPosition()/32 - 1;
239  SdFile s;
240  if (s.open(this, index, O_READ)) s.ls(flags, indent + 2);
241  seekSet(32 * (index + 1));
242  }
243  }
244 }
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
Access FAT16 and FAT32 files on SD and SDHC cards.
Definition: SdFat.h:137
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint16_t lastWriteDate
Definition: FatStructs.h:364
uint8_t const LS_DATE
Definition: SdFat.h:45
uint8_t name[11]
Definition: FatStructs.h:327
uint32_t fileSize
Definition: FatStructs.h:368
static void printFatTime(uint16_t fatTime)
Definition: SdFile.cpp:634
uint8_t const LS_R
Definition: SdFat.h:49
static void printFatDate(uint16_t fatDate)
Definition: SdFile.cpp:620
void rewind(void)
Definition: SdFat.h:248
uint16_t lastWriteTime
Definition: FatStructs.h:362
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
FAT short directory entry.
Definition: FatStructs.h:321
uint8_t const LS_SIZE
Definition: SdFat.h:47
void ls(uint8_t flags=0, uint8_t indent=0)
Definition: SdFile.cpp:203
uint8_t const O_READ
Definition: SdFat.h:53
uint32_t curPosition(void) const
Definition: SdFat.h:161
static void printDirName(const dir_t &dir, uint8_t width)
Definition: SdFile.cpp:593
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ls() [2/2]

void SdFile::ls ( uint8_t  flags = 0,
uint8_t  indent = 0 
)

◆ makeDir() [1/4]

uint8_t SdFile::makeDir ( SdFile dir,
const char *  dirName 
)

◆ makeDir() [2/4]

uint8_t SdFile::makeDir ( SdFile dir,
const char *  dirName 
)

Make a new directory.

Parameters
[in]dirAn open SdFat instance for the directory that will containing the new directory.
[in]dirNameA valid 8.3 DOS name for the new directory.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include this SdFile is already open, dir is not a directory, dirName is invalid or already exists in dir.

Definition at line 292 of file SdFile.cpp.

References directoryEntry::attributes, DIR_ATT_DIRECTORY, FAT_FILE_TYPE_SUBDIR, directoryEntry::firstClusterHigh, directoryEntry::firstClusterLow, directoryEntry::name, O_CREAT, O_EXCL, O_RDWR, O_READ, open(), and sync().

Referenced by SDLib::callback_makeDirPath(), isRoot(), and makeDir().

292  {
293  dir_t d;
294 
295  // create a normal file
296  if (!open(dir, dirName, O_CREAT | O_EXCL | O_RDWR)) return false;
297 
298  // convert SdFile to directory
299  flags_ = O_READ;
300  type_ = FAT_FILE_TYPE_SUBDIR;
301 
302  // allocate and zero first cluster
303  if (!addDirCluster())return false;
304 
305  // force entry to SD
306  if (!sync()) return false;
307 
308  // cache entry - should already be in cache due to sync() call
309  dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
310  if (!p) return false;
311 
312  // change directory entry attribute
314 
315  // make entry for '.'
316  memcpy(&d, p, sizeof(d));
317  for (uint8_t i = 1; i < 11; i++) d.name[i] = ' ';
318  d.name[0] = '.';
319 
320  // cache block for '.' and '..'
321  uint32_t block = vol_->clusterStartBlock(firstCluster_);
322  if (!SdVolume::cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) return false;
323 
324  // copy '.' to block
325  memcpy(&SdVolume::cacheBuffer_.dir[0], &d, sizeof(d));
326 
327  // make entry for '..'
328  d.name[1] = '.';
329  if (dir->isRoot()) {
330  d.firstClusterLow = 0;
331  d.firstClusterHigh = 0;
332  } else {
333  d.firstClusterLow = dir->firstCluster_ & 0XFFFF;
334  d.firstClusterHigh = dir->firstCluster_ >> 16;
335  }
336  // copy '..' to block
337  memcpy(&SdVolume::cacheBuffer_.dir[1], &d, sizeof(d));
338 
339  // set position after '..'
340  curPosition_ = 2 * sizeof(d);
341 
342  // write first block
343  return SdVolume::cacheFlush();
344 }
uint8_t attributes
Definition: FatStructs.h:334
uint8_t const O_EXCL
Definition: SdFat.h:71
uint8_t const O_CREAT
Definition: SdFat.h:69
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t const DIR_ATT_DIRECTORY
Definition: FatStructs.h:390
uint8_t sync(void)
Definition: SdFile.cpp:965
static void dirName(const dir_t &dir, char *name)
Definition: SdFile.cpp:180
uint8_t const FAT_FILE_TYPE_SUBDIR
Definition: SdFat.h:92
uint16_t firstClusterLow
Definition: FatStructs.h:366
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
uint16_t firstClusterHigh
Definition: FatStructs.h:360
FAT short directory entry.
Definition: FatStructs.h:321
uint8_t const O_RDWR
Definition: SdFat.h:61
uint8_t const O_READ
Definition: SdFat.h:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ makeDir() [3/4]

uint8_t SdFile::makeDir ( SdFile dir,
const char *  dirName 
)
Deprecated:
Use: uint8_t SdFile::makeDir(SdFile* dir, const char* dirName);

Definition at line 328 of file SdFat.h.

References makeDir().

328  { // NOLINT
329  return makeDir(&dir, dirName);
330  }
uint8_t makeDir(SdFile *dir, const char *dirName)
Definition: SdFile.cpp:292
static void dirName(const dir_t &dir, char *name)
Definition: SdFile.cpp:180
+ Here is the call graph for this function:

◆ makeDir() [4/4]

uint8_t SdFile::makeDir ( SdFile dir,
const char *  dirName 
)
Deprecated:
Use: uint8_t SdFile::makeDir(SdFile* dir, const char* dirName);

Definition at line 328 of file SdFat.h.

References makeDir().

328  { // NOLINT
329  return makeDir(&dir, dirName);
330  }
uint8_t makeDir(SdFile *dir, const char *dirName)
Definition: SdFile.cpp:292
static void dirName(const dir_t &dir, char *name)
Definition: SdFile.cpp:180
+ Here is the call graph for this function:

◆ open() [1/10]

uint8_t SdFile::open ( SdFile dirFile,
uint16_t  index,
uint8_t  oflag 
)

◆ open() [2/10]

uint8_t SdFile::open ( SdFile dirFile,
uint16_t  index,
uint8_t  oflag 
)

Open a file by index.

Parameters
[in]dirFileAn open SdFat instance for the directory.
[in]indexThe index of the directory entry for the file to be opened. The value for index is (directory file position)/32.
[in]oflagValues for oflag are constructed by a bitwise-inclusive OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.

See open() by fileName for definition of flags and return values.

Definition at line 484 of file SdFile.cpp.

References directoryEntry::attributes, cache_t::dir, DIR_ATT_DIRECTORY, DIR_ATT_READ_ONLY, DIR_NAME_DELETED, DIR_NAME_FREE, dirIndex(), directoryEntry::firstClusterHigh, directoryEntry::firstClusterLow, isOpen(), directoryEntry::name, O_CREAT, O_EXCL, O_TRUNC, O_WRITE, and seekSet().

Referenced by SDLib::SDClass::begin(), SDLib::callback_pathExists(), SDLib::callback_rmdir(), createContiguous(), SDLib::SDClass::end(), isRoot(), makeDir(), SDLib::SDClass::open(), open(), and remove().

484  {
485  // error if already open
486  if (isOpen())return false;
487 
488  // don't open existing file if O_CREAT and O_EXCL - user call error
489  if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) return false;
490 
491  vol_ = dirFile->vol_;
492 
493  // seek to location of entry
494  if (!dirFile->seekSet(32 * index)) return false;
495 
496  // read entry into cache
497  dir_t* p = dirFile->readDirCache();
498  if (p == NULL) return false;
499 
500  // error if empty slot or '.' or '..'
501  if (p->name[0] == DIR_NAME_FREE ||
502  p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
503  return false;
504  }
505  // open cached entry
506  return openCachedEntry(index & 0XF, oflag);
507 }
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint8_t const O_EXCL
Definition: SdFat.h:71
uint8_t const O_CREAT
Definition: SdFat.h:69
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t isOpen(void) const
Definition: SdFat.h:216
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
FAT short directory entry.
Definition: FatStructs.h:321
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ open() [3/10]

uint8_t SdFile::open ( SdFile dirFile,
const char *  fileName,
uint8_t  oflag 
)

◆ open() [4/10]

uint8_t SdFile::open ( SdFile dirFile,
const char *  fileName,
uint8_t  oflag 
)

Open a file or directory by name.

Parameters
[in]dirFileAn open SdFat instance for the directory containing the file to be opened.
[in]fileNameA valid 8.3 DOS name for a file to be opened.
[in]oflagValues for oflag are constructed by a bitwise-inclusive OR of flags from the following list

O_READ - Open for reading.

O_RDONLY - Same as O_READ.

O_WRITE - Open for writing.

O_WRONLY - Same as O_WRITE.

O_RDWR - Open for reading and writing.

O_APPEND - If set, the file offset shall be set to the end of the file prior to each write.

O_CREAT - If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file shall be created

O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.

O_SYNC - Call sync() after each write. This flag should not be used with write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class. These functions do character at a time writes so sync() will be called after each byte.

O_TRUNC - If the file exists and is a regular file, and the file is successfully opened and is not read only, its length shall be truncated to 0.

Note
Directory files must be opened read only. Write and truncation is not allowed for directory files.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include this SdFile is already open, difFile is not a directory, fileName is invalid, the file does not exist or can't be opened in the access mode specified by oflag.

Definition at line 392 of file SdFile.cpp.

References directoryEntry::creationDate, directoryEntry::creationTime, cache_t::dir, DIR_NAME_DELETED, DIR_NAME_FREE, FAT_DEFAULT_DATE, FAT_DEFAULT_TIME, FAT_FILE_TYPE_ROOT16, isOpen(), directoryEntry::lastAccessDate, directoryEntry::lastWriteDate, directoryEntry::lastWriteTime, directoryEntry::name, O_CREAT, O_EXCL, O_WRITE, and rewind().

392  {
393  uint8_t dname[11];
394  dir_t* p;
395 
396  // error if already open
397  if (isOpen())return false;
398 
399  if (!make83Name(fileName, dname)) return false;
400  vol_ = dirFile->vol_;
401  dirFile->rewind();
402 
403  // bool for empty entry found
404  uint8_t emptyFound = false;
405 
406  // search for file
407  while (dirFile->curPosition_ < dirFile->fileSize_) {
408  uint8_t index = 0XF & (dirFile->curPosition_ >> 5);
409  p = dirFile->readDirCache();
410  if (p == NULL) return false;
411 
412  if (p->name[0] == DIR_NAME_FREE || p->name[0] == DIR_NAME_DELETED) {
413  // remember first empty slot
414  if (!emptyFound) {
415  emptyFound = true;
416  dirIndex_ = index;
417  dirBlock_ = SdVolume::cacheBlockNumber_;
418  }
419  // done if no entries follow
420  if (p->name[0] == DIR_NAME_FREE) break;
421  } else if (!memcmp(dname, p->name, 11)) {
422  // don't open existing file if O_CREAT and O_EXCL
423  if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) return false;
424 
425  // open found file
426  return openCachedEntry(0XF & index, oflag);
427  }
428  }
429  // only create file if O_CREAT and O_WRITE
430  if ((oflag & (O_CREAT | O_WRITE)) != (O_CREAT | O_WRITE)) return false;
431 
432  // cache found slot or add cluster if end of file
433  if (emptyFound) {
434  p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
435  if (!p) return false;
436  } else {
437  if (dirFile->type_ == FAT_FILE_TYPE_ROOT16) return false;
438 
439  // add and zero cluster for dirFile - first cluster is in cache for write
440  if (!dirFile->addDirCluster()) return false;
441 
442  // use first entry in cluster
443  dirIndex_ = 0;
444  p = SdVolume::cacheBuffer_.dir;
445  }
446  // initialize as empty file
447  memset(p, 0, sizeof(dir_t));
448  memcpy(p->name, dname, 11);
449 
450  // set timestamps
451  if (dateTime_) {
452  // call user function
453  dateTime_(&p->creationDate, &p->creationTime);
454  } else {
455  // use default date/time
458  }
460  p->lastWriteDate = p->creationDate;
461  p->lastWriteTime = p->creationTime;
462 
463  // force write of entry to SD
464  if (!SdVolume::cacheFlush()) return false;
465 
466  // open entry in cache
467  return openCachedEntry(dirIndex_, oflag);
468 }
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint16_t lastWriteDate
Definition: FatStructs.h:364
uint16_t creationDate
Definition: FatStructs.h:349
uint16_t const FAT_DEFAULT_DATE
Definition: SdFat.h:129
uint8_t const O_EXCL
Definition: SdFat.h:71
uint8_t const O_CREAT
Definition: SdFat.h:69
uint16_t lastAccessDate
Definition: FatStructs.h:355
uint16_t creationTime
Definition: FatStructs.h:347
uint8_t name[11]
Definition: FatStructs.h:327
dir_t dir[16]
Definition: SdFat.h:420
uint8_t isOpen(void) const
Definition: SdFat.h:216
void rewind(void)
Definition: SdFat.h:248
uint16_t lastWriteTime
Definition: FatStructs.h:362
FAT short directory entry.
Definition: FatStructs.h:321
uint8_t const O_WRITE
Definition: SdFat.h:57
uint8_t const FAT_FILE_TYPE_ROOT16
Definition: SdFat.h:88
uint16_t const FAT_DEFAULT_TIME
Definition: SdFat.h:131
+ Here is the call graph for this function:

◆ open() [5/10]

uint8_t SdFile::open ( SdFile dirFile,
const char *  fileName,
uint8_t  oflag 
)
Deprecated:
Use: uint8_t SdFile::open(SdFile* dirFile, const char* fileName, uint8_t oflag);

Definition at line 334 of file SdFat.h.

References open().

335  {
336  return open(&dirFile, fileName, oflag);
337  }
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
+ Here is the call graph for this function:

◆ open() [6/10]

uint8_t SdFile::open ( SdFile dirFile,
const char *  fileName,
uint8_t  oflag 
)
Deprecated:
Use: uint8_t SdFile::open(SdFile* dirFile, const char* fileName, uint8_t oflag);

Definition at line 334 of file SdFat.h.

References open().

335  {
336  return open(&dirFile, fileName, oflag);
337  }
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
+ Here is the call graph for this function:

◆ open() [7/10]

uint8_t SdFile::open ( SdFile dirFile,
const char *  fileName 
)
Deprecated:
Do not use in new apps

Definition at line 339 of file SdFat.h.

References O_RDWR, and open().

339  { // NOLINT
340  return open(dirFile, fileName, O_RDWR);
341  }
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
uint8_t const O_RDWR
Definition: SdFat.h:61
+ Here is the call graph for this function:

◆ open() [8/10]

uint8_t SdFile::open ( SdFile dirFile,
const char *  fileName 
)
Deprecated:
Do not use in new apps

Definition at line 339 of file SdFat.h.

References O_RDWR, and open().

339  { // NOLINT
340  return open(dirFile, fileName, O_RDWR);
341  }
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
uint8_t const O_RDWR
Definition: SdFat.h:61
+ Here is the call graph for this function:

◆ open() [9/10]

uint8_t SdFile::open ( SdFile dirFile,
uint16_t  index,
uint8_t  oflag 
)
Deprecated:
Use: uint8_t SdFile::open(SdFile* dirFile, uint16_t index, uint8_t oflag);

Definition at line 345 of file SdFat.h.

References open().

345  { // NOLINT
346  return open(&dirFile, index, oflag);
347  }
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
+ Here is the call graph for this function:

◆ open() [10/10]

uint8_t SdFile::open ( SdFile dirFile,
uint16_t  index,
uint8_t  oflag 
)
Deprecated:
Use: uint8_t SdFile::open(SdFile* dirFile, uint16_t index, uint8_t oflag);

Definition at line 345 of file SdFat.h.

References open().

345  { // NOLINT
346  return open(&dirFile, index, oflag);
347  }
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
+ Here is the call graph for this function:

◆ openRoot() [1/4]

uint8_t SdFile::openRoot ( SdVolume vol)

◆ openRoot() [2/4]

uint8_t SdFile::openRoot ( SdVolume vol)

Open a volume's root directory.

Parameters
[in]volThe FAT volume containing the root directory to be opened.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the FAT volume has not been initialized or it a FAT12 volume.

Definition at line 558 of file SdFile.cpp.

References FAT_FILE_TYPE_ROOT16, FAT_FILE_TYPE_ROOT32, SdVolume::fatType(), isOpen(), O_READ, SdVolume::rootDirEntryCount(), and SdVolume::rootDirStart().

Referenced by SDLib::SDClass::begin(), SDLib::SDClass::end(), and isRoot().

558  {
559  // error if file is already open
560  if (isOpen()) return false;
561 
562  if (vol->fatType() == 16) {
563  type_ = FAT_FILE_TYPE_ROOT16;
564  firstCluster_ = 0;
565  fileSize_ = 32 * vol->rootDirEntryCount();
566  } else if (vol->fatType() == 32) {
567  type_ = FAT_FILE_TYPE_ROOT32;
568  firstCluster_ = vol->rootDirStart();
569  if (!vol->chainSize(firstCluster_, &fileSize_)) return false;
570  } else {
571  // volume is not initialized or FAT12
572  return false;
573  }
574  vol_ = vol;
575  // read only
576  flags_ = O_READ;
577 
578  // set to start of file
579  curCluster_ = 0;
580  curPosition_ = 0;
581 
582  // root has no directory entry
583  dirBlock_ = 0;
584  dirIndex_ = 0;
585  return true;
586 }
uint32_t rootDirStart(void) const
Definition: SdFat.h:478
uint8_t isOpen(void) const
Definition: SdFat.h:216
uint32_t rootDirEntryCount(void) const
Definition: SdFat.h:475
uint8_t const FAT_FILE_TYPE_ROOT32
Definition: SdFat.h:90
uint8_t const FAT_FILE_TYPE_ROOT16
Definition: SdFat.h:88
uint8_t const O_READ
Definition: SdFat.h:53
uint8_t fatType(void) const
Definition: SdFat.h:473
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ openRoot() [3/4]

uint8_t SdFile::openRoot ( SdVolume vol)
Deprecated:
Use: uint8_t SdFile::openRoot(SdVolume* vol);

Definition at line 349 of file SdFat.h.

References openRoot().

Referenced by openRoot().

349 {return openRoot(&vol);} // NOLINT
uint8_t openRoot(SdVolume *vol)
Definition: SdFile.cpp:558
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ openRoot() [4/4]

uint8_t SdFile::openRoot ( SdVolume vol)
Deprecated:
Use: uint8_t SdFile::openRoot(SdVolume* vol);

Definition at line 349 of file SdFat.h.

References openRoot().

Referenced by openRoot().

349 {return openRoot(&vol);} // NOLINT
uint8_t openRoot(SdVolume *vol)
Definition: SdFile.cpp:558
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ printDirName() [1/2]

static void SdFile::printDirName ( const dir_t dir,
uint8_t  width 
)
static

◆ printDirName() [2/2]

void SdFile::printDirName ( const dir_t dir,
uint8_t  width 
)
static

Print the name field of a directory entry in 8.3 format to Serial.

Parameters
[in]dirThe directory structure containing the name.
[in]widthBlank fill name if length is less than width.

Definition at line 593 of file SdFile.cpp.

References directoryEntry::name.

Referenced by isRoot().

593  {
594  uint8_t w = 0;
595  for (uint8_t i = 0; i < 11; i++) {
596  if (dir.name[i] == ' ')continue;
597  if (i == 8) {
598  Serial.print('.');
599  w++;
600  }
601  Serial.write(dir.name[i]);
602  w++;
603  }
604  if (DIR_IS_SUBDIR(&dir)) {
605  Serial.print('/');
606  w++;
607  }
608  while (w < width) {
609  Serial.print(' ');
610  w++;
611  }
612 }
uint8_t name[11]
Definition: FatStructs.h:327
+ Here is the caller graph for this function:

◆ printFatDate() [1/2]

static void SdFile::printFatDate ( uint16_t  fatDate)
static

◆ printFatDate() [2/2]

void SdFile::printFatDate ( uint16_t  fatDate)
static

Print a directory date field to Serial.

Format is yyyy-mm-dd.

Parameters
[in]fatDateThe date field from a directory entry.

Definition at line 620 of file SdFile.cpp.

Referenced by isRoot().

620  {
621  Serial.print(FAT_YEAR(fatDate));
622  Serial.print('-');
623  printTwoDigits(FAT_MONTH(fatDate));
624  Serial.print('-');
625  printTwoDigits(FAT_DAY(fatDate));
626 }
static void printTwoDigits(uint8_t v)
Definition: SdFile.cpp:646
+ Here is the caller graph for this function:

◆ printFatTime() [1/2]

static void SdFile::printFatTime ( uint16_t  fatTime)
static

◆ printFatTime() [2/2]

void SdFile::printFatTime ( uint16_t  fatTime)
static

Print a directory time field to Serial.

Format is hh:mm:ss.

Parameters
[in]fatTimeThe time field from a directory entry.

Definition at line 634 of file SdFile.cpp.

References printTwoDigits().

Referenced by isRoot().

634  {
635  printTwoDigits(FAT_HOUR(fatTime));
636  Serial.print(':');
637  printTwoDigits(FAT_MINUTE(fatTime));
638  Serial.print(':');
639  printTwoDigits(FAT_SECOND(fatTime));
640 }
static void printTwoDigits(uint8_t v)
Definition: SdFile.cpp:646
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ printTwoDigits() [1/2]

void SdFile::printTwoDigits ( uint8_t  v)
static

Print a value as two digits to Serial.

Parameters
[in]vValue to be printed, 0 <= v <= 99

Definition at line 646 of file SdFile.cpp.

Referenced by isRoot(), and printFatTime().

646  {
647  char str[3];
648  str[0] = '0' + v/10;
649  str[1] = '0' + v % 10;
650  str[2] = 0;
651  Serial.print(str);
652 }
+ Here is the caller graph for this function:

◆ printTwoDigits() [2/2]

static void SdFile::printTwoDigits ( uint8_t  v)
static

◆ read() [1/4]

int16_t SdFile::read ( void  )

Read the next byte from a file.

Returns
For success read returns the next byte in the file as an int. If an error occurs or end of file is reached -1 is returned.

Definition at line 239 of file SdFat.h.

References requests.packages.urllib3.packages.six::b(), read(), and readDir().

Referenced by requests.packages.urllib3.response.HTTPResponse::data().

239  {
240  uint8_t b;
241  return read(&b, 1) == 1 ? b : -1;
242  }
int16_t read(void)
Definition: SdFat.h:239
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read() [2/4]

int16_t SdFile::read ( void  )

Read the next byte from a file.

Returns
For success read returns the next byte in the file as an int. If an error occurs or end of file is reached -1 is returned.

Definition at line 239 of file SdFat.h.

References requests.packages.urllib3.packages.six::b(), and readDir().

Referenced by requests.packages.urllib3.response.HTTPResponse::data(), SDLib::File::peek(), SDLib::File::read(), read(), and readDir().

239  {
240  uint8_t b;
241  return read(&b, 1) == 1 ? b : -1;
242  }
int16_t read(void)
Definition: SdFat.h:239
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read() [3/4]

int16_t SdFile::read ( void *  buf,
uint16_t  nbyte 
)

Referenced by requests.packages.urllib3.response.HTTPResponse::data().

+ Here is the caller graph for this function:

◆ read() [4/4]

int16_t SdFile::read ( void *  buf,
uint16_t  nbyte 
)

Read data from a file starting at the current position.

Parameters
[out]bufPointer to the location that will receive the data.
[in]nbyteMaximum number of bytes to read.
Returns
For success read() returns the number of bytes read. A value less than nbyte, including zero, will be returned if end of file is reached. If an error occurs, read() returns -1. Possible errors include read() called before a file has been opened, corrupt file system or an I/O error occurred.

Definition at line 668 of file SdFile.cpp.

References cache_t::data, FAT_FILE_TYPE_ROOT16, isOpen(), O_READ, SdVolume::rootDirStart(), and unbufferedRead().

Referenced by requests.packages.urllib3.response.HTTPResponse::data().

668  {
669  uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
670 
671  // error if not open or write only
672  if (!isOpen() || !(flags_ & O_READ)) return -1;
673 
674  // max bytes left in file
675  if (nbyte > (fileSize_ - curPosition_)) nbyte = fileSize_ - curPosition_;
676 
677  // amount left to read
678  uint16_t toRead = nbyte;
679  while (toRead > 0) {
680  uint32_t block; // raw device block number
681  uint16_t offset = curPosition_ & 0X1FF; // offset in block
682  if (type_ == FAT_FILE_TYPE_ROOT16) {
683  block = vol_->rootDirStart() + (curPosition_ >> 9);
684  } else {
685  uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
686  if (offset == 0 && blockOfCluster == 0) {
687  // start of new cluster
688  if (curPosition_ == 0) {
689  // use first cluster in file
690  curCluster_ = firstCluster_;
691  } else {
692  // get next cluster from FAT
693  if (!vol_->fatGet(curCluster_, &curCluster_)) return -1;
694  }
695  }
696  block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
697  }
698  uint16_t n = toRead;
699 
700  // amount to be read from current block
701  if (n > (512 - offset)) n = 512 - offset;
702 
703  // no buffering needed if n == 512 or user requests no buffering
704  if ((unbufferedRead() || n == 512) &&
705  block != SdVolume::cacheBlockNumber_) {
706  if (!vol_->readData(block, offset, n, dst)) return -1;
707  dst += n;
708  } else {
709  // read block to cache and copy data to caller
710  if (!SdVolume::cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) return -1;
711  uint8_t* src = SdVolume::cacheBuffer_.data + offset;
712  uint8_t* end = src + n;
713  while (src != end) *dst++ = *src++;
714  }
715  curPosition_ += n;
716  toRead -= n;
717  }
718  return nbyte;
719 }
uint32_t rootDirStart(void) const
Definition: SdFat.h:478
uint8_t isOpen(void) const
Definition: SdFat.h:216
uint8_t unbufferedRead(void) const
Definition: SdFat.h:283
uint8_t data[512]
Definition: SdFat.h:414
uint8_t const FAT_FILE_TYPE_ROOT16
Definition: SdFat.h:88
uint8_t const O_READ
Definition: SdFat.h:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readDir() [1/4]

int8_t SdFile::readDir ( dir_t dir)

◆ readDir() [2/4]

int8_t SdFile::readDir ( dir_t dir)

Read the next directory entry from a directory file.

Parameters
[out]dirThe dir_t struct that will receive the data.
Returns
For success readDir() returns the number of bytes read. A value of zero will be returned if end of file is reached. If an error occurs, readDir() returns -1. Possible errors include readDir() called before a directory has been opened, this is not a directory file or an I/O error occurred.

Definition at line 732 of file SdFile.cpp.

References DIR_NAME_DELETED, DIR_NAME_FREE, isDir(), directoryEntry::name, and read().

Referenced by read().

732  {
733  int8_t n;
734  // if not a directory file or miss-positioned return an error
735  if (!isDir() || (0X1F & curPosition_)) return -1;
736 
737  while ((n = read(dir, sizeof(dir_t))) == sizeof(dir_t)) {
738  // last entry if DIR_NAME_FREE
739  if (dir->name[0] == DIR_NAME_FREE) break;
740  // skip empty entries and entry for . and ..
741  if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') continue;
742  // return if normal file or subdirectory
743  if (DIR_IS_FILE_OR_SUBDIR(dir)) return n;
744  }
745  // error, end of file, or past last entry
746  return n < 0 ? -1 : 0;
747 }
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t isDir(void) const
Definition: SdFat.h:212
FAT short directory entry.
Definition: FatStructs.h:321
int16_t read(void)
Definition: SdFat.h:239
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readDir() [3/4]

int8_t SdFile::readDir ( dir_t dir)
Deprecated:
Use: int8_t SdFile::readDir(dir_t* dir);

Definition at line 352 of file SdFat.h.

References readDir().

Referenced by readDir().

352 {return readDir(&dir);} // NOLINT
int8_t readDir(dir_t *dir)
Definition: SdFile.cpp:732
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readDir() [4/4]

int8_t SdFile::readDir ( dir_t dir)
Deprecated:
Use: int8_t SdFile::readDir(dir_t* dir);

Definition at line 352 of file SdFat.h.

References readDir().

Referenced by readDir().

352 {return readDir(&dir);} // NOLINT
int8_t readDir(dir_t *dir)
Definition: SdFile.cpp:732
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove() [1/6]

uint8_t SdFile::remove ( SdFile dirFile,
const char *  fileName 
)
static

Remove a file.

The directory entry and all data for the file are deleted.

Parameters
[in]dirFileThe directory that contains the file.
[in]fileNameThe name of the file to be removed.
Note
This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file is a directory, is read only, dirFile is not a directory, fileName is not found or an I/O error occurred.

Definition at line 818 of file SdFile.cpp.

References O_WRITE, open(), and remove().

Referenced by remove().

818  {
819  SdFile file;
820  if (!file.open(dirFile, fileName, O_WRITE)) return false;
821  return file.remove();
822 }
Access FAT16 and FAT32 files on SD and SDHC cards.
Definition: SdFat.h:137
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
static uint8_t remove(SdFile *dirFile, const char *fileName)
Definition: SdFile.cpp:818
uint8_t const O_WRITE
Definition: SdFat.h:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove() [2/6]

static uint8_t SdFile::remove ( SdFile dirFile,
const char *  fileName 
)
static

◆ remove() [3/6]

uint8_t SdFile::remove ( void  )

Remove a file.

The directory entry and all data for the file are deleted.

Note
This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file read-only, is a directory, or an I/O error occurred.

Definition at line 782 of file SdFile.cpp.

References DIR_NAME_DELETED, FAT_FILE_TYPE_CLOSED, directoryEntry::name, and truncate().

Referenced by SDLib::callback_remove().

782  {
783  // free any clusters - will fail if read-only or directory
784  if (!truncate(0)) return false;
785 
786  // cache directory entry
787  dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
788  if (!d) return false;
789 
790  // mark entry deleted
791  d->name[0] = DIR_NAME_DELETED;
792 
793  // set this SdFile closed
794  type_ = FAT_FILE_TYPE_CLOSED;
795 
796  // write entry to SD
797  return SdVolume::cacheFlush();
798 }
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
uint8_t truncate(uint32_t size)
Definition: SdFile.cpp:1073
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t const FAT_FILE_TYPE_CLOSED
Definition: SdFat.h:84
FAT short directory entry.
Definition: FatStructs.h:321
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove() [4/6]

uint8_t SdFile::remove ( void  )

◆ remove() [5/6]

static uint8_t SdFile::remove ( SdFile dirFile,
const char *  fileName 
)
static
Deprecated:
Use: static uint8_t SdFile::remove(SdFile* dirFile, const char* fileName);

Definition at line 356 of file SdFat.h.

References O_ACCMODE, O_APPEND, and O_SYNC.

356  { // NOLINT
357  return remove(&dirFile, fileName);
358  }

◆ remove() [6/6]

static uint8_t SdFile::remove ( SdFile dirFile,
const char *  fileName 
)
static
Deprecated:
Use: static uint8_t SdFile::remove(SdFile* dirFile, const char* fileName);

Definition at line 356 of file SdFat.h.

References O_ACCMODE, O_APPEND, and O_SYNC.

356  { // NOLINT
357  return remove(&dirFile, fileName);
358  }

◆ rewind() [1/2]

void SdFile::rewind ( void  )

Set the file's current position to zero.

Definition at line 248 of file SdFat.h.

References rmDir(), and rmRfStar().

Referenced by ls(), open(), rmDir(), and rmRfStar().

248  {
249  curPosition_ = curCluster_ = 0;
250  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rewind() [2/2]

void SdFile::rewind ( void  )

Set the file's current position to zero.

Definition at line 248 of file SdFat.h.

References rmDir(), and rmRfStar().

248  {
249  curPosition_ = curCluster_ = 0;
250  }
+ Here is the call graph for this function:

◆ rmDir() [1/2]

uint8_t SdFile::rmDir ( void  )

Remove a directory file.

The directory file will be removed only if it is empty and is not the root directory. rmDir() follows DOS and Windows and ignores the read-only attribute for the directory.

Note
This function should not be used to delete the 8.3 version of a directory that has a long name. For example if a directory has the long name "New folder" you should not delete the 8.3 name "NEWFOL~1".
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file is not a directory, is the root directory, is not empty, or an I/O error occurred.

Definition at line 839 of file SdFile.cpp.

References DIR_NAME_DELETED, DIR_NAME_FREE, isSubDir(), directoryEntry::name, and rewind().

Referenced by SDLib::callback_rmdir(), and rewind().

839  {
840  // must be open subdirectory
841  if (!isSubDir()) return false;
842 
843  rewind();
844 
845  // make sure directory is empty
846  while (curPosition_ < fileSize_) {
847  dir_t* p = readDirCache();
848  if (p == NULL) return false;
849  // done if past last used entry
850  if (p->name[0] == DIR_NAME_FREE) break;
851  // skip empty slot or '.' or '..'
852  if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
853  // error not empty
854  if (DIR_IS_FILE_OR_SUBDIR(p)) return false;
855  }
856  // convert empty directory to normal file for remove
857  type_ = FAT_FILE_TYPE_NORMAL;
858  flags_ |= O_WRITE;
859  return remove();
860 }
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint8_t name[11]
Definition: FatStructs.h:327
void rewind(void)
Definition: SdFat.h:248
FAT short directory entry.
Definition: FatStructs.h:321
uint8_t const O_WRITE
Definition: SdFat.h:57
uint8_t const FAT_FILE_TYPE_NORMAL
Definition: SdFat.h:86
uint8_t isSubDir(void) const
Definition: SdFat.h:218
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rmDir() [2/2]

uint8_t SdFile::rmDir ( void  )

◆ rmRfStar() [1/2]

uint8_t SdFile::rmRfStar ( void  )

Recursively delete a directory and all contained files.

This is like the Unix/Linux 'rm -rf *' if called with the root directory hence the name.

Warning - This will remove all contents of the directory including subdirectories. The directory will then be removed if it is not root. The read-only attribute for files will be ignored.

Note
This function should not be used to delete the 8.3 version of a directory that has a long name. See remove() and rmDir().
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 877 of file SdFile.cpp.

References DIR_NAME_DELETED, DIR_NAME_FREE, directoryEntry::name, and rewind().

Referenced by rewind().

877  {
878  rewind();
879  while (curPosition_ < fileSize_) {
880  SdFile f;
881 
882  // remember position
883  uint16_t index = curPosition_/32;
884 
885  dir_t* p = readDirCache();
886  if (!p) return false;
887 
888  // done if past last entry
889  if (p->name[0] == DIR_NAME_FREE) break;
890 
891  // skip empty slot or '.' or '..'
892  if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
893 
894  // skip if part of long file name or volume label in root
895  if (!DIR_IS_FILE_OR_SUBDIR(p)) continue;
896 
897  if (!f.open(this, index, O_READ)) return false;
898  if (f.isSubDir()) {
899  // recursively delete
900  if (!f.rmRfStar()) return false;
901  } else {
902  // ignore read-only
903  f.flags_ |= O_WRITE;
904  if (!f.remove()) return false;
905  }
906  // position to next entry if required
907  if (curPosition_ != (32u*(index + 1))) {
908  if (!seekSet(32u*(index + 1))) return false;
909  }
910  }
911  // don't try to delete root
912  if (isRoot()) return true;
913  return rmDir();
914 }
uint8_t const DIR_NAME_DELETED
Definition: FatStructs.h:378
Access FAT16 and FAT32 files on SD and SDHC cards.
Definition: SdFat.h:137
uint8_t const DIR_NAME_FREE
Definition: FatStructs.h:380
uint8_t isRoot(void) const
Definition: SdFat.h:220
uint8_t name[11]
Definition: FatStructs.h:327
uint8_t rmDir(void)
Definition: SdFile.cpp:839
void rewind(void)
Definition: SdFat.h:248
uint8_t open(SdFile *dirFile, uint16_t index, uint8_t oflag)
Definition: SdFile.cpp:484
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
static uint8_t remove(SdFile *dirFile, const char *fileName)
Definition: SdFile.cpp:818
FAT short directory entry.
Definition: FatStructs.h:321
uint8_t const O_WRITE
Definition: SdFat.h:57
uint8_t const O_READ
Definition: SdFat.h:53
uint8_t rmRfStar(void)
Definition: SdFile.cpp:877
uint8_t isSubDir(void) const
Definition: SdFat.h:218
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rmRfStar() [2/2]

uint8_t SdFile::rmRfStar ( void  )

◆ seekCur() [1/2]

uint8_t SdFile::seekCur ( uint32_t  pos)

Set the files position to current position + pos. See seekSet().

Definition at line 254 of file SdFat.h.

References seekSet().

Referenced by SDLib::File::peek().

254  {
255  return seekSet(curPosition_ + pos);
256  }
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ seekCur() [2/2]

uint8_t SdFile::seekCur ( uint32_t  pos)

Set the files position to current position + pos. See seekSet().

Definition at line 254 of file SdFat.h.

References seekSet().

254  {
255  return seekSet(curPosition_ + pos);
256  }
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
+ Here is the call graph for this function:

◆ seekEnd() [1/2]

uint8_t SdFile::seekEnd ( void  )

Set the files current position to end of file. Useful to position a file for append. See seekSet().

Definition at line 261 of file SdFat.h.

References seekSet().

Referenced by write().

261 {return seekSet(fileSize_);}
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ seekEnd() [2/2]

uint8_t SdFile::seekEnd ( void  )

Set the files current position to end of file. Useful to position a file for append. See seekSet().

Definition at line 261 of file SdFat.h.

References seekSet().

261 {return seekSet(fileSize_);}
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
+ Here is the call graph for this function:

◆ seekSet() [1/2]

uint8_t SdFile::seekSet ( uint32_t  pos)

◆ seekSet() [2/2]

uint8_t SdFile::seekSet ( uint32_t  pos)

Sets a file's position.

Parameters
[in]posThe new position in bytes from the beginning of the file.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 924 of file SdFile.cpp.

References FAT_FILE_TYPE_ROOT16, and isOpen().

Referenced by SDLib::SDClass::end(), SDLib::SDClass::open(), open(), SDLib::File::seek(), seekCur(), seekEnd(), and truncate().

924  {
925  // error if file not open or seek past end of file
926  if (!isOpen() || pos > fileSize_) return false;
927 
928  if (type_ == FAT_FILE_TYPE_ROOT16) {
929  curPosition_ = pos;
930  return true;
931  }
932  if (pos == 0) {
933  // set position to start of file
934  curCluster_ = 0;
935  curPosition_ = 0;
936  return true;
937  }
938  // calculate cluster index for cur and new position
939  uint32_t nCur = (curPosition_ - 1) >> (vol_->clusterSizeShift_ + 9);
940  uint32_t nNew = (pos - 1) >> (vol_->clusterSizeShift_ + 9);
941 
942  if (nNew < nCur || curPosition_ == 0) {
943  // must follow chain from first cluster
944  curCluster_ = firstCluster_;
945  } else {
946  // advance from curPosition
947  nNew -= nCur;
948  }
949  while (nNew--) {
950  if (!vol_->fatGet(curCluster_, &curCluster_)) return false;
951  }
952  curPosition_ = pos;
953  return true;
954 }
uint8_t isOpen(void) const
Definition: SdFat.h:216
uint8_t const FAT_FILE_TYPE_ROOT16
Definition: SdFat.h:88
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setUnbufferedRead() [1/2]

void SdFile::setUnbufferedRead ( void  )

Use unbuffered reads to access this file. Used with Wave Shield ISR. Used with Sd2Card::partialBlockRead() in WaveRP.

Not recommended for normal applications.

Definition at line 269 of file SdFat.h.

References isFile(), sync(), and timestamp().

269  {
270  if (isFile()) flags_ |= F_FILE_UNBUFFERED_READ;
271  }
uint8_t isFile(void) const
Definition: SdFat.h:214
+ Here is the call graph for this function:

◆ setUnbufferedRead() [2/2]

void SdFile::setUnbufferedRead ( void  )

Use unbuffered reads to access this file. Used with Wave Shield ISR. Used with Sd2Card::partialBlockRead() in WaveRP.

Not recommended for normal applications.

Definition at line 269 of file SdFat.h.

References isFile(), sync(), and timestamp().

269  {
270  if (isFile()) flags_ |= F_FILE_UNBUFFERED_READ;
271  }
uint8_t isFile(void) const
Definition: SdFat.h:214
+ Here is the call graph for this function:

◆ sync() [1/2]

uint8_t SdFile::sync ( void  )

◆ sync() [2/2]

uint8_t SdFile::sync ( void  )

The sync() call causes all modified data and directory fields to be written to the storage device.

Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include a call to sync() before a file has been opened or an I/O error.

Definition at line 965 of file SdFile.cpp.

References directoryEntry::fileSize, directoryEntry::firstClusterHigh, directoryEntry::firstClusterLow, isDir(), isOpen(), directoryEntry::lastAccessDate, directoryEntry::lastWriteDate, and directoryEntry::lastWriteTime.

Referenced by close(), createContiguous(), dirEntry(), SDLib::File::flush(), makeDir(), setUnbufferedRead(), truncate(), and write().

965  {
966  // only allow open files and directories
967  if (!isOpen()) return false;
968 
969  if (flags_ & F_FILE_DIR_DIRTY) {
970  dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
971  if (!d) return false;
972 
973  // do not set filesize for dir files
974  if (!isDir()) d->fileSize = fileSize_;
975 
976  // update first cluster fields
977  d->firstClusterLow = firstCluster_ & 0XFFFF;
978  d->firstClusterHigh = firstCluster_ >> 16;
979 
980  // set modify time if user supplied a callback date/time function
981  if (dateTime_) {
982  dateTime_(&d->lastWriteDate, &d->lastWriteTime);
984  }
985  // clear directory dirty
986  flags_ &= ~F_FILE_DIR_DIRTY;
987  }
988  return SdVolume::cacheFlush();
989 }
uint16_t lastWriteDate
Definition: FatStructs.h:364
uint16_t lastAccessDate
Definition: FatStructs.h:355
uint32_t fileSize
Definition: FatStructs.h:368
uint8_t isOpen(void) const
Definition: SdFat.h:216
uint16_t firstClusterLow
Definition: FatStructs.h:366
uint16_t lastWriteTime
Definition: FatStructs.h:362
uint8_t isDir(void) const
Definition: SdFat.h:212
uint16_t firstClusterHigh
Definition: FatStructs.h:360
FAT short directory entry.
Definition: FatStructs.h:321
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ timestamp() [1/2]

uint8_t SdFile::timestamp ( uint8_t  flag,
uint16_t  year,
uint8_t  month,
uint8_t  day,
uint8_t  hour,
uint8_t  minute,
uint8_t  second 
)

◆ timestamp() [2/2]

uint8_t SdFile::timestamp ( uint8_t  flags,
uint16_t  year,
uint8_t  month,
uint8_t  day,
uint8_t  hour,
uint8_t  minute,
uint8_t  second 
)

Set a file's timestamps in its directory entry.

Parameters
[in]flagsValues for flags are constructed by a bitwise-inclusive OR of flags from the following list

T_ACCESS - Set the file's last access date.

T_CREATE - Set the file's creation date and time.

T_WRITE - Set the file's last write/modification date and time.

Parameters
[in]yearValid range 1980 - 2107 inclusive.
[in]monthValid range 1 - 12 inclusive.
[in]dayValid range 1 - 31 inclusive.
[in]hourValid range 0 - 23 inclusive.
[in]minuteValid range 0 - 59 inclusive.
[in]secondValid range 0 - 59 inclusive
Note
It is possible to set an invalid date since there is no check for the number of days in a month.
Modify and access timestamps may be overwritten if a date time callback function has been set by dateTimeCallback().
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 1025 of file SdFile.cpp.

References isOpen().

Referenced by setUnbufferedRead().

1026  {
1027  if (!isOpen()
1028  || year < 1980
1029  || year > 2107
1030  || month < 1
1031  || month > 12
1032  || day < 1
1033  || day > 31
1034  || hour > 23
1035  || minute > 59
1036  || second > 59) {
1037  return false;
1038  }
1039  dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
1040  if (!d) return false;
1041 
1042  uint16_t dirDate = FAT_DATE(year, month, day);
1043  uint16_t dirTime = FAT_TIME(hour, minute, second);
1044  if (flags & T_ACCESS) {
1045  d->lastAccessDate = dirDate;
1046  }
1047  if (flags & T_CREATE) {
1048  d->creationDate = dirDate;
1049  d->creationTime = dirTime;
1050  // seems to be units of 1/100 second not 1/10 as Microsoft states
1051  d->creationTimeTenths = second & 1 ? 100 : 0;
1052  }
1053  if (flags & T_WRITE) {
1054  d->lastWriteDate = dirDate;
1055  d->lastWriteTime = dirTime;
1056  }
1057  SdVolume::cacheSetDirty();
1058  return sync();
1059 }
uint16_t lastWriteDate
Definition: FatStructs.h:364
uint16_t creationDate
Definition: FatStructs.h:349
uint16_t lastAccessDate
Definition: FatStructs.h:355
uint16_t creationTime
Definition: FatStructs.h:347
uint8_t isOpen(void) const
Definition: SdFat.h:216
uint8_t sync(void)
Definition: SdFile.cpp:965
uint8_t const T_ACCESS
Definition: SdFat.h:77
uint8_t creationTimeTenths
Definition: FatStructs.h:345
uint16_t lastWriteTime
Definition: FatStructs.h:362
FAT short directory entry.
Definition: FatStructs.h:321
uint8_t const T_CREATE
Definition: SdFat.h:79
uint8_t const T_WRITE
Definition: SdFat.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ truncate() [1/2]

uint8_t SdFile::truncate ( uint32_t  size)

◆ truncate() [2/2]

uint8_t SdFile::truncate ( uint32_t  length)

Truncate a file to a specified length. The current file position will be maintained if it is less than or equal to length otherwise it will be set to end of file.

Parameters
[in]lengthThe desired length for the file.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include file is read only, file is a directory, length is greater than the current file size or an I/O error occurs.

Definition at line 1073 of file SdFile.cpp.

References isFile(), O_WRITE, seekSet(), and sync().

Referenced by remove(), and type().

1073  {
1074 // error if not a normal file or read-only
1075  if (!isFile() || !(flags_ & O_WRITE)) return false;
1076 
1077  // error if length is greater than current size
1078  if (length > fileSize_) return false;
1079 
1080  // fileSize and length are zero - nothing to do
1081  if (fileSize_ == 0) return true;
1082 
1083  // remember position for seek after truncation
1084  uint32_t newPos = curPosition_ > length ? length : curPosition_;
1085 
1086  // position to last cluster in truncated file
1087  if (!seekSet(length)) return false;
1088 
1089  if (length == 0) {
1090  // free all clusters
1091  if (!vol_->freeChain(firstCluster_)) return false;
1092  firstCluster_ = 0;
1093  } else {
1094  uint32_t toFree;
1095  if (!vol_->fatGet(curCluster_, &toFree)) return false;
1096 
1097  if (!vol_->isEOC(toFree)) {
1098  // free extra clusters
1099  if (!vol_->freeChain(toFree)) return false;
1100 
1101  // current cluster is end of chain
1102  if (!vol_->fatPutEOC(curCluster_)) return false;
1103  }
1104  }
1105  fileSize_ = length;
1106 
1107  // need to update directory entry
1108  flags_ |= F_FILE_DIR_DIRTY;
1109 
1110  if (!sync()) return false;
1111 
1112  // set file to correct position
1113  return seekSet(newPos);
1114 }
uint8_t isFile(void) const
Definition: SdFat.h:214
uint8_t sync(void)
Definition: SdFile.cpp:965
uint8_t seekSet(uint32_t pos)
Definition: SdFile.cpp:924
uint8_t const O_WRITE
Definition: SdFat.h:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ type() [1/2]

uint8_t SdFile::type ( void  ) const

Type of this SdFile. You should use isFile() or isDir() instead of type() if possible.

Returns
The file or directory type.

Definition at line 280 of file SdFat.h.

References truncate().

280 {return type_;}
+ Here is the call graph for this function:

◆ type() [2/2]

uint8_t SdFile::type ( void  ) const

Type of this SdFile. You should use isFile() or isDir() instead of type() if possible.

Returns
The file or directory type.

Definition at line 280 of file SdFat.h.

References truncate().

280 {return type_;}
+ Here is the call graph for this function:

◆ unbufferedRead() [1/2]

uint8_t SdFile::unbufferedRead ( void  ) const
Returns
Unbuffered read flag.

Definition at line 283 of file SdFat.h.

Referenced by read().

283  {
284  return flags_ & F_FILE_UNBUFFERED_READ;
285  }
+ Here is the caller graph for this function:

◆ unbufferedRead() [2/2]

uint8_t SdFile::unbufferedRead ( void  ) const
Returns
Unbuffered read flag.

Definition at line 283 of file SdFat.h.

283  {
284  return flags_ & F_FILE_UNBUFFERED_READ;
285  }

◆ volume() [1/2]

SdVolume* SdFile::volume ( void  ) const
Returns
SdVolume that contains this file.

Definition at line 287 of file SdFat.h.

References requests.packages.urllib3.packages.six::b(), requests.compat::str, and write().

287 {return vol_;}
+ Here is the call graph for this function:

◆ volume() [2/2]

SdVolume* SdFile::volume ( void  ) const
Returns
SdVolume that contains this file.

Definition at line 287 of file SdFat.h.

References requests.packages.urllib3.packages.six::b(), requests.compat::str, and write().

287 {return vol_;}
+ Here is the call graph for this function:

◆ write() [1/6]

size_t SdFile::write ( uint8_t  b)

◆ write() [2/6]

size_t SdFile::write ( uint8_t  b)

Write a byte to a file. Required by the Arduino Print class.

Use SdFile::writeError to check for errors.

Definition at line 1231 of file SdFile.cpp.

Referenced by volume(), SDLib::File::write(), and write().

1231  {
1232  return write(&b, 1);
1233 }
size_t write(uint8_t b)
Definition: SdFile.cpp:1231
+ Here is the caller graph for this function:

◆ write() [3/6]

size_t SdFile::write ( const void *  buf,
uint16_t  nbyte 
)

◆ write() [4/6]

size_t SdFile::write ( const void *  buf,
uint16_t  nbyte 
)

Write data to an open file.

Note
Data is moved to the cache but may not be written to the storage device until sync() is called.
Parameters
[in]bufPointer to the location of the data to be written.
[in]nbyteNumber of bytes to write.
Returns
For success write() returns the number of bytes written, always nbyte. If an error occurs, write() returns 0. Possible errors include write() is called before a file has been opened, write is called for a read-only file, device is full, a corrupt file system or an I/O error.

Definition at line 1132 of file SdFile.cpp.

References cache_t::data, isFile(), requests.packages.urllib3.packages.six::next, O_APPEND, O_SYNC, O_WRITE, seekEnd(), and sync().

1132  {
1133  // convert void* to uint8_t* - must be before goto statements
1134  const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
1135 
1136  // number of bytes left to write - must be before goto statements
1137  uint16_t nToWrite = nbyte;
1138 
1139  // error if not a normal file or is read-only
1140  if (!isFile() || !(flags_ & O_WRITE)) goto writeErrorReturn;
1141 
1142  // seek to end of file if append flag
1143  if ((flags_ & O_APPEND) && curPosition_ != fileSize_) {
1144  if (!seekEnd()) goto writeErrorReturn;
1145  }
1146 
1147  while (nToWrite > 0) {
1148  uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
1149  uint16_t blockOffset = curPosition_ & 0X1FF;
1150  if (blockOfCluster == 0 && blockOffset == 0) {
1151  // start of new cluster
1152  if (curCluster_ == 0) {
1153  if (firstCluster_ == 0) {
1154  // allocate first cluster of file
1155  if (!addCluster()) goto writeErrorReturn;
1156  } else {
1157  curCluster_ = firstCluster_;
1158  }
1159  } else {
1160  uint32_t next;
1161  if (!vol_->fatGet(curCluster_, &next)) return false;
1162  if (vol_->isEOC(next)) {
1163  // add cluster if at end of chain
1164  if (!addCluster()) goto writeErrorReturn;
1165  } else {
1166  curCluster_ = next;
1167  }
1168  }
1169  }
1170  // max space in block
1171  uint16_t n = 512 - blockOffset;
1172 
1173  // lesser of space and amount to write
1174  if (n > nToWrite) n = nToWrite;
1175 
1176  // block for data write
1177  uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
1178  if (n == 512) {
1179  // full block - don't need to use cache
1180  // invalidate cache if block is in cache
1181  if (SdVolume::cacheBlockNumber_ == block) {
1182  SdVolume::cacheBlockNumber_ = 0XFFFFFFFF;
1183  }
1184  if (!vol_->writeBlock(block, src)) goto writeErrorReturn;
1185  src += 512;
1186  } else {
1187  if (blockOffset == 0 && curPosition_ >= fileSize_) {
1188  // start of new block don't need to read into cache
1189  if (!SdVolume::cacheFlush()) goto writeErrorReturn;
1190  SdVolume::cacheBlockNumber_ = block;
1191  SdVolume::cacheSetDirty();
1192  } else {
1193  // rewrite part of block
1194  if (!SdVolume::cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) {
1195  goto writeErrorReturn;
1196  }
1197  }
1198  uint8_t* dst = SdVolume::cacheBuffer_.data + blockOffset;
1199  uint8_t* end = dst + n;
1200  while (dst != end) *dst++ = *src++;
1201  }
1202  nToWrite -= n;
1203  curPosition_ += n;
1204  }
1205  if (curPosition_ > fileSize_) {
1206  // update fileSize and insure sync will update dir entry
1207  fileSize_ = curPosition_;
1208  flags_ |= F_FILE_DIR_DIRTY;
1209  } else if (dateTime_ && nbyte) {
1210  // insure sync will update modified date and time
1211  flags_ |= F_FILE_DIR_DIRTY;
1212  }
1213 
1214  if (flags_ & O_SYNC) {
1215  if (!sync()) goto writeErrorReturn;
1216  }
1217  return nbyte;
1218 
1219  writeErrorReturn:
1220  // return for write error
1221  //writeError = true;
1222  setWriteError();
1223  return 0;
1224 }
uint8_t seekEnd(void)
Definition: SdFat.h:261
uint8_t isFile(void) const
Definition: SdFat.h:214
uint8_t sync(void)
Definition: SdFile.cpp:965
uint8_t const O_APPEND
Definition: SdFat.h:65
uint8_t data[512]
Definition: SdFat.h:414
uint8_t const O_SYNC
Definition: SdFat.h:67
uint8_t const O_WRITE
Definition: SdFat.h:57
+ Here is the call graph for this function:

◆ write() [5/6]

size_t SdFile::write ( const char *  str)

Write a string to a file. Used by the Arduino Print class.

Use SdFile::writeError to check for errors.

Definition at line 1240 of file SdFile.cpp.

References write().

1240  {
1241  return write(str, strlen(str));
1242 }
size_t write(uint8_t b)
Definition: SdFile.cpp:1231
+ Here is the call graph for this function:

◆ write() [6/6]

size_t SdFile::write ( const char *  str)

The documentation for this class was generated from the following files: