Skip to content

Commit

Permalink
refactor(common-utils)!: Remove the force argument from delete func…
Browse files Browse the repository at this point in the history
…tions

The new implementation always applies force, which is also covered by a
test, so this code became obsolete.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Sep 5, 2024
1 parent a477ded commit bd4e76e
Show file tree
Hide file tree
Showing 26 changed files with 44 additions and 45 deletions.
2 changes: 1 addition & 1 deletion detekt-rules/src/test/kotlin/OrtPackageNamingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private fun TestConfiguration.createFile(dir: String, content: String): File {
val parent = File("build/classes/kotlin/test/$dir").safeMkdirs()

afterTest {
parent.safeDeleteRecursively(force = true)
parent.safeDeleteRecursively()
}

return File(parent, "Test.kt").apply { writeText(content) }
Expand Down
18 changes: 9 additions & 9 deletions downloader/src/main/kotlin/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ class Downloader(private val config: DownloaderConfiguration) {
"took ${vcsMark.elapsedNow()}."
}

// Clean up any left-over files (force to delete read-only files in ".git" directories on Windows).
outputDirectory.safeDeleteRecursively(force = true, baseDirectory = outputDirectory)
// Clean up any left-over files.
outputDirectory.safeDeleteRecursively(baseDirectory = outputDirectory)

exception.addSuppressed(e)
}
Expand Down Expand Up @@ -169,7 +169,7 @@ class Downloader(private val config: DownloaderConfiguration) {
}

// Clean up any left-over files.
outputDirectory.safeDeleteRecursively(force = true, baseDirectory = outputDirectory)
outputDirectory.safeDeleteRecursively(baseDirectory = outputDirectory)

exception.addSuppressed(e)
}
Expand Down Expand Up @@ -283,7 +283,7 @@ class Downloader(private val config: DownloaderConfiguration) {
}

// Clean up any files left from the failed VCS download (i.e. a ".git" directory).
outputDirectory.safeDeleteRecursively(force = true, baseDirectory = outputDirectory)
outputDirectory.safeDeleteRecursively(baseDirectory = outputDirectory)

val fallbackPkg = pkg.copy(vcsProcessed = pkg.vcsProcessed.copy(url = vcsUrlNoCredentials))
applicableVcs.download(fallbackPkg, outputDirectory, config.allowMovingRevisions, recursive)
Expand Down Expand Up @@ -331,7 +331,7 @@ class Downloader(private val config: DownloaderConfiguration) {
} else {
tempDir = createOrtTempDir()
okHttpClient.downloadFile(sourceArtifact.url, tempDir).getOrElse {
tempDir.safeDeleteRecursively(force = true)
tempDir.safeDeleteRecursively()
throw DownloadException("Failed to download source artifact from ${sourceArtifact.url}.", it)
}
}
Expand All @@ -354,7 +354,7 @@ class Downloader(private val config: DownloaderConfiguration) {
"Cannot verify source artifact with ${sourceArtifact.hash}, skipping verification."
}
} else if (!sourceArtifact.hash.verify(sourceArchive)) {
tempDir?.safeDeleteRecursively(force = true)
tempDir?.safeDeleteRecursively()
throw DownloadException("Source artifact does not match expected ${sourceArtifact.hash}.")
}
}
Expand All @@ -369,7 +369,7 @@ class Downloader(private val config: DownloaderConfiguration) {
sourceArchive.unpack(gemDirectory)
dataFile.unpack(outputDirectory)
} finally {
gemDirectory.safeDeleteRecursively(force = true)
gemDirectory.safeDeleteRecursively()
}
} else {
sourceArchive.unpackTryAllTypes(outputDirectory)
Expand All @@ -379,15 +379,15 @@ class Downloader(private val config: DownloaderConfiguration) {
"Could not unpack source artifact '${sourceArchive.absolutePath}': ${e.collectMessages()}"
}

tempDir?.safeDeleteRecursively(force = true)
tempDir?.safeDeleteRecursively()
throw DownloadException(e)
}

