Skip to content

Commit

Permalink
Don't include empty dirs in media backup
Browse files Browse the repository at this point in the history
Otherwise, the directories would be treated as small files and we would try to backup their content which would fail.
  • Loading branch information
grote committed Feb 12, 2024
1 parent 9c4f9d8 commit bc09248
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import org.calyxos.backup.storage.scanner.FileScannerResult
import java.io.IOException
import java.security.GeneralSecurityException
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

internal class BackupResult(
val chunkIds: Set<String>,
Expand Down Expand Up @@ -86,7 +85,6 @@ internal class Backup(
)

@Throws(IOException::class, GeneralSecurityException::class)
@OptIn(ExperimentalTime::class)
suspend fun runBackup(backupObserver: BackupObserver?) {
backupObserver?.onStartScanning()
var duration: Duration? = null
Expand Down Expand Up @@ -121,7 +119,6 @@ internal class Backup(
}

@Throws(IOException::class, GeneralSecurityException::class)
@OptIn(ExperimentalTime::class)
private suspend fun backupFiles(
filesResult: FileScannerResult,
availableChunkIds: HashSet<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.database.Cursor
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.DocumentsContract.Document.MIME_TYPE_DIR
import android.provider.MediaStore
import android.provider.MediaStore.MediaColumns.IS_DOWNLOAD
import android.util.Log
Expand Down Expand Up @@ -57,15 +58,18 @@ public class MediaScanner(context: Context) {

internal fun scanMediaUri(uri: Uri, extraQuery: String? = null): List<MediaFile> {
val extras = Bundle().apply {
val query = StringBuilder()
val query = StringBuilder().apply {
// don't include directories (if they are non-empty they will be in implicitly)
append("${MediaStore.MediaColumns.MIME_TYPE}!='$MIME_TYPE_DIR'")
}
if (uri != MediaType.Downloads.contentUri) {
query.append("$IS_DOWNLOAD=0")
query.append(" AND $IS_DOWNLOAD=0")
}
extraQuery?.let {
if (query.isNotEmpty()) query.append(" AND ")
query.append(" AND ")
query.append(it)
}
if (query.isNotEmpty()) putString(QUERY_ARG_SQL_SELECTION, query.toString())
putString(QUERY_ARG_SQL_SELECTION, query.toString())
}
val cursor = contentResolver.query(uri, PROJECTION, extras, null)
return ArrayList<MediaFile>(cursor?.count ?: 0).apply {
Expand Down Expand Up @@ -106,7 +110,6 @@ public class MediaScanner(context: Context) {
}

private fun getRealSize(mediaFile: MediaFile): Long {
@Suppress("DEPRECATION")
val extDir = Environment.getExternalStorageDirectory()
val path = "$extDir/${mediaFile.dirPath}/${mediaFile.fileName}"
return try {
Expand Down

0 comments on commit bc09248

Please sign in to comment.