@@ -1174,39 +1174,55 @@ func LayerReconvertFunc(opt UnpackOption) containerdReconverter.ConvertFunc {
11741174 if err != nil {
11751175 return nil , errors .Wrap (err , "open blob writer" )
11761176 }
1177+ defer cw .Close ()
11771178
11781179 var gw io.WriteCloser
11791180 var mediaType string
1180- switch opt .Compressor {
1181+ compressor := opt .Compressor
1182+ if compressor == "" {
1183+ compressor = "gzip"
1184+ }
1185+ switch compressor {
11811186 case "gzip" :
11821187 gw = gzip .NewWriter (cw )
11831188 mediaType = ocispec .MediaTypeImageLayerGzip
1184- default :
1189+ case "zstd" :
11851190 gw , err = zstd .NewWriter (cw )
11861191 if err != nil {
11871192 return nil , errors .Wrap (err , "create zstd writer" )
11881193 }
11891194 mediaType = ocispec .MediaTypeImageLayerZstd
1195+ case "uncompressed" :
1196+ gw = cw
1197+ mediaType = ocispec .MediaTypeImageLayer
1198+ default :
1199+ return nil , errors .Errorf ("unsupported compressor type: %s (support: gzip, zstd, uncompressed)" , opt .Compressor )
11901200 }
1191- var data bytes.Buffer
1192- writer := io .Writer (& data )
1201+
11931202 uncompressedDgster := digest .SHA256 .Digester ()
1203+ pr , pw := io .Pipe ()
11941204
1195- err = Unpack (ctx , ra , writer , opt )
1196- if err != nil {
1197- return nil , errors .Wrap (err , "unpack nydus to tar" )
1198- }
1205+ // Unpack nydus blob to pipe writer in background
1206+ go func () {
1207+ defer pw .Close ()
1208+ if err := Unpack (ctx , ra , pw , opt ); err != nil {
1209+ pw .CloseWithError (errors .Wrap (err , "unpack nydus to tar" ))
1210+ }
1211+ }()
11991212
1213+ // Stream data from pipe reader to compressed writer and digester
12001214 compressed := io .MultiWriter (gw , uncompressedDgster .Hash ())
1201- // _, err = uncompressedDgster.Hash().Write(data.Bytes())
1202-
12031215 buffer := bufPool .Get ().(* []byte )
12041216 defer bufPool .Put (buffer )
1205- if _ , err = io .CopyBuffer (compressed , bytes . NewReader ( data . Bytes ()) , * buffer ); err != nil {
1206- return nil , errors .Wrapf (err , "copy bootstrap targz into content store " )
1217+ if _ , err = io .CopyBuffer (compressed , pr , * buffer ); err != nil {
1218+ return nil , errors .Wrapf (err , "copy to compressed writer " )
12071219 }
1208- if err = gw .Close (); err != nil {
1209- return nil , errors .Wrap (err , "close gzip writer" )
1220+
1221+ // Close compressor writer if different from content writer
1222+ if gw != cw {
1223+ if err = gw .Close (); err != nil {
1224+ return nil , errors .Wrap (err , "close compressor writer" )
1225+ }
12101226 }
12111227
12121228 uncompressedDigest := uncompressedDgster .Digest ()
0 commit comments