Skip to content

Commit f30debb

Browse files
authored
refactor: pre-factors for fix in copy_to_directory (#858)
1 parent 22c33df commit f30debb

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

tools/common/file.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package common
22

33
import (
4-
"fmt"
54
"os"
65
"path"
76
"path/filepath"
@@ -23,7 +22,7 @@ func FileRel(basepath, targpath string) (string, error) {
2322
func Realpath(p string) (string, error) {
2423
t, err := os.Readlink(p)
2524
if err != nil {
26-
return "", fmt.Errorf("readlink %s failed: %w", p, err)
25+
return "", err
2726
}
2827

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

3332
info, err := os.Lstat(t)
3433
if err != nil {
35-
return "", fmt.Errorf("lstat %s failed: %w", p, err)
34+
return "", err
3635
}
3736

3837
if info.Mode()&os.ModeSymlink == os.ModeSymlink {

tools/copy_directory/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ func (w *walker) copyDir(src string, dst string) error {
5050
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
5151
linkPath, err := common.Realpath(p)
5252
if err != nil {
53-
return err
53+
if os.IsNotExist(err) {
54+
return fmt.Errorf("failed to get realpath of dangling symlink %s: %w", p, err)
55+
}
56+
return fmt.Errorf("failed to get realpath of %s: %w", p, err)
5457
}
5558
if srcPaths[linkPath] {
5659
// recursive symlink; silently ignore

tools/copy_to_directory/main.go

+35-18
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
161161
// symlink to directories are intentionally never followed by filepath.Walk to avoid infinite recursion
162162
linkPath, err := common.Realpath(p)
163163
if err != nil {
164-
return err
164+
if os.IsNotExist(err) {
165+
return fmt.Errorf("failed to get realpath of dangling symlink %s: %w", p, err)
166+
}
167+
return fmt.Errorf("failed to get realpath of %s: %w", p, err)
165168
}
166169
if srcPaths[linkPath] {
167170
// recursive symlink; silently ignore
@@ -177,9 +180,9 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
177180
Package: file.Package,
178181
Path: linkPath,
179182
RootPath: file.RootPath,
180-
ShortPath: path.Join(file.ShortPath),
183+
ShortPath: file.ShortPath,
181184
Workspace: file.Workspace,
182-
WorkspacePath: path.Join(file.WorkspacePath),
185+
WorkspacePath: file.WorkspacePath,
183186
Hardlink: file.Hardlink,
184187
FileInfo: stat,
185188
}
@@ -215,7 +218,7 @@ func (w *walker) copyDir(cfg *config, srcPaths pathSet, file fileInfo) error {
215218
})
216219
}
217220

218-
func (w *walker) copyPath(cfg *config, file fileInfo) error {
221+
func (w *walker) calculateOutputPath(cfg *config, file fileInfo) (string, error) {
219222
// Apply filters and transformations in the following order:
220223
//
221224
// - `include_external_repositories`
@@ -237,35 +240,35 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error {
237240
if file.Workspace != "" && (cfg.TargetWorkspace == nil || file.Workspace != *cfg.TargetWorkspace) {
238241
match, err := anyGlobsMatch(cfg.IncludeExternalRepositories, file.Workspace)
239242
if err != nil {
240-
return err
243+
return "", err
241244
}
242245
if !match {
243-
return nil // external workspace is not included
246+
return "", nil // external workspace is not included
244247
}
245248
}
246249

247250
// apply include_srcs_packages
248251
match, err := anyGlobsMatch(cfg.IncludeSrcsPackages, file.Package)
249252
if err != nil {
250-
return err
253+
return "", err
251254
}
252255
if !match {
253-
return nil // package is not included
256+
return "", nil // package is not included
254257
}
255258

256259
// apply exclude_srcs_packages
257260
match, err = anyGlobsMatch(cfg.ExcludeSrcsPackages, file.Package)
258261
if err != nil {
259-
return err
262+
return "", err
260263
}
261264
if match {
262-
return nil // package is excluded
265+
return "", nil // package is excluded
263266
}
264267

265268
// apply root_paths
266269
rootPathMatch, _, err := longestGlobsMatch(cfg.RootPaths, outputRoot)
267270
if err != nil {
268-
return err
271+
return "", err
269272
}
270273
if rootPathMatch != "" {
271274
outputPath = strings.TrimPrefix(outputPath[len(rootPathMatch):], "/")
@@ -274,32 +277,43 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error {
274277
// apply include_srcs_patterns
275278
match, err = anyGlobsMatch(cfg.IncludeSrcsPatterns, outputPath)
276279
if err != nil {
277-
return err
280+
return "", err
278281
}
279282
if !match {
280-
return nil // outputPath is not included
283+
return "", nil // outputPath is not included
281284
}
282285

283286
// apply exclude_srcs_patterns
284287
match, err = anyGlobsMatch(cfg.ExcludeSrcsPatterns, outputPath)
285288
if err != nil {
286-
return err
289+
return "", err
287290
}
288291
if match {
289-
return nil // outputPath is excluded
292+
return "", nil // outputPath is excluded
290293
}
291294

292295
// apply replace_prefixes
293296
replacePrefixMatch, replacePrefixIndex, err := longestGlobsMatch(cfg.ReplacePrefixesKeys, outputPath)
294297
if err != nil {
295-
return err
298+
return "", err
296299
}
297300
if replacePrefixMatch != "" {
298301
replaceWith := cfg.ReplacePrefixes[cfg.ReplacePrefixesKeys[replacePrefixIndex]]
299302
outputPath = replaceWith + outputPath[len(replacePrefixMatch):]
300303
}
301304

302-
outputPath = path.Join(cfg.Dst, outputPath)
305+
return path.Join(cfg.Dst, outputPath), nil
306+
}
307+
308+
func (w *walker) copyPath(cfg *config, file fileInfo) error {
309+
outputPath, err := w.calculateOutputPath(cfg, file)
310+
if err != nil {
311+
return fmt.Errorf("failed to calculate output path %s: %w", file.WorkspacePath, err)
312+
}
313+
if outputPath == "" {
314+
// this path is excluded
315+
return nil
316+
}
303317

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

0 commit comments

Comments
 (0)