Setup CI testing and building #1

Merged
cyclane merged 6 commits from ci into main 2023-12-29 15:43:55 +00:00
9 changed files with 92 additions and 16 deletions
Showing only changes of commit fa49939a4d - Show all commits

View File

@ -24,11 +24,18 @@ import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import kotlin.io.path.createDirectory
/**
* AWS S3 backup client.
*/
class BackupClient(
private val s3: S3Client,
private val bucketName: String,
private val bufSize: Int = 1024 * 1024 * 100
) {
/**
* Upload a file/directory backup to AWS S3.
* @param file The File object for the file or directory.
*/
suspend fun upload(file: File) = coroutineScope {
val backupKey = "${file.name}/${Instant.now()}.zip"
PipedInputStream().use { inputStream ->
@ -58,7 +65,7 @@ class BackupClient(
partNumber = number
uploadId = upload.uploadId
body = ByteStream.fromBytes(data.take(bytesRead))
}.asCompletedPart(number)
}.toCompletedPart(number)
uploadParts.add(part)
number++
bytesRead = inputStream.readNBytes(data, 0, bufSize)
@ -92,6 +99,11 @@ class BackupClient(
backupKey
}
/**
* Restore a backup from AWS S3.
* @param destination The destination directory path for the backup contents.
* @param backupKey The S3 key of the backup.
*/
suspend fun restore(destination: Path, backupKey: String) = coroutineScope {
val req = GetObjectRequest {
bucket = bucketName
@ -107,6 +119,12 @@ class BackupClient(
}
}
/**
* Restore a single file from a backup from AWS S3.
* @param destination The destination directory path for the file from the backup.
* @param backupKey The S3 key of the backup.
* @param fileName The full name of the file to restore (including directories if it was under a subdirectory).
*/
suspend fun restoreFile(destination: Path, backupKey: String, fileName: String) = coroutineScope {
// For byte ranges refer to https://pkware.cachefly.net/webdocs/APPNOTE/APPNOTE-6.3.9.TXT
val eocdReq = GetObjectRequest {
@ -187,7 +205,12 @@ class BackupClient(
}
}
private fun UploadPartResponse.asCompletedPart(number: Int): CompletedPart {
/**
* Convert an UploadPartResponse to a CompletedPart.
* @param number The part number that was used for this part upload.
* @return The CompletedPart object.
*/
private fun UploadPartResponse.toCompletedPart(number: Int): CompletedPart {
val part = this
return CompletedPart {
partNumber = number
@ -199,10 +222,19 @@ private fun UploadPartResponse.asCompletedPart(number: Int): CompletedPart {
}
}
/**
* Take first `n` items from the beginning of a ByteArray.
* @param n The number of items to take.
* @return A ByteArray of the first `n` items.
*/
private fun ByteArray.take(n: Int) =
if (n == size) this // No copy
else asList().subList(0, n).toByteArray() // TODO: One copy (toByteArray()), not sure how to do 0 copies here
/**
* Compress a file or directory as a ZIP file to an `OutputStream`.
* @param outputStream The `OutputStream` to write the ZIP file contents to.
*/
private fun File.compressToZip(outputStream: OutputStream) = ZipOutputStream(outputStream).use { zipStream ->
val parentDir = this.absoluteFile.parent + "/"
val fileQueue = ArrayDeque<File>()
@ -226,6 +258,11 @@ private fun File.compressToZip(outputStream: OutputStream) = ZipOutputStream(out
}
}
/**
* Decompress `ZipInputStream` contents to specified destination paths.
* @param bufSize The buffer size to use for writing the decompressed files.
* @param entryNameToPath A function to convert ZIP entry names to destination `Path`s.
*/
private fun ZipInputStream.decompress(
bufSize: Int = 1024 * 1024,
entryNameToPath: (String) -> Path
@ -250,6 +287,11 @@ private fun ZipInputStream.decompress(
}
}
/**
* Set a `ZipEntry`'s attributes given a file's path.
* @param entry The `ZipEntry` to set attributes of.
* @param path The `Path` of the file to get the attributes from.
*/
private fun setZipAttributes(entry: ZipEntry, path: Path) {
try {
val attrs = Files.getFileAttributeView(path, BasicFileAttributeView::class.java).readAttributes()
@ -260,6 +302,11 @@ private fun setZipAttributes(entry: ZipEntry, path: Path) {
}
}
/**
* Set a file's attributes given a `ZipEntry`.
* @param entry The `ZipEntry` to get the attributes from.
* @param path The `Path` of the file to set the attributes of.
*/
private fun applyZipAttributes(entry: ZipEntry, path: Path) {
try {
val attrs = Files.getFileAttributeView(path, BasicFileAttributeView::class.java)

View File

@ -1,11 +1,7 @@
package backup
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.ListBucketsRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import java.io.File
import kotlin.io.path.Path
fun main() = runBlocking {
S3Client.fromEnvironment().use { s3 ->

View File

@ -3,6 +3,9 @@ package ziputils
import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Represents a partial ZIP central directory file header.
*/
internal class CentralDirectoryFileHeader(
val compressedSize: UInt,
val uncompressedSize: UInt,
@ -22,8 +25,11 @@ internal class CentralDirectoryFileHeader(
const val SIZE = 46
/**
* Create CentralDirectoryFileHeader from raw byte data.
* @throws InvalidDataException provided ByteArray is not a supported CEN.
* Create `CentralDirectoryFileHeader` from raw byte data.
* @throws InvalidDataException provided `ByteArray` is not a supported CEN.
* @param data Raw byte data.
* @param offset Skip first <offset> bytes in data array.
* @return A `CentralDirectoryFileHeader`.
*/
@Throws(InvalidDataException::class)
fun fromByteArray(data: ByteArray, offset: Int): CentralDirectoryFileHeader {

View File

@ -3,12 +3,22 @@ package ziputils
import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Represents a partial ZIP64 end of central directory locator.
*/
internal class EndOfCentralDirectoryLocator(
val endOfCentralDirectory64Offset: ULong
) {
companion object {
const val SIGNATURE = 0x07064b50U
const val SIZE = 20
/**
* Create `EndOfCentralDirectoryLocator` from raw byte data.
* @throws InvalidDataException Provided `ByteArray` is not a supported EOCD locator.
* @param data Raw byte data.
* @param offset Skip first <offset> bytes in data array.
* @return A `EndOfCentralDirectoryLocator`.
*/
@Throws(InvalidDataException::class)
fun fromByteArray(data: ByteArray, offset: Int): EndOfCentralDirectoryLocator {
if (data.size - offset < SIZE) {

View File

@ -4,8 +4,7 @@ import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Partial End of Central Directory record class.
* Only supports data required by the backup tool.
* Represents a partial ZIP end of central directory record.
*/
internal class EndOfCentralDirectoryRecord(
val centralDirectoryOffset: UInt
@ -17,8 +16,11 @@ internal class EndOfCentralDirectoryRecord(
const val SIGNATURE = 0x06054b50U
const val SIZE = 22
/**
* Create EndOfCentralDirectoryRecord from raw byte data.
* @throws InvalidDataException provided ByteArray is not a supported EOCD64.
* Create `EndOfCentralDirectoryRecord` from raw byte data.
* @throws InvalidDataException Provided `ByteArray` is not a supported EOCD64.
* @param data Raw byte data.
* @param offset Skip first <offset> bytes in data array.
* @return A `EndOfCentralDirectoryRecord`.
*/
@Throws(InvalidDataException::class)
fun fromByteArray(data: ByteArray, offset: Int): EndOfCentralDirectoryRecord {

View File

@ -4,8 +4,7 @@ import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Partial End of Central Directory record (ZIP64) class.
* Only supports data required by the backup tool.
* Represents a partial ZIP64 end of central directory record.
*/
internal class EndOfCentralDirectoryRecord64(
val centralDirectoryOffset: ULong
@ -14,8 +13,11 @@ internal class EndOfCentralDirectoryRecord64(
const val SIGNATURE = 0x06064b50U
const val SIZE = 56
/**
* Create EndOfCentralDirectoryRecord64 from raw byte data.
* @throws InvalidDataException provided ByteArray is not a supported EOCD.
* Create `EndOfCentralDirectoryRecord64` from raw byte data.
* @throws InvalidDataException Provided `ByteArray` is not a supported EOCD.
* @param data Raw byte data.
* @param offset Skip first <offset> bytes in data array.
* @return A `EndOfCentralDirectoryRecord64`.
*/
@Throws(InvalidDataException::class)
fun fromByteArray(data: ByteArray, offset: Int): EndOfCentralDirectoryRecord64 {

View File

@ -1,4 +1,11 @@
package ziputils
/**
* Represents an invalid raw byte data exception.
*/
class InvalidDataException(message: String): Exception(message)
/**
* Represents an invalid raw byte signature exception.
*/
class InvalidSignatureException(message: String): Exception(message)

View File

@ -1,5 +1,8 @@
package ziputils
/**
* Represents a partial ZIP extra field record.
*/
internal open class ExtraFieldRecord(
val id: UShort,
val size: UShort

View File

@ -1,5 +1,8 @@
package ziputils
/**
* Represents a ZIP ZIP64 extra field record (ID 0x0001).
*/
internal class Zip64ExtraFieldRecord(
size: UShort,
val uncompressedSize: ULong?,