Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
More consistent listFiles behavior _Safe vs _Throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Dec 2, 2019
1 parent c40dda2 commit 33e2565
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ fun File.tryMkFile(): File {
}
}

@Throws(IOException::class)
fun File.deleteAll() {
if (isDirectory) {
val dirContent: Array<File>? = listFiles()
dirContent?.forEach { it.deleteAll() }
listFiles()?.forEach { it.deleteAll() }
}
if (delete()) {
Timber.v("File.release(): Deleted %s", this)
Expand All @@ -100,12 +100,13 @@ fun File.deleteAll() {
}
}

fun File.safeListFiles(): Array<File> {
return this.listFiles() ?: throw IOException("listFiles() returned NULL")
fun File.listFilesSafe(): List<File>? {
return this.listFiles()?.toList()
}

fun File.safeListFiles(filter: (File) -> Boolean): Array<File> {
return this.listFiles(filter) ?: throw IOException("listFiles(filter=$filter) returned NULL")
@Throws(IOException::class)
fun File.listFilesThrowing(): List<File> {
return this.listFiles()?.toList() ?: throw IOException("listFiles() returned NULL on $path")
}

fun File.isSymbolicLink(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ class LocalGateway @Inject constructor(

@Throws(IOException::class)
fun listFiles(path: LocalPath, mode: Mode = Mode.AUTO): List<LocalPath> = try {
val nonRootList: Array<File>? = path.asFile().listFiles()
val nonRootList: List<File>? = path.asFile().listFilesSafe()
when {
mode == Mode.NORMAL || nonRootList != null && mode == Mode.AUTO -> {
path.asFile().listFiles().map { LocalPath.build(it) }
if (nonRootList == null) throw ReadException(path)
nonRootList.map { LocalPath.build(it) }
}
mode == Mode.ROOT || nonRootList == null && mode == Mode.AUTO -> {
rootOps {
Expand All @@ -136,7 +137,8 @@ class LocalGateway @Inject constructor(

@Throws(IOException::class)
fun lookupFiles(path: LocalPath, mode: Mode = Mode.AUTO): List<LocalPathLookup> = try {
val nonRootList: List<LocalPath>? = path.asFile().listFiles().map { it.toLocalPath() }
val nonRootList: List<LocalPath>? = path.asFile().listFilesSafe()?.map { it.toLocalPath() }

when {
mode == Mode.NORMAL || nonRootList != null && mode == Mode.AUTO -> {
if (nonRootList == null) throw ReadException(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import eu.darken.bb.common.file.core.local.*
import timber.log.Timber
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException

class FileOpsHost : FileOps.Stub() {
override fun lookUp(path: LocalPath): LocalPathLookup = try {
Expand All @@ -19,9 +18,7 @@ class FileOpsHost : FileOps.Stub() {
}

override fun listFiles(path: LocalPath): List<LocalPath> = try {
val result = path.asFile().listFiles()
if (result == null) throw IOException("listFiles() returned null for ${path.path}")
result.map { LocalPath.build(it) }
path.asFile().listFilesThrowing().map { LocalPath.build(it) }
} catch (e: Exception) {
Timber.tag(TAG).e(e, "listFiles(path=$path) failed.")
throw wrapPropagating(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.squareup.moshi.Moshi
import eu.darken.bb.App
import eu.darken.bb.backup.core.Backup
import eu.darken.bb.common.dagger.PerApp
import eu.darken.bb.common.file.core.local.listFilesThrowing
import eu.darken.bb.common.moshi.from
import eu.darken.bb.common.moshi.into
import okio.Sink
Expand All @@ -28,7 +29,7 @@ class MMDataRepo @Inject constructor(
Timber.tag(TAG).d("TMP dirs created: %s", tmpDir)
} else {
thread(start = true, name = "MMRepo Cleanup") {
tmpDir.listFiles().forEach {
tmpDir.listFilesThrowing().forEach {
it.deleteRecursively()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class LocalStorage @AssistedInject constructor(
override val progress: Observable<Progress.Data> = progressPub.data

private val dataDirEvents = Observable
.fromCallable { dataDir.safeListFiles() }
.fromCallable { dataDir.listFilesThrowing() }
.subscribeOn(Schedulers.io())
.onErrorReturnItem(emptyArray())
.onErrorReturnItem(emptyList())
.repeatWhen { it.delay(1, TimeUnit.SECONDS) }
.filterUnchanged { old, new -> old.toList() != new.toList() }
.replayingShare()
Expand Down Expand Up @@ -163,11 +163,11 @@ class LocalStorage @AssistedInject constructor(
val backupSpec = readSpec(specId)
val metaData = readBackupMeta(item.specId, backupId)

return@flatMap Observable.fromCallable { backupDir.safeListFiles() }
return@flatMap Observable.fromCallable { backupDir.listFilesThrowing() }
.repeatWhen { it.delay(1, TimeUnit.SECONDS) }
.filterUnchanged()
.map {
return@map versionDir.safeListFiles()
return@map versionDir.listFilesThrowing()
.filter { it.path.endsWith(PROP_EXT) }
.map { file ->
val props = file.source().use { mmDataRepo.readProps(it) }
Expand Down Expand Up @@ -200,7 +200,7 @@ class LocalStorage @AssistedInject constructor(
val dataMap = mutableMapOf<String, MutableList<MMRef>>()

val versionPath = getVersionDir(specId, backupId).requireExists()
versionPath.safeListFiles { file: File -> file.path.endsWith(PROP_EXT) }.forEach { propFile ->
versionPath.listFilesThrowing().filter { file: File -> file.path.endsWith(PROP_EXT) }.forEach { propFile ->

val dataFile = File(propFile.parent, propFile.name.replace(PROP_EXT, DATA_EXT))

Expand Down Expand Up @@ -315,7 +315,7 @@ class LocalStorage @AssistedInject constructor(

private fun getMetaDatas(specId: BackupSpec.Id): Collection<Backup.MetaData> {
val metaDatas = mutableListOf<Backup.MetaData>()
getSpecDir(specId).safeListFiles().filter { it.isDirectory }.forEach { dir ->
getSpecDir(specId).listFilesThrowing().filter { it.isDirectory }.forEach { dir ->
try {
val metaData = readBackupMeta(specId, Backup.Id(dir.name))
metaDatas.add(metaData)
Expand Down

0 comments on commit 33e2565

Please sign in to comment.