1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 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
;
20 import android
.accounts
.Account
;
21 import android
.content
.Context
;
22 import android
.content
.Intent
;
23 import android
.util
.Log
;
25 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
26 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import com
.owncloud
.android
.files
.services
.FileDownloader
;
28 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
30 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
31 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
32 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
33 import com
.owncloud
.android
.lib
.resources
.files
.FileUtils
;
34 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
35 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFolderOperation
;
36 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
37 import com
.owncloud
.android
.lib
.resources
.shares
.GetRemoteSharesForFileOperation
;
38 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
39 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
40 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
41 import com
.owncloud
.android
.utils
.FileStorageUtils
;
43 import org
.apache
.http
.HttpStatus
;
46 import java
.io
.FileInputStream
;
47 import java
.io
.FileOutputStream
;
48 import java
.io
.IOException
;
49 import java
.io
.InputStream
;
50 import java
.io
.OutputStream
;
51 import java
.util
.ArrayList
;
52 import java
.util
.HashMap
;
53 import java
.util
.List
;
55 import java
.util
.Vector
;
57 //import android.support.v4.content.LocalBroadcastManager;
61 * Remote operation performing the synchronization of the list of files contained
62 * in a folder identified with its remote path.
64 * Fetches the list and properties of the files contained in the given folder, including their
65 * properties, and updates the local database with them.
67 * Does NOT enter in the child folders to synchronize their contents also.
69 * @author David A. Velasco
71 public class SyncFolderOperation
extends SyncOperation
{
73 private static final String TAG
= SyncFolderOperation
.class.getSimpleName();
75 /** Time stamp for the synchronization process in progress */
76 private long mCurrentSyncTime
;
78 /** Remote folder to synchronize */
79 private OCFile mLocalFolder
;
81 /** Access to the local database */
82 private FileDataStorageManager mStorageManager
;
84 /** Account where the file to synchronize belongs */
85 private Account mAccount
;
87 /** Android context; necessary to send requests to the download service */
88 private Context mContext
;
90 /** Files and folders contained in the synchronized folder after a successful operation */
91 private List
<OCFile
> mChildren
;
93 /** Counter of conflicts found between local and remote files */
94 private int mConflictsFound
;
96 /** Counter of failed operations in synchronization of kept-in-sync files */
97 private int mFailsInFavouritesFound
;
100 * Map of remote and local paths to files that where locally stored in a location
101 * out of the ownCloud folder and couldn't be copied automatically into it
103 private Map
<String
, String
> mForgottenLocalFiles
;
105 /** 'True' means that the remote folder changed and should be fetched */
106 private boolean mRemoteFolderChanged
;
110 * Creates a new instance of {@link SyncFolderOperation}.
112 * @param context Application context.
113 * @param remotePath Path to synchronize.
114 * @param account ownCloud account where the folder is located.
115 * @param currentSyncTime Time stamp for the synchronization process in progress.
117 public SyncFolderOperation(Context context
, String remotePath
, Account account
, long currentSyncTime
){
118 mLocalFolder
= new OCFile(remotePath
);
119 mCurrentSyncTime
= currentSyncTime
;
120 mStorageManager
= getStorageManager();
123 mForgottenLocalFiles
= new HashMap
<String
, String
>();
124 mRemoteFolderChanged
= false
;
128 public int getConflictsFound() {
129 return mConflictsFound
;
132 public int getFailsInFavouritesFound() {
133 return mFailsInFavouritesFound
;
136 public Map
<String
, String
> getForgottenLocalFiles() {
137 return mForgottenLocalFiles
;
141 * Returns the list of files and folders contained in the synchronized folder,
142 * if called after synchronization is complete.
144 * @return List of files and folders contained in the synchronized folder.
146 public List
<OCFile
> getChildren() {
151 * Performs the synchronization.
156 protected RemoteOperationResult
run(OwnCloudClient client
) {
157 RemoteOperationResult result
= null
;
158 mFailsInFavouritesFound
= 0;
160 mForgottenLocalFiles
.clear();
162 result
= checkForChanges(client
);
164 if (result
.isSuccess()) {
165 if (mRemoteFolderChanged
) {
166 result
= fetchAndSyncRemoteFolder(client
);
168 mChildren
= mStorageManager
.getFolderContent(mLocalFolder
);
176 private RemoteOperationResult
checkForChanges(OwnCloudClient client
) {
177 mRemoteFolderChanged
= true
;
178 RemoteOperationResult result
= null
;
179 String remotePath
= null
;
181 remotePath
= mLocalFolder
.getRemotePath();
182 Log_OC
.d(TAG
, "Checking changes in " + mAccount
.name
+ remotePath
);
185 ReadRemoteFileOperation operation
= new ReadRemoteFileOperation(remotePath
);
186 result
= operation
.execute(client
);
187 if (result
.isSuccess()){
188 OCFile remoteFolder
= FileStorageUtils
.fillOCFile((RemoteFile
) result
.getData().get(0));
190 // check if remote and local folder are different
191 mRemoteFolderChanged
=
192 !(remoteFolder
.getEtag().equalsIgnoreCase(mLocalFolder
.getEtag()));
194 result
= new RemoteOperationResult(ResultCode
.OK
);
196 Log_OC
.i(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
197 (mRemoteFolderChanged ?
"changed" : "not changed"));
201 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
204 if (result
.isException()) {
205 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
206 result
.getLogMessage(), result
.getException());
208 Log_OC
.e(TAG
, "Checked " + mAccount
.name
+ remotePath
+ " : " +
209 result
.getLogMessage());
217 private RemoteOperationResult
fetchAndSyncRemoteFolder(OwnCloudClient client
) {
218 String remotePath
= mLocalFolder
.getRemotePath();
219 ReadRemoteFolderOperation operation
= new ReadRemoteFolderOperation(remotePath
);
220 RemoteOperationResult result
= operation
.execute(client
);
221 Log_OC
.d(TAG
, "Synchronizing " + mAccount
.name
+ remotePath
);
223 if (result
.isSuccess()) {
224 synchronizeData(result
.getData(), client
);
225 if (mConflictsFound
> 0 || mFailsInFavouritesFound
> 0) {
226 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
227 // should be a different result code, but will do the job
230 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
)
238 private void removeLocalFolder() {
239 if (mStorageManager
.fileExists(mLocalFolder
.getFileId())) {
240 String currentSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
241 mStorageManager
.removeFolder(
244 ( mLocalFolder
.isDown() &&
245 mLocalFolder
.getStoragePath().startsWith(currentSavePath
)
253 * Synchronizes the data retrieved from the server about the contents of the target folder
254 * with the current data in the local database.
256 * Grants that mChildren is updated with fresh data after execution.
258 * @param folderAndFiles Remote folder and children files in Folder
260 * @param client Client instance to the remote server where the data were
262 * @return 'True' when any change was made in the local data, 'false' otherwise
264 private void synchronizeData(ArrayList
<Object
> folderAndFiles
, OwnCloudClient client
) {
265 // get 'fresh data' from the database
266 mLocalFolder
= mStorageManager
.getFileByPath(mLocalFolder
.getRemotePath());
268 // parse data from remote folder
269 OCFile remoteFolder
= fillOCFile((RemoteFile
)folderAndFiles
.get(0));
270 remoteFolder
.setParentId(mLocalFolder
.getParentId());
271 remoteFolder
.setFileId(mLocalFolder
.getFileId());
273 Log_OC
.d(TAG
, "Remote folder " + mLocalFolder
.getRemotePath()
274 + " changed - starting update of local data ");
276 List
<OCFile
> updatedFiles
= new Vector
<OCFile
>(folderAndFiles
.size() - 1);
277 List
<SynchronizeFileOperation
> filesToSyncContents
= new Vector
<SynchronizeFileOperation
>();
279 // get current data about local contents of the folder to synchronize
280 List
<OCFile
> localFiles
= mStorageManager
.getFolderContent(mLocalFolder
);
281 Map
<String
, OCFile
> localFilesMap
= new HashMap
<String
, OCFile
>(localFiles
.size());
282 for (OCFile file
: localFiles
) {
283 localFilesMap
.put(file
.getRemotePath(), file
);
286 // loop to update every child
287 OCFile remoteFile
= null
, localFile
= null
;
288 for (int i
=1; i
<folderAndFiles
.size(); i
++) {
289 /// new OCFile instance with the data from the server
290 remoteFile
= fillOCFile((RemoteFile
)folderAndFiles
.get(i
));
291 remoteFile
.setParentId(mLocalFolder
.getFileId());
293 /// retrieve local data for the read file
294 // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
295 localFile
= localFilesMap
.remove(remoteFile
.getRemotePath());
297 /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in server)
298 remoteFile
.setLastSyncDateForProperties(mCurrentSyncTime
);
299 if (localFile
!= null
) {
300 // some properties of local state are kept unmodified
301 remoteFile
.setFileId(localFile
.getFileId());
302 remoteFile
.setKeepInSync(localFile
.keepInSync());
303 remoteFile
.setLastSyncDateForData(localFile
.getLastSyncDateForData());
304 remoteFile
.setModificationTimestampAtLastSyncForData(
305 localFile
.getModificationTimestampAtLastSyncForData()
307 remoteFile
.setStoragePath(localFile
.getStoragePath());
308 // eTag will not be updated unless contents are synchronized
309 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
310 remoteFile
.setEtag(localFile
.getEtag());
311 if (remoteFile
.isFolder()) {
312 remoteFile
.setFileLength(localFile
.getFileLength());
313 // TODO move operations about size of folders to FileContentProvider
314 } else if (mRemoteFolderChanged
&& remoteFile
.isImage() &&
315 remoteFile
.getModificationTimestamp() != localFile
.getModificationTimestamp()) {
316 remoteFile
.setNeedsUpdateThumbnail(true
);
317 Log
.d(TAG
, "Image " + remoteFile
.getFileName() + " updated on the server");
319 remoteFile
.setPublicLink(localFile
.getPublicLink());
320 remoteFile
.setShareByLink(localFile
.isShareByLink());
322 // remote eTag will not be updated unless contents are synchronized
323 // (Synchronize[File|Folder]Operation with remoteFile as parameter)
324 remoteFile
.setEtag("");
327 /// check and fix, if needed, local storage path
328 checkAndFixForeignStoragePath(remoteFile
); // policy - local files are COPIED
329 // into the ownCloud local folder;
330 searchForLocalFileInDefaultPath(remoteFile
); // legacy
332 /// prepare content synchronization for kept-in-sync files
333 if (remoteFile
.keepInSync()) {
334 SynchronizeFileOperation operation
= new SynchronizeFileOperation( localFile
,
341 filesToSyncContents
.add(operation
);
344 // Start the download of all the files in the folder (non recursively)
345 if (!remoteFile
.isFolder()) {
346 requestForDownloadFile(remoteFile
);
349 updatedFiles
.add(remoteFile
);
352 // save updated contents in local database
353 mStorageManager
.saveFolder(remoteFolder
, updatedFiles
, localFilesMap
.values());
355 // request for the synchronization of file contents AFTER saving current remote properties
356 startContentSynchronizations(filesToSyncContents
, client
);
358 mChildren
= updatedFiles
;
362 * Performs a list of synchronization operations, determining if a download or upload is needed
363 * or if exists conflict due to changes both in local and remote contents of the each file.
365 * If download or upload is needed, request the operation to the corresponding service and goes
368 * @param filesToSyncContents Synchronization operations to execute.
369 * @param client Interface to the remote ownCloud server.
371 private void startContentSynchronizations(
372 List
<SynchronizeFileOperation
> filesToSyncContents
, OwnCloudClient client
374 RemoteOperationResult contentsResult
= null
;
375 for (SynchronizeFileOperation op
: filesToSyncContents
) {
376 contentsResult
= op
.execute(mStorageManager
, mContext
); // async
377 if (!contentsResult
.isSuccess()) {
378 if (contentsResult
.getCode() == ResultCode
.SYNC_CONFLICT
) {
381 mFailsInFavouritesFound
++;
382 if (contentsResult
.getException() != null
) {
383 Log_OC
.e(TAG
, "Error while synchronizing favourites : "
384 + contentsResult
.getLogMessage(), contentsResult
.getException());
386 Log_OC
.e(TAG
, "Error while synchronizing favourites : "
387 + contentsResult
.getLogMessage());
390 } // won't let these fails break the synchronization process
395 public boolean isMultiStatus(int status
) {
396 return (status
== HttpStatus
.SC_MULTI_STATUS
);
400 * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile} object with the data read from the server.
402 * @param remote remote file read from the server (remote file or folder).
403 * @return New OCFile instance representing the remote resource described by we.
405 private OCFile
fillOCFile(RemoteFile remote
) {
406 OCFile file
= new OCFile(remote
.getRemotePath());
407 file
.setCreationTimestamp(remote
.getCreationTimestamp());
408 file
.setFileLength(remote
.getLength());
409 file
.setMimetype(remote
.getMimeType());
410 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
411 file
.setEtag(remote
.getEtag());
412 file
.setPermissions(remote
.getPermissions());
413 file
.setRemoteId(remote
.getRemoteId());
419 * Checks the storage path of the OCFile received as parameter.
420 * If it's out of the local ownCloud folder, tries to copy the file inside it.
422 * If the copy fails, the link to the local file is nullified. The account of forgotten
423 * files is kept in {@link #mForgottenLocalFiles}
425 * @param file File to check and fix.
427 private void checkAndFixForeignStoragePath(OCFile file
) {
428 String storagePath
= file
.getStoragePath();
429 String expectedPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
);
430 if (storagePath
!= null
&& !storagePath
.equals(expectedPath
)) {
431 /// fix storagePaths out of the local ownCloud folder
432 File originalFile
= new File(storagePath
);
433 if (FileStorageUtils
.getUsableSpace(mAccount
.name
) < originalFile
.length()) {
434 mForgottenLocalFiles
.put(file
.getRemotePath(), storagePath
);
435 file
.setStoragePath(null
);
438 InputStream
in = null
;
439 OutputStream out
= null
;
441 File expectedFile
= new File(expectedPath
);
442 File expectedParent
= expectedFile
.getParentFile();
443 expectedParent
.mkdirs();
444 if (!expectedParent
.isDirectory()) {
445 throw new IOException(
446 "Unexpected error: parent directory could not be created"
449 expectedFile
.createNewFile();
450 if (!expectedFile
.isFile()) {
451 throw new IOException("Unexpected error: target file could not be created");
453 in = new FileInputStream(originalFile
);
454 out
= new FileOutputStream(expectedFile
);
455 byte[] buf
= new byte[1024];
457 while ((len
= in.read(buf
)) > 0){
458 out
.write(buf
, 0, len
);
460 file
.setStoragePath(expectedPath
);
462 } catch (Exception e
) {
463 Log_OC
.e(TAG
, "Exception while copying foreign file " + expectedPath
, e
);
464 mForgottenLocalFiles
.put(file
.getRemotePath(), storagePath
);
465 file
.setStoragePath(null
);
469 if (in != null
) in.close();
470 } catch (Exception e
) {
471 Log_OC
.d(TAG
, "Weird exception while closing input stream for "
472 + storagePath
+ " (ignoring)", e
);
475 if (out
!= null
) out
.close();
476 } catch (Exception e
) {
477 Log_OC
.d(TAG
, "Weird exception while closing output stream for "
478 + expectedPath
+ " (ignoring)", e
);
487 * Scans the default location for saving local copies of files searching for
488 * a 'lost' file with the same full name as the {@link com.owncloud.android.datamodel.OCFile} received as
491 * @param file File to associate a possible 'lost' local file.
493 private void searchForLocalFileInDefaultPath(OCFile file
) {
494 if (file
.getStoragePath() == null
&& !file
.isFolder()) {
495 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
497 file
.setStoragePath(f
.getAbsolutePath());
498 file
.setLastSyncDateForData(f
.lastModified());
504 * Requests for a download to the FileDownloader service
506 * @param file OCFile object representing the file to download
508 private void requestForDownloadFile(OCFile file
) {
509 Intent i
= new Intent(mContext
, FileDownloader
.class);
510 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
511 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
512 mContext
.startService(i
);
515 public boolean getRemoteFolderChanged() {
516 return mRemoteFolderChanged
;