From ce7dbb437f7cdbef28b2ce8024a01e536188722f Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Mon, 11 Dec 2017 01:10:55 +0100 Subject: [PATCH] CRC32 magic Signed-off-by: Mario Danic --- .../android/datamodel/FileSystemDataSet.java | 14 ++++++- .../datamodel/FilesystemDataProvider.java | 41 ++++++++++++++++++- .../com/owncloud/android/db/ProviderMeta.java | 3 +- .../providers/FileContentProvider.java | 16 ++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileSystemDataSet.java b/src/main/java/com/owncloud/android/datamodel/FileSystemDataSet.java index 59bc0c9c67..35afe1a944 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileSystemDataSet.java +++ b/src/main/java/com/owncloud/android/datamodel/FileSystemDataSet.java @@ -20,6 +20,8 @@ */ package com.owncloud.android.datamodel; +import android.support.annotation.Nullable; + /* Model for filesystem data from the database */ @@ -32,12 +34,13 @@ public class FileSystemDataSet { private boolean isSentForUpload; private long foundAt; private long syncedFolderId; + @Nullable private String crc32; public FileSystemDataSet() { } public FileSystemDataSet(int id, String localPath, long modifiedAt, boolean isFolder, - boolean isSentForUpload, long foundAt, long syncedFolderId) { + boolean isSentForUpload, long foundAt, long syncedFolderId, @Nullable String crc32) { this.id = id; this.localPath = localPath; this.modifiedAt = modifiedAt; @@ -45,6 +48,15 @@ public class FileSystemDataSet { this.isSentForUpload = isSentForUpload; this.foundAt = foundAt; this.syncedFolderId = syncedFolderId; + this.crc32 = crc32; + } + + public String getCrc32() { + return crc32; + } + + public void setCrc32(String crc32) { + this.crc32 = crc32; } public int getId() { diff --git a/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java b/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java index 72c6930e17..7937cea0d8 100644 --- a/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java +++ b/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java @@ -27,8 +27,14 @@ import android.net.Uri; import com.owncloud.android.db.ProviderMeta; import com.owncloud.android.lib.common.utils.Log_OC; +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.util.HashSet; import java.util.Set; +import java.util.zip.CRC32; /** * Provider for stored filesystem data. @@ -119,6 +125,11 @@ public class FilesystemDataProvider { cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, false); cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID, syncedFolder.getId()); + long newCrc32 = getFileChecksum(localPath); + if (newCrc32 != -1) { + cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_CRC32, Long.toString(newCrc32)); + } + Uri result = contentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM, cv); if (result == null) { @@ -127,7 +138,11 @@ public class FilesystemDataProvider { } else { if (data.getModifiedAt() != modifiedAt) { - cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, 0); + long newCrc32 = getFileChecksum(localPath); + if (data.getCrc32() == null || (newCrc32 != -1 && data.getCrc32().equals(Long.toString(newCrc32)))) { + cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_CRC32, Long.toString(newCrc32)); + cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, 0); + } } @@ -177,11 +192,13 @@ public class FilesystemDataProvider { isSentForUpload = true; } + String crc32 = cursor.getString(cursor.getColumnIndex(ProviderMeta.ProviderTableMeta.FILESYSTEM_CRC32)); + if (id == -1) { Log_OC.e(TAG, "Arbitrary value could not be created from cursor"); } else { dataSet = new FileSystemDataSet(id, localPath, modifiedAt, isFolder, isSentForUpload, foundAt, - syncedFolder.getId()); + syncedFolder.getId(), crc32); } } cursor.close(); @@ -191,4 +208,24 @@ public class FilesystemDataProvider { return dataSet; } + + private static long getFileChecksum(String filepath) { + + InputStream inputStream = null; + try { + inputStream = new BufferedInputStream(new FileInputStream(filepath)); + CRC32 crc = new CRC32(); + int cnt; + while ((cnt = inputStream.read()) != -1) { + crc.update(cnt); + } + + return crc.getValue(); + + } catch (FileNotFoundException e) { + return -1; + } catch (IOException e) { + return -1; + } + } } diff --git a/src/main/java/com/owncloud/android/db/ProviderMeta.java b/src/main/java/com/owncloud/android/db/ProviderMeta.java index 13eb79f9e2..9cc3189443 100644 --- a/src/main/java/com/owncloud/android/db/ProviderMeta.java +++ b/src/main/java/com/owncloud/android/db/ProviderMeta.java @@ -32,7 +32,7 @@ import com.owncloud.android.MainApp; public class ProviderMeta { public static final String DB_NAME = "filelist"; - public static final int DB_VERSION = 25; + public static final int DB_VERSION = 26; private ProviderMeta() { } @@ -218,5 +218,6 @@ public class ProviderMeta { public static final String FILESYSTEM_FILE_FOUND_RECENTLY = "found_at"; public static final String FILESYSTEM_FILE_SENT_FOR_UPLOAD = "upload_triggered"; public static final String FILESYSTEM_SYNCED_FOLDER_ID = "syncedfolder_id"; + public static final String FILESYSTEM_CRC32 = "crc32"; } } \ No newline at end of file diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index 8abc432766..895b719144 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -1189,6 +1189,21 @@ public class FileContentProvider extends ContentProvider { } } + if (oldVersion < 26 && newVersion >= 26) { + Log_OC.i(SQL, "Entering in the #25 Adding text and element color to capabilities"); + db.beginTransaction(); + try { + db.execSQL(ALTER_TABLE + ProviderTableMeta.FILESYSTEM_TABLE_NAME + + ADD_COLUMN + ProviderTableMeta.FILESYSTEM_CRC32 + " TEXT "); + + upgraded = true; + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + } + + if (!upgraded) { Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion)); } @@ -1380,6 +1395,7 @@ public class FileContentProvider extends ContentProvider { + ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY + " LONG, " + ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD + " INTEGER, " + ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID + " STRING, " + + ProviderTableMeta.FILESYSTEM_CRC32 + " STRING, " + ProviderTableMeta.FILESYSTEM_FILE_MODIFIED + " LONG );" ); }