@@ -113,6 +113,7 @@ func buildCosiFile(sourceDir string, outputFile string, partitions []outputParti
113
113
114
114
veritySourcePath := path .Join (sourceDir , hashPartition .PartitionFilename )
115
115
imageDataEntry .VeritySource = veritySourcePath
116
+ break
116
117
}
117
118
}
118
119
@@ -177,41 +178,40 @@ func buildCosiFile(sourceDir string, outputFile string, partitions []outputParti
177
178
}
178
179
179
180
func addToCosi (data ImageBuildData , tw * tar.Writer ) error {
180
- imageFile , err := os .Open (data .Source )
181
- if err != nil {
182
- return fmt .Errorf ("failed to open image file:\n %w" , err )
183
- }
184
- defer imageFile .Close ()
185
-
186
- addFileToCosi (tw , data .Metadata .Image .Path , int64 (data .Metadata .Image .CompressedSize ), imageFile )
181
+ addFileToCosi (tw , data .Source , data .Metadata .Image )
187
182
188
183
if data .VeritySource != "" && data .Metadata .Verity != nil {
189
- verityFile , err := os . Open ( data .VeritySource )
184
+ err := addFileToCosi ( tw , data .VeritySource , data . Metadata . Verity . Image )
190
185
if err != nil {
191
- return fmt .Errorf ("failed to open verity file:\n %w" , err )
186
+ return fmt .Errorf ("failed to add verity file to COSI :\n %w" , err )
192
187
}
193
- defer verityFile .Close ()
194
- addFileToCosi (tw , data .Metadata .Verity .Image .Path , int64 (data .Metadata .Verity .Image .CompressedSize ), verityFile )
195
188
}
196
189
197
190
return nil
198
191
}
199
192
200
- func addFileToCosi (tw * tar.Writer , name string , size int64 , file * os.File ) error {
201
- err := tw .WriteHeader (& tar.Header {
193
+ func addFileToCosi (tw * tar.Writer , source string , image ImageFile ) error {
194
+ file , err := os .Open (source )
195
+ if err != nil {
196
+ return fmt .Errorf ("failed to open file :\n %w" , err )
197
+ }
198
+ defer file .Close ()
199
+
200
+ err = tw .WriteHeader (& tar.Header {
202
201
Typeflag : tar .TypeReg ,
203
- Name : name ,
204
- Size : size ,
202
+ Name : image . Path ,
203
+ Size : int64 ( image . CompressedSize ) ,
205
204
Mode : 0o400 ,
206
205
Format : tar .FormatPAX ,
207
206
})
207
+
208
208
if err != nil {
209
- return fmt .Errorf ("failed to write tar header for file '%s':\n %w" , name , err )
209
+ return fmt .Errorf ("failed to write tar header for file '%s':\n %w" , image . Path , err )
210
210
}
211
211
212
212
_ , err = io .Copy (tw , file )
213
213
if err != nil {
214
- return fmt .Errorf ("failed to write image '%s' to COSI:\n %w" , name , err )
214
+ return fmt .Errorf ("failed to write image '%s' to COSI:\n %w" , image . Path , err )
215
215
}
216
216
217
217
return nil
0 commit comments