Skip to content

Commit

Permalink
refactor: pre-factors for fix in copy_to_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed May 28, 2024
1 parent 22c33df commit 9f0e43a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
5 changes: 2 additions & 3 deletions tools/common/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"fmt"
"os"
"path"
"path/filepath"
Expand All @@ -23,7 +22,7 @@ func FileRel(basepath, targpath string) (string, error) {
func Realpath(p string) (string, error) {
t, err := os.Readlink(p)
if err != nil {
return "", fmt.Errorf("readlink %s failed: %w", p, err)
return "", err
}

if !filepath.IsAbs(t) {
Expand All @@ -32,7 +31,7 @@ func Realpath(p string) (string, error) {

info, err := os.Lstat(t)
if err != nil {
return "", fmt.Errorf("lstat %s failed: %w", p, err)
return "", err
}

if info.Mode()&os.ModeSymlink == os.ModeSymlink {
Expand Down
5 changes: 4 additions & 1 deletion tools/copy_directory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func (w *walker) copyDir(src string, dst string) error {
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
linkPath, err := common.Realpath(p)
if err != nil {
return err
if os.IsNotExist(err) {
return fmt.Errorf("failed to get realpath of dangling symlink %s: %w", p, err)
}
return fmt.Errorf("failed to get realpath of %s: %w", p, err)
}
if srcPaths[linkPath] {
// recursive symlink; silently ignore
Expand Down
49 changes: 33 additions & 16 deletions tools/copy_to_directory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
linkPath, err := common.Realpath(p)
if err != nil {
return err
if os.IsNotExist(err) {
return fmt.Errorf("failed to get realpath of dangling symlink %s: %w", p, err)
}
return fmt.Errorf("failed to get realpath of %s: %w", p, err)
}
if srcPaths[linkPath] {
// recursive symlink; silently ignore
Expand Down Expand Up @@ -215,7 +218,7 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
})
}

func (w *walker) copyPath(cfg *config, file fileInfo) error {
func (w *walker) calculateOutputPath(cfg *config, file fileInfo) (string, error) {
// Apply filters and transformations in the following order:
//
// - `include_external_repositories`
Expand All @@ -237,35 +240,35 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error {
if file.Workspace != "" && (cfg.TargetWorkspace == nil || file.Workspace != *cfg.TargetWorkspace) {
match, err := anyGlobsMatch(cfg.IncludeExternalRepositories, file.Workspace)
if err != nil {
return err
return "", err
}
if !match {
return nil // external workspace is not included
return "", nil // external workspace is not included
}
}

// apply include_srcs_packages
match, err := anyGlobsMatch(cfg.IncludeSrcsPackages, file.Package)
if err != nil {
return err
return "", err
}
if !match {
return nil // package is not included
return "", nil // package is not included
}

// apply exclude_srcs_packages
match, err = anyGlobsMatch(cfg.ExcludeSrcsPackages, file.Package)
if err != nil {
return err
return "", err
}
if match {
return nil // package is excluded
return "", nil // package is excluded
}

// apply root_paths
rootPathMatch, _, err := longestGlobsMatch(cfg.RootPaths, outputRoot)
if err != nil {
return err
return "", err
}
if rootPathMatch != "" {
outputPath = strings.TrimPrefix(outputPath[len(rootPathMatch):], "/")
Expand All @@ -274,32 +277,43 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error {
// apply include_srcs_patterns
match, err = anyGlobsMatch(cfg.IncludeSrcsPatterns, outputPath)
if err != nil {
return err
return "", err
}
if !match {
return nil // outputPath is not included
return "", nil // outputPath is not included
}

// apply exclude_srcs_patterns
match, err = anyGlobsMatch(cfg.ExcludeSrcsPatterns, outputPath)
if err != nil {
return err
return "", err
}
if match {
return nil // outputPath is excluded
return "", nil // outputPath is excluded
}

// apply replace_prefixes
replacePrefixMatch, replacePrefixIndex, err := longestGlobsMatch(cfg.ReplacePrefixesKeys, outputPath)
if err != nil {
return err
return "", err
}
if replacePrefixMatch != "" {
replaceWith := cfg.ReplacePrefixes[cfg.ReplacePrefixesKeys[replacePrefixIndex]]
outputPath = replaceWith + outputPath[len(replacePrefixMatch):]
}

outputPath = path.Join(cfg.Dst, outputPath)
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
}

// add this file to the copy Paths
dup, exists := copySet[outputPath]
Expand Down Expand Up @@ -345,7 +359,10 @@ func (w *walker) copyPaths(cfg *config) error {
// call filepath.WalkDir on the realpath
realpath, err := common.Realpath(file.Path)
if err != nil {
return err
if os.IsNotExist(err) {
return fmt.Errorf("failed to get realpath of dangling symlink %s: %w", file.Path, err)
}
return fmt.Errorf("failed to get realpath of %s: %w", file.Path, err)
}
stat, err := os.Stat(realpath)
if err != nil {
Expand Down

0 comments on commit 9f0e43a

Please sign in to comment.