450cc3edd048514a1ad1f0ee7c134bac3e1fdf2c
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFolderOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Vector;
30
31 import org.apache.http.HttpStatus;
32 import org.apache.jackrabbit.webdav.DavConstants;
33 import org.apache.jackrabbit.webdav.MultiStatus;
34 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
35
36 import android.accounts.Account;
37 import android.content.Context;
38 import android.content.Intent;
39
40 import com.owncloud.android.datamodel.FileDataStorageManager;
41 import com.owncloud.android.datamodel.OCFile;
42 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
43 import com.owncloud.android.oc_framework.network.webdav.WebdavEntry;
44 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
45 import com.owncloud.android.oc_framework.operations.RemoteOperation;
46 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
47 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
48 import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFileOperation;
49 import com.owncloud.android.syncadapter.FileSyncService;
50 import com.owncloud.android.utils.FileStorageUtils;
51 import com.owncloud.android.utils.Log_OC;
52
53
54
55 /**
56 * Remote operation performing the synchronization of the list of files contained
57 * in a folder identified with its remote path.
58 *
59 * Fetches the list and properties of the files contained in the given folder, including their
60 * properties, and updates the local database with them.
61 *
62 * Does NOT enter in the child folders to synchronize their contents also.
63 *
64 * @author David A. Velasco
65 */
66 public class SynchronizeFolderOperation extends RemoteOperation {
67
68 private static final String TAG = SynchronizeFolderOperation.class.getSimpleName();
69
70
71 /** Time stamp for the synchronization process in progress */
72 private long mCurrentSyncTime;
73
74 /** Remote folder to synchronize */
75 private OCFile mLocalFolder;
76
77 /** Access to the local database */
78 private FileDataStorageManager mStorageManager;
79
80 /** Account where the file to synchronize belongs */
81 private Account mAccount;
82
83 /** Android context; necessary to send requests to the download service */
84 private Context mContext;
85
86 /** Files and folders contained in the synchronized folder after a successful operation */
87 private List<OCFile> mChildren;
88
89 /** Counter of conflicts found between local and remote files */
90 private int mConflictsFound;
91
92 /** Counter of failed operations in synchronization of kept-in-sync files */
93 private int mFailsInFavouritesFound;
94
95 /** Map of remote and local paths to files that where locally stored in a location out of the ownCloud folder and couldn't be copied automatically into it */
96 private Map<String, String> mForgottenLocalFiles;
97
98 /** 'True' means that this operation is part of a full account synchronization */
99 private boolean mSyncFullAccount;
100
101 /** 'True' means that the remote folder changed from last synchronization and should be fetched */
102 private boolean mRemoteFolderChanged;
103
104
105 /**
106 * Creates a new instance of {@link SynchronizeFolderOperation}.
107 *
108 * @param remoteFolderPath Remote folder to synchronize.
109 * @param currentSyncTime Time stamp for the synchronization process in progress.
110 * @param localFolderId Identifier in the local database of the folder to synchronize.
111 * @param updateFolderProperties 'True' means that the properties of the folder should be updated also, not just its content.
112 * @param syncFullAccount 'True' means that this operation is part of a full account synchronization.
113 * @param dataStorageManager Interface with the local database.
114 * @param account ownCloud account where the folder is located.
115 * @param context Application context.
116 */
117 public SynchronizeFolderOperation( OCFile folder,
118 long currentSyncTime,
119 boolean syncFullAccount,
120 FileDataStorageManager dataStorageManager,
121 Account account,
122 Context context ) {
123 mLocalFolder = folder;
124 mCurrentSyncTime = currentSyncTime;
125 mSyncFullAccount = syncFullAccount;
126 mStorageManager = dataStorageManager;
127 mAccount = account;
128 mContext = context;
129 mForgottenLocalFiles = new HashMap<String, String>();
130 mRemoteFolderChanged = false;
131 }
132
133
134 public int getConflictsFound() {
135 return mConflictsFound;
136 }
137
138 public int getFailsInFavouritesFound() {
139 return mFailsInFavouritesFound;
140 }
141
142 public Map<String, String> getForgottenLocalFiles() {
143 return mForgottenLocalFiles;
144 }
145
146 /**
147 * Returns the list of files and folders contained in the synchronized folder, if called after synchronization is complete.
148 *
149 * @return List of files and folders contained in the synchronized folder.
150 */
151 public List<OCFile> getChildren() {
152 return mChildren;
153 }
154
155 /**
156 * Performs the synchronization.
157 *
158 * {@inheritDoc}
159 */
160 @Override
161 protected RemoteOperationResult run(WebdavClient client) {
162 RemoteOperationResult result = null;
163 mFailsInFavouritesFound = 0;
164 mConflictsFound = 0;
165 mForgottenLocalFiles.clear();
166
167 result = checkForChanges(client);
168
169 if (result.isSuccess()) {
170 if (mRemoteFolderChanged) {
171 result = fetchAndSyncRemoteFolder(client);
172 } else {
173 mChildren = mStorageManager.getFolderContent(mLocalFolder);
174 }
175 }
176
177 if (!mSyncFullAccount) {
178 sendStickyBroadcast(false, mLocalFolder.getRemotePath(), result);
179 }
180
181 return result;
182
183 }
184
185
186 private RemoteOperationResult checkForChanges(WebdavClient client) {
187 mRemoteFolderChanged = false;
188 RemoteOperationResult result = null;
189 String remotePath = null;
190 PropFindMethod query = null;
191
192 try {
193 remotePath = mLocalFolder.getRemotePath();
194 Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
195
196 // remote request
197 query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath),
198 DavConstants.PROPFIND_ALL_PROP,
199 DavConstants.DEPTH_0);
200 int status = client.executeMethod(query);
201
202 // check and process response
203 if (isMultiStatus(status)) {
204 // parse data from remote folder
205 WebdavEntry we = new WebdavEntry(query.getResponseBodyAsMultiStatus().getResponses()[0], client.getBaseUri().getPath());
206 OCFile remoteFolder = fillOCFile(we);
207
208 // check if remote and local folder are different
209 mRemoteFolderChanged = !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
210
211 result = new RemoteOperationResult(ResultCode.OK);
212
213 } else {
214 // check failed
215 client.exhaustResponse(query.getResponseBodyAsStream());
216 if (status == HttpStatus.SC_NOT_FOUND) {
217 removeLocalFolder();
218 }
219 result = new RemoteOperationResult(false, status, query.getResponseHeaders());
220 }
221
222 } catch (Exception e) {
223 result = new RemoteOperationResult(e);
224
225
226 } finally {
227 if (query != null)
228 query.releaseConnection(); // let the connection available for other methods
229 if (result.isSuccess()) {
230 Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " + (mRemoteFolderChanged ? "changed" : "not changed"));
231 } else {
232 if (result.isException()) {
233 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage(), result.getException());
234 } else {
235 Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage());
236 }
237 }
238
239 }
240 return result;
241 }
242
243
244 private RemoteOperationResult fetchAndSyncRemoteFolder(WebdavClient client) {
245 String remotePath = mLocalFolder.getRemotePath();
246 ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath);
247 RemoteOperationResult result = operation.execute(client);
248 Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
249
250 if (result.isSuccess()) {
251 MultiStatus dataInServer = ((ReadRemoteFileOperation) operation).getDataInServer();
252 synchronizeData(dataInServer, client);
253 if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
254 result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); // should be different result, but will do the job
255 }
256 } else {
257 if (result.getCode() == ResultCode.FILE_NOT_FOUND)
258 removeLocalFolder();
259 }
260
261 // RemoteOperationResult result = null;
262 // String remotePath = null;
263 // PropFindMethod query = null;
264 // try {
265 // remotePath = mLocalFolder.getRemotePath();
266 // Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
267 //
268 // // remote request
269 // query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath),
270 // DavConstants.PROPFIND_ALL_PROP,
271 // DavConstants.DEPTH_1);
272 // int status = client.executeMethod(query);
273 //
274 // // check and process response
275 // if (isMultiStatus(status)) {
276 // synchronizeData(query.getResponseBodyAsMultiStatus(), client);
277 // if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
278 // result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); // should be different result, but will do the job
279 // } else {
280 // result = new RemoteOperationResult(true, status, query.getResponseHeaders());
281 // }
282 //
283 // } else {
284 // // synchronization failed
285 // client.exhaustResponse(query.getResponseBodyAsStream());
286 // if (status == HttpStatus.SC_NOT_FOUND) {
287 // removeLocalFolder();
288 // }
289 // result = new RemoteOperationResult(false, status, query.getResponseHeaders());
290 // }
291 //
292 // } catch (Exception e) {
293 // result = new RemoteOperationResult(e);
294 //
295 //
296 // } finally {
297 // if (query != null)
298 // query.releaseConnection(); // let the connection available for other methods
299 // if (result.isSuccess()) {
300 // Log_OC.i(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage());
301 // } else {
302 // if (result.isException()) {
303 // Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage(), result.getException());
304 // } else {
305 // Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage());
306 // }
307 // }
308 //
309 // }
310 return result;
311 }
312
313
314 // @Override
315 // public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
316 // if (operation instanceof ReadRemoteFileOperation) {
317 // if (result.isSuccess()) {
318 // MultiStatus dataInServer = ((ReadRemoteFileOperation) operation).getDataInServer();
319 // synchronizeData(dataInServer, client)
320 // } else {
321 //
322 // }
323 //
324 // }
325 //
326 // }
327
328 private void removeLocalFolder() {
329 if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
330 String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
331 mStorageManager.removeFolder(mLocalFolder, true, (mLocalFolder.isDown() && mLocalFolder.getStoragePath().startsWith(currentSavePath)));
332 }
333 }
334
335
336 /**
337 * Synchronizes the data retrieved from the server about the contents of the target folder
338 * with the current data in the local database.
339 *
340 * Grants that mChildren is updated with fresh data after execution.
341 *
342 * @param dataInServer Full response got from the server with the data of the target
343 * folder and its direct children.
344 * @param client Client instance to the remote server where the data were
345 * retrieved.
346 * @return 'True' when any change was made in the local data, 'false' otherwise.
347 */
348 private void synchronizeData(MultiStatus dataInServer, WebdavClient client) {
349 // get 'fresh data' from the database
350 mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
351
352 // parse data from remote folder
353 WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getBaseUri().getPath());
354 OCFile remoteFolder = fillOCFile(we);
355 remoteFolder.setParentId(mLocalFolder.getParentId());
356 remoteFolder.setFileId(mLocalFolder.getFileId());
357
358 Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
359
360 List<OCFile> updatedFiles = new Vector<OCFile>(dataInServer.getResponses().length - 1);
361 List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
362
363 // get current data about local contents of the folder to synchronize
364 List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder);
365 Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
366 for (OCFile file : localFiles) {
367 localFilesMap.put(file.getRemotePath(), file);
368 }
369
370 // loop to update every child
371 OCFile remoteFile = null, localFile = null;
372 for (int i = 1; i < dataInServer.getResponses().length; ++i) {
373 /// new OCFile instance with the data from the server
374 we = new WebdavEntry(dataInServer.getResponses()[i], client.getBaseUri().getPath());
375 remoteFile = fillOCFile(we);
376 remoteFile.setParentId(mLocalFolder.getFileId());
377
378 /// retrieve local data for the read file
379 //localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
380 localFile = localFilesMap.remove(remoteFile.getRemotePath());
381
382 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in the server side)
383 remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
384 if (localFile != null) {
385 // some properties of local state are kept unmodified
386 remoteFile.setFileId(localFile.getFileId());
387 remoteFile.setKeepInSync(localFile.keepInSync());
388 remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
389 remoteFile.setModificationTimestampAtLastSyncForData(localFile.getModificationTimestampAtLastSyncForData());
390 remoteFile.setStoragePath(localFile.getStoragePath());
391 remoteFile.setEtag(localFile.getEtag()); // eTag will not be updated unless contents are synchronized (Synchronize[File|Folder]Operation with remoteFile as parameter)
392 if (remoteFile.isFolder()) {
393 remoteFile.setFileLength(localFile.getFileLength()); // TODO move operations about size of folders to FileContentProvider
394 }
395 } else {
396 remoteFile.setEtag(""); // remote eTag will not be updated unless contents are synchronized (Synchronize[File|Folder]Operation with remoteFile as parameter)
397 }
398
399 /// check and fix, if needed, local storage path
400 checkAndFixForeignStoragePath(remoteFile); // fixing old policy - now local files must be copied into the ownCloud local folder
401 searchForLocalFileInDefaultPath(remoteFile); // legacy
402
403 /// prepare content synchronization for kept-in-sync files
404 if (remoteFile.keepInSync()) {
405 SynchronizeFileOperation operation = new SynchronizeFileOperation( localFile,
406 remoteFile,
407 mStorageManager,
408 mAccount,
409 true,
410 mContext
411 );
412 filesToSyncContents.add(operation);
413 }
414
415 updatedFiles.add(remoteFile);
416 }
417
418 // save updated contents in local database; all at once, trying to get a best performance in database update (not a big deal, indeed)
419 mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
420
421 // request for the synchronization of file contents AFTER saving current remote properties
422 startContentSynchronizations(filesToSyncContents, client);
423
424 // removal of obsolete files
425 //removeObsoleteFiles();
426
427 // must be done AFTER saving all the children information, so that eTag is not updated in the database in case of unexpected exceptions
428 //mStorageManager.saveFile(remoteFolder);
429 mChildren = updatedFiles;
430
431 }
432
433 /**
434 * Performs a list of synchronization operations, determining if a download or upload is needed or
435 * if exists conflict due to changes both in local and remote contents of the each file.
436 *
437 * If download or upload is needed, request the operation to the corresponding service and goes on.
438 *
439 * @param filesToSyncContents Synchronization operations to execute.
440 * @param client Interface to the remote ownCloud server.
441 */
442 private void startContentSynchronizations(List<SynchronizeFileOperation> filesToSyncContents, WebdavClient client) {
443 RemoteOperationResult contentsResult = null;
444 for (SynchronizeFileOperation op: filesToSyncContents) {
445 contentsResult = op.execute(client); // returns without waiting for upload or download finishes
446 if (!contentsResult.isSuccess()) {
447 if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
448 mConflictsFound++;
449 } else {
450 mFailsInFavouritesFound++;
451 if (contentsResult.getException() != null) {
452 Log_OC.e(TAG, "Error while synchronizing favourites : " + contentsResult.getLogMessage(), contentsResult.getException());
453 } else {
454 Log_OC.e(TAG, "Error while synchronizing favourites : " + contentsResult.getLogMessage());
455 }
456 }
457 } // won't let these fails break the synchronization process
458 }
459 }
460
461
462 public boolean isMultiStatus(int status) {
463 return (status == HttpStatus.SC_MULTI_STATUS);
464 }
465
466
467 /**
468 * Creates and populates a new {@link OCFile} object with the data read from the server.
469 *
470 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
471 * @return New OCFile instance representing the remote resource described by we.
472 */
473 private OCFile fillOCFile(WebdavEntry we) {
474 OCFile file = new OCFile(we.decodedPath());
475 file.setCreationTimestamp(we.createTimestamp());
476 file.setFileLength(we.contentLength());
477 file.setMimetype(we.contentType());
478 file.setModificationTimestamp(we.modifiedTimestamp());
479 file.setEtag(we.etag());
480 return file;
481 }
482
483
484 /**
485 * Checks the storage path of the OCFile received as parameter. If it's out of the local ownCloud folder,
486 * tries to copy the file inside it.
487 *
488 * If the copy fails, the link to the local file is nullified. The account of forgotten files is kept in
489 * {@link #mForgottenLocalFiles}
490 *)
491 * @param file File to check and fix.
492 */
493 private void checkAndFixForeignStoragePath(OCFile file) {
494 String storagePath = file.getStoragePath();
495 String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
496 if (storagePath != null && !storagePath.equals(expectedPath)) {
497 /// fix storagePaths out of the local ownCloud folder
498 File originalFile = new File(storagePath);
499 if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
500 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
501 file.setStoragePath(null);
502
503 } else {
504 InputStream in = null;
505 OutputStream out = null;
506 try {
507 File expectedFile = new File(expectedPath);
508 File expectedParent = expectedFile.getParentFile();
509 expectedParent.mkdirs();
510 if (!expectedParent.isDirectory()) {
511 throw new IOException("Unexpected error: parent directory could not be created");
512 }
513 expectedFile.createNewFile();
514 if (!expectedFile.isFile()) {
515 throw new IOException("Unexpected error: target file could not be created");
516 }
517 in = new FileInputStream(originalFile);
518 out = new FileOutputStream(expectedFile);
519 byte[] buf = new byte[1024];
520 int len;
521 while ((len = in.read(buf)) > 0){
522 out.write(buf, 0, len);
523 }
524 file.setStoragePath(expectedPath);
525
526 } catch (Exception e) {
527 Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e);
528 mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
529 file.setStoragePath(null);
530
531 } finally {
532 try {
533 if (in != null) in.close();
534 } catch (Exception e) {
535 Log_OC.d(TAG, "Weird exception while closing input stream for " + storagePath + " (ignoring)", e);
536 }
537 try {
538 if (out != null) out.close();
539 } catch (Exception e) {
540 Log_OC.d(TAG, "Weird exception while closing output stream for " + expectedPath + " (ignoring)", e);
541 }
542 }
543 }
544 }
545 }
546
547 /**
548 * Scans the default location for saving local copies of files searching for
549 * a 'lost' file with the same full name as the {@link OCFile} received as
550 * parameter.
551 *
552 * @param file File to associate a possible 'lost' local file.
553 */
554 private void searchForLocalFileInDefaultPath(OCFile file) {
555 if (file.getStoragePath() == null && !file.isFolder()) {
556 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
557 if (f.exists()) {
558 file.setStoragePath(f.getAbsolutePath());
559 file.setLastSyncDateForData(f.lastModified());
560 }
561 }
562 }
563
564
565 /**
566 * Sends a message to any application component interested in the progress of the synchronization.
567 *
568 * @param inProgress 'True' when the synchronization progress is not finished.
569 * @param dirRemotePath Remote path of a folder that was just synchronized (with or without success)
570 */
571 private void sendStickyBroadcast(boolean inProgress, String dirRemotePath, RemoteOperationResult result) {
572 Intent i = new Intent(FileSyncService.getSyncMessage());
573 i.putExtra(FileSyncService.IN_PROGRESS, inProgress);
574 i.putExtra(FileSyncService.ACCOUNT_NAME, mAccount.name);
575 if (dirRemotePath != null) {
576 i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);
577 }
578 if (result != null) {
579 i.putExtra(FileSyncService.SYNC_RESULT, result);
580 }
581 mContext.sendStickyBroadcast(i);
582 }
583
584
585 public boolean getRemoteFolderChanged() {
586 return mRemoteFolderChanged;
587 }
588
589 }