FilesystemDataProvider.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * Copyright (C) 2017 Mario Danic
  5. * Copyright (C) 2017 Nextcloud.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.datamodel;
  21. import android.content.ContentResolver;
  22. import android.content.ContentValues;
  23. import android.database.Cursor;
  24. import android.net.Uri;
  25. import com.owncloud.android.db.ProviderMeta;
  26. import com.owncloud.android.lib.common.utils.Log_OC;
  27. import java.io.BufferedInputStream;
  28. import java.io.FileInputStream;
  29. import java.io.FileNotFoundException;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.util.HashSet;
  33. import java.util.Set;
  34. import java.util.zip.CRC32;
  35. /**
  36. * Provider for stored filesystem data.
  37. */
  38. public class FilesystemDataProvider {
  39. static private final String TAG = FilesystemDataProvider.class.getSimpleName();
  40. private ContentResolver contentResolver;
  41. public FilesystemDataProvider(ContentResolver contentResolver) {
  42. if (contentResolver == null) {
  43. throw new IllegalArgumentException("Cannot create an instance with a NULL contentResolver");
  44. }
  45. this.contentResolver = contentResolver;
  46. }
  47. public int deleteAllEntriesForSyncedFolder(String syncedFolderId) {
  48. return contentResolver.delete(
  49. ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
  50. ProviderMeta.ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID + " = ?",
  51. new String[]{syncedFolderId}
  52. );
  53. }
  54. public void updateFilesystemFileAsSentForUpload(String path, String syncedFolderId) {
  55. ContentValues cv = new ContentValues();
  56. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, 1);
  57. contentResolver.update(
  58. ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
  59. cv,
  60. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " = ? and " +
  61. ProviderMeta.ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID + " = ?",
  62. new String[]{path, syncedFolderId}
  63. );
  64. }
  65. public Set<String> getFilesForUpload(String localPath, String syncedFolderId) {
  66. Set<String> localPathsToUpload = new HashSet<>();
  67. String likeParam = localPath + "%";
  68. Cursor cursor = contentResolver.query(
  69. ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
  70. null,
  71. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " LIKE ? and " +
  72. ProviderMeta.ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID + " = ? and " +
  73. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD + " = ? and " +
  74. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER + " = ?",
  75. new String[]{likeParam, syncedFolderId, "0", "0"},
  76. null);
  77. if (cursor != null && cursor.moveToFirst()) {
  78. do {
  79. String value = cursor.getString(cursor.getColumnIndex(
  80. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH));
  81. if (value == null) {
  82. Log_OC.e(TAG, "Cannot get local path");
  83. } else {
  84. localPathsToUpload.add(value);
  85. }
  86. } while (cursor.moveToNext());
  87. cursor.close();
  88. }
  89. return localPathsToUpload;
  90. }
  91. public void storeOrUpdateFileValue(String localPath, long modifiedAt, boolean isFolder, SyncedFolder syncedFolder) {
  92. FileSystemDataSet data = getFilesystemDataSet(localPath, syncedFolder);
  93. int isFolderValue = 0;
  94. if (isFolder) {
  95. isFolderValue = 1;
  96. }
  97. ContentValues cv = new ContentValues();
  98. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY, System.currentTimeMillis());
  99. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_MODIFIED, modifiedAt);
  100. if (data == null) {
  101. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH, localPath);
  102. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER, isFolderValue);
  103. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, false);
  104. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID, syncedFolder.getId());
  105. long newCrc32 = getFileChecksum(localPath);
  106. if (newCrc32 != -1) {
  107. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_CRC32, Long.toString(newCrc32));
  108. }
  109. Uri result = contentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM, cv);
  110. if (result == null) {
  111. Log_OC.v(TAG, "Failed to insert filesystem data with local path: " + localPath);
  112. }
  113. } else {
  114. if (data.getModifiedAt() != modifiedAt) {
  115. long newCrc32 = getFileChecksum(localPath);
  116. if (data.getCrc32() == null || (newCrc32 != -1 && !data.getCrc32().equals(Long.toString(newCrc32)))) {
  117. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_CRC32, Long.toString(newCrc32));
  118. cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, 0);
  119. }
  120. }
  121. int result = contentResolver.update(
  122. ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
  123. cv,
  124. ProviderMeta.ProviderTableMeta._ID + "=?",
  125. new String[]{String.valueOf(data.getId())}
  126. );
  127. if (result == 0) {
  128. Log_OC.v(TAG, "Failed to update filesystem data with local path: " + localPath);
  129. }
  130. }
  131. }
  132. private FileSystemDataSet getFilesystemDataSet(String localPathParam, SyncedFolder syncedFolder) {
  133. Cursor cursor = contentResolver.query(
  134. ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
  135. null,
  136. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " = ? and " +
  137. ProviderMeta.ProviderTableMeta.FILESYSTEM_SYNCED_FOLDER_ID + " = ?",
  138. new String[]{localPathParam, Long.toString(syncedFolder.getId())},
  139. null
  140. );
  141. FileSystemDataSet dataSet = null;
  142. if (cursor != null) {
  143. if (cursor.moveToFirst()) {
  144. int id = cursor.getInt(cursor.getColumnIndex(ProviderMeta.ProviderTableMeta._ID));
  145. String localPath = cursor.getString(cursor.getColumnIndex(
  146. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH));
  147. long modifiedAt = cursor.getLong(cursor.getColumnIndex(
  148. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_MODIFIED));
  149. boolean isFolder = false;
  150. if (cursor.getInt(cursor.getColumnIndex(
  151. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER)) != 0) {
  152. isFolder = true;
  153. }
  154. long foundAt = cursor.getLong(cursor.getColumnIndex(ProviderMeta.
  155. ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY));
  156. boolean isSentForUpload = false;
  157. if (cursor.getInt(cursor.getColumnIndex(
  158. ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD)) != 0) {
  159. isSentForUpload = true;
  160. }
  161. String crc32 = cursor.getString(cursor.getColumnIndex(ProviderMeta.ProviderTableMeta.FILESYSTEM_CRC32));
  162. if (id == -1) {
  163. Log_OC.e(TAG, "Arbitrary value could not be created from cursor");
  164. } else {
  165. dataSet = new FileSystemDataSet(id, localPath, modifiedAt, isFolder, isSentForUpload, foundAt,
  166. syncedFolder.getId(), crc32);
  167. }
  168. }
  169. cursor.close();
  170. } else {
  171. Log_OC.e(TAG, "DB error restoring arbitrary values.");
  172. }
  173. return dataSet;
  174. }
  175. private long getFileChecksum(String filepath) {
  176. InputStream inputStream = null;
  177. try {
  178. inputStream = new BufferedInputStream(new FileInputStream(filepath));
  179. CRC32 crc = new CRC32();
  180. int cnt;
  181. while ((cnt = inputStream.read()) != -1) {
  182. crc.update(cnt);
  183. }
  184. return crc.getValue();
  185. } catch (FileNotFoundException e) {
  186. return -1;
  187. } catch (IOException e) {
  188. return -1;
  189. }
  190. }
  191. }