Skip to content

Commit a64a768

Browse files
committed
Replace inline applyWindowsLayer using hcsshim
Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
1 parent 149fa36 commit a64a768

File tree

5 files changed

+248
-137
lines changed

5 files changed

+248
-137
lines changed

archive/tar_opts_windows.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818

1919
package archive
2020

21+
import (
22+
"context"
23+
"io"
24+
25+
"github.com/Microsoft/hcsshim/pkg/ociwclayer"
26+
)
27+
28+
// applyWindowsLayer applies a tar stream of an OCI style diff tar of a Windows layer
29+
// See https://github.com/opencontainers/image-spec/blob/master/layer.md#applying-changesets
30+
func applyWindowsLayer(ctx context.Context, root string, r io.Reader, options ApplyOptions) (size int64, err error) {
31+
return ociwclayer.ImportLayerFromTar(ctx, r, root, options.Parents)
32+
}
33+
2134
// AsWindowsContainerLayer indicates that the tar stream to apply is that of
2235
// a Windows Container Layer. The caller must be holding SeBackupPrivilege and
2336
// SeRestorePrivilege.

archive/tar_windows.go

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,14 @@ package archive
2020

2121
import (
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 {
141122
func 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-
}

vendor/github.com/Microsoft/hcsshim/pkg/ociwclayer/export.go

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)