k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/conformance/image/go-runner/tar.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "archive/tar" 21 "compress/gzip" 22 "fmt" 23 "io" 24 "os" 25 "path/filepath" 26 "strings" 27 ) 28 29 // tarDir takes a source and variable writers and walks 'source' writing each file 30 // found to the tar writer. 31 func tarDir(dir, outpath string) error { 32 // ensure the src actually exists before trying to tar it 33 if _, err := os.Stat(dir); err != nil { 34 return fmt.Errorf("tar unable to stat directory %v: %w", dir, err) 35 } 36 37 outfile, err := os.Create(outpath) 38 if err != nil { 39 return fmt.Errorf("creating tarball %v: %w", outpath, err) 40 } 41 defer outfile.Close() 42 43 gzw := gzip.NewWriter(outfile) 44 defer gzw.Close() 45 46 tw := tar.NewWriter(gzw) 47 defer tw.Close() 48 49 return filepath.Walk(dir, func(file string, fi os.FileInfo, err error) error { 50 // Return on any error. 51 if err != nil { 52 return err 53 } 54 55 // Only write regular files and don't include the archive itself. 56 if !fi.Mode().IsRegular() || filepath.Join(dir, fi.Name()) == outpath { 57 return nil 58 } 59 60 // Create a new dir/file header. 61 header, err := tar.FileInfoHeader(fi, fi.Name()) 62 if err != nil { 63 return fmt.Errorf("creating file info header %v: %w", fi.Name(), err) 64 } 65 66 // Update the name to correctly reflect the desired destination when untaring. 67 header.Name = strings.TrimPrefix(strings.Replace(file, dir, "", -1), string(filepath.Separator)) 68 if err := tw.WriteHeader(header); err != nil { 69 return fmt.Errorf("writing header for tarball %v: %w", header.Name, err) 70 } 71 72 // Open files, copy into tarfile, and close. 73 f, err := os.Open(file) 74 if err != nil { 75 return fmt.Errorf("opening file %v for writing into tarball: %w", file, err) 76 } 77 defer f.Close() 78 79 _, err = io.Copy(tw, f) 80 if err != nil { 81 return fmt.Errorf("creating file %v contents into tarball: %w", file, err) 82 } 83 84 return nil 85 }) 86 }