001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.operations.down.actions; 019 020import java.io.File; 021import java.util.logging.Level; 022 023import org.syncany.config.Config; 024import org.syncany.database.FileVersion; 025import org.syncany.database.FileVersion.FileType; 026import org.syncany.database.MemoryDatabase; 027import org.syncany.operations.Assembler; 028import org.syncany.util.NormalizedPath; 029 030public abstract class FileCreatingFileSystemAction extends FileSystemAction { 031 protected Assembler assembler; 032 033 public FileCreatingFileSystemAction(Config config, MemoryDatabase winningDatabase, Assembler assembler, FileVersion file1, FileVersion file2) { 034 super(config, winningDatabase, file1, file2); 035 this.assembler = assembler; 036 } 037 038 protected void createFileFolderOrSymlink(FileVersion reconstructedFileVersion) throws Exception { 039 if (reconstructedFileVersion.getType() == FileType.FILE) { 040 createFile(reconstructedFileVersion); 041 } 042 else if (reconstructedFileVersion.getType() == FileType.FOLDER) { 043 createFolder(reconstructedFileVersion); 044 } 045 else if (reconstructedFileVersion.getType() == FileType.SYMLINK) { 046 createSymlink(reconstructedFileVersion); 047 } 048 else { 049 logger.log(Level.INFO, " - Unknown file type: " + reconstructedFileVersion.getType()); 050 throw new Exception("Unknown file type: " + reconstructedFileVersion.getType()); 051 } 052 } 053 054 protected void createFolder(FileVersion targetFileVersion) throws Exception { 055 NormalizedPath targetDirPath = new NormalizedPath(config.getLocalDir(), targetFileVersion.getPath()); 056 057 logger.log(Level.INFO, " - Creating folder at " + targetFileVersion + " ..."); 058 059 try { 060 // Clean filename 061 if (targetDirPath.hasIllegalChars()) { 062 targetDirPath = targetDirPath.toCreatable("filename conflict", true); 063 logger.log(Level.INFO, " - Had illegal chars, cleaned to "+targetDirPath); 064 } 065 066 // Try creating it 067 createFolder(targetDirPath); 068 setFileAttributes(targetFileVersion, targetDirPath.toFile()); 069 } 070 catch (Exception e) { 071 throw new RuntimeException("What to do here?!", e); 072 } 073 } 074 075 protected void createFile(FileVersion reconstructedFileVersion) throws Exception { 076 File reconstructedFileInCache = assembleFileToCache(reconstructedFileVersion); 077 moveFileToFinalLocation(reconstructedFileInCache, reconstructedFileVersion); 078 } 079 080 protected File assembleFileToCache(FileVersion reconstructedFileVersion) throws Exception { 081 File reconstructedFileInCache = assembler.assembleToCache(reconstructedFileVersion); 082 083 setFileAttributes(reconstructedFileVersion, reconstructedFileInCache); 084 setLastModified(reconstructedFileVersion, reconstructedFileInCache); 085 086 return reconstructedFileInCache; 087 } 088}