logger.info {
"Successfully unpacked ${sourceArtifact.url} to '${outputDirectory.absolutePath}'..."
}

tempDir?.safeDeleteRecursively(force = true)
tempDir?.safeDeleteRecursively()
return ArtifactProvenance(sourceArtifact)
}
}
Expand Down
2 changes: 1 addition & 1 deletion downloader/src/main/kotlin/WorkingTreeCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DefaultWorkingTreeCache : WorkingTreeCache {
"${getRevision()} ..."
}

getRootPath().safeDeleteRecursively(force = true)
getRootPath().safeDeleteRecursively()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal class VerifySourceArtifactCurationsCommand : CliktCommand(
println(message)
false
} finally {
tempDir.safeDeleteRecursively(force = true)
tempDir.safeDeleteRecursively()
}
} != false
}
Expand Down
2 changes: 1 addition & 1 deletion model/src/main/kotlin/utils/FileArchiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class FileArchiver(

logger.info { "Wrote archive of directory '$directory' to storage in $writeDuration." }

zipFile.parentFile.safeDeleteRecursively(force = true)
zipFile.parentFile.safeDeleteRecursively()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class UploadResultToSw360Command : OrtCommand(
logger.error { "Could not upload source attachment: " + uploadResult.failedUploads() }
}
} finally {
tempDirectory.safeDeleteRecursively(force = true)
tempDirectory.safeDeleteRecursively()
}
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/package-managers/conan/src/main/kotlin/Conan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Conan(
}

val pkgInfos = parsePackageInfos(jsonFile)
jsonFile.parentFile.safeDeleteRecursively(force = true)
jsonFile.parentFile.safeDeleteRecursively()

