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 * The transaction file only exists as an indicator to other clients a cleanup has occurred.
027 * 
028 * <p><b>Name pattern:</b> The name pattern of a cleanup file is
029 * <b>cleanup-&lt;cleanupnumber&gt;</b>.
030 * 
031 * @author Pim Otte
032 */
033public class CleanupRemoteFile extends RemoteFile {
034        private static final Pattern NAME_PATTERN = Pattern.compile("cleanup-([0-9]+)");
035        private static final String NAME_FORMAT = "cleanup-%s";
036
037        private long cleanupNumber;
038
039        /**
040         * Initializes a new cleanup file, given a name. 
041         * 
042         * @param name cleanup file name; <b>must</b> always match the {@link #NAME_PATTERN} 
043         * @throws StorageException If the name is not match the name pattern
044         */
045        public CleanupRemoteFile(String name) throws StorageException {
046                super(name);
047        }
048
049        /**
050         * Initializes a new transaction file, given which cleanup has occurred 
051         * 
052         * @throws StorageException If the name is not match the name pattern
053         */
054        public CleanupRemoteFile(long cleanupNumber) throws StorageException {
055                super(String.format(NAME_FORMAT, Long.toString(cleanupNumber)));
056        }
057
058        @Override
059        protected String validateName(String name) throws StorageException {
060                Matcher matcher = NAME_PATTERN.matcher(name);
061
062                if (!matcher.matches()) {
063                        throw new StorageException(name + ": remote filename pattern does not match: " + NAME_PATTERN.pattern() + " expected.");
064                }
065
066                cleanupNumber = Long.parseLong(matcher.group(1));
067
068                return name;
069        }
070
071        public long getCleanupNumber() {
072                return cleanupNumber;
073        }
074}