CRC32 magic

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-12-11 01:10:55 +01:00 committed by AndyScherzinger
parent 05c9a3cf9c
commit ce7dbb437f
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
4 changed files with 70 additions and 4 deletions

View file

@ -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() {

View file

@ -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;
}
}
}

View file

@ -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";
}
}

View file

@ -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 );"
);
}