val packageList = removeProjectPackage(pkgInfos, definitionFile.name)
val packages = parsePackages(packageList, workingDir)
Expand Down Expand Up @@ -334,7 +334,7 @@ class Conan(
val jsonFile = createOrtTempDir().resolve("inspect.json")
run(workingDir, "inspect", pkgName, "--json", jsonFile.absolutePath)
Json.parseToJsonElement(jsonFile.readText()).jsonObject.also {
jsonFile.parentFile.safeDeleteRecursively(force = true)
jsonFile.parentFile.safeDeleteRecursively()
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/gradle/src/main/kotlin/Gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Gradle(
}
}

initScriptFile.parentFile.safeDeleteRecursively(force = true)
initScriptFile.parentFile.safeDeleteRecursively()

val repositories = dependencyTreeModel.repositories.map {
// TODO: Also handle authentication and snapshot policy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MavenFunTest : StringSpec({
// Delete the parent POM from the local repository to make sure it has to be resolved from Maven central.
Os.userHomeDirectory
.resolve(".m2/repository/org/springframework/boot/spring-boot-starter-parent/1.5.3.RELEASE")
.safeDeleteRecursively(force = true)
.safeDeleteRecursively()

val definitionFile = getAssetFile("projects/synthetic/maven-parent/pom.xml")
val expectedResultFile = getAssetFile("projects/synthetic/maven-parent-expected-output-root.yml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ internal object NuGetInspector : CommandLineTool {
run(workingDir, *commandLineOptions.toTypedArray())
outputFile.inputStream().use { json.decodeFromStream(it) }
} finally {
workingDir.resolve(".cache").safeDeleteRecursively(force = true)
outputFile.parentFile.safeDeleteRecursively(force = true)
workingDir.resolve(".cache").safeDeleteRecursively()
outputFile.parentFile.safeDeleteRecursively()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PythonInspectorFunTest : StringSpec({
analyzeSetupPyInsecurely = true
)
} finally {
workingDir.resolve(".cache").safeDeleteRecursively(force = true)
workingDir.resolve(".cache").safeDeleteRecursively()
}

result.projects should haveSize(2)
Expand All @@ -63,7 +63,7 @@ class PythonInspectorFunTest : StringSpec({
analyzeSetupPyInsecurely = false
)
} finally {
workingDir.resolve(".cache").safeDeleteRecursively(force = true)
workingDir.resolve(".cache").safeDeleteRecursively()
}

result.projects should haveSize(1)
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/python/src/main/kotlin/Pip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Pip(
analyzeSetupPyInsecurely = analyzeSetupPyInsecurely
)
} finally {
workingDir.resolve(".cache").safeDeleteRecursively(force = true)
workingDir.resolve(".cache").safeDeleteRecursively()
}
}.onFailure { e ->
e.showStackTrace()
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/python/src/main/kotlin/Poetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Poetry(
return Pip(managerName, analysisRoot, analyzerConfig, repoConfig).runPythonInspector(requirementsFile) {
detectPythonVersion(workingDir)
}.also {
requirementsFile.parentFile.safeDeleteRecursively(force = true)
requirementsFile.parentFile.safeDeleteRecursively()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal object PythonInspector : CommandLineTool {
run(workingDir, *commandLineOptions.toTypedArray())
outputFile.inputStream().use { json.decodeFromStream(it) }
} finally {
outputFile.parentFile.safeDeleteRecursively(force = true)
outputFile.parentFile.safeDeleteRecursively()
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/sbt/src/main/kotlin/Sbt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Sbt(
resolve("project").mkdir()
}

return super.getVersion(dummyProjectDir).also { dummyProjectDir.safeDeleteRecursively(force = true) }
return super.getVersion(dummyProjectDir).also { dummyProjectDir.safeDeleteRecursively() }
}

override fun getVersionArguments() = "${SBT_OPTIONS.joinToString(" ")} sbtVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ private fun SpdxExternalDocumentReference.resolveFromDownload(

ResolutionResult(document, uri, verifyChecksum(file, baseUri, managerName))
} finally {
tempDir.safeDeleteRecursively(force = true)
tempDir.safeDeleteRecursively()
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/scanners/boyterlc/src/main/kotlin/BoyterLc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BoyterLc internal constructor(name: String, private val wrapperConfig: Sca
if (stderr.isNotBlank()) logger.debug { stderr }
if (isError) throw ScanException(errorMessage)

resultFile.readText().also { resultFile.parentFile.safeDeleteRecursively(force = true) }
resultFile.readText().also { resultFile.parentFile.safeDeleteRecursively() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/scanners/dos/src/main/kotlin/DosScanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class DosScanner internal constructor(
val zipFile = tmpDir.resolve(zipName)

sourceDir.packZip(zipFile)
sourceDir.safeDeleteRecursively(force = true)
sourceDir.safeDeleteRecursively()

val uploadUrl = client.getUploadUrl(zipName)
if (uploadUrl == null) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/scanners/scancode/src/main/kotlin/ScanCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class ScanCode internal constructor(
// Do not throw yet if the process exited with an error as some errors might turn out to be tolerable during
// parsing.

resultFile.readText().also { resultFile.parentFile.safeDeleteRecursively(force = true) }
resultFile.readText().also { resultFile.parentFile.safeDeleteRecursively() }
}
}

Expand Down
4 changes: 2 additions & 2 deletions scanner/src/main/kotlin/Scanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ class Scanner(
ScanResult(provenance, scanner.details, summaryWithMappedLicenses)
}
} finally {
downloadDir.safeDeleteRecursively(force = true)
downloadDir.safeDeleteRecursively()
}
}

Expand Down Expand Up @@ -718,7 +718,7 @@ class Scanner(
)
)
} finally {
dir?.safeDeleteRecursively(force = true)
dir?.safeDeleteRecursively()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion scanner/src/main/kotlin/provenance/ProvenanceDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class DefaultProvenanceDownloader(
// Make sure that all nested repositories are removed. Even though we do not clone recursively above, nested
// repositories could exist if the same working tree was previously cloned recursively.
workingTree.getNested().forEach { (path, _) ->
root.resolve(path).safeDeleteRecursively(force = true)
root.resolve(path).safeDeleteRecursively()
}

// We need to make a copy of the working tree, because it could be used by another coroutine after this
Expand Down
4 changes: 2 additions & 2 deletions scanner/src/main/kotlin/storages/Sw360Storage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Sw360Storage(
throw ScanStorageException(message)
}

tempScanResultFile.safeDeleteRecursively(force = true)
tempScanResultFile.safeDeleteRecursively()

return result
}
Expand Down Expand Up @@ -136,7 +136,7 @@ class Sw360Storage(
throw ScanStorageException(message)
}

tempScanResultFile.safeDeleteRecursively(force = true)
tempScanResultFile.safeDeleteRecursively()

return result
}
Expand Down
2 changes: 1 addition & 1 deletion scanner/src/main/kotlin/utils/FileListResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal class FileListResolver(
try {
storage.putFileList(provenance, it)
} finally {
dir.safeDeleteRecursively(force = true)
dir.safeDeleteRecursively()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SafeDeleteRecursivelyFunTest : WordSpec({
}

shouldNotThrow<IOException> {
dir.safeDeleteRecursively(force = true)
dir.safeDeleteRecursively()
}

dir.exists() shouldBe false
Expand All @@ -61,7 +61,7 @@ class SafeDeleteRecursivelyFunTest : WordSpec({
Git().download(pkg, nodeDir)

shouldNotThrow<IOException> {
nodeDir.safeDeleteRecursively(force = true)
nodeDir.safeDeleteRecursively()
}

nodeDir.exists() shouldBe false
Expand All @@ -73,7 +73,7 @@ class SafeDeleteRecursivelyFunTest : WordSpec({
val deleteDir = baseDir.resolve("c/delete").apply { safeMkdirs() }

shouldNotThrow<IOException> {
deleteDir.safeDeleteRecursively(force = true, baseDir)
deleteDir.safeDeleteRecursively(baseDir)
}

deleteDir.exists() shouldBe false
Expand Down
2 changes: 1 addition & 1 deletion utils/common/src/main/kotlin/ArchiveUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fun File.unpackDeb(targetDirectory: File, filter: (ArchiveEntry) -> Boolean = {
file.unpack(subDirectory, filter = filter)
}
} finally {
tempDir.safeDeleteRecursively(force = true)
tempDir.safeDeleteRecursively()
}
}

Expand Down
9 changes: 4 additions & 5 deletions utils/common/src/main/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,11 @@ fun File.isSymbolicLink(): Boolean =
fun File.realFile(): File = toPath().toRealPath().toFile()

/**
* Delete files recursively without following symbolic links (Unix) or junctions (Windows). If [force] is `true`, files
* which were not deleted in the first attempt are set to be writable and then tried to be deleted again. If
* [baseDirectory] is given, all empty parent directories along the path to [baseDirectory] are also deleted;
* [baseDirectory] itself is not deleted. Throws an [IOException] if a file could not be deleted.
* Delete a directory recursively without following symbolic links (Unix) or junctions (Windows). If a [baseDirectory]
* is provided, all empty parent directories along the path to [baseDirectory] are also deleted; [baseDirectory] itself
* is not deleted. Throws an [IOException] if a directory or file could not be deleted.
*/
fun File.safeDeleteRecursively(force: Boolean = false, baseDirectory: File? = null) {
fun File.safeDeleteRecursively(baseDirectory: File? = null) {
// Note that Kotlin's `File.deleteRecursively()` extension function does not work here to delete files with
// unmappable characters in their names, so use the `Path.deleteRecursively()` extension function instead.
toPath().deleteRecursively()
Expand Down

0 comments on commit bd4e76e

Please sign in to comment.