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.util.logging.Level;
021
022import org.syncany.config.Config;
023import org.syncany.database.FileVersion;
024import org.syncany.database.FileVersion.FileStatus;
025import org.syncany.database.MemoryDatabase;
026import org.syncany.operations.Assembler;
027
028public class ChangeFileSystemAction extends FileCreatingFileSystemAction {
029        public ChangeFileSystemAction(Config config, MemoryDatabase winningDatabase, Assembler assembler, FileVersion fromFileVersion,
030                        FileVersion toFileVersion) {
031                super(config, winningDatabase, assembler, fromFileVersion, toFileVersion);
032        }
033        
034        @Override
035        public FileSystemActionResult execute() throws Exception {
036                boolean fromFileExists = fileExists(fileVersion1);
037                boolean fromFileMatches = fromFileExists && fileAsExpected(fileVersion1);
038                
039                boolean toFileExists = fileExists(fileVersion2);
040                boolean toFileMatches = toFileExists && fileAsExpected(fileVersion2);
041                
042                boolean filesAtSameLocation = fileVersion1.getPath().equals(fileVersion2.getPath());
043
044                if (fromFileMatches && !toFileMatches) { // Normal case 
045                        // Original file matches, so we can delete it
046                        // Create conflict file for winning file, if it exists
047                        if (!toFileExists) {
048                                logger.log(Level.INFO, "     - (1) Original file matches, target file does NOT match: deleting original file, creating target file at: "+fileVersion2);
049                                
050                                deleteFile(fileVersion1);       
051                                createFileFolderOrSymlink(fileVersion2);                                                
052                        }
053                        else {
054                                logger.log(Level.INFO, "     - (2) Original file matches, target file does NOT match (EXISTS!): deleting original file, creating conflict file and creating target file at: "+fileVersion2);
055                                
056                                deleteFile(fileVersion1);       
057                                moveToConflictFile(fileVersion2);
058                                createFileFolderOrSymlink(fileVersion2);                                                                                
059                        }                               
060                }
061                else if (fromFileMatches && toFileMatches) {
062                        // Original file matches, so we can delete it
063                        // Nothing to do for winning file, matches
064                        
065                        if (!filesAtSameLocation) {
066                                logger.log(Level.INFO, "     - (3) Original file matches, target file matches, and they are not the same: deleting orig. file, nothing else!");
067                                deleteFile(fileVersion1);
068                        }
069                        else {
070                                logger.log(Level.INFO, "     - (4) Original file matches, target file matches, but they are in the same location (!!): Do nothing!");                           
071                        }
072                }
073                else if (!fromFileMatches && toFileMatches) {
074                        // Leave original file untouched. Will be untracked from now on
075                        // Nothing to do for winning file, matches
076                        
077                        logger.log(Level.INFO, "     - (5) Original does NOT match, target file matches: Leaving orig. file untouched. Do nothing!");                           
078                }
079                else if (!fromFileMatches && !toFileMatches) {
080                        // Leave original file untouched. Will be untracked from now on
081                        // Create conflict file for winning file, if it exists
082                        
083                        if (toFileExists) {
084                                logger.log(Level.INFO, "     - (6) Original does NOT match, target file does NOT match, but exists: Creating conflict file, and creating file at: "+fileVersion2);
085                                
086                                moveToConflictFile(fileVersion2);
087                                createFileFolderOrSymlink(fileVersion2);        
088                        }
089                        else {
090                                if (fileVersion2.getStatus() == FileStatus.DELETED) {
091                                        logger.log(Level.INFO, "     - (7) Original does NOT match, target file does not exist (and SHOUDN'T): Nothing to do!");                                        
092                                }
093                                else {
094                                        logger.log(Level.INFO, "     - (8) Original does NOT match, target file does not exist: Creating file at: "+fileVersion2);
095                                        createFileFolderOrSymlink(fileVersion2);
096                                }
097                        }
098                }
099                
100                return new FileSystemActionResult();
101        }
102
103        @Override
104        public String toString() {
105                return "ChangeFileSystemAction [file1=" + fileVersion1 + ", file2=" + fileVersion2 + "]";
106        }                               
107}