MCPcopy Index your code
hub / github.com/coder/coder / Untar

Function Untar

provisionersdk/archive.go:150–194  ·  view source on GitHub ↗

Untar extracts the archive to a provided directory.

(directory string, r io.Reader)

Source from the content-addressed store, hash-verified

148
149// Untar extracts the archive to a provided directory.
150func Untar(directory string, r io.Reader) error {
151 tarReader := tar.NewReader(r)
152 for {
153 header, err := tarReader.Next()
154 if xerrors.Is(err, io.EOF) {
155 return nil
156 }
157 if err != nil {
158 return err
159 }
160 if header.Name == "." || strings.Contains(header.Name, "..") {
161 continue
162 }
163 // #nosec
164 target := filepath.Join(directory, filepath.FromSlash(header.Name))
165 switch header.Typeflag {
166 case tar.TypeDir:
167 if _, err := os.Stat(target); err != nil {
168 if err := os.MkdirAll(target, 0o755); err != nil {
169 return err
170 }
171 }
172 case tar.TypeReg:
173 // #nosec G115 - Safe conversion as tar header mode fits within uint32
174 err := os.MkdirAll(filepath.Dir(target), os.FileMode(header.Mode)|os.ModeDir|100)
175 if err != nil {
176 return err
177 }
178 // #nosec G115 - Safe conversion as tar header mode fits within uint32
179 file, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.FileMode(header.Mode))
180 if err != nil {
181 return err
182 }
183 // Max file size of 10MB.
184 _, err = io.CopyN(file, tarReader, (1<<20)*10)
185 if xerrors.Is(err, io.EOF) {
186 err = nil
187 }
188 if err != nil {
189 return err
190 }
191 _ = file.Close()
192 }
193 }
194}

Callers 7

TestTarFunction · 0.92
TestUntarFunction · 0.92
TestTemplatePull_ToDirFunction · 0.92
templateInitMethod · 0.92
templatePullMethod · 0.92
WriteArchiveFunction · 0.92

Calls 5

MkdirAllMethod · 0.80
NextMethod · 0.65
CloseMethod · 0.65
IsMethod · 0.45
ContainsMethod · 0.45

Tested by 4

TestTarFunction · 0.74
TestUntarFunction · 0.74
TestTemplatePull_ToDirFunction · 0.74