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.apache.commons.io.FileUtils; 024import org.syncany.config.Config; 025import org.syncany.database.FileVersion; 026import org.syncany.database.MemoryDatabase; 027import org.syncany.util.FileUtil; 028 029public class RenameFileSystemAction extends FileSystemAction { 030 public RenameFileSystemAction(Config config, FileVersion from, FileVersion to, MemoryDatabase winningDatabase) { 031 super(config, winningDatabase, from, to); 032 } 033 034 @Override 035 public FileSystemActionResult execute() throws Exception { 036 File fromFileOnDisk = getAbsolutePathFile(fileVersion1.getPath()); 037 File targetFileOnDisk = getAbsolutePathFile(fileVersion2.getPath()); 038 039 boolean fromFileExists = FileUtil.exists(fromFileOnDisk); 040 boolean targetFileExists = FileUtil.exists(targetFileOnDisk); 041 042 boolean fileRenamed = !targetFileOnDisk.equals(fromFileOnDisk); 043 044 if (fileRenamed) { 045 if (fromFileExists && !targetFileExists) { 046 if (fileAsExpected(fileVersion1)) { // << Expected case! 047 logger.log(Level.INFO, " - (1) Renaming file "+fromFileOnDisk+" to "+targetFileOnDisk+" ..."); 048 targetFileOnDisk = moveFileToFinalLocation(fromFileOnDisk, fileVersion2); 049 } 050 else { 051 logger.log(Level.INFO, " - (2) Source file differs from what we expected."); 052 throw new Exception("Source file differs from what we expected."); 053 } 054 } 055 else if (fromFileExists && targetFileExists) { 056 if (fileAsExpected(fileVersion1)) { 057 if (fileAsExpected(fileVersion2)) { 058 logger.log(Level.INFO, " - (3) File at destination is what was expected. Nothing to do for "+targetFileOnDisk+" ..."); 059 } 060 else { 061 logger.log(Level.INFO, " - (4) File at destination differs, creating conflict file for "+targetFileOnDisk+" ..."); 062 063 moveToConflictFile(fileVersion2); 064 FileUtils.moveFile(fromFileOnDisk, targetFileOnDisk); 065 } 066 } 067 else { 068 logger.log(Level.INFO, " - (5) Cannot rename because orig. file does not exist."); 069 throw new Exception("Cannot rename because orig. file does not exist"); 070 } 071 } 072 else if (!fromFileExists && !targetFileExists) { 073 logger.log(Level.INFO, " - (6) Cannot rename because orig. file does not exist."); 074 throw new Exception("Cannot rename because orig. file does not exist"); 075 } 076 else if (!fromFileExists && targetFileExists) { 077 if (fileAsExpected(fileVersion2)) { 078 logger.log(Level.INFO, " - (7) File at destination is what was expected. Nothing to do for "+targetFileOnDisk+" ..."); 079 } 080 else { 081 logger.log(Level.INFO, " - (8) Cannot rename because orig. file does not exist."); 082 throw new Exception("Cannot rename because orig. file does not exist"); 083 } 084 } 085 } 086 087 // Set attributes 088 setFileAttributes(fileVersion2, targetFileOnDisk); // TODO [low] check for fileAsExpected 089 setLastModified(fileVersion2, targetFileOnDisk); 090 091 return new FileSystemActionResult(); 092 } 093 094 @Override 095 public String toString() { 096 return "RenameFileSystemAction [file1=" + fileVersion1 + ", file2=" + fileVersion2 + "]"; 097 } 098 099 100}