github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/builder/tarsum.go (about) 1 package builder 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 9 "github.com/docker/docker/pkg/archive" 10 "github.com/docker/docker/pkg/chrootarchive" 11 "github.com/docker/docker/pkg/ioutils" 12 "github.com/docker/docker/pkg/symlink" 13 "github.com/docker/docker/pkg/tarsum" 14 ) 15 16 type tarSumContext struct { 17 root string 18 sums tarsum.FileInfoSums 19 } 20 21 func (c *tarSumContext) Close() error { 22 return os.RemoveAll(c.root) 23 } 24 25 func convertPathError(err error, cleanpath string) error { 26 if err, ok := err.(*os.PathError); ok { 27 err.Path = cleanpath 28 return err 29 } 30 return err 31 } 32 33 func (c *tarSumContext) Open(path string) (io.ReadCloser, error) { 34 cleanpath, fullpath, err := c.normalize(path) 35 if err != nil { 36 return nil, err 37 } 38 r, err := os.Open(fullpath) 39 if err != nil { 40 return nil, convertPathError(err, cleanpath) 41 } 42 return r, nil 43 } 44 45 func (c *tarSumContext) Stat(path string) (string, FileInfo, error) { 46 cleanpath, fullpath, err := c.normalize(path) 47 if err != nil { 48 return "", nil, err 49 } 50 51 st, err := os.Lstat(fullpath) 52 if err != nil { 53 return "", nil, convertPathError(err, cleanpath) 54 } 55 56 rel, err := filepath.Rel(c.root, fullpath) 57 if err != nil { 58 return "", nil, convertPathError(err, cleanpath) 59 } 60 61 // We set sum to path by default for the case where GetFile returns nil. 62 // The usual case is if relative path is empty. 63 sum := path 64 // Use the checksum of the followed path(not the possible symlink) because 65 // this is the file that is actually copied. 66 if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil { 67 sum = tsInfo.Sum() 68 } 69 fi := &HashedFileInfo{PathFileInfo{st, fullpath, filepath.Base(cleanpath)}, sum} 70 return rel, fi, nil 71 } 72 73 // MakeTarSumContext returns a build Context from a tar stream. 74 // 75 // It extracts the tar stream to a temporary folder that is deleted as soon as 76 // the Context is closed. 77 // As the extraction happens, a tarsum is calculated for every file, and the set of 78 // all those sums then becomes the source of truth for all operations on this Context. 79 // 80 // Closing tarStream has to be done by the caller. 81 func MakeTarSumContext(tarStream io.Reader) (ModifiableContext, error) { 82 root, err := ioutils.TempDir("", "docker-builder") 83 if err != nil { 84 return nil, err 85 } 86 87 tsc := &tarSumContext{root: root} 88 89 // Make sure we clean-up upon error. In the happy case the caller 90 // is expected to manage the clean-up 91 defer func() { 92 if err != nil { 93 tsc.Close() 94 } 95 }() 96 97 decompressedStream, err := archive.DecompressStream(tarStream) 98 if err != nil { 99 return nil, err 100 } 101 102 sum, err := tarsum.NewTarSum(decompressedStream, true, tarsum.Version1) 103 if err != nil { 104 return nil, err 105 } 106 107 if err := chrootarchive.Untar(sum, root, nil); err != nil { 108 return nil, err 109 } 110 111 tsc.sums = sum.GetSums() 112 113 return tsc, nil 114 } 115 116 func (c *tarSumContext) normalize(path string) (cleanpath, fullpath string, err error) { 117 cleanpath = filepath.Clean(string(os.PathSeparator) + path)[1:] 118 fullpath, err = symlink.FollowSymlinkInScope(filepath.Join(c.root, path), c.root) 119 if err != nil { 120 return "", "", fmt.Errorf("Forbidden path outside the build context: %s (%s)", path, fullpath) 121 } 122 _, err = os.Lstat(fullpath) 123 if err != nil { 124 return "", "", convertPathError(err, path) 125 } 126 return 127 } 128 129 func (c *tarSumContext) Walk(root string, walkFn WalkFunc) error { 130 root = filepath.Join(c.root, filepath.Join(string(filepath.Separator), root)) 131 return filepath.Walk(root, func(fullpath string, info os.FileInfo, err error) error { 132 rel, err := filepath.Rel(c.root, fullpath) 133 if err != nil { 134 return err 135 } 136 if rel == "." { 137 return nil 138 } 139 140 sum := rel 141 if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil { 142 sum = tsInfo.Sum() 143 } 144 fi := &HashedFileInfo{PathFileInfo{FileInfo: info, FilePath: fullpath}, sum} 145 if err := walkFn(rel, fi, nil); err != nil { 146 return err 147 } 148 return nil 149 }) 150 } 151 152 func (c *tarSumContext) Remove(path string) error { 153 _, fullpath, err := c.normalize(path) 154 if err != nil { 155 return err 156 } 157 return os.RemoveAll(fullpath) 158 }