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.plugins.transfer.files;
019
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022
023import org.syncany.plugins.transfer.StorageException;
024
025/**
026 * Action remote files represent a running transfer operation on the remote storage. Transfer
027 * operations are operations that modify the repository and/or are relevant for the 
028 * consistency of the local directory or the remote repository. Action files have no content,
029 * they are just markers and exist only while an operation is running.
030 * 
031 * <p><b>Name pattern:</b> The name pattern of an action file is
032 * <b>action-&lt;operationname&gt;-&lt;machinename&gt;-&lt;timestamp&gt;</b>. Initializing an 
033 * instance with a non-matching name will throw an exception.
034 * 
035 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
036 */
037public class ActionRemoteFile extends RemoteFile {
038        private static final Pattern NAME_PATTERN = Pattern.compile("action-(up|down|cleanup|restore)-([^-]+)-(\\d+)");
039        private static final String NAME_FORMAT = "action-%s-%s-%010d";
040
041        private String operationName;
042        private String clientName;
043        private long timestamp;
044
045        public ActionRemoteFile(String name) throws StorageException {
046                super(name);
047        }
048
049        public ActionRemoteFile(String operationName, String clientName, long timestamp) throws StorageException {
050                super(String.format(NAME_FORMAT, operationName, clientName, timestamp));
051        }
052
053        public String getOperationName() {
054                return operationName;
055        }
056
057        public String getClientName() {
058                return clientName;
059        }
060
061        public long getTimestamp() {
062                return timestamp;
063        }
064        
065        @Override
066        protected String validateName(String name) throws StorageException {
067                Matcher matcher = NAME_PATTERN.matcher(name);
068
069                if (!matcher.matches()) {
070                        throw new StorageException(name + ": remote database filename pattern does not match: " + NAME_PATTERN.pattern() + " expected.");
071                }
072
073                operationName = matcher.group(1);
074                clientName = matcher.group(2);
075                timestamp = Long.parseLong(matcher.group(3));
076
077                return name;
078        }
079}