~nicoulaj/syncany/maven-local-repo

« back to all changes in this revision

Viewing changes to syncany/src/org/syncany/connection/plugins/local/LocalTransferManager.java

  • Committer: Philipp Heckel
  • Date: 2011-06-27 13:38:42 UTC
  • Revision ID: philipp.heckel@gmail.com-20110627133842-mo606nxt0d3e7i11
meta-chunking stuff; still not properly functioning; XMPP notification improved but not integrated yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
    public LocalTransferManager(LocalConnection connection) {
41
41
        super(connection);
42
 
    folder = connection.getFolder();
 
42
        folder = connection.getFolder();
43
43
    }
44
44
 
45
45
    @Override
49
49
 
50
50
    @Override
51
51
    public void connect() throws StorageConnectException {
52
 
    if (folder == null || !folder.exists() || !folder.canRead() || !folder.canWrite() || !folder.isDirectory())
53
 
        throw new StorageConnectException("Repository folder '"+folder+"' does not exist or is not writable.");
 
52
        if (folder == null || !folder.exists() || !folder.canRead() || !folder.canWrite() || !folder.isDirectory()) {
 
53
            throw new StorageConnectException("Repository folder '"+folder+"' does not exist or is not writable.");
 
54
        }
54
55
    }
55
56
 
56
57
    @Override
57
58
    public void disconnect() throws StorageException {
58
 
    // Fressen.
 
59
        // Nothing.
59
60
    }
60
61
 
61
62
    @Override
62
63
    public void download(RemoteFile remoteFile, File localFile) throws RemoteFileNotFoundException, StorageException {
63
 
    connect();
64
 
  
65
 
    File repoFile = getRepoFile(remoteFile);
66
 
 
67
 
    if (!repoFile.exists())
68
 
        throw new RemoteFileNotFoundException("No such file in local repository: "+repoFile);
69
 
 
70
 
    try {
71
 
        File tempLocalFile = config.getCache().createTempFile();
72
 
 
73
 
        FileUtil.copy(repoFile, tempLocalFile, getConnection().getThrottleKbps());
74
 
 
75
 
        // SNM 6/01/11 windows doesn't support rename *onto* another file
76
 
        if(localFile.exists()){
77
 
            localFile.delete();
78
 
        }
79
 
        tempLocalFile.renameTo(localFile);
80
 
    }
81
 
    catch (IOException ex) {
82
 
        throw new StorageException("Unable to copy file "+repoFile+" from local repository to "+localFile, ex);
83
 
    }
 
64
        connect();
 
65
 
 
66
        File repoFile = getRepoFile(remoteFile);
 
67
 
 
68
        if (!repoFile.exists()) {
 
69
            throw new RemoteFileNotFoundException("No such file in local repository: "+repoFile);
 
70
        }  
 
71
 
 
72
        try {
 
73
            File tempLocalFile = config.getCache().createTempFile("local-tm-download");
 
74
            tempLocalFile.deleteOnExit();
 
75
 
 
76
            FileUtil.copy(repoFile, tempLocalFile, getConnection().getThrottleKbps());
 
77
 
 
78
            // SNM 6/01/11 windows doesn't support rename *onto* another file
 
79
            if (localFile.exists()){
 
80
                localFile.delete();
 
81
            }
 
82
            
 
83
            if (!tempLocalFile.renameTo(localFile)) {
 
84
                throw new StorageException("Unable to move temp local file "+tempLocalFile+" to "+localFile);
 
85
            }
 
86
            
 
87
            tempLocalFile.delete();
 
88
        }
 
89
        catch (IOException ex) {
 
90
            throw new StorageException("Unable to copy file "+repoFile+" from local repository to "+localFile, ex);
 
91
        }
84
92
    }
85
93
 
86
94
    @Override
