Skip to content

Commit

Permalink
feat(scanner): Add delete functionality to storage interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Feb 21, 2024
1 parent 4cda5b4 commit 64ddf2a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,6 @@ internal class DummyProvenanceStorage : PackageProvenanceStorage {
sourceArtifact: RemoteArtifact,
result: PackageProvenanceResolutionResult
) { /* no-op */ }

override fun deleteProvenances(id: Identifier) { /* no-op */ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ class FileBasedPackageProvenanceStorage(val backend: FileStorage) : PackageProve
}
}
}

override fun deleteProvenances(id: Identifier) {
val path = storagePath(id)
if (!backend.delete(path)) {
logger.warn { "Could not delete resolved provenances for '${id.toCoordinates()}' at path '$path'." }
}
}
}

private const val FILE_NAME = "resolved_provenance.yml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ interface PackageProvenanceStorage {
* for [id] and [vcs] it is overwritten.
*/
fun writeProvenance(id: Identifier, vcs: VcsInfo, result: PackageProvenanceResolutionResult)

/**
* Delete all [PackageProvenanceResolutionResult]s for the [id].
*/
fun deleteProvenances(id: Identifier)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ class PostgresPackageProvenanceStorage(
}
}
}

override fun deleteProvenances(id: Identifier) {
database.transaction {
table.deleteWhere {
table.identifier eq id.toCoordinates()
}
}
}
}

private class PackageProvenances(tableName: String) : IntIdTable(tableName) {
Expand Down
5 changes: 5 additions & 0 deletions utils/ort/src/main/kotlin/storage/FileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ interface FileStorage {
* provided [inputStream] is closed after writing it to the file.
*/
fun write(path: String, inputStream: InputStream)

/**
* Delete the file at the given [path] and return whether the operation was successful.
*/
fun delete(path: String): Boolean
}
12 changes: 12 additions & 0 deletions utils/ort/src/main/kotlin/storage/HttpFileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,16 @@ class HttpFileStorage(
}

private fun urlForPath(path: String) = "$url/$path$query"

override fun delete(path: String): Boolean {
val request = requestBuilder()
.delete()
.url(urlForPath(path))
.build()

logger.debug { "Deleting file from storage: ${request.url}" }

val response = httpClient.execute(request)
return response.isSuccessful
}
}
3 changes: 3 additions & 0 deletions utils/ort/src/main/kotlin/storage/LocalFileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@ open class LocalFileStorage(
inputStream.use { it.copyTo(outputStream) }
}
}

@Synchronized
override fun delete(path: String): Boolean = directory.resolve(path).delete()
}
11 changes: 11 additions & 0 deletions utils/ort/src/main/kotlin/storage/S3FileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest
import software.amazon.awssdk.services.s3.model.GetObjectRequest
import software.amazon.awssdk.services.s3.model.HeadObjectRequest
import software.amazon.awssdk.services.s3.model.NoSuchKeyException
Expand Down Expand Up @@ -136,4 +137,14 @@ class S3FileStorage(
if (exception is S3Exception) logger.warn { "Can not write '$path' to S3 bucket '$bucketName'." }
}
}

override fun delete(path: String): Boolean {
val request = DeleteObjectRequest.builder()
.key(path)
.bucket(bucketName)
.build()

val response = s3Client.deleteObject(request)
return response.sdkHttpResponse().isSuccessful
}
}

0 comments on commit 64ddf2a

Please sign in to comment.