private int compareByFullName(FileSystemAction a1, FileSystemAction a2) { // For deleted, do the longest path first if (a1.getClass().equals(DeleteFileSystemAction.class)) { return -1 * a1.getFile2().getPath().compareTo(a2.getFile2().getPath()); } // For new folder actions, do the shortest path first else if (a1.getClass().equals(NewFileSystemAction.class)) { return a1.getFile2().getPath().compareTo(a2.getFile2().getPath()); } return 0; }
protected void deleteFile(FileVersion deleteFileVersion) { File fromFileOnDisk = getAbsolutePathFile(deleteFileVersion.getPath()); fromFileOnDisk.delete(); }
private int getPathConflictingWithDeleteActionIndex(FileSystemAction currentAction, List<FileSystemAction> deleteFolderActions) { for (int i=0; i<deleteFolderActions.size(); i++) { FileSystemAction action = deleteFolderActions.get(i); if (action instanceof DeleteFileSystemAction && action.getType() == FileType.FOLDER) { if (action.getFile2().getPath().equals(currentAction.getFile2().getPath())) { return i; } } } return -1; }
private void determineActionFileHistoryNotInWinnerBranch(FileVersion localLastVersion, File localLastFile, List<FileSystemAction> fileSystemActions) { // No local file version in local database if (localLastVersion == null) { throw new RuntimeException("This should not happen."); } // Local version found in local database else { FileSystemAction action = new DeleteFileSystemAction(config, localLastVersion, localLastVersion, null); fileSystemActions.add(action); logger.log(Level.INFO, " -> (17) Local file exists, but not in winner branch -> File was deleted remotely: local file = "+localLastFile+", local version = "+localLastVersion+", winning version = (none)"); logger.log(Level.INFO, " -> "+action); changeSet.getDeletedFiles().add(localLastVersion.getPath()); } }
private void findAndAppendDeletedFiles(ChangeSet localChanges, Map<String, FileVersion> filesInDatabase) { for (FileVersion lastLocalVersion : filesInDatabase.values()) { // Check if file exists, remove if it doesn't File lastLocalVersionOnDisk = new File(config.getLocalDir() + File.separator + lastLocalVersion.getPath()); // Ignore this file history if the last version is marked "DELETED" if (lastLocalVersion.getStatus() == FileStatus.DELETED) { continue; } // If file has VANISHED, mark as DELETED if (!FileUtil.exists(lastLocalVersionOnDisk)) { localChanges.getDeletedFiles().add(lastLocalVersion.getPath()); } } }
protected FileVersionComparison fileChanges(FileVersion expectedLocalFileVersion) { File actualLocalFile = getAbsolutePathFile(expectedLocalFileVersion.getPath()); FileVersionComparison fileVersionComparison = fileVersionHelper.compare(expectedLocalFileVersion, actualLocalFile, true); return fileVersionComparison; }
protected void setFileAttributes(FileVersion reconstructedFileVersion) throws IOException { File reconstructedFilesAtFinalLocation = getAbsolutePathFile(reconstructedFileVersion.getPath()); setFileAttributes(reconstructedFileVersion, reconstructedFilesAtFinalLocation); }
private void updateFilenameHistoryCache() { // TODO [medium] Performance: This throws away the unchanged entries. It should only update new database version filenameHistoryCache.clear(); for (PartialFileHistory cacheFileHistory : fullDatabaseVersionCache.getFileHistories()) { FileVersion lastVersion = cacheFileHistory.getLastVersion(); String fileName = lastVersion.getPath(); if (lastVersion.getStatus() != FileStatus.DELETED) { filenameHistoryCache.put(fileName, cacheFileHistory); } } }
protected boolean fileExists(FileVersion expectedLocalFileVersion) { File actualLocalFile = getAbsolutePathFile(expectedLocalFileVersion.getPath()); return FileUtil.exists(actualLocalFile); }
/** * Queries the database for the currently active {@link FileVersion}s and returns it * as a map. If the current file tree (on the disk) has not changed, the result will * match the files on the disk. * * <p>Keys in the returned map correspond to the file version's relative file path, * and values to the actual {@link FileVersion} object. * * @return Returns the current file tree as a map of relative paths to {@link FileVersion} objects */ public Map<String, FileVersion> getCurrentFileTree() { try (PreparedStatement preparedStatement = getStatement("fileversion.select.master.getCurrentFileTree.sql")) { Map<String, FileVersion> fileTree = new TreeMap<>(); List<FileVersion> fileList = getFileTree(preparedStatement); for (FileVersion fileVersion : fileList) { fileTree.put(fileVersion.getPath(), fileVersion); } return fileTree; } catch (SQLException e) { throw new RuntimeException(e); } }
private PartialFileHistory getFileHistoryByPathFromDatabaseVersion(DatabaseVersion databaseVersion, String path) { // TODO [medium] Extremely performance intensive, because this is called inside a loop above. Implement better caching for database version!!! for (PartialFileHistory fileHistory : databaseVersion.getFileHistories()) { FileVersion lastVersion = fileHistory.getLastVersion(); if (lastVersion.getStatus() != FileStatus.DELETED && lastVersion.getPath().equals(path)) { return fileHistory; } } return null; }
protected void setLastModified(FileVersion reconstructedFileVersion, File reconstructedFilesAtFinalLocation) { // Using Files.setLastModifiedTime() instead of File.setLastModified() // due to pre-1970 issue. See #374 for details. try { FileTime newLastModifiedTime = FileTime.fromMillis(reconstructedFileVersion.getLastModified().getTime()); Files.setLastModifiedTime(reconstructedFilesAtFinalLocation.toPath(), newLastModifiedTime); } catch (IOException e) { logger.log(Level.WARNING, "Warning: Could not set last modified date for file " + reconstructedFilesAtFinalLocation + "; Ignoring error.", e); } }
protected void moveToConflictFile(FileVersion targetFileVersion) throws IOException { NormalizedPath targetConflictingFile = new NormalizedPath(config.getLocalDir(), targetFileVersion.getPath()); moveToConflictFile(targetConflictingFile); }
protected void createSymlink(FileVersion reconstructedFileVersion) throws Exception { File reconstructedFileAtFinalLocation = getAbsolutePathFile(reconstructedFileVersion.getPath()); if (EnvironmentUtil.symlinksSupported()) { // 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(); } // Make link logger.log(Level.INFO, " - Creating symlink at " + reconstructedFileAtFinalLocation + " (target: " + reconstructedFileVersion.getLinkTarget() + ") ..."); FileUtil.createSymlink(reconstructedFileVersion.getLinkTarget(), reconstructedFileAtFinalLocation); } else { logger.log(Level.INFO, " - Skipping symlink (not supported) at " + reconstructedFileAtFinalLocation + " (target: " + reconstructedFileVersion.getLinkTarget() + ") ..."); } }
protected void createFolder(FileVersion targetFileVersion) throws Exception { NormalizedPath targetDirPath = new NormalizedPath(config.getLocalDir(), targetFileVersion.getPath()); logger.log(Level.INFO, " - Creating folder at " + targetFileVersion + " ..."); try { // Clean filename if (targetDirPath.hasIllegalChars()) { targetDirPath = targetDirPath.toCreatable("filename conflict", true); logger.log(Level.INFO, " - Had illegal chars, cleaned to "+targetDirPath); } // Try creating it createFolder(targetDirPath); setFileAttributes(targetFileVersion, targetDirPath.toFile()); } catch (Exception e) { throw new RuntimeException("What to do here?!", e); } }
private void printGroupedHistories(LsOperationResult operationResult, int longestSize, int longestVersion) { Iterator<FileVersion> fileVersionIterator = operationResult.getFileList().iterator(); while (fileVersionIterator.hasNext()) { FileVersion fileVersion = fileVersionIterator.next(); PartialFileHistory fileHistory = operationResult.getFileVersions().get(fileVersion.getFileHistoryId()); out.printf("File %s, %s\n", formatObjectId(fileHistory.getFileHistoryId()), fileVersion.getPath()); for (FileVersion fileVersionInHistory : fileHistory.getFileVersions().values()) { if (fileVersionInHistory.equals(fileVersion)) { out.print(" * "); } else { out.print(" "); } printOneVersion(fileVersionInHistory, longestVersion, longestSize); } if (fileVersionIterator.hasNext()) { out.println(); } } }
private NormalizedPath findTargetPath() throws Exception { NormalizedPath targetPath = null; if (relativeTargetPath == null) { String restoredSuffix = "restored version " + fileVersion2.getVersion(); targetPath = new NormalizedPath(config.getLocalDir(), fileVersion2.getPath()).withSuffix(restoredSuffix, false); } else { targetPath = new NormalizedPath(config.getLocalDir(), relativeTargetPath); } return targetPath; } }
private void printOneVersion(FileVersion fileVersion, int longestVersion, int longestSize) { String fileStatus = formatFileStatusShortStr(fileVersion.getStatus()); String fileType = formatFileTypeShortStr(fileVersion.getType()); String posixPermissions = (fileVersion.getPosixPermissions() != null) ? fileVersion.getPosixPermissions() : ""; String dosAttributes = (fileVersion.getDosAttributes() != null) ? fileVersion.getDosAttributes() : ""; String fileChecksum = formatObjectId(fileVersion.getChecksum()); String fileHistoryId = formatObjectId(fileVersion.getFileHistoryId()); String path = (fileVersion.getType() == FileType.SYMLINK) ? fileVersion.getPath() + " -> " + fileVersion.getLinkTarget() : fileVersion.getPath(); out.printf("%s %s %s %9s %4s %" + longestSize + "d %" + checksumLength + "s %" + checksumLength + "s %"+longestVersion+"d %s\n", DATE_FORMAT.format(fileVersion.getUpdated()), fileStatus, fileType, posixPermissions, dosAttributes, fileVersion.getSize(), fileChecksum, fileHistoryId, fileVersion.getVersion(), path); }
public FileProperties captureFileProperties(FileVersion fileVersion) { if (fileVersion == null) { return null; } FileProperties fileProperties = new FileProperties(); fileProperties.lastModified = fileVersion.getLastModified().getTime(); fileProperties.size = fileVersion.getSize(); fileProperties.relativePath = fileVersion.getPath(); fileProperties.linkTarget = fileVersion.getLinkTarget(); fileProperties.checksum = fileVersion.getChecksum(); fileProperties.type = fileVersion.getType(); fileProperties.posixPermissions = fileVersion.getPosixPermissions(); fileProperties.dosAttributes = fileVersion.getDosAttributes(); fileProperties.exists = fileVersion.getStatus() != FileStatus.DELETED; fileProperties.locked = false; return fileProperties; }
@Override public FileVersion clone() { try { FileVersion clone = (FileVersion) super.clone(); clone.setChecksum(getChecksum()); clone.setLastModified(getLastModified()); clone.setUpdated(getUpdated()); clone.setPath(getPath()); clone.setType(getType()); clone.setVersion(getVersion()); clone.setSize(getSize()); clone.setDosAttributes(getDosAttributes()); clone.setPosixPermissions(getPosixPermissions()); clone.setLinkTarget(getLinkTarget()); clone.setStatus(getStatus()); return clone; } catch (Exception ex) { throw new RuntimeException(ex); } }