Generate SSD staged import installs from typed Jerboa

ober

38b7f1b52e8bb40a65aebc7d5033792ced31090a

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 5b6b6a7..2de610b 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -15594,6 +15594,7 @@
                 truthStoreSaveTruthSnapshotLocal
                 truthStoreMergeJsonLinesLocal
                 truthStorePushRemoteDumpLocal
+                truthStoreInstallStagedZipEntryLocal
                 truthStorePullRemoteDumpLocal)
         (type AssetManager)
         (type Any)
@@ -15695,6 +15696,14 @@
                   (destination : File)
                   (text : String)) : Unit
           (kotlin-member-call atomicWriteText))
+        (extern (truthStoreAtomicWriteRaw
+                  (store : TruthStore)
+                  (destination : File)
+                  (bytes : Bytes)) : Unit
+          (kotlin-member-call atomicWrite))
+        (extern (truthStoreStagedZipEntryFile
+                  (entry : StagedZipEntry)) : File
+          (kotlin-member-get _file))
         (extern (truthStoreJsonToPrettyString
                   (json : JSONObject)
                   (indent : Int32)) : String
@@ -16598,6 +16607,69 @@
                                 connection))))))
                     (truthStoreConnectionDisconnect connection))))
               (truthStoreDeleteFileUnit archive))))
+        (def (truthStoreReadStagedZipEntryBytesLocal
+               (entry : StagedZipEntry)) : Bytes
+          (let ((input
+                  (truthStoreFileInputStream
+                    (truthStoreStagedZipEntryFile entry))))
+            (try-finally
+              (readBoundedBytes
+                input
+                (truthStoreMaxZipEntryBytes))
+              (truthStoreInputStreamClose input))))
+        (def (truthStoreInstallStagedJsonLinesEntryLocal
+               (store : TruthStore)
+               (destination : File)
+               (bytes : Bytes)) : Bool
+          (let ((before
+                  (if (fileExists destination)
+                    (truthStoreReadLocalText destination)
+                    "")))
+            (begin
+              (truthStoreMergeJsonLinesLocal
+                store
+                destination
+                (utf8->string bytes))
+              (or (localFileMissing destination)
+                  (not
+                    (localTextMatchesRemote
+                      (truthStoreReadLocalText destination)
+                      before))))))
+        (def (truthStoreInstallStagedJsonEntryLocal
+               (store : TruthStore)
+               (destination : File)
+               (entry : StagedZipEntry)
+               (bytes : Bytes)) : Bool
+          (let ((text (utf8->string bytes)))
+            (if (truthStoreShouldReplaceLocal
+                  destination
+                  text
+                  (StagedZipEntry-modified entry))
+              (begin
+                (truthStoreAtomicWriteRaw
+                  store
+                  destination
+                  bytes)
+                #t)
+              #f)))
+        (def (truthStoreInstallStagedZipEntryLocal
+               (store : TruthStore)
+               (entry : StagedZipEntry)) : Bool
+          (let ((name (StagedZipEntry-name entry)))
+            (let ((destination
+                    (truthStoreContainedImportPathLocal store name))
+                  (bytes
+                    (truthStoreReadStagedZipEntryBytesLocal entry)))
+              (if (jsonLinesFileName name)
+                (truthStoreInstallStagedJsonLinesEntryLocal
+                  store
+                  destination
+                  bytes)
+                (truthStoreInstallStagedJsonEntryLocal
+                  store
+                  destination
+                  entry
+                  bytes)))))
         (def (truthStorePullRemoteDumpLocal
                (store : TruthStore)) : Int32
           (let ((connection
@@ -16765,7 +16837,7 @@
        "    private fun ensureStorageCapacity(destination: File, bytes: Long) ="
        "        truthStoreEnsureStorageCapacityLocal(this, destination, bytes)"
        ""
-       "    private fun atomicWrite(destination: File, bytes: ByteArray) {"
+       "    internal fun atomicWrite(destination: File, bytes: ByteArray) {"
        "        ensureStorageCapacity(destination, byteArraySizeBytes(bytes))"
        "        if (!fileParentFilePresent(destination)) throw IllegalArgumentException(\"Destination has no parent\")"
        "        val parent = checkNotNull(destination.parentFile)"
@@ -16999,23 +17071,8 @@
        "        }"
        "    }"
        ""
-       "    private fun installStagedZipEntry(entry: StagedZipEntry): Boolean {"
-       "        val destination = containedImportPath(entry.name)"
-       "        val bytes = entry._file.inputStream().use { readBoundedBytes(it, MAX_ZIP_ENTRY_BYTES) }"
-       "        return if (jsonLinesFileName(entry.name)) {"
-       "            val before = if (fileExists(destination)) readLocalText(destination) else \"\""
-       "            mergeJsonLines(destination, bytes.decodeToString())"
-       "            !fileExists(destination) || readLocalText(destination) != before"
-       "        } else {"
-       "            val text = bytes.decodeToString()"
-       "            if (shouldReplaceLocal(destination, text, entry.modified)) {"
-       "                atomicWrite(destination, bytes)"
-       "                true"
-       "            } else {"
-       "                false"
-       "            }"
-       "        }"
-       "    }"
+       "    private fun installStagedZipEntry(entry: StagedZipEntry): Boolean ="
+       "        truthStoreInstallStagedZipEntryLocal(this, entry)"
        ""
        "    private fun validateImportedFile(name: String, file: File) ="
        "        truthStoreValidateImportedFileLocal(name, file)"