Skip to content

Commit 5832332

Browse files
committed
fix: first tests of ignoring symlinks
1 parent bdd8687 commit 5832332

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

cmd/format/format.go

+23-14
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
8888

8989
// ensure db is closed after we're finished
9090
defer func() {
91-
if err := db.Close(); err != nil {
92-
log.Errorf("failed to close cache: %v", err)
91+
if e := db.Close(); e != nil {
92+
log.Errorf("failed to close cache: %v", e)
9393
}
9494
}()
9595
}
@@ -129,27 +129,36 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
129129
// checks all paths are contained within the tree root and exist
130130
// also "normalize" paths so they're relative to cfg.TreeRoot
131131
for i, path := range paths {
132-
absolutePath, err := filepath.Abs(path)
133-
if err != nil {
134-
return fmt.Errorf("error computing absolute path of %s: %w", path, err)
132+
absolutePath, e := filepath.Abs(path)
133+
if e != nil {
134+
return fmt.Errorf("error computing absolute path of %s: %w", path, e)
135135
}
136136

137-
relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath)
138-
if err != nil {
139-
return fmt.Errorf("error computing relative path from %s to %s: %w", cfg.TreeRoot, absolutePath, err)
137+
if walkType != walk.Stdin {
138+
if _, e = os.Stat(absolutePath); e != nil {
139+
return fmt.Errorf("path %s not found", path)
140+
}
141+
142+
// // symlinks are allowed on `paths` input, we resolve them here, since
143+
// // the readers will k
144+
// realPath, ee := filepath.EvalSymlinks(absolutePath)
145+
// if ee != nil {
146+
// return fmt.Errorf("could not determine real path of %s (evaluating all symlinks)", absolutePath)
147+
// }
148+
//
149+
// absolutePath = realPath
150+
}
151+
152+
relativePath, e := filepath.Rel(cfg.TreeRoot, absolutePath)
153+
if e != nil {
154+
return fmt.Errorf("error computing relative path from %s to %s: %w", cfg.TreeRoot, absolutePath, e)
140155
}
141156

142157
if strings.HasPrefix(relativePath, "..") {
143158
return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot)
144159
}
145160

146161
paths[i] = relativePath
147-
148-
if walkType != walk.Stdin {
149-
if _, err = os.Stat(absolutePath); err != nil {
150-
return fmt.Errorf("path %s not found", path)
151-
}
152-
}
153162
}
154163

155164
// create a composite formatter which will handle applying the correct formatters to each file we traverse
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../yaml/test.yaml

walk/git.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ LOOP:
9191

9292
g.log.Debugf("processing file: %s", path)
9393

94-
info, err := os.Stat(path)
94+
info, err := os.Lstat(path)
95+
9596
if os.IsNotExist(err) {
9697
// the underlying file might have been removed
9798
g.log.Warnf(
@@ -101,13 +102,18 @@ LOOP:
101102
continue
102103
} else if err != nil {
103104
return n, fmt.Errorf("failed to stat %s: %w", path, err)
105+
} else if info.Mode()&os.ModeSymlink == os.ModeSymlink {
106+
// we skip reporting symlinks stored in Git, they should
107+
// point to local files which we would list anyway.
108+
continue
104109
}
105110

106111
files[n] = &File{
107112
Path: path,
108113
RelPath: filepath.Join(g.path, entry),
109114
Info: info,
110115
}
116+
111117
n++
112118
} else {
113119
// nothing more to read

walk/walk.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ func NewReader(
233233
return reader, err
234234
}
235235

236+
// NewCompositeReader returns a composite reader for the `root` and all `paths`. It
237+
// never follows symlinks.
238+
//
236239
//nolint:ireturn
237240
func NewCompositeReader(
238241
walkType Type,
@@ -267,16 +270,21 @@ func NewCompositeReader(
267270
// create a clean absolute path
268271
path := filepath.Clean(filepath.Join(root, relPath))
269272

270-
// check the path exists
273+
// check the path exists (don't follow symlinks)
271274
info, err = os.Lstat(path)
272275
if err != nil {
273276
return nil, fmt.Errorf("failed to stat %s: %w", path, err)
274277
}
275278

276-
if info.IsDir() {
279+
switch {
280+
case info.Mode()&os.ModeSymlink == os.ModeSymlink:
281+
// for symlinks -> we ignore them since it does not make sense to follow them
282+
// as normal files in the `root` will be picked up nevertheless.
283+
continue
284+
case info.IsDir():
277285
// for directories, we honour the walk type as we traverse them
278286
readers[idx], err = NewReader(walkType, root, relPath, db, statz)
279-
} else {
287+
default:
280288
// for files, we enforce a simple filesystem read
281289
readers[idx], err = NewReader(Filesystem, root, relPath, db, statz)
282290
}

0 commit comments

Comments
 (0)