SynchronizeFolderOperation.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2014 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. package com.owncloud.android.operations;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Vector;
  29. import org.apache.http.HttpStatus;
  30. import android.accounts.Account;
  31. import android.content.Context;
  32. import android.content.Intent;
  33. //import android.support.v4.content.LocalBroadcastManager;
  34. import com.owncloud.android.datamodel.FileDataStorageManager;
  35. import com.owncloud.android.datamodel.OCFile;
  36. import com.owncloud.android.lib.common.OwnCloudClient;
  37. import com.owncloud.android.lib.resources.shares.OCShare;
  38. import com.owncloud.android.lib.common.operations.RemoteOperation;
  39. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  40. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  41. import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
  42. import com.owncloud.android.lib.resources.files.FileUtils;
  43. import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
  44. import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
  45. import com.owncloud.android.lib.resources.files.RemoteFile;
  46. import com.owncloud.android.syncadapter.FileSyncAdapter;
  47. import com.owncloud.android.utils.FileStorageUtils;
  48. import com.owncloud.android.utils.Log_OC;
  49. /**
  50. * Remote operation performing the synchronization of the list of files contained
  51. * in a folder identified with its remote path.
  52. *
  53. * Fetches the list and properties of the files contained in the given folder, including their
  54. * properties, and updates the local database with them.
  55. *
  56. * Does NOT enter in the child folders to synchronize their contents also.
  57. *
  58. * @author David A. Velasco
  59. */
  60. public class SynchronizeFolderOperation extends RemoteOperation {
  61. private static final String TAG = SynchronizeFolderOperation.class.getSimpleName();
  62. public static final String EVENT_SINGLE_FOLDER_CONTENTS_SYNCED = SynchronizeFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_CONTENTS_SYNCED";
  63. public static final String EVENT_SINGLE_FOLDER_SHARES_SYNCED = SynchronizeFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_SHARES_SYNCED";
  64. /** Time stamp for the synchronization process in progress */
  65. private long mCurrentSyncTime;
  66. /** Remote folder to synchronize */
  67. private OCFile mLocalFolder;
  68. /** Access to the local database */
  69. private FileDataStorageManager mStorageManager;
  70. /** Account where the file to synchronize belongs */
  71. private Account mAccount;
  72. /** Android context; necessary to send requests to the download service */
  73. private Context mContext;
  74. /** Files and folders contained in the synchronized folder after a successful operation */
  75. private List<OCFile> mChildren;
  76. /** Counter of conflicts found between local and remote files */
  77. private int mConflictsFound;
  78. /** Counter of failed operations in synchronization of kept-in-sync files */
  79. private int mFailsInFavouritesFound;
  80. /** 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 */
  81. private Map<String, String> mForgottenLocalFiles;
  82. /** 'True' means that this operation is part of a full account synchronization */
  83. private boolean mSyncFullAccount;
  84. /** 'True' means that Share resources bound to the files into the folder should be refreshed also */
  85. private boolean mIsShareSupported;
  86. /** 'True' means that the remote folder changed from last synchronization and should be fetched */
  87. private boolean mRemoteFolderChanged;
  88. /**
  89. * Creates a new instance of {@link SynchronizeFolderOperation}.
  90. *
  91. * @param remoteFolderPath Remote folder to synchronize.
  92. * @param currentSyncTime Time stamp for the synchronization process in progress.
  93. * @param localFolderId Identifier in the local database of the folder to synchronize.
  94. * @param updateFolderProperties 'True' means that the properties of the folder should be updated also, not just its content.
  95. * @param syncFullAccount 'True' means that this operation is part of a full account synchronization.
  96. * @param dataStorageManager Interface with the local database.
  97. * @param account ownCloud account where the folder is located.
  98. * @param context Application context.
  99. */
  100. public SynchronizeFolderOperation( OCFile folder,
  101. long currentSyncTime,
  102. boolean syncFullAccount,
  103. boolean isShareSupported,
  104. FileDataStorageManager dataStorageManager,
  105. Account account,
  106. Context context ) {
  107. mLocalFolder = folder;
  108. mCurrentSyncTime = currentSyncTime;
  109. mSyncFullAccount = syncFullAccount;
  110. mIsShareSupported = isShareSupported;
  111. mStorageManager = dataStorageManager;
  112. mAccount = account;
  113. mContext = context;
  114. mForgottenLocalFiles = new HashMap<String, String>();
  115. mRemoteFolderChanged = false;
  116. }
  117. public int getConflictsFound() {
  118. return mConflictsFound;
  119. }
  120. public int getFailsInFavouritesFound() {
  121. return mFailsInFavouritesFound;
  122. }
  123. public Map<String, String> getForgottenLocalFiles() {
  124. return mForgottenLocalFiles;
  125. }
  126. /**
  127. * Returns the list of files and folders contained in the synchronized folder, if called after synchronization is complete.
  128. *
  129. * @return List of files and folders contained in the synchronized folder.
  130. */
  131. public List<OCFile> getChildren() {
  132. return mChildren;
  133. }
  134. /**
  135. * Performs the synchronization.
  136. *
  137. * {@inheritDoc}
  138. */
  139. @Override
  140. protected RemoteOperationResult run(OwnCloudClient client) {
  141. RemoteOperationResult result = null;
  142. mFailsInFavouritesFound = 0;
  143. mConflictsFound = 0;
  144. mForgottenLocalFiles.clear();
  145. if (FileUtils.PATH_SEPARATOR.equals(mLocalFolder.getRemotePath()) && !mSyncFullAccount) {
  146. updateOCVersion(client);
  147. }
  148. result = checkForChanges(client);
  149. if (result.isSuccess()) {
  150. if (mRemoteFolderChanged) {
  151. result = fetchAndSyncRemoteFolder(client);
  152. } else {
  153. mChildren = mStorageManager.getFolderContent(mLocalFolder);
  154. }
  155. }
  156. if (!mSyncFullAccount) {
  157. sendLocalBroadcast(EVENT_SINGLE_FOLDER_CONTENTS_SYNCED, mLocalFolder.getRemotePath(), result);
  158. }
  159. if (result.isSuccess() && mIsShareSupported && !mSyncFullAccount) {
  160. refreshSharesForFolder(client); // share result is ignored
  161. }
  162. if (!mSyncFullAccount) {
  163. sendLocalBroadcast(EVENT_SINGLE_FOLDER_SHARES_SYNCED, mLocalFolder.getRemotePath(), result);
  164. }
  165. return result;
  166. }
  167. private void updateOCVersion(OwnCloudClient client) {
  168. UpdateOCVersionOperation update = new UpdateOCVersionOperation(mAccount, mContext);
  169. RemoteOperationResult result = update.execute(client);
  170. if (result.isSuccess()) {
  171. mIsShareSupported = update.getOCVersion().isSharedSupported();
  172. }
  173. }
  174. private RemoteOperationResult checkForChanges(OwnCloudClient client) {
  175. mRemoteFolderChanged = false;
  176. RemoteOperationResult result = null;
  177. String remotePath = null;
  178. remotePath = mLocalFolder.getRemotePath();
  179. Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath);
  180. // remote request
  181. ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath);
  182. result = operation.execute(client);
  183. if (result.isSuccess()){
  184. OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
  185. // check if remote and local folder are different
  186. mRemoteFolderChanged = !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
  187. result = new RemoteOperationResult(ResultCode.OK);
  188. Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " + (mRemoteFolderChanged ? "changed" : "not changed"));
  189. } else {
  190. // check failed
  191. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  192. removeLocalFolder();
  193. }
  194. if (result.isException()) {
  195. Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage(), result.getException());
  196. } else {
  197. Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage());
  198. }
  199. }
  200. return result;
  201. }
  202. private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) {
  203. String remotePath = mLocalFolder.getRemotePath();
  204. ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(remotePath);
  205. RemoteOperationResult result = operation.execute(client);
  206. Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
  207. if (result.isSuccess()) {
  208. synchronizeData(result.getData(), client);
  209. if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
  210. result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); // should be different result, but will do the job
  211. }
  212. } else {
  213. if (result.getCode() == ResultCode.FILE_NOT_FOUND)
  214. removeLocalFolder();
  215. }
  216. return result;
  217. }
  218. private void removeLocalFolder() {
  219. if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
  220. String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);
  221. mStorageManager.removeFolder(mLocalFolder, true, (mLocalFolder.isDown() && mLocalFolder.getStoragePath().startsWith(currentSavePath)));
  222. }
  223. }
  224. /**
  225. * Synchronizes the data retrieved from the server about the contents of the target folder
  226. * with the current data in the local database.
  227. *
  228. * Grants that mChildren is updated with fresh data after execution.
  229. *
  230. * @param folderAndFiles Remote folder and children files in Folder
  231. *
  232. * @param client Client instance to the remote server where the data were
  233. * retrieved.
  234. * @return 'True' when any change was made in the local data, 'false' otherwise.
  235. */
  236. private void synchronizeData(ArrayList<Object> folderAndFiles, OwnCloudClient client) {
  237. // get 'fresh data' from the database
  238. mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath());
  239. // parse data from remote folder
  240. OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0));
  241. remoteFolder.setParentId(mLocalFolder.getParentId());
  242. remoteFolder.setFileId(mLocalFolder.getFileId());
  243. Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
  244. List<OCFile> updatedFiles = new Vector<OCFile>(folderAndFiles.size() - 1);
  245. List<SynchronizeFileOperation> filesToSyncContents = new Vector<SynchronizeFileOperation>();
  246. // get current data about local contents of the folder to synchronize
  247. List<OCFile> localFiles = mStorageManager.getFolderContent(mLocalFolder);
  248. Map<String, OCFile> localFilesMap = new HashMap<String, OCFile>(localFiles.size());
  249. for (OCFile file : localFiles) {
  250. localFilesMap.put(file.getRemotePath(), file);
  251. }
  252. // loop to update every child
  253. OCFile remoteFile = null, localFile = null;
  254. for (int i=1; i<folderAndFiles.size(); i++) {
  255. /// new OCFile instance with the data from the server
  256. remoteFile = fillOCFile((RemoteFile)folderAndFiles.get(i));
  257. remoteFile.setParentId(mLocalFolder.getFileId());
  258. /// retrieve local data for the read file
  259. //localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
  260. localFile = localFilesMap.remove(remoteFile.getRemotePath());
  261. /// add to the remoteFile (the new one) data about LOCAL STATE (not existing in the server side)
  262. remoteFile.setLastSyncDateForProperties(mCurrentSyncTime);
  263. if (localFile != null) {
  264. // some properties of local state are kept unmodified
  265. remoteFile.setFileId(localFile.getFileId());
  266. remoteFile.setKeepInSync(localFile.keepInSync());
  267. remoteFile.setLastSyncDateForData(localFile.getLastSyncDateForData());
  268. remoteFile.setModificationTimestampAtLastSyncForData(localFile.getModificationTimestampAtLastSyncForData());
  269. remoteFile.setStoragePath(localFile.getStoragePath());
  270. remoteFile.setEtag(localFile.getEtag()); // eTag will not be updated unless contents are synchronized (Synchronize[File|Folder]Operation with remoteFile as parameter)
  271. if (remoteFile.isFolder()) {
  272. remoteFile.setFileLength(localFile.getFileLength()); // TODO move operations about size of folders to FileContentProvider
  273. }
  274. remoteFile.setPublicLink(localFile.getPublicLink());
  275. remoteFile.setShareByLink(localFile.isShareByLink());
  276. } else {
  277. remoteFile.setEtag(""); // remote eTag will not be updated unless contents are synchronized (Synchronize[File|Folder]Operation with remoteFile as parameter)
  278. }
  279. /// check and fix, if needed, local storage path
  280. checkAndFixForeignStoragePath(remoteFile); // fixing old policy - now local files must be copied into the ownCloud local folder
  281. searchForLocalFileInDefaultPath(remoteFile); // legacy
  282. /// prepare content synchronization for kept-in-sync files
  283. if (remoteFile.keepInSync()) {
  284. SynchronizeFileOperation operation = new SynchronizeFileOperation( localFile,
  285. remoteFile,
  286. mAccount,
  287. true,
  288. mContext
  289. );
  290. filesToSyncContents.add(operation);
  291. }
  292. updatedFiles.add(remoteFile);
  293. }
  294. // save updated contents in local database; all at once, trying to get a best performance in database update (not a big deal, indeed)
  295. mStorageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
  296. // request for the synchronization of file contents AFTER saving current remote properties
  297. startContentSynchronizations(filesToSyncContents, client);
  298. mChildren = updatedFiles;
  299. }
  300. /**
  301. * Performs a list of synchronization operations, determining if a download or upload is needed or
  302. * if exists conflict due to changes both in local and remote contents of the each file.
  303. *
  304. * If download or upload is needed, request the operation to the corresponding service and goes on.
  305. *
  306. * @param filesToSyncContents Synchronization operations to execute.
  307. * @param client Interface to the remote ownCloud server.
  308. */
  309. private void startContentSynchronizations(List<SynchronizeFileOperation> filesToSyncContents, OwnCloudClient client) {
  310. RemoteOperationResult contentsResult = null;
  311. for (SynchronizeFileOperation op: filesToSyncContents) {
  312. contentsResult = op.execute(mStorageManager, mContext); // returns without waiting for upload or download finishes
  313. if (!contentsResult.isSuccess()) {
  314. if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
  315. mConflictsFound++;
  316. } else {
  317. mFailsInFavouritesFound++;
  318. if (contentsResult.getException() != null) {
  319. Log_OC.e(TAG, "Error while synchronizing favourites : " + contentsResult.getLogMessage(), contentsResult.getException());
  320. } else {
  321. Log_OC.e(TAG, "Error while synchronizing favourites : " + contentsResult.getLogMessage());
  322. }
  323. }
  324. } // won't let these fails break the synchronization process
  325. }
  326. }
  327. public boolean isMultiStatus(int status) {
  328. return (status == HttpStatus.SC_MULTI_STATUS);
  329. }
  330. /**
  331. * Creates and populates a new {@link OCFile} object with the data read from the server.
  332. *
  333. * @param remote remote file read from the server (remote file or folder).
  334. * @return New OCFile instance representing the remote resource described by we.
  335. */
  336. private OCFile fillOCFile(RemoteFile remote) {
  337. OCFile file = new OCFile(remote.getRemotePath());
  338. file.setCreationTimestamp(remote.getCreationTimestamp());
  339. file.setFileLength(remote.getLength());
  340. file.setMimetype(remote.getMimeType());
  341. file.setModificationTimestamp(remote.getModifiedTimestamp());
  342. file.setEtag(remote.getEtag());
  343. return file;
  344. }
  345. /**
  346. * Checks the storage path of the OCFile received as parameter. If it's out of the local ownCloud folder,
  347. * tries to copy the file inside it.
  348. *
  349. * If the copy fails, the link to the local file is nullified. The account of forgotten files is kept in
  350. * {@link #mForgottenLocalFiles}
  351. *)
  352. * @param file File to check and fix.
  353. */
  354. private void checkAndFixForeignStoragePath(OCFile file) {
  355. String storagePath = file.getStoragePath();
  356. String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  357. if (storagePath != null && !storagePath.equals(expectedPath)) {
  358. /// fix storagePaths out of the local ownCloud folder
  359. File originalFile = new File(storagePath);
  360. if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
  361. mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
  362. file.setStoragePath(null);
  363. } else {
  364. InputStream in = null;
  365. OutputStream out = null;
  366. try {
  367. File expectedFile = new File(expectedPath);
  368. File expectedParent = expectedFile.getParentFile();
  369. expectedParent.mkdirs();
  370. if (!expectedParent.isDirectory()) {
  371. throw new IOException("Unexpected error: parent directory could not be created");
  372. }
  373. expectedFile.createNewFile();
  374. if (!expectedFile.isFile()) {
  375. throw new IOException("Unexpected error: target file could not be created");
  376. }
  377. in = new FileInputStream(originalFile);
  378. out = new FileOutputStream(expectedFile);
  379. byte[] buf = new byte[1024];
  380. int len;
  381. while ((len = in.read(buf)) > 0){
  382. out.write(buf, 0, len);
  383. }
  384. file.setStoragePath(expectedPath);
  385. } catch (Exception e) {
  386. Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e);
  387. mForgottenLocalFiles.put(file.getRemotePath(), storagePath);
  388. file.setStoragePath(null);
  389. } finally {
  390. try {
  391. if (in != null) in.close();
  392. } catch (Exception e) {
  393. Log_OC.d(TAG, "Weird exception while closing input stream for " + storagePath + " (ignoring)", e);
  394. }
  395. try {
  396. if (out != null) out.close();
  397. } catch (Exception e) {
  398. Log_OC.d(TAG, "Weird exception while closing output stream for " + expectedPath + " (ignoring)", e);
  399. }
  400. }
  401. }
  402. }
  403. }
  404. private RemoteOperationResult refreshSharesForFolder(OwnCloudClient client) {
  405. RemoteOperationResult result = null;
  406. // remote request
  407. GetRemoteSharesForFileOperation operation = new GetRemoteSharesForFileOperation(mLocalFolder.getRemotePath(), false, true);
  408. result = operation.execute(client);
  409. if (result.isSuccess()) {
  410. // update local database
  411. ArrayList<OCShare> shares = new ArrayList<OCShare>();
  412. for(Object obj: result.getData()) {
  413. shares.add((OCShare) obj);
  414. }
  415. mStorageManager.saveSharesInFolder(shares, mLocalFolder);
  416. }
  417. return result;
  418. }
  419. /**
  420. * Scans the default location for saving local copies of files searching for
  421. * a 'lost' file with the same full name as the {@link OCFile} received as
  422. * parameter.
  423. *
  424. * @param file File to associate a possible 'lost' local file.
  425. */
  426. private void searchForLocalFileInDefaultPath(OCFile file) {
  427. if (file.getStoragePath() == null && !file.isFolder()) {
  428. File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  429. if (f.exists()) {
  430. file.setStoragePath(f.getAbsolutePath());
  431. file.setLastSyncDateForData(f.lastModified());
  432. }
  433. }
  434. }
  435. /**
  436. * Sends a message to any application component interested in the progress of the synchronization.
  437. *
  438. * @param event
  439. * @param dirRemotePath Remote path of a folder that was just synchronized (with or without success)
  440. * @param result
  441. */
  442. private void sendLocalBroadcast(String event, String dirRemotePath, RemoteOperationResult result) {
  443. Log_OC.d(TAG, "Send broadcast " + event);
  444. Intent intent = new Intent(event);
  445. intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, mAccount.name);
  446. if (dirRemotePath != null) {
  447. intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath);
  448. }
  449. intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result);
  450. mContext.sendStickyBroadcast(intent);
  451. //LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
  452. }
  453. public boolean getRemoteFolderChanged() {
  454. return mRemoteFolderChanged;
  455. }
  456. }