87
95
    public void upload(File localFile, RemoteFile remoteFile) throws LocalFileNotFoundException, StorageException {
88
 
    connect();
 
96
        connect();
89
97
 
90
 
    File repoFile = getRepoFile(remoteFile);
91
 
    File tempRepoFile = new File(FileUtil.getAbsoluteParentDirectory(repoFile)+File.separator+".temp-"+repoFile.getName());
 
98
        File repoFile = getRepoFile(remoteFile);
 
99
        File tempRepoFile = new File(FileUtil.getAbsoluteParentDirectory(repoFile)+File.separator+".temp-"+repoFile.getName());
92
100
 
93
101
        // Do not overwrite files!
94
 
    if (repoFile.exists())
95
 
        return;
 
102
        if (repoFile.exists()) {
 
103
            return;
 
104
        }
96
105
 
97
106
        // No such local file
98
 
    if (!localFile.exists())
99
 
        throw new LocalFileNotFoundException("No such file on local disk: "+localFile);
 
107
        if (!localFile.exists()) {
 
108
            throw new LocalFileNotFoundException("No such file on local disk: "+localFile);
 
109
        }
100
110
 
101
 
    try {
102
 
        FileUtil.copy(localFile, tempRepoFile, getConnection().getThrottleKbps());
103
 
        tempRepoFile.renameTo(repoFile);
104
 
    }
105
 
    catch (IOException ex) {
106
 
        throw new StorageException("Unable to copy file "+localFile+" to local repository "+repoFile, ex);
107
 
    }
 
111
        try {
 
112
            FileUtil.copy(localFile, tempRepoFile, getConnection().getThrottleKbps());
 
113
            tempRepoFile.renameTo(repoFile);
 
114
        }
 
115
        catch (IOException ex) {
 
116
            throw new StorageException("Unable to copy file "+localFile+" to local repository "+repoFile, ex);
 
117
        }
108
118
    }
109
119
 
110
120
    @Override
111
121
    public void delete(RemoteFile remoteFile) throws RemoteFileNotFoundException, StorageException {
112
 
    connect();
113
 
 
114
 
    File repoFile = getRepoFile(remoteFile);
115
 
 
116
 
    if (!repoFile.exists())
117
 
        return;
118
 
 
119
 
    repoFile.delete();
 
122
        connect();
 
123
 
 
124
        File repoFile = getRepoFile(remoteFile);
 
125
 
 
126
        if (!repoFile.exists()) {
 
127
            return;
 
128
        }
 
129
 
 
130
        repoFile.delete();
120
131
    }
121
132
 
122
133
    @Override
126
137
 
127
138
    @Override
128
139
    public Map<String, RemoteFile> list(final String namePrefix) throws StorageException {
129
 
    connect();
130
 
        
131
 
    File[] files;
132
 
 
133
 
    if (namePrefix == null) {
134
 
        files = folder.listFiles();
135
 
    }
136
 
    else {
137
 
        files = folder.listFiles(new FilenameFilter() {
138
 
        @Override public boolean accept(File dir, String name) {
139
 
                    return name.startsWith(namePrefix); }
140
 
        });
141
 
    }
142
 
 
143
 
    if (files == null)
144
 
        throw new StorageException("Unable to read local respository "+folder);
145
 
 
146
 
    Map<String, RemoteFile> remoteFiles = new HashMap<String, RemoteFile>();
147
 
 
148
 
    for (File file : files)
149
 
        remoteFiles.put(file.getName(), new RemoteFile(file.getName(), file.length(), file));
 
140
        connect();
 
141
 
 
142
        File[] files;
 
143
 
 
144
        if (namePrefix == null) {
 
145
            files = folder.listFiles();
 
146
        }
 
147
        else {
 
148
            files = folder.listFiles(new FilenameFilter() {
 
149
            @Override public boolean accept(File dir, String name) {
 
150
                return name.startsWith(namePrefix); }
 
151
            });
 
152
        }
 
153
 
 
154
        if (files == null) {
 
155
            throw new StorageException("Unable to read local respository "+folder);
 
156
        }
 
157
 
 
158
        Map<String, RemoteFile> remoteFiles = new HashMap<String, RemoteFile>();
 
159
 
 
160
        for (File file : files) {
 
161
            remoteFiles.put(file.getName(), new RemoteFile(file.getName(), file.length(), file));
 
162
        }
150
163
 
151
164
        return remoteFiles;
152
165
    }