From ff507ec8362884659560e0f215768c9154e69dab Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Tue, 28 May 2024 02:09:46 -0700 Subject: [PATCH] fix: exclude files in copy_to_directory before checking their realpath --- tools/copy_to_directory/main.go | 70 ++++++++++++++++----------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/tools/copy_to_directory/main.go b/tools/copy_to_directory/main.go index aadf7481d..0084e222f 100644 --- a/tools/copy_to_directory/main.go +++ b/tools/copy_to_directory/main.go @@ -157,6 +157,26 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error { return err } + f := fileInfo{ + Package: file.Package, + Path: p, + RootPath: file.RootPath, + ShortPath: path.Join(file.ShortPath, r), + Workspace: file.Workspace, + WorkspacePath: path.Join(file.WorkspacePath, r), + Hardlink: file.Hardlink, + FileInfo: info, + } + + outputPath, err := w.calculateOutputPath(cfg, f) + if err != nil { + return fmt.Errorf("failed to calculate output path %s: %w", file.WorkspacePath, err) + } + if outputPath == "" { + // this path is excluded + return nil + } + if info.Mode()&os.ModeSymlink == os.ModeSymlink { // symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion linkPath, err := common.Realpath(p) @@ -176,7 +196,7 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error { } if stat.IsDir() { // symlink points to a directory - f := fileInfo{ + f = fileInfo{ Package: file.Package, Path: linkPath, RootPath: file.RootPath, @@ -189,32 +209,12 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error { return w.copyDir(cfg, srcPaths, f) } else { // symlink points to a regular file - f := fileInfo{ - Package: file.Package, - Path: linkPath, - RootPath: file.RootPath, - ShortPath: path.Join(file.ShortPath, r), - Workspace: file.Workspace, - WorkspacePath: path.Join(file.WorkspacePath, r), - Hardlink: file.Hardlink, - FileInfo: stat, - } - return w.copyPath(cfg, f) + f.Path = linkPath + f.FileInfo = stat } } - // a regular file - f := fileInfo{ - Package: file.Package, - Path: p, - RootPath: file.RootPath, - ShortPath: path.Join(file.ShortPath, r), - Workspace: file.Workspace, - WorkspacePath: path.Join(file.WorkspacePath, r), - Hardlink: file.Hardlink, - FileInfo: info, - } - return w.copyPath(cfg, f) + return w.copyPath(cfg, f, outputPath) }) } @@ -305,16 +305,7 @@ func (w *walker) calculateOutputPath(cfg *config, file fileInfo) (string, error) return path.Join(cfg.Dst, outputPath), nil } -func (w *walker) copyPath(cfg *config, file fileInfo) error { - outputPath, err := w.calculateOutputPath(cfg, file) - if err != nil { - return fmt.Errorf("failed to calculate output path %s: %w", file.WorkspacePath, err) - } - if outputPath == "" { - // this path is excluded - return nil - } - +func (w *walker) copyPath(cfg *config, file fileInfo, outputPath string) error { // add this file to the copy Paths dup, exists := copySet[outputPath] if exists { @@ -349,6 +340,15 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error { func (w *walker) copyPaths(cfg *config) error { for _, file := range cfg.Files { + outputPath, err := w.calculateOutputPath(cfg, file) + if err != nil { + return fmt.Errorf("failed to calculate output path %s: %w", file.WorkspacePath, err) + } + if outputPath == "" { + // this path is excluded + continue + } + info, err := os.Lstat(file.Path) if err != nil { return fmt.Errorf("failed to lstat file %s: %w", file.Path, err) @@ -379,7 +379,7 @@ func (w *walker) copyPaths(cfg *config) error { return err } } else { - if err := w.copyPath(cfg, file); err != nil { + if err := w.copyPath(cfg, file, outputPath); err != nil { return err } }