mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-29 00:27:27 -04:00
Add RsFiles::requestFiles to de API to download whole colletions
Fix filetree creation from single file RsDirUtil::moveFile now works also if parent directories doesn't exists Backport std::filesystem::create_directories from C++17
This commit is contained in:
parent
d666e58403
commit
55aab6c447
8 changed files with 227 additions and 54 deletions
|
@ -265,36 +265,48 @@ bool RsDirUtil::fileExists(const std::string& filename)
|
|||
|
||||
bool RsDirUtil::moveFile(const std::string& source,const std::string& dest)
|
||||
{
|
||||
// Check that the destination directory exists. If not, create it.
|
||||
Dbg3() << __PRETTY_FUNCTION__<< " source: " << source
|
||||
<< " dest: " << dest << std::endl;
|
||||
|
||||
std::string dest_dir ;
|
||||
std::string dest_file ;
|
||||
|
||||
splitDirFromFile(dest,dest_dir,dest_file) ;
|
||||
splitDirFromFile(dest, dest_dir, dest_file);
|
||||
|
||||
std::cerr << "Moving file " << source << " to " << dest << std::endl;
|
||||
std::cerr << "Checking that directory " << dest_dir << " actually exists." << std::endl;
|
||||
if(!checkDirectory(dest_dir))
|
||||
{
|
||||
if(!std::filesystem::create_directories(dest_dir))
|
||||
{
|
||||
RsErr() << __PRETTY_FUNCTION__ << " failure creating directory: "
|
||||
<< dest_dir << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!checkCreateDirectory(dest_dir))
|
||||
return false ;
|
||||
// First try a rename
|
||||
if(renameFile(source,dest))
|
||||
{
|
||||
Dbg3() << __PRETTY_FUNCTION__ << " plain rename worked" << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
// First try a rename
|
||||
//
|
||||
/* If not, try to copy. The src and dest probably belong to different file
|
||||
* systems */
|
||||
if(!copyFile(source,dest))
|
||||
{
|
||||
RsErr() << __PRETTY_FUNCTION__ << " failure copying file" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(renameFile(source,dest))
|
||||
return true ;
|
||||
// delete the original
|
||||
if(!removeFile(source))
|
||||
{
|
||||
RsErr() << __PRETTY_FUNCTION__ << " failure deleting original file"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// If not, try to copy. The src and dest probably belong to different file systems
|
||||
|
||||
if(!copyFile(source,dest))
|
||||
return false ;
|
||||
|
||||
// copy was successful, let's delete the original
|
||||
|
||||
if(!removeFile(source))
|
||||
return false ;
|
||||
|
||||
return true ;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RsDirUtil::removeFile(const std::string& filename)
|
||||
|
@ -425,7 +437,7 @@ bool RsDirUtil::checkFile(const std::string& filename,uint64_t& file_size,bool d
|
|||
}
|
||||
|
||||
|
||||
bool RsDirUtil::checkDirectory(const std::string& dir)
|
||||
bool RsDirUtil::checkDirectory(const std::string& dir)
|
||||
{
|
||||
int val;
|
||||
mode_t st_mode;
|
||||
|
@ -461,11 +473,9 @@ bool RsDirUtil::checkDirectory(const std::string& dir)
|
|||
}
|
||||
|
||||
|
||||
bool RsDirUtil::checkCreateDirectory(const std::string& dir)
|
||||
bool RsDirUtil::checkCreateDirectory(const std::string& dir)
|
||||
{
|
||||
#ifdef RSDIR_DEBUG
|
||||
std::cerr << "RsDirUtil::checkCreateDirectory() dir: " << dir << std::endl;
|
||||
#endif
|
||||
Dbg3() << __PRETTY_FUNCTION__ << " " << dir << std::endl;
|
||||
|
||||
#ifdef WINDOWS_SYS
|
||||
std::wstring wdir;
|
||||
|
@ -516,6 +526,23 @@ bool RsDirUtil::checkCreateDirectory(const std::string& dir)
|
|||
return true;
|
||||
}
|
||||
|
||||
#if __cplusplus < 201703L
|
||||
bool std::filesystem::create_directories(const std::string& path)
|
||||
{
|
||||
for( std::string::size_type lastIndex = 0; lastIndex < std::string::npos;
|
||||
lastIndex = path.find('/', lastIndex) )
|
||||
{
|
||||
std::string&& curDir = path.substr(0, ++lastIndex);
|
||||
if(!RsDirUtil::checkCreateDirectory(curDir))
|
||||
{
|
||||
RsErr() << __PRETTY_FUNCTION__ << " failure creating: " << curDir
|
||||
<< " of: " << path << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif // __cplusplus < 201703L
|
||||
|
||||
std::string RsDirUtil::removeSymLinks(const std::string& path)
|
||||
{
|
||||
|
|
|
@ -84,7 +84,10 @@ int breakupDirList(const std::string& path, std::list<std::string> &subdirs
|
|||
bool splitDirFromFile(const std::string& full_path,std::string& dir, std::string& file);
|
||||
|
||||
bool copyFile(const std::string& source,const std::string& dest);
|
||||
bool moveFile(const std::string& source,const std::string& dest);
|
||||
|
||||
/** Move file. If destination directory doesn't exists create it. */
|
||||
bool moveFile(const std::string& source, const std::string& dest);
|
||||
|
||||
bool removeFile(const std::string& file);
|
||||
bool fileExists(const std::string& file);
|
||||
bool checkFile(const std::string& filename,uint64_t& file_size,bool disallow_empty_file = false);
|
||||
|
@ -141,8 +144,23 @@ bool getWideFileHash(std::wstring filepath, RsFileHash &hash, u
|
|||
FILE *rs_fopen(const char* filename, const char* mode);
|
||||
|
||||
std::string convertPathToUnix(std::string path);
|
||||
|
||||
/** Concatenate two path pieces putting '/' separator between them only if
|
||||
* needed */
|
||||
std::string makePath(const std::string &path1, const std::string &path2);
|
||||
|
||||
RS_SET_CONTEXT_DEBUG_LEVEL(1);
|
||||
}
|
||||
|
||||
|
||||
#if __cplusplus < 201703L
|
||||
namespace std
|
||||
{
|
||||
namespace filesystem
|
||||
{
|
||||
bool create_directories(const std::string& path);
|
||||
}
|
||||
}
|
||||
#endif // __cplusplus < 201703L
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue