-
Notifications
You must be signed in to change notification settings - Fork 331
/
release_cleaver.ps1
200 lines (169 loc) · 6.84 KB
/
release_cleaver.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# release_cleaver.ps1
# Copyright (c) 2024 Battelle Energy Alliance, LLC. All rights reserved.
# release_cleaver.sh
# Split and join large files into 2 gigabyte chunks. sha256 sum is
# also calculated and saved on split and checked on join.
$ErrorActionPreference = "Stop"
# Split a binary file into a series of smaller files
# - FilePath - path to file to be split
# - OutDir - directory containing resultant fragment files
# - ChunkSize - maximum size of each file part
# - BufferSize - intermediate in-memory buffer size
function Split-BinaryFile {
param (
[string]$FilePath,
[string]$OutDir,
[int64]$ChunkSize = 2000000000,
[int64]$BufferSize = 1000000
)
$fileStream = [System.IO.File]::OpenRead($FilePath)
try {
$chunkIndex = 1
$bytesReadTotal = 0
while ($bytesReadTotal -lt $fileStream.Length) {
$chunkFilePath = "{0}.{1:D2}" -f (Join-Path -Path $OutDir -ChildPath (Split-Path -Path $FilePath -Leaf)), $chunkIndex
$chunkIndex++
$chunkFileStream = [System.IO.File]::Create($chunkFilePath)
try {
$bytesRead = 0
$buffer = New-Object byte[] $BufferSize
while ($bytesRead -lt $ChunkSize -and ($bytesReadTotal + $bytesRead) -lt $fileStream.Length) {
$bytesToRead = [math]::Min($ChunkSize - $bytesRead, $BufferSize)
$read = $fileStream.Read($buffer, 0, $bytesToRead)
$chunkFileStream.Write($buffer, 0, $read)
$bytesRead += $read
}
$bytesReadTotal += $bytesRead
} finally {
$chunkFileStream.Close()
}
}
} finally {
$fileStream.Close()
}
}
# Split a binary file into a series of smaller files
# - FilePaths - array of files to join (in the order to be reassembled)
# - OutputFile - Filename of resulting joined file
function Concatenate-BinaryFiles {
param (
[string[]]$FilePaths,
[string]$OutputFile
)
$outputFileStream = [System.IO.File]::Create($OutputFile)
try {
foreach ($filePath in $FilePaths) {
$inputFileStream = [System.IO.File]::OpenRead($filePath)
try {
$inputFileStream.CopyTo($outputFileStream)
} finally {
$inputFileStream.Close()
}
}
}
finally {
$outputFileStream.Close()
}
}
# first expand wildcard arguments ($args -> $allFileArgs)
$allFileArgs = @()
foreach ($filename in $args) {
$expandedFiles = Get-ChildItem -Path $filename
foreach ($expandedFile in $expandedFiles) {
If (-not ($allFileArgs -contains $expandedFile)) {
$allFileArgs += $expandedFile.FullName
}
}
}
if ($allFileArgs.Count -eq 0) {
Write-Host "Usage:"
Write-Host " $(Split-Path -Path $MyInvocation.MyCommand.Path -Leaf) <file_to_split>"
Write-Host "OR"
Write-Host " $(Split-Path -Path $MyInvocation.MyCommand.Path -Leaf) <file_to_join.00> <file_to_join.01> ... <file_to_join.sha>"
exit 1
} elseif ($allFileArgs.Count -gt 1) {
Write-Host "Joining..."
# separate the sha file from the files to join
$shaFiles = @()
$splitFiles = @()
foreach ($filename in $allFileArgs) {
if (Test-Path $filename -PathType Leaf) {
if ($filename -like "*.sha") {
$shaFiles += $filename
} else {
$splitFiles += $filename
}
} else {
Write-Host """$($filename)"" does not exist"
exit 1
}
}
# make sure the base names of the files to join match
$prevBase = ""
foreach ($filename in $splitFiles) {
$curBase = [System.IO.Path]::GetFileNameWithoutExtension($filename);
if ($prevBase -and ($prevBase -ne $curBase)) {
Write-Host "File basenames ""$($prevBase)"" and ""$($curBase)"" do not match, giving up"
exit 1
} else {
$prevBase = $curBase
}
}
$outFileBase = $prevBase
$outFile = Join-Path -Path (Get-Location) -ChildPath (Split-Path -Path $outFileBase -Leaf)
# don't overwrite an existing file
if (Test-Path $outFile -PathType Leaf) {
Write-Host "Output file ""$($outFileBase)"" already exists"
exit 1
}
# join the files
Concatenate-BinaryFiles $splitFiles $outFile
# check the results and sha sum
if (Test-Path $outFile -PathType Leaf) {
$outFileItem = Get-Item $outFile
if ($outFileItem.Length -gt 0) {
if ($shaFiles.Count -ne 1) {
Write-Host "Files joined to ""$($outFileBase)"", but could not verify file integrity"
exit 1
} else {
# calculate the sha256 sum
$outFileHash = Get-FileHash -Path $outFile -Algorithm SHA256
$outFileHashSha256 = $outFileHash.Hash.ToLower()
# Read the contents of the sha file for comparison
$shaFileContent = Get-Content $shaFiles[0]
$shaFileContents = @()
foreach ($line in $shaFileContent) {
$parts = $line -split '\s+'
if ($parts.Length -eq 2) {
$shaFileContents += @($parts[0].ToLower(), $parts[1])
break
}
}
# compare the joined file and hash from the sha file
if ($shaFileContents[0] -eq $outFileHashSha256.ToLower()) {
Write-Host """$($outFileBase)"" OK"
} else {
Write-Host """$($outFileBase)"" SHA256 hash mismatch ($($shaFileContents[0]) vs $($outFileHashSha256))"
exit 1
}
}
} else {
Write-Host "Attempted to join files to ""$($outFileBase)"", but an empty file resulted"
exit 1
}
} else {
Write-Host "Attempted to join files to ""$($outFileBase)"", but could not create the file"
exit 1
}
} else {
Write-Host "Splitting..."
$fileToSplit = $allFileArgs[0]
# generate sha256 sum file
$shaFile = Join-Path -Path (Get-Location) -ChildPath ((Split-Path -Path $fileToSplit -Leaf) + ".sha")
(Get-FileHash -Algorithm SHA256 -Path $fileToSplit | Select-Object -ExpandProperty Hash).ToLower() | Select-Object -First 64 | Out-File -FilePath $shaFile -NoNewline
Add-Content -Path $shaFile -NoNewline -Value ' '
Add-Content -Path $shaFile -Value (Split-Path -Path $fileToSplit -Leaf)
# split the file into its parts
Split-BinaryFile $fileToSplit (Get-Location)
Get-Content $shaFile | Write-Host
}