don't restore files that still exist unchanged
(same size and lastModified)
This commit is contained in:
parent
dc92e41aa8
commit
f51c758493
5 changed files with 76 additions and 33 deletions
|
@ -46,8 +46,6 @@ internal abstract class AbstractChunkRestore(
|
|||
tag: String,
|
||||
streamWriter: suspend (outputStream: OutputStream) -> Long,
|
||||
) {
|
||||
// TODO check if the file exists already (same name, size, chunk IDs)
|
||||
// and skip it in this case
|
||||
fileRestore.restoreFile(file, observer, tag, streamWriter)
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,6 @@ import kotlin.random.Random
|
|||
|
||||
private const val TAG = "FileRestore"
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
internal class FileRestore(
|
||||
private val context: Context,
|
||||
private val mediaScanner: MediaScanner,
|
||||
|
@ -46,10 +45,12 @@ internal class FileRestore(
|
|||
bytes = restoreFile(file.mediaFile, streamWriter)
|
||||
finalTag = "M$tag"
|
||||
}
|
||||
|
||||
file.docFile != null -> {
|
||||
bytes = restoreFile(file, streamWriter)
|
||||
finalTag = "D$tag"
|
||||
}
|
||||
|
||||
else -> {
|
||||
error("unexpected file: $file")
|
||||
}
|
||||
|
@ -63,16 +64,21 @@ internal class FileRestore(
|
|||
streamWriter: suspend (outputStream: OutputStream) -> Long,
|
||||
): Long {
|
||||
// ensure directory exists
|
||||
@Suppress("DEPRECATION")
|
||||
val dir = File("${getExternalStorageDirectory()}/${docFile.dir}")
|
||||
if (!dir.mkdirs() && !dir.isDirectory) {
|
||||
throw IOException("Could not create ${dir.absolutePath}")
|
||||
}
|
||||
// find non-existing file-name
|
||||
var file = File(dir, docFile.name)
|
||||
// TODO should we also calculate and check the chunk IDs?
|
||||
if (file.isFile && file.length() == docFile.size &&
|
||||
file.lastModified() == docFile.lastModified
|
||||
) {
|
||||
Log.i(TAG, "Not restoring $file, already there unchanged.")
|
||||
return file.length() // not restoring existing file with same length and date
|
||||
} else {
|
||||
var i = 0
|
||||
// we don't support existing files, but at least don't overwrite them when they do exist
|
||||
while (file.exists()) {
|
||||
// don't overwrite existing files, if they exist
|
||||
while (file.exists()) { // find non-existing file-name
|
||||
i++
|
||||
val lastDot = docFile.name.lastIndexOf('.')
|
||||
val newName = if (lastDot == -1) "${docFile.name} ($i)"
|
||||
|
@ -97,12 +103,21 @@ internal class FileRestore(
|
|||
|
||||
return bytesWritten
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private suspend fun restoreFile(
|
||||
mediaFile: BackupMediaFile,
|
||||
streamWriter: suspend (outputStream: OutputStream) -> Long,
|
||||
): Long {
|
||||
// TODO should we also calculate and check the chunk IDs?
|
||||
if (mediaScanner.existsMediaFileUnchanged(mediaFile)) {
|
||||
Log.i(
|
||||
TAG,
|
||||
"Not restoring ${mediaFile.path}/${mediaFile.name}, already there unchanged."
|
||||
)
|
||||
return mediaFile.size
|
||||
}
|
||||
// Insert pending media item into MediaStore
|
||||
val contentValues = ContentValues().apply {
|
||||
put(MediaColumns.DISPLAY_NAME, mediaFile.name)
|
||||
|
@ -143,7 +158,6 @@ internal class FileRestore(
|
|||
}
|
||||
|
||||
private fun setLastModifiedOnMediaFile(mediaFile: BackupMediaFile, uri: Uri) {
|
||||
@Suppress("DEPRECATION")
|
||||
val extDir = getExternalStorageDirectory()
|
||||
|
||||
// re-set lastModified as we can't use the MediaStore for this (read-only property)
|
||||
|
|
|
@ -56,7 +56,7 @@ public class DocumentScanner(context: Context) {
|
|||
queryUri, PROJECTION, null, null, null
|
||||
)
|
||||
val documentFiles = ArrayList<DocFile>(cursor?.count ?: 0)
|
||||
cursor?.use { it ->
|
||||
cursor?.use {
|
||||
while (it.moveToNext()) {
|
||||
val id = it.getString(PROJECTION_ID)
|
||||
val documentUri = DocumentsContract.buildDocumentUriUsingTree(uri, id)
|
||||
|
|
|
@ -16,7 +16,6 @@ import org.calyxos.backup.storage.content.DocFile
|
|||
import org.calyxos.backup.storage.content.MediaFile
|
||||
import org.calyxos.backup.storage.db.UriStore
|
||||
import org.calyxos.backup.storage.measure
|
||||
import kotlin.time.ExperimentalTime
|
||||
|
||||
internal class FileScannerResult(
|
||||
val smallFiles: List<ContentFile>,
|
||||
|
@ -36,7 +35,6 @@ internal class FileScanner(
|
|||
private const val FILES_LARGE = "large"
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
fun getFiles(): FileScannerResult {
|
||||
// scan both APIs
|
||||
val mediaFiles = ArrayList<ContentFile>()
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
package org.calyxos.backup.storage.scanner
|
||||
|
||||
import android.content.ContentResolver.QUERY_ARG_SQL_SELECTION
|
||||
import android.content.ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
|
@ -21,6 +22,7 @@ import androidx.core.database.getLongOrNull
|
|||
import androidx.core.database.getStringOrNull
|
||||
import org.calyxos.backup.storage.api.BackupFile
|
||||
import org.calyxos.backup.storage.api.MediaType
|
||||
import org.calyxos.backup.storage.backup.BackupMediaFile
|
||||
import org.calyxos.backup.storage.content.MediaFile
|
||||
import java.io.File
|
||||
|
||||
|
@ -79,6 +81,37 @@ public class MediaScanner(context: Context) {
|
|||
}
|
||||
}
|
||||
|
||||
internal fun existsMediaFileUnchanged(mediaFile: BackupMediaFile): Boolean {
|
||||
val uri = MediaType.fromBackupMediaType(mediaFile.type).contentUri
|
||||
val extras = Bundle().apply {
|
||||
// search for files with same path and name
|
||||
val query = StringBuilder().apply {
|
||||
append("${MediaStore.MediaColumns.MIME_TYPE}!='$MIME_TYPE_DIR'")
|
||||
append(" AND ")
|
||||
append("${MediaStore.MediaColumns.RELATIVE_PATH}=?")
|
||||
append(" AND ")
|
||||
append("${MediaStore.MediaColumns.DISPLAY_NAME}=?")
|
||||
}
|
||||
putString(QUERY_ARG_SQL_SELECTION, query.toString())
|
||||
val args = arrayOf(
|
||||
mediaFile.path + "/", // Note trailing slash that is important
|
||||
mediaFile.name,
|
||||
)
|
||||
putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, args)
|
||||
}
|
||||
|
||||
contentResolver.query(uri, PROJECTION, extras, null)?.use { c ->
|
||||
while (c.moveToNext()) {
|
||||
val f = createMediaFile(c, uri)
|
||||
// note that we get seconds, but store milliseconds
|
||||
if (f.dateModified == mediaFile.lastModified / 1000 && f.size == mediaFile.size) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
internal fun getPath(uri: Uri): String? {
|
||||
val projection = arrayOf(
|
||||
MediaStore.MediaColumns.RELATIVE_PATH,
|
||||
|
|
Loading…
Reference in a new issue