entriesForPath writes the given source path into tarWriter at the given dest (recursively for directories). e.g. tarring my_dir --> dest d: d/file_a, d/file_b If source path does not exist, quietly skips it and returns no err
(localPath, containerPath string)
| 248 | // e.g. tarring my_dir --> dest d: d/file_a, d/file_b |
| 249 | // If source path does not exist, quietly skips it and returns no err |
| 250 | func (a *ArchiveBuilder) entriesForPath(localPath, containerPath string) ([]archiveEntry, error) { |
| 251 | localInfo, err := os.Stat(localPath) |
| 252 | if err != nil { |
| 253 | if os.IsNotExist(err) { |
| 254 | return nil, nil |
| 255 | } |
| 256 | return nil, err |
| 257 | } |
| 258 | |
| 259 | localPathIsDir := localInfo.IsDir() |
| 260 | if localPathIsDir { |
| 261 | // Make sure we can trim this off filenames to get valid relative filepaths |
| 262 | if !strings.HasSuffix(localPath, string(filepath.Separator)) { |
| 263 | localPath += string(filepath.Separator) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | containerPath = strings.TrimPrefix(containerPath, "/") |
| 268 | |
| 269 | result := make([]archiveEntry, 0) |
| 270 | err = filepath.Walk(localPath, func(curLocalPath string, info os.FileInfo, err error) error { |
| 271 | if err != nil { |
| 272 | return fmt.Errorf("walking %q: %w", curLocalPath, err) |
| 273 | } |
| 274 | |
| 275 | linkname := "" |
| 276 | if info.Mode()&os.ModeSymlink != 0 { |
| 277 | var err error |
| 278 | linkname, err = os.Readlink(curLocalPath) |
| 279 | if err != nil { |
| 280 | return err |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | var name string |
| 285 | //nolint:gocritic |
| 286 | if localPathIsDir { |
| 287 | // Name of file in tar should be relative to source directory... |
| 288 | tmp, err := filepath.Rel(localPath, curLocalPath) |
| 289 | if err != nil { |
| 290 | return fmt.Errorf("making %q relative to %q: %w", curLocalPath, localPath, err) |
| 291 | } |
| 292 | // ...and live inside `dest` |
| 293 | name = path.Join(containerPath, filepath.ToSlash(tmp)) |
| 294 | } else if strings.HasSuffix(containerPath, "/") { |
| 295 | name = containerPath + filepath.Base(curLocalPath) |
| 296 | } else { |
| 297 | name = containerPath |
| 298 | } |
| 299 | |
| 300 | header, err := archive.FileInfoHeader(name, info, linkname) |
| 301 | if err != nil { |
| 302 | // Not all types of files are allowed in a tarball. That's OK. |
| 303 | // Mimic the Docker behavior and just skip the file. |
| 304 | return nil |
| 305 | } |
| 306 | |
| 307 | result = append(result, archiveEntry{ |