cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.50.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/discovery/symlinks.go

84lines · modecode

1package discovery
2
3import (
4 "fmt"
5 "io/fs"
6 "log/slog"
7 "os"
8 "path/filepath"
9)
10
11type symlink struct {
12 from string
13 to string
14}
15
16func findSymlinks() (slinks []symlink, err error) {
17 err = filepath.WalkDir(".",
18 func(path string, d fs.DirEntry, err error) error {
19 if err != nil {
20 return err
21 }
22
23 if d.Type() != fs.ModeSymlink {
24 return nil
25 }
26
27 dest, err := filepath.EvalSymlinks(path)
28 if err != nil {
29 return fmt.Errorf("%s is a symlink but target file cannot be evaluated: %w", path, err)
30 }
31
32 info, err := os.Stat(dest)
33 if err != nil {
34 return fmt.Errorf("%s is a symlink but target file cannot be read: %w", path, err)
35 }
36
37 if !info.IsDir() {
38 slinks = append(slinks, symlink{from: path, to: dest})
39 }
40
41 return nil
42 })
43
44 return slinks, err
45}
46
47func addSymlinkedEntries(entries []Entry) ([]Entry, error) {
48 slinks, err := findSymlinks()
49 if err != nil {
50 return nil, err
51 }
52
53 nentries := []Entry{}
54 for _, entry := range entries {
55 if entry.State == Removed {
56 continue
57 }
58 if entry.PathError != nil {
59 continue
60 }
61 if entry.Rule.Error.Err != nil {
62 continue
63 }
64 if entry.SourcePath != entry.ReportedPath {
65 continue
66 }
67
68 for _, sl := range slinks {
69 if sl.to == entry.SourcePath {
70 slog.Debug("Found a symlink", slog.String("to", sl.to), slog.String("from", sl.from))
71 nentries = append(nentries, Entry{
72 ReportedPath: sl.to,
73 SourcePath: sl.from,
74 ModifiedLines: entry.ModifiedLines,
75 Rule: entry.Rule,
76 Owner: entry.Owner,
77 DisabledChecks: entry.DisabledChecks,
78 })
79 }
80 }
81 }
82
83 return nentries, nil
84}
85