Skip to content

Commit

Permalink
fix: exclude files in copy_to_directory before checking their realpath
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed May 28, 2024
1 parent 32b9b7f commit d2d7546
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions tools/copy_to_directory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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)
})
}

Expand Down Expand Up @@ -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 {
Expand All @@ -329,7 +320,7 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error {

outputDir := path.Dir(outputPath)
if !mkdirSet[outputDir] {
if err = os.MkdirAll(outputDir, os.ModePerm); err != nil {
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
return err
}
// https://pkg.go.dev/path#Dir
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit d2d7546

Please sign in to comment.