import com.owncloud.android.R;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.utils.FileStorageUtils;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
/**
* Created by Bartosz Przybylski on 07.11.2015.
private String mStorageTarget;
private String mStorageSource;
+ private int mProgress;
+
+ private static final int mProgressCopyUpperBound = 98;
private class MigrationException extends Exception {
private int mResId;
mStorageSource = args[0];
mStorageTarget = args[1];
+ mProgress = 0;
- int progress = 0;
- publishProgress(progress++, R.string.file_migration_preparing);
+ publishProgress(mProgress++, R.string.file_migration_preparing);
Context context = StorageMigrationActivity.this;
String ocAuthority = context.getString(R.string.authority);
Account[] ocAccounts = AccountManager.get(context).getAccountsByType(MainApp.getAccountType());
boolean[] oldAutoSync = new boolean[ocAccounts.length];
+ Log_OC.stopLogging();
+
try {
- publishProgress(progress++, R.string.file_migration_checking_destination);
+ publishProgress(mProgress++, R.string.file_migration_checking_destination);
checkDestinationAvailability();
- publishProgress(progress++, R.string.file_migration_saving_accounts_configuration);
+ publishProgress(mProgress++, R.string.file_migration_saving_accounts_configuration);
saveAccountsSyncStatus(ocAuthority, ocAccounts, oldAutoSync);
- publishProgress(progress++, R.string.file_migration_waiting_for_unfinished_sync);
+ publishProgress(mProgress++, R.string.file_migration_waiting_for_unfinished_sync);
stopAccountsSyncing(ocAuthority, ocAccounts);
waitForUnfinishedSynchronizations(ocAuthority, ocAccounts);
- publishProgress(progress++, R.string.file_migration_migrating);
+ publishProgress(mProgress++, R.string.file_migration_migrating);
copyFiles();
- publishProgress(progress++, R.string.file_migration_updating_index);
+ publishProgress(mProgress++, R.string.file_migration_updating_index);
updateIndex(context);
- publishProgress(progress++, R.string.file_migration_cleaning);
+ publishProgress(mProgress++, R.string.file_migration_cleaning);
cleanup();
} catch (MigrationException e) {
+ rollback();
+ Log_OC.startLogging(mStorageSource);
return e.getResId();
} finally {
- publishProgress(progress++, R.string.file_migration_restoring_accounts_configuration);
+ publishProgress(mProgress++, R.string.file_migration_restoring_accounts_configuration);
restoreAccountsSyncStatus(ocAuthority, ocAccounts, oldAutoSync);
}
- publishProgress(progress++, R.string.file_migration_ok_finished);
+ Log_OC.startLogging(mStorageTarget);
+ publishProgress(mProgress++, R.string.file_migration_ok_finished);
return 0;
}
if (new File(dstFile, MainApp.getDataFolder()).exists())
throw new MigrationException(R.string.file_migration_failed_dir_already_exists);
- if (dstFile.getFreeSpace() < calculateUsedSpace(new File(srcFile, MainApp.getDataFolder())))
+ if (dstFile.getFreeSpace() < FileStorageUtils.getFolderSize(new File(srcFile, MainApp.getDataFolder())))
throw new MigrationException(R.string.file_migration_failed_not_enough_space);
}
File dstFile = new File(mStorageTarget + File.separator + MainApp.getDataFolder());
copyDirs(srcFile, dstFile);
- }
-
- private boolean copyFile(File src, File target) {
- boolean ret = true;
-
- InputStream in = null;
- OutputStream out = null;
-
- try {
- in = new FileInputStream(src);
- out = new FileOutputStream(target);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- } catch (IOException ex) {
- ret = false;
- } finally {
- if (in != null) try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace(System.err);
- }
- if (out != null) try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace(System.err);
- }
- }
-
- return ret;
+ mProgress = Math.max(mProgress, mProgressCopyUpperBound);
+ publishProgress(mProgress);
}
void copyDirs(File src, File dst) throws MigrationException {
throw new MigrationException(R.string.file_migration_failed_while_coping);
for (File f : src.listFiles()) {
+
+ mProgress = Math.min(mProgress+1, mProgressCopyUpperBound);
+ publishProgress(mProgress);
+
if (f.isDirectory())
copyDirs(f, new File(dst, f.getName()));
- else if (!copyFile(f, new File(dst, f.getName())))
+ else if (!FileStorageUtils.copyFile(f, new File(dst, f.getName())))
throw new MigrationException(R.string.file_migration_failed_while_coping);
}
} catch (Exception e) {
throw new MigrationException(R.string.file_migration_failed_while_updating_index);
}
-
}
void cleanup() {
-
+ File srcFile = new File(mStorageSource + File.separator + MainApp.getDataFolder());
+ if (!srcFile.delete())
+ Log_OC.w(TAG, "Migration cleanup step failed");
}
- long calculateUsedSpace(File dir) {
- long result = 0;
-
- for (File f : dir.listFiles()) {
- if (f.isDirectory())
- result += calculateUsedSpace(f);
- else
- result += f.length();
- }
-
- return result;
+ void rollback() {
+ File dstFile = new File(mStorageTarget + File.separator + MainApp.getDataFolder());
+ if (dstFile.exists())
+ if (!dstFile.delete())
+ Log_OC.w(TAG, "Rollback step failed");
}
void saveAccountsSyncStatus(String authority, Account accounts[], boolean syncs[]) {
- for (int i = 0; i < accounts.length; ++i) {
+ for (int i = 0; i < accounts.length; ++i)
syncs[i] = ContentResolver.getSyncAutomatically(accounts[i], authority);
- }
}
void stopAccountsSyncing(String authority, Account accounts[]) {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log_OC.w(TAG, "Thread interrupted while waiting for account to end syncing");
+ Thread.currentThread().interrupt();
}
}
-
void restoreAccountsSyncStatus(String authority, Account accounts[], boolean oldSync[]) {
for (int i = 0; i < accounts.length; ++i)
ContentResolver.setSyncAutomatically(accounts[i], authority, oldSync[i]);