Skip to content

Commit

Permalink
feat: Clear empty directories after deleting restoring items
Browse files Browse the repository at this point in the history
Change-Id: I616a2fc0cd2d900c5b77a72673519ea4b18f4e82
  • Loading branch information
XayahSuSuSu committed Oct 4, 2023
1 parent 37a3e8f commit f022fa9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ private suspend fun DialogState.openDeleteDialog(
selectedPackages.forEach {
remoteRootService.deleteRecursively("${PathUtil.getRestorePackagesSavePath()}/${it.packageName}/${it.timestamp}")
}
remoteRootService.clearEmptyDirectoriesRecursively(PathUtil.getRestorePackagesSavePath())
}
remoteRootService.destroyService()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ private suspend fun DialogState.openMediaRestoreDeleteDialog(
}) {
val path = MediumRestoreUtil.getMediaItemSavePath(entity.name, entity.timestamp)
remoteRootService.deleteRecursively(path)
remoteRootService.clearEmptyDirectoriesRecursively(PathUtil.getRestoreMediumSavePath())
}
remoteRootService.destroyService()
viewModel.initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface IRemoteRootService {
List<String> listFilePaths(String path);
ParcelFileDescriptor readText(String path);
long calculateSize(String path);
void clearEmptyDirectoriesRecursively(String path);

ParcelFileDescriptor getInstalledPackagesAsUser(int flags, int userId);
List<String> getPackageSourceDir(String packageName, int userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,34 @@ internal class RemoteRootServiceImpl : IRemoteRootService.Stub() {
size.get()
}

override fun clearEmptyDirectoriesRecursively(path: String) = synchronized(lock) {
tryOn {
Files.walkFileTree(Paths.get(path), object : SimpleFileVisitor<Path>() {
override fun preVisitDirectory(dir: Path?, attrs: BasicFileAttributes?): FileVisitResult {
if (dir != null && attrs != null) {
if (Files.isDirectory(dir) && Files.list(dir).count() == 0L) {
// Empty dir
Files.delete(dir)
}
}
return FileVisitResult.CONTINUE
}

override fun visitFile(file: Path?, attrs: BasicFileAttributes?): FileVisitResult {
return FileVisitResult.CONTINUE
}

override fun visitFileFailed(file: Path?, exc: IOException?): FileVisitResult {
return FileVisitResult.CONTINUE
}

override fun postVisitDirectory(dir: Path?, exc: IOException?): FileVisitResult {
return FileVisitResult.CONTINUE
}
})
}
}

/**
* AIDL limits transaction to 1M which means it may throw [android.os.TransactionTooLargeException]
* when the package list is too large. So we just make it parcelable and write into tmp file to avoid that.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class RemoteRootService(private val context: Context) {

suspend fun calculateSize(path: String): Long = getService().calculateSize(path)

suspend fun clearEmptyDirectoriesRecursively(path: String) = getService().clearEmptyDirectoriesRecursively(path)

suspend fun getInstalledPackagesAsUser(flags: Int, userId: Int): List<PackageInfo> {
val pfd = getService().getInstalledPackagesAsUser(flags, userId)
val stream = ParcelFileDescriptor.AutoCloseInputStream(pfd)
Expand Down

0 comments on commit f022fa9

Please sign in to comment.