if (current_save_location.equals(new_save_location)) {
// null operation
return;
}
DiskManager dm = getDiskManager();
if ( dm == null || dm.getFiles() == null){
if ( !old_file.exists()){
// files not created yet
FileUtil.mkdirs(new_save_location.getParentFile());
setTorrentSaveDir(new_save_location.getParent().toString(), new_save_location.getName());
return;
}
try{
new_save_location = new_save_location.getCanonicalFile();
}catch( Throwable e ){
Debug.printStackTrace(e);
}
if ( old_file.equals( new_save_location )){
// nothing to do
} else if (torrent.isSimpleTorrent()) {
// Have to keep the file name in sync if we're renaming.
if (controller.getDiskManagerFileInfo()[0].setLinkAtomic(new_save_location)) {
setTorrentSaveDir( new_save_location.getParentFile().toString(), new_save_location.getName());
} else {throw new DownloadManagerException( "rename operation failed");}
}else{
if (FileUtil.isAncestorOf(old_file, new_save_location)) {
Logger.logTextResource(new LogAlert(this, LogAlert.REPEATABLE,
LogAlert.AT_ERROR, "DiskManager.alert.movefilefails"),
new String[] {old_file.toString(), "Target is sub-directory of files" });
throw( new DownloadManagerException( "rename operation failed" ));
}
// The files we move must be limited to those mentioned in the torrent.
final HashSet files_to_move = new HashSet();
// Required for the adding of parent directories logic.
files_to_move.add(null);
DiskManagerFileInfo[] info_files = controller.getDiskManagerFileInfo();
for (int i=0; i<info_files.length; i++) {
File f = info_files[i].getFile(true);
try {f = f.getCanonicalFile();}
catch (IOException ioe) {f = f.getAbsoluteFile();}
boolean added_entry = files_to_move.add(f);
/**
* Start adding all the parent directories to the
* files_to_move list. Doesn't matter if we include
* files which are outside of the file path, the
* renameFile call won't try to move those directories
* anyway.
*/
while (added_entry) {
f = f.getParentFile();
added_entry = files_to_move.add(f);
}
}
FileFilter ff = new FileFilter() {
public boolean accept(File f) {return files_to_move.contains(f);}
};
if ( FileUtil.renameFile( old_file, new_save_location, false, ff )){
setTorrentSaveDir( new_save_location.getParentFile().toString(), new_save_location.getName());
}else{
throw( new DownloadManagerException( "rename operation failed" ));
}
}
}else{
dm.moveDataFiles( new_save_location.getParentFile(), new_save_location.getName());
}
}