1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
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
;
29 import java
.util
.Vector
;
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
;
36 import android
.accounts
.Account
;
37 import android
.content
.Context
;
38 import android
.content
.Intent
;
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
;
56 * Remote operation performing the synchronization of the list of files contained
57 * in a folder identified with its remote path.
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.
62 * Does NOT enter in the child folders to synchronize their contents also.
64 * @author David A. Velasco
66 public class SynchronizeFolderOperation
extends RemoteOperation
{
68 private static final String TAG
= SynchronizeFolderOperation
.class.getSimpleName();
71 /** Time stamp for the synchronization process in progress */
72 private long mCurrentSyncTime
;
74 /** Remote folder to synchronize */
75 private OCFile mLocalFolder
;
77 /** Access to the local database */
78 private FileDataStorageManager mStorageManager
;
80 /** Account where the file to synchronize belongs */
81 private Account mAccount
;
83 /** Android context; necessary to send requests to the download service */
84 private Context mContext
;
86 /** Files and folders contained in the synchronized folder after a successful operation */
87 private List
<OCFile
> mChildren
;
89 /** Counter of conflicts found between local and remote files */
90 private int mConflictsFound
;
92 /** Counter of failed operations in synchronization of kept-in-sync files */
93 private int mFailsInFavouritesFound
;
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
;
98 /** 'True' means that this operation is part of a full account synchronization */
99 private boolean mSyncFullAccount
;
101 /** 'True' means that the remote folder changed from last synchronization and should be fetched */
102 private boolean mRemoteFolderChanged
;
106 * Creates a new instance of {@link SynchronizeFolderOperation}.
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.
117 public SynchronizeFolderOperation( OCFile folder
,
118 long currentSyncTime
,
119 boolean syncFullAccount
,
120 FileDataStorageManager dataStorageManager
,
123 mLocalFolder
= folder
;
124 mCurrentSyncTime
= currentSyncTime
;
125 mSyncFullAccount
= syncFullAccount
;
126 mStorageManager
= dataStorageManager
;
129 mForgottenLocalFiles
= new HashMap
<String
, String
>();
130 mRemoteFolderChanged
= false
;
134 public int getConflictsFound() {
135 return mConflictsFound
;
138 public int getFailsInFavouritesFound() {
139 return mFailsInFavouritesFound
;
142 public Map
<String
, String
> getForgottenLocalFiles() {
143 return mForgottenLocalFiles
;
147 * Returns the list of files and folders contained in the synchronized folder, if called after synchronization is complete.
149 * @return List of files and folders contained in the synchronized folder.
151 public List
<OCFile
> getChildren() {
156 * Performs the synchronization.
161 protected RemoteOperationResult
run(WebdavClient client
) {
162 RemoteOperationResult result
= null
;
163 mFailsInFavouritesFound
= 0;
165 mForgottenLocalFiles
.clear();
167 result
= checkForChanges(client
);
169 if (result
.isSuccess()) {
170 if (mRemoteFolderChanged
) {
171 result
= fetchAndSyncRemoteFolder(client
);
173 mChildren
= mStorageManager
.getFolderContent(mLocalFolder
);
177 if (!mSyncFullAccount
) {
178 sendStickyBroadcast(false
, mLocalFolder
.getRemotePath(), result
);
186 private RemoteOperationResult
checkForChanges(WebdavClient client
) {
187 mRemoteFolderChanged
= false
;
188 RemoteOperationResult result
= null
;
189 String remotePath
= null
;
190 PropFindMethod query
= null
;
193 remotePath
= mLocalFolder
.getRemotePath();
194 Log_OC
.d(TAG
, "Checking changes in " + mAccount
.name
+ remotePath
);
197 query
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(remotePath
),
198 DavConstants
.PROPFIND_ALL_PROP
,
199 DavConstants
.DEPTH_0
);
200 int status
= client
.executeMethod(query
);
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
);
208 // check if remote and local folder are different
209 mRemoteFolderChanged
= !(remoteFolder
.getEtag().equalsIgnoreCase(mLocalFolder
.getEtag()));
211 result
= new RemoteOperationResult(ResultCode
.OK
);
215 client
.exhaustResponse(query
.getResponseBodyAsStream());
216 if (status
== HttpStatus
.SC_NOT_FOUND
) {
219 result
= new RemoteOperationResult(false
, status
, query
.getResponseHeaders());
222 } catch (Exception e
) {
223 result
= new RemoteOperationResult(e
);
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"));
232 if (result
.isException()) {
233 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " + result
.getLogMessage(), result
.getException());
235 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " + result
.getLogMessage());
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
);
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
257 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
)
261 // RemoteOperationResult result = null;
262 // String remotePath = null;
263 // PropFindMethod query = null;
265 // remotePath = mLocalFolder.getRemotePath();
266 // Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
269 // query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath),
270 // DavConstants.PROPFIND_ALL_PROP,
271 // DavConstants.DEPTH_1);
272 // int status = client.executeMethod(query);
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
280 // result = new RemoteOperationResult(true, status, query.getResponseHeaders());
284 // // synchronization failed
285 // client.exhaustResponse(query.getResponseBodyAsStream());
286 // if (status == HttpStatus.SC_NOT_FOUND) {
287 // removeLocalFolder();
289 // result = new RemoteOperationResult(false, status, query.getResponseHeaders());
292 // } catch (Exception e) {
293 // result = new RemoteOperationResult(e);
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());
302 // if (result.isException()) {
303 // Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage(), result.getException());
305 // Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage());
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)
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
)));
337 * Synchronizes the data retrieved from the server about the contents of the target folder
338 * with the current data in the local database.
340 * Grants that mChildren is updated with fresh data after execution.
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
346 * @return 'True' when any change was made in the local data, 'false' otherwise.
348 private void synchronizeData(MultiStatus dataInServer
, WebdavClient client
) {
349 // get 'fresh data' from the database
350 mLocalFolder
= mStorageManager
.getFileByPath(mLocalFolder
.getRemotePath());
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());
358 Log_OC
.d(TAG
, "Remote folder " + mLocalFolder
.getRemotePath() + " changed - starting update of local data ");
360 List
<OCFile
> updatedFiles
= new Vector
<OCFile
>(dataInServer
.getResponses().length
- 1);
361 List
<SynchronizeFileOperation
> filesToSyncContents
= new Vector
<SynchronizeFileOperation
>();
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
);
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());
378 /// retrieve local data for the read file
379 //localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
380 localFile
= localFilesMap
.remove(remoteFile
.getRemotePath());
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
396 remoteFile
.setEtag(""); // remote eTag will not be updated unless contents are synchronized (Synchronize[File|Folder]Operation with remoteFile as parameter)
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
403 /// prepare content synchronization for kept-in-sync files
404 if (remoteFile
.keepInSync()) {
405 SynchronizeFileOperation operation
= new SynchronizeFileOperation( localFile
,
412 filesToSyncContents
.add(operation
);
415 updatedFiles
.add(remoteFile
);
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());
421 // request for the synchronization of file contents AFTER saving current remote properties
422 startContentSynchronizations(filesToSyncContents
, client
);
424 // removal of obsolete files
425 //removeObsoleteFiles();
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
;
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.
437 * If download or upload is needed, request the operation to the corresponding service and goes on.
439 * @param filesToSyncContents Synchronization operations to execute.
440 * @param client Interface to the remote ownCloud server.
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
) {
450 mFailsInFavouritesFound
++;
451 if (contentsResult
.getException() != null
) {
452 Log_OC
.e(TAG
, "Error while synchronizing favourites : " + contentsResult
.getLogMessage(), contentsResult
.getException());
454 Log_OC
.e(TAG
, "Error while synchronizing favourites : " + contentsResult
.getLogMessage());
457 } // won't let these fails break the synchronization process
462 public boolean isMultiStatus(int status
) {
463 return (status
== HttpStatus
.SC_MULTI_STATUS
);
468 * Creates and populates a new {@link OCFile} object with the data read from the server.
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.
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());
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.
488 * If the copy fails, the link to the local file is nullified. The account of forgotten files is kept in
489 * {@link #mForgottenLocalFiles}
491 * @param file File to check and fix.
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
);
504 InputStream
in = null
;
505 OutputStream out
= null
;
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");
513 expectedFile
.createNewFile();
514 if (!expectedFile
.isFile()) {
515 throw new IOException("Unexpected error: target file could not be created");
517 in = new FileInputStream(originalFile
);
518 out
= new FileOutputStream(expectedFile
);
519 byte[] buf
= new byte[1024];
521 while ((len
= in.read(buf
)) > 0){
522 out
.write(buf
, 0, len
);
524 file
.setStoragePath(expectedPath
);
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
);
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
);
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
);
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
552 * @param file File to associate a possible 'lost' local file.
554 private void searchForLocalFileInDefaultPath(OCFile file
) {
555 if (file
.getStoragePath() == null
&& !file
.isFolder()) {
556 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
558 file
.setStoragePath(f
.getAbsolutePath());
559 file
.setLastSyncDateForData(f
.lastModified());
566 * Sends a message to any application component interested in the progress of the synchronization.
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)
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
);
578 if (result
!= null
) {
579 i
.putExtra(FileSyncService
.SYNC_RESULT
, result
);
581 mContext
.sendStickyBroadcast(i
);
585 public boolean getRemoteFolderChanged() {
586 return mRemoteFolderChanged
;