don't restore files that still exist unchanged

(same size and lastModified)
This commit is contained in:
Torsten Grote 2024-08-16 17:06:28 -03:00
parent dc92e41aa8
commit f51c758493
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
5 changed files with 76 additions and 33 deletions

View file

@ -46,8 +46,6 @@ internal abstract class AbstractChunkRestore(
tag: String, tag: String,
streamWriter: suspend (outputStream: OutputStream) -> Long, 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) fileRestore.restoreFile(file, observer, tag, streamWriter)
} }

View file

@ -24,7 +24,6 @@ import kotlin.random.Random
private const val TAG = "FileRestore" private const val TAG = "FileRestore"
@Suppress("BlockingMethodInNonBlockingContext")
internal class FileRestore( internal class FileRestore(
private val context: Context, private val context: Context,
private val mediaScanner: MediaScanner, private val mediaScanner: MediaScanner,
@ -46,10 +45,12 @@ internal class FileRestore(
bytes = restoreFile(file.mediaFile, streamWriter) bytes = restoreFile(file.mediaFile, streamWriter)
finalTag = "M$tag" finalTag = "M$tag"
} }
file.docFile != null -> { file.docFile != null -> {
bytes = restoreFile(file, streamWriter) bytes = restoreFile(file, streamWriter)
finalTag = "D$tag" finalTag = "D$tag"
} }
else -> { else -> {
error("unexpected file: $file") error("unexpected file: $file")
} }
@ -63,39 +64,45 @@ internal class FileRestore(
streamWriter: suspend (outputStream: OutputStream) -> Long, streamWriter: suspend (outputStream: OutputStream) -> Long,
): Long { ): Long {
// ensure directory exists // ensure directory exists
@Suppress("DEPRECATION")
val dir = File("${getExternalStorageDirectory()}/${docFile.dir}") val dir = File("${getExternalStorageDirectory()}/${docFile.dir}")
if (!dir.mkdirs() && !dir.isDirectory) { if (!dir.mkdirs() && !dir.isDirectory) {
throw IOException("Could not create ${dir.absolutePath}") throw IOException("Could not create ${dir.absolutePath}")
} }
// find non-existing file-name
var file = File(dir, docFile.name) var file = File(dir, docFile.name)
var i = 0 // TODO should we also calculate and check the chunk IDs?
// we don't support existing files, but at least don't overwrite them when they do exist if (file.isFile && file.length() == docFile.size &&
while (file.exists()) { file.lastModified() == docFile.lastModified
i++ ) {
val lastDot = docFile.name.lastIndexOf('.') Log.i(TAG, "Not restoring $file, already there unchanged.")
val newName = if (lastDot == -1) "${docFile.name} ($i)" return file.length() // not restoring existing file with same length and date
else docFile.name.replaceRange(lastDot..lastDot, " ($i).") } else {
file = File(dir, newName) var i = 0
} // don't overwrite existing files, if they exist
val bytesWritten = try { while (file.exists()) { // find non-existing file-name
// copy chunk(s) into file i++
file.outputStream().use { outputStream -> val lastDot = docFile.name.lastIndexOf('.')
streamWriter(outputStream) val newName = if (lastDot == -1) "${docFile.name} ($i)"
else docFile.name.replaceRange(lastDot..lastDot, " ($i).")
file = File(dir, newName)
} }
} catch (e: IOException) { val bytesWritten = try {
file.delete() // copy chunk(s) into file
throw e file.outputStream().use { outputStream ->
streamWriter(outputStream)
}
} catch (e: IOException) {
file.delete()
throw e
}
// re-set lastModified timestamp
file.setLastModified(docFile.lastModified ?: 0)
// This might be a media file, so do we need to index it.
// Otherwise things like a wrong size of 0 bytes in MediaStore can happen.
indexFile(file)
return bytesWritten
} }
// re-set lastModified timestamp
file.setLastModified(docFile.lastModified ?: 0)
// This might be a media file, so do we need to index it.
// Otherwise things like a wrong size of 0 bytes in MediaStore can happen.
indexFile(file)
return bytesWritten
} }
@Throws(IOException::class) @Throws(IOException::class)
@ -103,6 +110,14 @@ internal class FileRestore(
mediaFile: BackupMediaFile, mediaFile: BackupMediaFile,
streamWriter: suspend (outputStream: OutputStream) -> Long, streamWriter: suspend (outputStream: OutputStream) -> Long,
): 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 // Insert pending media item into MediaStore
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(MediaColumns.DISPLAY_NAME, mediaFile.name) put(MediaColumns.DISPLAY_NAME, mediaFile.name)
@ -143,7 +158,6 @@ internal class FileRestore(
} }
private fun setLastModifiedOnMediaFile(mediaFile: BackupMediaFile, uri: Uri) { private fun setLastModifiedOnMediaFile(mediaFile: BackupMediaFile, uri: Uri) {
@Suppress("DEPRECATION")
val extDir = getExternalStorageDirectory() val extDir = getExternalStorageDirectory()
// re-set lastModified as we can't use the MediaStore for this (read-only property) // re-set lastModified as we can't use the MediaStore for this (read-only property)

View file

@ -56,7 +56,7 @@ public class DocumentScanner(context: Context) {
queryUri, PROJECTION, null, null, null queryUri, PROJECTION, null, null, null
) )
val documentFiles = ArrayList<DocFile>(cursor?.count ?: 0) val documentFiles = ArrayList<DocFile>(cursor?.count ?: 0)
cursor?.use { it -> cursor?.use {
while (it.moveToNext()) { while (it.moveToNext()) {
val id = it.getString(PROJECTION_ID) val id = it.getString(PROJECTION_ID)
val documentUri = DocumentsContract.buildDocumentUriUsingTree(uri, id) val documentUri = DocumentsContract.buildDocumentUriUsingTree(uri, id)

View file

@ -16,7 +16,6 @@ import org.calyxos.backup.storage.content.DocFile
import org.calyxos.backup.storage.content.MediaFile import org.calyxos.backup.storage.content.MediaFile
import org.calyxos.backup.storage.db.UriStore import org.calyxos.backup.storage.db.UriStore
import org.calyxos.backup.storage.measure import org.calyxos.backup.storage.measure
import kotlin.time.ExperimentalTime
internal class FileScannerResult( internal class FileScannerResult(
val smallFiles: List<ContentFile>, val smallFiles: List<ContentFile>,
@ -36,7 +35,6 @@ internal class FileScanner(
private const val FILES_LARGE = "large" private const val FILES_LARGE = "large"
} }
@OptIn(ExperimentalTime::class)
fun getFiles(): FileScannerResult { fun getFiles(): FileScannerResult {
// scan both APIs // scan both APIs
val mediaFiles = ArrayList<ContentFile>() val mediaFiles = ArrayList<ContentFile>()

View file

@ -6,6 +6,7 @@
package org.calyxos.backup.storage.scanner package org.calyxos.backup.storage.scanner
import android.content.ContentResolver.QUERY_ARG_SQL_SELECTION import android.content.ContentResolver.QUERY_ARG_SQL_SELECTION
import android.content.ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS
import android.content.ContentUris import android.content.ContentUris
import android.content.Context import android.content.Context
import android.database.Cursor import android.database.Cursor
@ -21,6 +22,7 @@ import androidx.core.database.getLongOrNull
import androidx.core.database.getStringOrNull import androidx.core.database.getStringOrNull
import org.calyxos.backup.storage.api.BackupFile import org.calyxos.backup.storage.api.BackupFile
import org.calyxos.backup.storage.api.MediaType import org.calyxos.backup.storage.api.MediaType
import org.calyxos.backup.storage.backup.BackupMediaFile
import org.calyxos.backup.storage.content.MediaFile import org.calyxos.backup.storage.content.MediaFile
import java.io.File 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? { internal fun getPath(uri: Uri): String? {
val projection = arrayOf( val projection = arrayOf(
MediaStore.MediaColumns.RELATIVE_PATH, MediaStore.MediaColumns.RELATIVE_PATH,