List of usage examples for org.apache.commons.io FileUtils moveFile
public static void moveFile(File srcFile, File destFile) throws IOException
From source file:org.syncany.connection.plugins.local.LocalTransferManager.java
@Override public void upload(File localFile, RemoteFile remoteFile) throws StorageException { connect();//from w ww .j a va2 s. c o m File repoFile = getRemoteFile(remoteFile); File tempRepoFile = new File( getAbsoluteParentDirectory(repoFile) + File.separator + ".temp-" + repoFile.getName()); // Do not overwrite files with same size! if (repoFile.exists() && repoFile.length() == localFile.length()) { return; } // No such local file if (!localFile.exists()) { throw new StorageException("No such file on local disk: " + localFile); } try { copyLocalFile(localFile, tempRepoFile); FileUtils.moveFile(tempRepoFile, repoFile); } catch (IOException ex) { throw new StorageException("Unable to copy file " + localFile + " to local repository " + repoFile, ex); } }
From source file:org.syncany.connection.plugins.rest.RestTransferManager.java
@Override public void download(RemoteFile remoteFile, File localFile) throws StorageException { connect();/*from w ww. jav a 2 s . c o m*/ File tempFile = null; String remotePath = getRemoteFile(remoteFile); try { // Download StorageObject fileObj = service.getObject(bucket.getName(), remotePath); InputStream fileObjInputStream = fileObj.getDataInputStream(); logger.log(Level.FINE, "- Downloading from bucket " + bucket.getName() + ": " + fileObj + " ..."); tempFile = createTempFile(remoteFile.getName()); FileUtil.writeToFile(fileObjInputStream, tempFile); fileObjInputStream.close(); // Move to final location if (localFile.exists()) { localFile.delete(); } FileUtils.moveFile(tempFile, localFile); } catch (Exception ex) { if (tempFile != null) { tempFile.delete(); } throw new StorageException("Unable to download file '" + remoteFile.getName(), ex); } }
From source file:org.syncany.operations.actions.FileCreatingFileSystemAction.java
protected void createFile(FileVersion reconstructedFileVersion) throws Exception { File reconstructedFileAtFinalLocation = getAbsolutePathFile(reconstructedFileVersion.getPath()); File reconstructedFileInCache = config.getCache().createTempFile("reconstructedFileVersion"); logger.log(Level.INFO, " - Creating file " + reconstructedFileVersion.getPath() + " to " + reconstructedFileInCache + " ..."); FileContent fileContent = localDatabase.getContent(reconstructedFileVersion.getChecksum()); if (fileContent == null) { fileContent = winningDatabase.getContent(reconstructedFileVersion.getChecksum()); }/*from www . j a v a2 s. c o m*/ // Create file // TODO [low] Create an assembler/reconstructor class to package re-assembly in the chunk-package MultiChunker multiChunker = config.getMultiChunker(); FileOutputStream reconstructedFileOutputStream = new FileOutputStream(reconstructedFileInCache); if (fileContent != null) { // File can be empty! Collection<ChunkChecksum> fileChunks = fileContent.getChunks(); for (ChunkChecksum chunkChecksum : fileChunks) { MultiChunkEntry multiChunkForChunk = localDatabase.getMultiChunkForChunk(chunkChecksum); if (multiChunkForChunk == null) { multiChunkForChunk = winningDatabase.getMultiChunkForChunk(chunkChecksum); } File decryptedMultiChunkFile = config.getCache() .getDecryptedMultiChunkFile(multiChunkForChunk.getId().getRaw()); MultiChunk multiChunk = multiChunker.createMultiChunk(decryptedMultiChunkFile); InputStream chunkInputStream = multiChunk.getChunkInputStream(chunkChecksum.getRaw()); FileUtil.appendToOutputStream(chunkInputStream, reconstructedFileOutputStream); } } reconstructedFileOutputStream.close(); // Make directory if it does not exist File reconstructedFileParentDir = reconstructedFileAtFinalLocation.getParentFile(); if (!FileUtil.exists(reconstructedFileParentDir)) { logger.log(Level.INFO, " - Parent folder does not exist, creating " + reconstructedFileParentDir + " ..."); reconstructedFileParentDir.mkdirs(); } // Okay. Now move to real place if (!isLegalFilename(reconstructedFileAtFinalLocation)) { File illegalFile = reconstructedFileAtFinalLocation; reconstructedFileAtFinalLocation = cleanFilename(reconstructedFileAtFinalLocation); logger.log(Level.SEVERE, " - Filename was ILLEGAL, cleaned from {0} to {1}", new Object[] { illegalFile.getName(), reconstructedFileAtFinalLocation.getName() }); } logger.log(Level.INFO, " - Okay, now moving to " + reconstructedFileAtFinalLocation + " ..."); FileUtils.moveFile(reconstructedFileInCache, reconstructedFileAtFinalLocation); // TODO [medium] This should be in a try/catch block // Set attributes & timestamp setFileAttributes(reconstructedFileVersion, reconstructedFileAtFinalLocation); setLastModified(reconstructedFileVersion); }
From source file:org.syncany.operations.actions.FileSystemAction.java
protected void createConflictFile(FileVersion conflictingLocalVersion) throws IOException { File conflictingLocalFile = getAbsolutePathFile(conflictingLocalVersion.getPath()); if (!conflictingLocalFile.exists()) { logger.log(Level.INFO,/*from w w w .ja v a 2 s . c o m*/ " - Creation of conflict file not necessary. Locally conflicting file vanished from " + conflictingLocalFile); return; } String conflictDirectory = FileUtil.getAbsoluteParentDirectory(conflictingLocalFile); String conflictBasename = FileUtil.getBasename(conflictingLocalFile); String conflictFileExtension = FileUtil.getExtension(conflictingLocalFile); String conflictUserName = (config.getDisplayName() != null) ? config.getDisplayName() : config.getMachineName(); String conflictDate = new SimpleDateFormat("d MMM yy, h-mm a") .format(conflictingLocalVersion.getLastModified()); boolean conflictCreatedByEndsWithS = conflictingLocalVersion.getCreatedBy().endsWith("s"); boolean conflictFileHasExtension = conflictFileExtension != null && !"".equals(conflictFileExtension); String newFullName; if (conflictFileHasExtension) { if (conflictCreatedByEndsWithS) { newFullName = String.format("%s (%s' conflicted copy, %s).%s", conflictBasename, conflictUserName, conflictDate, conflictFileExtension); } else { newFullName = String.format("%s (%s's conflicted copy, %s).%s", conflictBasename, conflictUserName, conflictDate, conflictFileExtension); } } else { if (conflictCreatedByEndsWithS) { newFullName = String.format("%s (%s' conflicted copy, %s)", conflictBasename, conflictUserName, conflictDate); } else { newFullName = String.format("%s (%s's conflicted copy, %s)", conflictBasename, conflictUserName, conflictDate); } } File newConflictFile = new File(conflictDirectory + File.separator + newFullName); logger.log(Level.INFO, " - Local version conflicts, moving local file " + conflictingLocalFile + " to " + newConflictFile + " ..."); if (conflictingLocalFile.isDirectory()) { conflictingLocalFile.renameTo(newConflictFile); } else { FileUtils.moveFile(conflictingLocalFile, newConflictFile); } }
From source file:org.syncany.operations.actions.RenameFileSystemAction.java
@Override public void execute() throws Exception { File fromFileOnDisk = getAbsolutePathFile(fileVersion1.getPath()); File toFileOnDisk = getAbsolutePathFile(fileVersion2.getPath()); boolean fromFileExists = fromFileOnDisk.exists(); boolean toFileExists = toFileOnDisk.exists(); boolean fileRenamed = !toFileOnDisk.equals(fromFileOnDisk); if (fileRenamed) { if (fromFileExists && !toFileExists) { if (fileAsExpected(fileVersion1)) { // << Expected case! logger.log(Level.INFO, " - (1) Renaming file " + fromFileOnDisk + " to " + toFileOnDisk + " ..."); FileUtils.moveFile(fromFileOnDisk, toFileOnDisk); } else { logger.log(Level.INFO, " - (2) Source file differs from what we expected."); throw new Exception("Source file differs from what we expected."); }// ww w.j a va 2s . c o m } else if (fromFileExists && toFileExists) { if (fileAsExpected(fileVersion1)) { if (fileAsExpected(fileVersion2)) { logger.log(Level.INFO, " - (3) File at destination is what was expected. Nothing to do for " + toFileOnDisk + " ..."); } else { logger.log(Level.INFO, " - (4) File at destination differs, creating conflict file for " + toFileOnDisk + " ..."); createConflictFile(fileVersion2); FileUtils.moveFile(fromFileOnDisk, toFileOnDisk); } } else { logger.log(Level.INFO, " - (5) Cannot rename because orig. file does not exist."); throw new Exception("Cannot rename because orig. file does not exist"); } } else if (!fromFileExists && !toFileExists) { logger.log(Level.INFO, " - (6) Cannot rename because orig. file does not exist."); throw new Exception("Cannot rename because orig. file does not exist"); } else if (!fromFileExists && toFileExists) { if (fileAsExpected(fileVersion2)) { logger.log(Level.INFO, " - (7) File at destination is what was expected. Nothing to do for " + toFileOnDisk + " ..."); } else { logger.log(Level.INFO, " - (8) Cannot rename because orig. file does not exist."); throw new Exception("Cannot rename because orig. file does not exist"); } } } // Set attributes setFileAttributes(fileVersion2); // TODO [low] check for fileAsExpected setLastModified(fileVersion2); }
From source file:org.syncany.operations.down.actions.FileCreatingFileSystemAction.java
private void moveFileToFinalLocation(File reconstructedFileInCache, FileVersion targetFileVersion) throws IOException { NormalizedPath originalPath = new NormalizedPath(config.getLocalDir(), targetFileVersion.getPath()); NormalizedPath targetPath = originalPath; try {/* ww w .ja va2 s . c o m*/ // Clean filename if (targetPath.hasIllegalChars()) { targetPath = targetPath.toCreatable("filename conflict", true); } // Try creating folder createFolder(targetPath.getParent()); } catch (Exception e) { throw new RuntimeException("What to do here?!"); } // Try moving file to final destination try { FileUtils.moveFile(reconstructedFileInCache, targetPath.toFile()); } catch (FileExistsException e) { moveToConflictFile(targetPath); } catch (Exception e) { throw new RuntimeException("What to do here?!"); } }
From source file:org.syncany.operations.down.actions.FileSystemAction.java
protected void moveToConflictFile(NormalizedPath conflictingPath) throws IOException { if (!FileUtil.exists(conflictingPath.toFile())) { logger.log(Level.INFO,//from www . ja v a 2 s . c o m " - Creation of conflict file not necessary. Locally conflicting file vanished from " + conflictingPath); return; } int attempts = 0; while (attempts++ < 10) { NormalizedPath conflictedCopyPath = null; try { conflictedCopyPath = findConflictFilename(conflictingPath); logger.log(Level.INFO, " - Local version conflicts, moving local file " + conflictingPath + " to " + conflictedCopyPath + " ..."); if (conflictingPath.toFile().isDirectory()) { FileUtils.moveDirectory(conflictingPath.toFile(), conflictedCopyPath.toFile()); } else { FileUtils.moveFile(conflictingPath.toFile(), conflictedCopyPath.toFile()); } // Success! break; } catch (FileExistsException e) { logger.log(Level.SEVERE, " - Cannot create conflict file; attempt = " + attempts + " for file: " + conflictedCopyPath, e); } catch (FileNotFoundException e) { logger.log(Level.INFO, " - Conflict file vanished. Don't care!", e); } catch (Exception e) { throw new RuntimeException("What to do here?", e); } } }
From source file:org.syncany.operations.down.actions.FileSystemAction.java
protected File moveFileToFinalLocation(File reconstructedFileInCache, FileVersion targetFileVersion) throws IOException { NormalizedPath originalPath = new NormalizedPath(config.getLocalDir(), targetFileVersion.getPath()); NormalizedPath targetPath = originalPath; try {/* w w w.j a v a 2s . c om*/ // Clean filename if (targetPath.hasIllegalChars()) { targetPath = targetPath.toCreatable("filename conflict", true); } // Try creating folder createFolder(targetPath.getParent()); } catch (Exception e) { throw new RuntimeException("What to do here?!"); } // Try moving file to final destination try { FileUtils.moveFile(reconstructedFileInCache, targetPath.toFile()); } catch (FileExistsException e) { logger.log(Level.FINE, "File already existed", e); moveToConflictFile(targetPath); } catch (Exception e) { throw new RuntimeException("What to do here?!"); } return targetPath.toFile(); }
From source file:org.syncany.operations.down.actions.RenameFileSystemAction.java
@Override public FileSystemActionResult execute() throws Exception { File fromFileOnDisk = getAbsolutePathFile(fileVersion1.getPath()); File targetFileOnDisk = getAbsolutePathFile(fileVersion2.getPath()); boolean fromFileExists = FileUtil.exists(fromFileOnDisk); boolean targetFileExists = FileUtil.exists(targetFileOnDisk); boolean fileRenamed = !targetFileOnDisk.equals(fromFileOnDisk); if (fileRenamed) { if (fromFileExists && !targetFileExists) { if (fileAsExpected(fileVersion1)) { // << Expected case! logger.log(Level.INFO, " - (1) Renaming file " + fromFileOnDisk + " to " + targetFileOnDisk + " ..."); targetFileOnDisk = moveFileToFinalLocation(fromFileOnDisk, fileVersion2); } else { logger.log(Level.INFO, " - (2) Source file differs from what we expected."); throw new Exception("Source file differs from what we expected."); }// w ww . j ava 2 s . com } else if (fromFileExists && targetFileExists) { if (fileAsExpected(fileVersion1)) { if (fileAsExpected(fileVersion2)) { logger.log(Level.INFO, " - (3) File at destination is what was expected. Nothing to do for " + targetFileOnDisk + " ..."); } else { logger.log(Level.INFO, " - (4) File at destination differs, creating conflict file for " + targetFileOnDisk + " ..."); moveToConflictFile(fileVersion2); FileUtils.moveFile(fromFileOnDisk, targetFileOnDisk); } } else { logger.log(Level.INFO, " - (5) Cannot rename because orig. file does not exist."); throw new Exception("Cannot rename because orig. file does not exist"); } } else if (!fromFileExists && !targetFileExists) { logger.log(Level.INFO, " - (6) Cannot rename because orig. file does not exist."); throw new Exception("Cannot rename because orig. file does not exist"); } else if (!fromFileExists && targetFileExists) { if (fileAsExpected(fileVersion2)) { logger.log(Level.INFO, " - (7) File at destination is what was expected. Nothing to do for " + targetFileOnDisk + " ..."); } else { logger.log(Level.INFO, " - (8) Cannot rename because orig. file does not exist."); throw new Exception("Cannot rename because orig. file does not exist"); } } } // Set attributes setFileAttributes(fileVersion2, targetFileOnDisk); // TODO [low] check for fileAsExpected setLastModified(fileVersion2, targetFileOnDisk); return new FileSystemActionResult(); }
From source file:org.syncany.operations.restore.RestoreFileSystemAction.java
@Override public RestoreFileSystemActionResult execute() throws Exception { if (fileVersion2.getType() == FileType.FOLDER) { throw new Exception("Cannot restore folders."); } else if (fileVersion2.getType() == FileType.SYMLINK) { throw new Exception("Not yet implemented."); } else {//w w w . j ava 2s . co m if (fileVersion2.getStatus() == FileStatus.DELETED) { throw new Exception("Cannot restore version marked DELETED. Try previous version."); } // Assemble file to cache File cacheFile = assembleFileToCache(fileVersion2); // Find target path & folder NormalizedPath targetPath = findTargetPath(); NormalizedPath targetFolder = targetPath.getParent(); // Create folder (if necessary) and move file if (!targetFolder.toFile().isDirectory()) { targetFolder.toFile().mkdirs(); } FileUtils.moveFile(cacheFile, targetPath.toFile()); return new RestoreFileSystemActionResult(targetPath.toFile()); } }