@@ -20,33 +20,14 @@ package archive
2020
2121import (
2222 "archive/tar"
23- "bufio"
24- "context"
2523 "fmt"
26- "io"
2724 "os"
28- "path"
29- "path/filepath"
3025 "strings"
3126
32- "github.com/Microsoft/go-winio"
33- "github.com/Microsoft/go-winio/backuptar"
34- "github.com/Microsoft/hcsshim"
3527 "github.com/containerd/containerd/sys"
3628 "github.com/pkg/errors"
3729)
3830
39- var (
40- // mutatedFiles is a list of files that are mutated by the import process
41- // and must be backed up and restored.
42- mutatedFiles = map [string ]string {
43- "UtilityVM/Files/EFI/Microsoft/Boot/BCD" : "bcd.bak" ,
44- "UtilityVM/Files/EFI/Microsoft/Boot/BCD.LOG" : "bcd.log.bak" ,
45- "UtilityVM/Files/EFI/Microsoft/Boot/BCD.LOG1" : "bcd.log1.bak" ,
46- "UtilityVM/Files/EFI/Microsoft/Boot/BCD.LOG2" : "bcd.log2.bak" ,
47- }
48- )
49-
5031// tarName returns platform-specific filepath
5132// to canonical posix-style path for tar archival. p is relative
5233// path.
@@ -141,121 +122,3 @@ func copyDirInfo(fi os.FileInfo, path string) error {
141122func copyUpXAttrs (dst , src string ) error {
142123 return nil
143124}
144-
145- // applyWindowsLayer applies a tar stream of an OCI style diff tar of a Windows
146- // layer using the hcsshim layer writer and backup streams.
147- // See https://github.com/opencontainers/image-spec/blob/master/layer.md#applying-changesets
148- func applyWindowsLayer (ctx context.Context , root string , r io.Reader , options ApplyOptions ) (size int64 , err error ) {
149- home , id := filepath .Split (root )
150- info := hcsshim.DriverInfo {
151- HomeDir : home ,
152- }
153-
154- w , err := hcsshim .NewLayerWriter (info , id , options .Parents )
155- if err != nil {
156- return 0 , err
157- }
158- defer func () {
159- if err2 := w .Close (); err2 != nil {
160- // This error should not be discarded as a failure here
161- // could result in an invalid layer on disk
162- if err == nil {
163- err = err2
164- }
165- }
166- }()
167-
168- tr := tar .NewReader (r )
169- buf := bufio .NewWriter (nil )
170- hdr , nextErr := tr .Next ()
171- // Iterate through the files in the archive.
172- for {
173- select {
174- case <- ctx .Done ():
175- return 0 , ctx .Err ()
176- default :
177- }
178-
179- if nextErr == io .EOF {
180- // end of tar archive
181- break
182- }
183- if nextErr != nil {
184- return 0 , nextErr
185- }
186-
187- // Note: path is used instead of filepath to prevent OS specific handling
188- // of the tar path
189- base := path .Base (hdr .Name )
190- if strings .HasPrefix (base , whiteoutPrefix ) {
191- dir := path .Dir (hdr .Name )
192- originalBase := base [len (whiteoutPrefix ):]
193- originalPath := path .Join (dir , originalBase )
194- if err := w .Remove (filepath .FromSlash (originalPath )); err != nil {
195- return 0 , err
196- }
197- hdr , nextErr = tr .Next ()
198- } else if hdr .Typeflag == tar .TypeLink {
199- err := w .AddLink (filepath .FromSlash (hdr .Name ), filepath .FromSlash (hdr .Linkname ))
200- if err != nil {
201- return 0 , err
202- }
203- hdr , nextErr = tr .Next ()
204- } else {
205- name , fileSize , fileInfo , err := backuptar .FileInfoFromHeader (hdr )
206- if err != nil {
207- return 0 , err
208- }
209- if err := w .Add (filepath .FromSlash (name ), fileInfo ); err != nil {
210- return 0 , err
211- }
212- size += fileSize
213- hdr , nextErr = tarToBackupStreamWithMutatedFiles (buf , w , tr , hdr , root )
214- }
215- }
216-
217- return
218- }
219-
220- // tarToBackupStreamWithMutatedFiles reads data from a tar stream and
221- // writes it to a backup stream, and also saves any files that will be mutated
222- // by the import layer process to a backup location.
223- func tarToBackupStreamWithMutatedFiles (buf * bufio.Writer , w io.Writer , t * tar.Reader , hdr * tar.Header , root string ) (nextHdr * tar.Header , err error ) {
224- var (
225- bcdBackup * os.File
226- bcdBackupWriter * winio.BackupFileWriter
227- )
228- if backupPath , ok := mutatedFiles [hdr .Name ]; ok {
229- bcdBackup , err = os .Create (filepath .Join (root , backupPath ))
230- if err != nil {
231- return nil , err
232- }
233- defer func () {
234- cerr := bcdBackup .Close ()
235- if err == nil {
236- err = cerr
237- }
238- }()
239-
240- bcdBackupWriter = winio .NewBackupFileWriter (bcdBackup , false )
241- defer func () {
242- cerr := bcdBackupWriter .Close ()
243- if err == nil {
244- err = cerr
245- }
246- }()
247-
248- buf .Reset (io .MultiWriter (w , bcdBackupWriter ))
249- } else {
250- buf .Reset (w )
251- }
252-
253- defer func () {
254- ferr := buf .Flush ()
255- if err == nil {
256- err = ferr
257- }
258- }()
259-
260- return backuptar .WriteBackupStreamFromTarFile (buf , t , hdr )
261- }
0 commit comments