pic32/libraries/SD/utility/SdFile.cpp
//------------------------------------------------------------------------------
/**
 * Truncate a file to a specified length.  The current file position
 * will be maintained if it is less than or equal to \a length otherwise
 * it will be set to end of file.
 *
 * \param[in] length The desired length for the file.
 *
 * \return 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,
 * \a length is greater than the current file size or an I/O error occurs.
 */
Truncate a file to a specified length.  The current file position
will be maintained if it is less than or equal to \a length otherwise
it will be set to end of file.

\param[in] length The desired length for the file.

\return 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,
\a length is greater than the current file size or an I/O error occurs.
Truncate a file to a specified length . The current file position will be maintained if it is less than or equal to \ a length otherwise it will be set to end of file . \ param [ in ] length The desired length for the file . \ return 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 \ a length is greater than the current file size or an I / O error occurs .
uint8_t SdFile::truncate(uint32_t length) {
  if (!isFile() || !(flags_ & O_WRITE)) return false;
  if (length > fileSize_) return false;
  if (fileSize_ == 0) return true;
  uint32_t newPos = curPosition_ > length ? length : curPosition_;
  if (!seekSet(length)) return false;
  if (length == 0) {
    if (!vol_->freeChain(firstCluster_)) return false;
    firstCluster_ = 0;
  } else {
    uint32_t toFree;
    if (!vol_->fatGet(curCluster_, &toFree)) return false;
    if (!vol_->isEOC(toFree)) {
      if (!vol_->freeChain(toFree)) return false;
      if (!vol_->fatPutEOC(curCluster_)) return false;
    }
  }
  fileSize_ = length;
  flags_ |= F_FILE_DIR_DIRTY;
  if (!sync()) return false;
  return seekSet(newPos);
}
uint8_t SdFile :: truncate ( uint32_t length ) { if ( ! isFile ( ) || ! ( flags_ & O_WRITE ) ) return false ; if ( length > fileSize_ ) return false ; if ( fileSize_ == 0 ) return true ; uint32_t newPos = curPosition_ > length ? length : curPosition_ ; if ( ! seekSet ( length ) ) return false ; if ( length == 0 ) { if ( ! vol_ -> freeChain ( firstCluster_ ) ) return false ; firstCluster_ = 0 ; } else { uint32_t toFree ; if ( ! vol_ -> fatGet ( curCluster_ , & toFree ) ) return false ; if ( ! vol_ -> isEOC ( toFree ) ) { if ( ! vol_ -> freeChain ( toFree ) ) return false ; if ( ! vol_ -> fatPutEOC ( curCluster_ ) ) return false ; } } fileSize_ = length ; flags_ |= F_FILE_DIR_DIRTY ; if ( ! sync ( ) ) return false ; return seekSet ( newPos ) ; }
Truncate a file to a specified length.
Truncate a file to a specified length .
// error if not a normal file or read-only // error if length is greater than current size // fileSize and length are zero - nothing to do // remember position for seek after truncation // position to last cluster in truncated file // free all clusters // free extra clusters // current cluster is end of chain // need to update directory entry // set file to correct position