mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
rename file in partials into real name before moving it; check for destination directory before moving file
This commit is contained in:
parent
acbcba8a64
commit
99e35b131c
@ -734,13 +734,39 @@ bool ftController::completeFile(const RsFileHash& hash)
|
||||
|
||||
fc->mState = ftFileControl::COMPLETED;
|
||||
|
||||
std::string dst_dir,src_dir,src_file,dst_file ;
|
||||
|
||||
RsDirUtil::splitDirFromFile(fc->mCurrentPath,src_dir,src_file) ;
|
||||
RsDirUtil::splitDirFromFile(fc->mDestination,dst_dir,dst_file) ;
|
||||
|
||||
// We use this intermediate file in case the destination directory is not available, so as to not keep the partial file name.
|
||||
|
||||
std::string intermediate_file_name = src_dir+'/'+dst_file ;
|
||||
|
||||
// I don't know how the size can be zero, but believe me, this happens,
|
||||
// and it causes an error on linux because then the file may not even exist.
|
||||
//
|
||||
if( fc->mSize > 0 && RsDirUtil::moveFile(fc->mCurrentPath,fc->mDestination) )
|
||||
fc->mCurrentPath = fc->mDestination;
|
||||
else
|
||||
if( fc->mSize == 0)
|
||||
fc->mState = ftFileControl::ERROR_COMPLETION;
|
||||
else
|
||||
{
|
||||
std::cerr << "CompleteFile(): renaming " << fc->mCurrentPath << " into " << fc->mDestination << std::endl;
|
||||
std::cerr << "CompleteFile(): 1 - renaming " << fc->mCurrentPath << " info " << intermediate_file_name << std::endl;
|
||||
|
||||
if(RsDirUtil::moveFile(fc->mCurrentPath,intermediate_file_name) )
|
||||
{
|
||||
fc->mCurrentPath = intermediate_file_name ;
|
||||
|
||||
std::cerr << "CompleteFile(): 2 - renaming/copying " << intermediate_file_name << " into " << fc->mDestination << std::endl;
|
||||
|
||||
if(RsDirUtil::moveFile(intermediate_file_name,fc->mDestination) )
|
||||
fc->mCurrentPath = fc->mDestination;
|
||||
else
|
||||
fc->mState = ftFileControl::ERROR_COMPLETION;
|
||||
}
|
||||
else
|
||||
fc->mState = ftFileControl::ERROR_COMPLETION;
|
||||
}
|
||||
|
||||
/* for extralist additions */
|
||||
path = fc->mDestination;
|
||||
|
@ -104,6 +104,23 @@ const char *RsDirUtil::scanf_string_for_uint(int bytes)
|
||||
return strgs[0] ;
|
||||
}
|
||||
|
||||
bool RsDirUtil::splitDirFromFile(const std::string& full_path,std::string& dir, std::string& file)
|
||||
{
|
||||
int i = full_path.rfind('/', full_path.size()-1);
|
||||
|
||||
if(i == full_path.size()-1) // '/' not found!
|
||||
{
|
||||
file = full_path ;
|
||||
dir = "." ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
dir.assign(full_path,0,i+1) ;
|
||||
file.assign(full_path,i+1,full_path.size()) ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
void RsDirUtil::removeTopDir(const std::string& dir, std::string& path)
|
||||
{
|
||||
path.clear();
|
||||
@ -245,6 +262,19 @@ 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.
|
||||
|
||||
std::string dest_dir ;
|
||||
std::string 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(!checkCreateDirectory(dest_dir))
|
||||
return false ;
|
||||
|
||||
// First try a rename
|
||||
//
|
||||
|
||||
@ -458,7 +488,7 @@ bool RsDirUtil::checkCreateDirectory(const std::string& dir)
|
||||
std::cerr << "check_create_directory() Fatal Error et oui--";
|
||||
std::cerr <<std::endl<< "\tcannot create:" <<dir<<std::endl;
|
||||
#endif
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef RSDIR_DEBUG
|
||||
@ -466,7 +496,7 @@ bool RsDirUtil::checkCreateDirectory(const std::string& dir)
|
||||
std::cerr <<std::endl<< "\tcreated:" <<dir<<std::endl;
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef RSDIR_DEBUG
|
||||
@ -480,7 +510,7 @@ bool RsDirUtil::checkCreateDirectory(const std::string& dir)
|
||||
closedir(direc) ;
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -83,6 +83,11 @@ const char *scanf_string_for_uint(int bytes) ;
|
||||
|
||||
int breakupDirList(const std::string& path, std::list<std::string> &subdirs);
|
||||
|
||||
// Splits the path into parent directory and file. File can be empty if full_path is a dir ending with '/'
|
||||
// if full_path does not contain a directory, then dir will be "." and file will be full_path.
|
||||
|
||||
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);
|
||||
bool removeFile(const std::string& file);
|
||||
|
Loading…
Reference in New Issue
Block a user