~nicoulaj/syncany/maven-local-repo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Syncany, www.syncany.org
 * Copyright (C) 2011 Philipp C. Heckel <philipp.heckel@gmail.com> 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.syncany.connection.plugins.local;

import org.syncany.connection.plugins.AbstractTransferManager;
import org.syncany.exceptions.LocalFileNotFoundException;
import org.syncany.exceptions.RemoteFileNotFoundException;
import org.syncany.exceptions.StorageConnectException;
import org.syncany.exceptions.StorageException;
import org.syncany.watch.remote.files.RemoteFile;
import org.syncany.util.FileUtil;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Philipp C. Heckel
 */
public class LocalTransferManager extends AbstractTransferManager {
    private File folder; 

    public LocalTransferManager(LocalConnection connection) {
        super(connection);
        folder = connection.getFolder();
    }

    @Override
    public LocalConnection getConnection() {
        return (LocalConnection) super.getConnection();
    }

    @Override
    public void connect() throws StorageConnectException {
        if (folder == null || !folder.exists() || !folder.canRead() || !folder.canWrite() || !folder.isDirectory()) {
            throw new StorageConnectException("Repository folder '"+folder+"' does not exist or is not writable.");
        }
    }

    @Override
    public void disconnect() throws StorageException {
        // Nothing.
    }

    @Override
    public void download(RemoteFile remoteFile, File localFile) throws RemoteFileNotFoundException, StorageException {
        connect();

        File repoFile = getRepoFile(remoteFile);

        if (!repoFile.exists()) {
            throw new RemoteFileNotFoundException("No such file in local repository: "+repoFile);
        }  

        try {
            File tempLocalFile = config.getCache().createTempFile("local-tm-download");
            tempLocalFile.deleteOnExit();

            FileUtil.copy(repoFile, tempLocalFile, getConnection().getThrottleKbps());

            // SNM 6/01/11 windows doesn't support rename *onto* another file
            if (localFile.exists()){
                localFile.delete();
            }
            
            if (!tempLocalFile.renameTo(localFile)) {
                throw new StorageException("Unable to move temp local file "+tempLocalFile+" to "+localFile);
            }
            
            tempLocalFile.delete();
        }
        catch (IOException ex) {
            throw new StorageException("Unable to copy file "+repoFile+" from local repository to "+localFile, ex);
        }
    }

    @Override
    public void upload(File localFile, RemoteFile remoteFile) throws LocalFileNotFoundException, StorageException {
        connect();

        File repoFile = getRepoFile(remoteFile);
        File tempRepoFile = new File(FileUtil.getAbsoluteParentDirectory(repoFile)+File.separator+".temp-"+repoFile.getName());

        // Do not overwrite files!
        if (repoFile.exists()) {
            return;
        }

        // No such local file
        if (!localFile.exists()) {
            throw new LocalFileNotFoundException("No such file on local disk: "+localFile);
        }

        try {
            FileUtil.copy(localFile, tempRepoFile, getConnection().getThrottleKbps());
            tempRepoFile.renameTo(repoFile);
        }
        catch (IOException ex) {
            throw new StorageException("Unable to copy file "+localFile+" to local repository "+repoFile, ex);
        }
    }

    @Override
    public void delete(RemoteFile remoteFile) throws RemoteFileNotFoundException, StorageException {
        connect();

        File repoFile = getRepoFile(remoteFile);

        if (!repoFile.exists()) {
            return;
        }

        repoFile.delete();
    }

    @Override
    public Map<String, RemoteFile> list() throws StorageException {
        return list(null);
    }

    @Override
    public Map<String, RemoteFile> list(final String namePrefix) throws StorageException {
        connect();

        File[] files;

        if (namePrefix == null) {
            files = folder.listFiles();
        }
        else {
            files = folder.listFiles(new FilenameFilter() {
            @Override public boolean accept(File dir, String name) {
                return name.startsWith(namePrefix); }
            });
        }

        if (files == null) {
            throw new StorageException("Unable to read local respository "+folder);
        }

        Map<String, RemoteFile> remoteFiles = new HashMap<String, RemoteFile>();

        for (File file : files) {
            remoteFiles.put(file.getName(), new RemoteFile(file.getName(), file.length(), file));
        }

        return remoteFiles;
    }

    private File getRepoFile(RemoteFile remoteFile) {
        return new File(folder+File.separator+remoteFile.getName());
    }
}