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.restore; 019 020import java.io.File; 021 022import org.apache.commons.io.FileUtils; 023import org.syncany.config.Config; 024import org.syncany.database.FileVersion; 025import org.syncany.database.FileVersion.FileStatus; 026import org.syncany.database.FileVersion.FileType; 027import org.syncany.database.MemoryDatabase; 028import org.syncany.operations.Assembler; 029import org.syncany.operations.down.actions.FileCreatingFileSystemAction; 030import org.syncany.util.NormalizedPath; 031 032public class RestoreFileSystemAction extends FileCreatingFileSystemAction { 033 private String relativeTargetPath; 034 035 public RestoreFileSystemAction(Config config, Assembler assembler, FileVersion fileVersion, String relativeTargetPath) { 036 super(config, new MemoryDatabase(), assembler, null, fileVersion); 037 this.relativeTargetPath = relativeTargetPath; 038 } 039 040 @Override 041 public RestoreFileSystemActionResult execute() throws Exception { 042 if (fileVersion2.getType() == FileType.FOLDER) { 043 throw new Exception("Cannot restore folders."); 044 } 045 else if (fileVersion2.getType() == FileType.SYMLINK) { 046 throw new Exception("Not yet implemented."); 047 } 048 else { 049 // Assemble file to cache 050 File cacheFile = assembleFileToCache(fileVersion2); 051 052 // Find target path & folder 053 NormalizedPath targetPath = findTargetPath(); 054 NormalizedPath targetFolder = targetPath.getParent(); 055 056 // Create folder (if necessary) and move file 057 if (!targetFolder.toFile().isDirectory()) { 058 targetFolder.toFile().mkdirs(); 059 } 060 061 FileUtils.moveFile(cacheFile, targetPath.toFile()); 062 063 return new RestoreFileSystemActionResult(targetPath.toFile()); 064 } 065 } 066 067 private NormalizedPath findTargetPath() throws Exception { 068 NormalizedPath targetPath = null; 069 070 if (relativeTargetPath == null) { 071 String restoredSuffix = "restored version " + fileVersion2.getVersion(); 072 targetPath = new NormalizedPath(config.getLocalDir(), fileVersion2.getPath()).withSuffix(restoredSuffix, false); 073 } 074 else { 075 targetPath = new NormalizedPath(config.getLocalDir(), relativeTargetPath); 076 } 077 078 return targetPath; 079 } 080}