pic32/libraries/SD/utility/SdFile.cpp
//------------------------------------------------------------------------------
/** Make a new directory.
 *
 * \param[in] dir An open SdFat instance for the directory that will containing
 * the new directory.
 *
 * \param[in] dirName A valid 8.3 DOS name for the new directory.
 *
 * \return 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, \a dir is not a
 * directory, \a dirName is invalid or already exists in \a dir.
 */
Make a new directory.
\param[in] dir An open SdFat instance for the directory that will containing
the new directory.

\param[in] dirName A valid 8.3 DOS name for the new directory.

\return 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, \a dir is not a
directory, \a dirName is invalid or already exists in \a dir.
Make a new directory . \ param [ in ] dir An open SdFat instance for the directory that will containing the new directory . \ param [ in ] dirName A valid 8 . 3 DOS name for the new directory . \ return 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 \ a dir is not a directory \ a dirName is invalid or already exists in \ a dir .
uint8_t SdFile::makeDir(SdFile* dir, const char* dirName) {
  dir_t d;
  if (!open(dir, dirName, O_CREAT | O_EXCL | O_RDWR)) return false;
  flags_ = O_READ;
  type_ = FAT_FILE_TYPE_SUBDIR;
  if (!addDirCluster())return false;
  if (!sync()) return false;
  dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
  if (!p) return false;
  p->attributes = DIR_ATT_DIRECTORY;
  memcpy(&d, p, sizeof(d));
  for (uint8_t i = 1; i < 11; i++) d.name[i] = ' ';
  d.name[0] = '.';
  uint32_t block = vol_->clusterStartBlock(firstCluster_);
  if (!SdVolume::cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) return false;
  memcpy(&SdVolume::cacheBuffer_.dir[0], &d, sizeof(d));
  d.name[1] = '.';
  if (dir->isRoot()) {
    d.firstClusterLow = 0;
    d.firstClusterHigh = 0;
  } else {
    d.firstClusterLow = dir->firstCluster_ & 0XFFFF;
    d.firstClusterHigh = dir->firstCluster_ >> 16;
  }
  memcpy(&SdVolume::cacheBuffer_.dir[1], &d, sizeof(d));
  curPosition_ = 2 * sizeof(d);
  return SdVolume::cacheFlush();
}
uint8_t SdFile :: makeDir ( SdFile * dir , const char * dirName ) { dir_t d ; if ( ! open ( dir , dirName , O_CREAT | O_EXCL | O_RDWR ) ) return false ; flags_ = O_READ ; type_ = FAT_FILE_TYPE_SUBDIR ; if ( ! addDirCluster ( ) ) return false ; if ( ! sync ( ) ) return false ; dir_t * p = cacheDirEntry ( SdVolume :: CACHE_FOR_WRITE ) ; if ( ! p ) return false ; p -> attributes = DIR_ATT_DIRECTORY ; memcpy ( & d , p , sizeof ( d ) ) ; for ( uint8_t i = 1 ; i < 11 ; i ++ ) d . name [ i ] = ' ' ; d . name [ 0 ] = ' ' ; uint32_t block = vol_ -> clusterStartBlock ( firstCluster_ ) ; if ( ! SdVolume :: cacheRawBlock ( block , SdVolume :: CACHE_FOR_WRITE ) ) return false ; memcpy ( & SdVolume :: cacheBuffer_ . dir [ 0 ] , & d , sizeof ( d ) ) ; d . name [ 1 ] = ' ' ; if ( dir -> isRoot ( ) ) { d . firstClusterLow = 0 ; d . firstClusterHigh = 0 ; } else { d . firstClusterLow = dir -> firstCluster_ & 0 XFFFF ; d . firstClusterHigh = dir -> firstCluster_ >> 16 ; } memcpy ( & SdVolume :: cacheBuffer_ . dir [ 1 ] , & d , sizeof ( d ) ) ; curPosition_ = 2 * sizeof ( d ) ; return SdVolume :: cacheFlush ( ) ; }
Make a new directory.
Make a new directory .
// create a normal file // convert SdFile to directory // allocate and zero first cluster // force entry to SD // cache entry - should already be in cache due to sync() call // change directory entry  attribute // make entry for '.' // cache block for '.'  and '..' // copy '.' to block // make entry for '..' // copy '..' to block // set position after '..' // write first block