本文整理汇总了Golang中github.com/appc/docker2aci/lib/internal/tarball.Walk函数的典型用法代码示例。如果您正苦于以下问题:Golang Walk函数的具体用法?Golang Walk怎么用?Golang Walk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Walk函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getParent
func getParent(file *os.File, imgID string) (string, error) {
var parent string
_, err := file.Seek(0, 0)
if err != nil {
return "", fmt.Errorf("error seeking file: %v", err)
}
jsonPath := filepath.Join(imgID, "json")
parentWalker := func(t *tarball.TarFile) error {
if filepath.Clean(t.Name()) == jsonPath {
jsonb, err := ioutil.ReadAll(t.TarStream)
if err != nil {
return fmt.Errorf("error reading layer json: %v", err)
}
var dockerData types.DockerImageData
if err := json.Unmarshal(jsonb, &dockerData); err != nil {
return fmt.Errorf("error unmarshaling layer data: %v", err)
}
parent = dockerData.Parent
}
return nil
}
tr := tar.NewReader(file)
if err := tarball.Walk(*tr, parentWalker); err != nil {
return "", err
}
log.Debug(fmt.Sprintf("Layer %q depends on layer %q", imgID, parent))
return parent, nil
}
示例2: getTarFileBytes
func getTarFileBytes(file *os.File, path string) ([]byte, error) {
_, err := file.Seek(0, 0)
if err != nil {
return nil, fmt.Errorf("error seeking file: %v", err)
}
var fileBytes []byte
fileWalker := func(t *tarball.TarFile) error {
if filepath.Clean(t.Name()) == path {
fileBytes, err = ioutil.ReadAll(t.TarStream)
if err != nil {
return err
}
}
return nil
}
tr := tar.NewReader(file)
if err := tarball.Walk(*tr, fileWalker); err != nil {
return nil, err
}
if fileBytes == nil {
return nil, fmt.Errorf("file %q not found", path)
}
return fileBytes, nil
}
示例3: extractEmbeddedLayer
func extractEmbeddedLayer(file *os.File, layerTarPath string, outputPath string) (*os.File, error) {
log.Info("Extracting ", layerTarPath, "\n")
_, err := file.Seek(0, 0)
if err != nil {
return nil, fmt.Errorf("error seeking file: %v", err)
}
var layerFile *os.File
fileWalker := func(t *tarball.TarFile) error {
if filepath.Clean(t.Name()) == layerTarPath {
layerFile, err = os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating layer: %v", err)
}
_, err = io.Copy(layerFile, t.TarStream)
if err != nil {
return fmt.Errorf("error getting layer: %v", err)
}
}
return nil
}
tr := tar.NewReader(file)
if err := tarball.Walk(*tr, fileWalker); err != nil {
return nil, err
}
if layerFile == nil {
return nil, fmt.Errorf("file %q not found", layerTarPath)
}
return layerFile, nil
}
示例4: getDataFromManifest
func getDataFromManifest(file *os.File, manifestID string) (string, []string, error) {
_, err := file.Seek(0, 0)
if err != nil {
return "", nil, fmt.Errorf("error seeking file: %v", err)
}
parts := append([]string{"blobs"}, strings.Split(manifestID, ":")...)
jsonPath := path.Join(parts...)
var imageID string
var ancestry []string
reposWalker := func(t *tarball.TarFile) error {
clean := filepath.Clean(t.Name())
if clean == jsonPath {
manb, err := ioutil.ReadAll(t.TarStream)
if err != nil {
return fmt.Errorf("error reading image manifest: %v", err)
}
var manifest typesV2.ImageManifest
if err := json.Unmarshal(manb, &manifest); err != nil {
return fmt.Errorf("error unmarshaling image manifest")
}
if manifest.Config == nil {
return fmt.Errorf("manifest does not contain a config")
}
imageID = manifest.Config.Digest
// put them in reverse order
for i := len(manifest.Layers) - 1; i >= 0; i-- {
ancestry = append(ancestry, manifest.Layers[i].Digest)
}
}
return nil
}
tr := tar.NewReader(file)
if err := tarball.Walk(*tr, reposWalker); err != nil {
return "", nil, err
}
return imageID, ancestry, nil
}
示例5: getImageID
func getImageID(file *os.File, dockerURL *types.ParsedDockerURL, name string) (string, []string, *types.ParsedDockerURL, error) {
log.Debug("getting image id...")
type tags map[string]string
type apps map[string]tags
_, err := file.Seek(0, 0)
if err != nil {
return "", nil, nil, fmt.Errorf("error seeking file: %v", err)
}
tag := "latest"
if dockerURL != nil {
tag = dockerURL.Tag
}
var imageID string
var ancestry []string
var appName string
reposWalker := func(t *tarball.TarFile) error {
clean := filepath.Clean(t.Name())
if clean == "repositories" {
repob, err := ioutil.ReadAll(t.TarStream)
if err != nil {
return fmt.Errorf("error reading repositories file: %v", err)
}
var repositories apps
if err := json.Unmarshal(repob, &repositories); err != nil {
return fmt.Errorf("error unmarshaling repositories file")
}
if dockerURL == nil {
n := len(repositories)
switch {
case n == 1:
for key, _ := range repositories {
appName = key
}
case n > 1:
var appNames []string
for key, _ := range repositories {
appNames = append(appNames, key)
}
return &common.ErrSeveralImages{
Msg: "several images found",
Images: appNames,
}
default:
return fmt.Errorf("no images found")
}
} else {
appName = dockerURL.ImageName
}
app, ok := repositories[appName]
if !ok {
return fmt.Errorf("app %q not found", appName)
}
_, ok = app[tag]
if !ok {
if len(app) == 1 {
for key, _ := range app {
tag = key
}
} else {
return fmt.Errorf("tag %q not found", tag)
}
}
if dockerURL == nil {
dockerURL = &types.ParsedDockerURL{
IndexURL: "",
Tag: tag,
ImageName: appName,
}
}
imageID = string(app[tag])
}
if clean == "refs/"+tag {
refb, err := ioutil.ReadAll(t.TarStream)
if err != nil {
return fmt.Errorf("error reading ref descriptor for tag %s: %v", tag, err)
}
if dockerURL == nil {
dockerURL = &types.ParsedDockerURL{
IndexURL: "",
Tag: tag,
ImageName: name,
}
}
var ref spec.Descriptor
if err := json.Unmarshal(refb, &ref); err != nil {
return fmt.Errorf("error unmarshaling ref descriptor for tag %s", tag)
}
imageID, ancestry, err = getDataFromManifest(file, ref.Digest)
//.........这里部分代码省略.........
示例6: writeSquashedImage
func writeSquashedImage(outputFile *os.File, renderedACI acirenderer.RenderedACI, aciProvider acirenderer.ACIProvider, manifests []schema.ImageManifest, compression common.Compression) error {
var tarWriterTarget io.WriteCloser = outputFile
switch compression {
case common.NoCompression:
case common.GzipCompression:
tarWriterTarget = gzip.NewWriter(outputFile)
defer tarWriterTarget.Close()
default:
return fmt.Errorf("unexpected compression enum value: %d", compression)
}
outputWriter := tar.NewWriter(tarWriterTarget)
defer outputWriter.Close()
finalManifest := mergeManifests(manifests)
if err := internal.WriteManifest(outputWriter, finalManifest); err != nil {
return err
}
if err := internal.WriteRootfsDir(outputWriter); err != nil {
return err
}
type hardLinkEntry struct {
firstLinkCleanName string
firstLinkHeader tar.Header
keepOriginal bool
walked bool
}
// map aciFileKey -> cleanTarget -> hardLinkEntry
hardLinks := make(map[string]map[string]hardLinkEntry)
// first pass: read all the entries and build the hardLinks map in memory
// but don't write on disk
for _, aciFile := range renderedACI {
rs, err := aciProvider.ReadStream(aciFile.Key)
if err != nil {
return err
}
defer rs.Close()
hardLinks[aciFile.Key] = map[string]hardLinkEntry{}
squashWalker := func(t *tarball.TarFile) error {
cleanName := filepath.Clean(t.Name())
// the rootfs and the squashed manifest are added separately
if cleanName == "manifest" || cleanName == "rootfs" {
return nil
}
_, keep := aciFile.FileMap[cleanName]
if keep && t.Header.Typeflag == tar.TypeLink {
cleanTarget := filepath.Clean(t.Linkname())
if _, ok := hardLinks[aciFile.Key][cleanTarget]; !ok {
_, keepOriginal := aciFile.FileMap[cleanTarget]
hardLinks[aciFile.Key][cleanTarget] = hardLinkEntry{cleanName, *t.Header, keepOriginal, false}
}
}
return nil
}
tr := tar.NewReader(rs)
if err := tarball.Walk(*tr, squashWalker); err != nil {
return err
}
}
// second pass: write on disk
for _, aciFile := range renderedACI {
rs, err := aciProvider.ReadStream(aciFile.Key)
if err != nil {
return err
}
defer rs.Close()
squashWalker := func(t *tarball.TarFile) error {
cleanName := filepath.Clean(t.Name())
// the rootfs and the squashed manifest are added separately
if cleanName == "manifest" || cleanName == "rootfs" {
return nil
}
_, keep := aciFile.FileMap[cleanName]
if link, ok := hardLinks[aciFile.Key][cleanName]; ok {
if keep != link.keepOriginal {
return fmt.Errorf("logic error: should we keep file %q?", cleanName)
}
if keep {
if err := outputWriter.WriteHeader(t.Header); err != nil {
return fmt.Errorf("error writing header: %v", err)
}
if _, err := io.Copy(outputWriter, t.TarStream); err != nil {
return fmt.Errorf("error copying file into the tar out: %v", err)
}
} else {
// The current file does not remain but there is a hard link pointing to
// it. Write the current file but with the filename of the first hard link
// pointing to it. That first hard link will not be written later, see
// variable "alreadyWritten".
//.........这里部分代码省略.........
示例7: writeACI
func writeACI(layer io.ReadSeeker, manifest schema.ImageManifest, curPwl []string, output string, compression common.Compression) (*schema.ImageManifest, error) {
aciFile, err := os.Create(output)
if err != nil {
return nil, fmt.Errorf("error creating ACI file: %v", err)
}
defer aciFile.Close()
var w io.WriteCloser = aciFile
if compression == common.GzipCompression {
w = gzip.NewWriter(aciFile)
defer w.Close()
}
trw := tar.NewWriter(w)
defer trw.Close()
if err := WriteRootfsDir(trw); err != nil {
return nil, fmt.Errorf("error writing rootfs entry: %v", err)
}
fileMap := make(map[string]struct{})
var whiteouts []string
convWalker := func(t *tarball.TarFile) error {
name := t.Name()
if name == "./" {
return nil
}
t.Header.Name = path.Join("rootfs", name)
absolutePath := strings.TrimPrefix(t.Header.Name, "rootfs")
if filepath.Clean(absolutePath) == "/dev" && t.Header.Typeflag != tar.TypeDir {
return fmt.Errorf(`invalid layer: "/dev" is not a directory`)
}
fileMap[absolutePath] = struct{}{}
if strings.Contains(t.Header.Name, "/.wh.") {
whiteouts = append(whiteouts, strings.Replace(absolutePath, ".wh.", "", 1))
return nil
}
if t.Header.Typeflag == tar.TypeLink {
t.Header.Linkname = path.Join("rootfs", t.Linkname())
}
if err := trw.WriteHeader(t.Header); err != nil {
return err
}
if _, err := io.Copy(trw, t.TarStream); err != nil {
return err
}
if !util.In(curPwl, absolutePath) {
curPwl = append(curPwl, absolutePath)
}
return nil
}
tr, err := aci.NewCompressedTarReader(layer)
if err == nil {
defer tr.Close()
// write files in rootfs/
if err := tarball.Walk(*tr.Reader, convWalker); err != nil {
return nil, err
}
} else {
// ignore errors: empty layers in tars generated by docker save are not
// valid tar files so we ignore errors trying to open them. Converted
// ACIs will have the manifest and an empty rootfs directory in any
// case.
}
newPwl := subtractWhiteouts(curPwl, whiteouts)
newPwl, err = writeStdioSymlinks(trw, fileMap, newPwl)
if err != nil {
return nil, err
}
// Let's copy the newly generated PathWhitelist to avoid unintended
// side-effects
manifest.PathWhitelist = make([]string, len(newPwl))
copy(manifest.PathWhitelist, newPwl)
if err := WriteManifest(trw, manifest); err != nil {
return nil, fmt.Errorf("error writing manifest: %v", err)
}
return &manifest, nil
}
示例8: getImageID
func getImageID(file *os.File, dockerURL *types.ParsedDockerURL) (string, *types.ParsedDockerURL, error) {
type tags map[string]string
type apps map[string]tags
_, err := file.Seek(0, 0)
if err != nil {
return "", nil, fmt.Errorf("error seeking file: %v", err)
}
var imageID string
var appName string
reposWalker := func(t *tarball.TarFile) error {
if filepath.Clean(t.Name()) == "repositories" {
repob, err := ioutil.ReadAll(t.TarStream)
if err != nil {
return fmt.Errorf("error reading repositories file: %v", err)
}
var repositories apps
if err := json.Unmarshal(repob, &repositories); err != nil {
return fmt.Errorf("error unmarshaling repositories file")
}
if dockerURL == nil {
n := len(repositories)
switch {
case n == 1:
for key, _ := range repositories {
appName = key
}
case n > 1:
var appNames []string
for key, _ := range repositories {
appNames = append(appNames, key)
}
return &common.ErrSeveralImages{
Msg: "several images found",
Images: appNames,
}
default:
return fmt.Errorf("no images found")
}
} else {
appName = dockerURL.ImageName
}
tag := "latest"
if dockerURL != nil {
tag = dockerURL.Tag
}
app, ok := repositories[appName]
if !ok {
return fmt.Errorf("app %q not found", appName)
}
_, ok = app[tag]
if !ok {
if len(app) == 1 {
for key, _ := range app {
tag = key
}
} else {
return fmt.Errorf("tag %q not found", tag)
}
}
if dockerURL == nil {
dockerURL = &types.ParsedDockerURL{
IndexURL: "",
Tag: tag,
ImageName: appName,
}
}
imageID = string(app[tag])
}
return nil
}
tr := tar.NewReader(file)
if err := tarball.Walk(*tr, reposWalker); err != nil {
return "", nil, err
}
if imageID == "" {
return "", nil, fmt.Errorf("repositories file not found")
}
return imageID, dockerURL, nil
}