...

Source file src/github.com/Microsoft/hcsshim/cmd/wclayer/import.go

Documentation: github.com/Microsoft/hcsshim/cmd/wclayer

     1  //go:build windows
     2  
     3  package main
     4  
     5  import (
     6  	"bufio"
     7  	"compress/gzip"
     8  	"context"
     9  	"io"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/Microsoft/go-winio"
    14  	"github.com/Microsoft/hcsshim/internal/appargs"
    15  	"github.com/Microsoft/hcsshim/pkg/ociwclayer"
    16  	"github.com/urfave/cli"
    17  )
    18  
    19  var importCommand = cli.Command{
    20  	Name:  "import",
    21  	Usage: "imports a layer from a tar file",
    22  	Flags: []cli.Flag{
    23  		cli.StringSliceFlag{
    24  			Name:  "layer, l",
    25  			Usage: "paths to the read-only parent layers",
    26  		},
    27  		cli.StringFlag{
    28  			Name:  "input, i",
    29  			Usage: "input layer tar (defaults to stdin)",
    30  		},
    31  	},
    32  	ArgsUsage: "<layer path>",
    33  	Before:    appargs.Validate(appargs.NonEmptyString),
    34  	Action: func(cliContext *cli.Context) (err error) {
    35  		path, err := filepath.Abs(cliContext.Args().First())
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		layers, err := normalizeLayers(cliContext.StringSlice("layer"), false)
    41  		if err != nil {
    42  			return err
    43  		}
    44  
    45  		fp := cliContext.String("input")
    46  		f := os.Stdin
    47  		if fp != "" {
    48  			f, err = os.Open(fp)
    49  			if err != nil {
    50  				return err
    51  			}
    52  			defer f.Close()
    53  		}
    54  		r, err := addDecompressor(f)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		err = winio.EnableProcessPrivileges([]string{winio.SeBackupPrivilege, winio.SeRestorePrivilege})
    59  		if err != nil {
    60  			return err
    61  		}
    62  		_, err = ociwclayer.ImportLayerFromTar(context.Background(), r, path, layers)
    63  		return err
    64  	},
    65  }
    66  
    67  func addDecompressor(r io.Reader) (io.Reader, error) {
    68  	b := bufio.NewReader(r)
    69  	hdr, err := b.Peek(3)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	if hdr[0] == 0x1f && hdr[1] == 0x8b && hdr[2] == 8 {
    74  		return gzip.NewReader(b)
    75  	}
    76  	return b, nil
    77  }
    78  

View as plain text