cwFile.h : Added new version of backup() which takes a complete file name rather than file path parts.

This commit is contained in:
kevin 2023-03-17 17:41:04 -04:00
parent a01f7e8185
commit dd7dcc654e
2 changed files with 40 additions and 7 deletions

View File

@ -463,16 +463,29 @@ cw::rc_t cw::file::copy(
} }
cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext, const char* dst_dir ) cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext, const char* dst0_dir )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
char* newName = nullptr; char* newName = nullptr;
char* newFn = nullptr; char* newFn = nullptr;
unsigned n = 0; unsigned n = 0;
char* srcFn = nullptr; char* srcFn = nullptr;
filesys::pathPart_t* pp = nullptr; filesys::pathPart_t* pp = nullptr;
const char* dst_dir = nullptr;
char* dst_base_dir = nullptr;
// form the name of the backup file // expand the destination path
if( dst0_dir != nullptr )
{
if((dst_base_dir = filesys::expandPath(dst0_dir)) == nullptr )
{
rc = cwLogError(kOpFailRC,"The backup dest directory '%s' could not be expanded.");
goto errLabel;
}
dst_dir = dst_base_dir;
}
// form the name of the backup file to backup
if((srcFn = filesys::makeFn(dir,name,ext,nullptr)) == nullptr ) if((srcFn = filesys::makeFn(dir,name,ext,nullptr)) == nullptr )
{ {
rc = cwLogError(kOpFailRC,"Backup source file name formation failed."); rc = cwLogError(kOpFailRC,"Backup source file name formation failed.");
@ -523,6 +536,7 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext, c
errLabel: errLabel:
mem::release(dst_base_dir);
mem::release(srcFn); mem::release(srcFn);
mem::release(newFn); mem::release(newFn);
mem::release(newName); mem::release(newName);
@ -532,6 +546,24 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext, c
} }
cw::rc_t cw::file::backup( const char* fname, const char* dst_dir )
{
rc_t rc = kOkRC;
filesys::pathPart_t* pp = nullptr;
if((pp = filesys::pathParts(fname)) == nullptr )
{
rc = cwLogError(kInvalidArgRC,"The parts (dir,name,ext) of the file name '%s' could not be parsed.");
goto errLabel;
}
rc = backup(pp->dirStr,pp->fnStr,pp->extStr,dst_dir);
errLabel:
mem::release(pp);
return rc;
}
char* cw::file::toBuf( handle_t h, unsigned* bufByteCntPtr ) char* cw::file::toBuf( handle_t h, unsigned* bufByteCntPtr )
{ return _fileToBuf(h,0,bufByteCntPtr); } { return _fileToBuf(h,0,bufByteCntPtr); }

View File

@ -113,6 +113,7 @@ namespace cw
// unused file name is found in the same directory as 'fn'. // unused file name is found in the same directory as 'fn'.
// If the file identified by 'fn' is not found then the function returns quietly. // If the file identified by 'fn' is not found then the function returns quietly.
rc_t backup( const char* dir, const char* name, const char* ext, const char* dst_dir=nullptr ); rc_t backup( const char* dir, const char* name, const char* ext, const char* dst_dir=nullptr );
rc_t backup( const char* fname, const char* dst_dir=nullptr );
// Allocate and fill a zero terminated string from a file. // Allocate and fill a zero terminated string from a file.
// Set *bufByteCntPtr to count of bytes read into the buffer.= // Set *bufByteCntPtr to count of bytes read into the buffer.=