summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/pkglint.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkgtools/pkglint/files/pkglint.go')
-rw-r--r--pkgtools/pkglint/files/pkglint.go95
1 files changed, 47 insertions, 48 deletions
diff --git a/pkgtools/pkglint/files/pkglint.go b/pkgtools/pkglint/files/pkglint.go
index 1b9f0888329..8a4ff984465 100644
--- a/pkgtools/pkglint/files/pkglint.go
+++ b/pkgtools/pkglint/files/pkglint.go
@@ -10,7 +10,6 @@ import (
"os"
"os/user"
"path"
- "path/filepath"
"runtime"
"runtime/debug"
"runtime/pprof"
@@ -26,14 +25,14 @@ type Pkglint struct {
Pkgsrc Pkgsrc // Global data, mostly extracted from mk/*.
Pkg *Package // The package that is currently checked, or nil.
- Todo StringQueue // The files or directories that still need to be checked.
- Wip bool // Is the currently checked file or package from pkgsrc-wip?
- Infrastructure bool // Is the currently checked file from the pkgsrc infrastructure?
- Testing bool // Is pkglint in self-testing mode (only during development)?
- Experimental bool // For experimental features, only enabled individually in tests
- Username string // For checking against OWNER and MAINTAINER
+ Todo PathQueue // The files or directories that still need to be checked.
+ Wip bool // Is the currently checked file or package from pkgsrc-wip?
+ Infrastructure bool // Is the currently checked file from the pkgsrc infrastructure?
+ Testing bool // Is pkglint in self-testing mode (only during development)?
+ Experimental bool // For experimental features, only enabled individually in tests
+ Username string // For checking against OWNER and MAINTAINER
- cvsEntriesDir string // Cached to avoid I/O
+ cvsEntriesDir Path // Cached to avoid I/O
cvsEntries map[string]CvsEntry
Logger Logger
@@ -43,10 +42,10 @@ type Pkglint struct {
fileCache *FileCache
interner StringInterner
- // cwd is the slash-separated absolute path to the current working
+ // cwd is the absolute path to the current working
// directory. It is used for speeding up relpath and abspath.
// There is no other use for it.
- cwd string
+ cwd Path
InterPackage InterPackage
}
@@ -58,7 +57,7 @@ func NewPkglint(stdout io.Writer, stderr io.Writer) Pkglint {
p := Pkglint{
res: regex.NewRegistry(),
fileCache: NewFileCache(200),
- cwd: filepath.ToSlash(cwd),
+ cwd: NewPathSlash(cwd),
interner: NewStringInterner()}
p.Logger.out = NewSeparatorWriter(stdout)
p.Logger.err = NewSeparatorWriter(stderr)
@@ -208,8 +207,8 @@ func (pkglint *Pkglint) setUpProfiling() func() {
func (pkglint *Pkglint) prepareMainLoop() {
firstDir := pkglint.Todo.Front()
- if fileExists(firstDir) {
- firstDir = path.Dir(firstDir)
+ if firstDir.IsFile() {
+ firstDir = firstDir.Dir()
}
relTopdir := findPkgsrcTopdir(firstDir)
@@ -222,7 +221,7 @@ func (pkglint *Pkglint) prepareMainLoop() {
}
pkglint.Pkgsrc = NewPkgsrc(joinPath(firstDir, relTopdir))
- pkglint.Wip = matches(pkglint.Pkgsrc.ToRel(firstDir), `^wip(/|$)`) // Same as in Pkglint.Check.
+ pkglint.Wip = pkglint.Pkgsrc.ToRel(firstDir).HasPrefixPath("wip") // Same as in Pkglint.Check.
pkglint.Pkgsrc.LoadInfrastructure()
currentUser, err := user.Current()
@@ -283,7 +282,7 @@ func (pkglint *Pkglint) ParseCommandLine(args []string) int {
}
for _, arg := range pkglint.Opts.args {
- pkglint.Todo.Push(filepath.ToSlash(arg))
+ pkglint.Todo.Push(NewPathSlash(arg))
}
if pkglint.Todo.IsEmpty() {
pkglint.Todo.Push(".")
@@ -299,12 +298,12 @@ func (pkglint *Pkglint) ParseCommandLine(args []string) int {
//
// It sets up all the global state (infrastructure, wip) for accurately
// classifying the entry.
-func (pkglint *Pkglint) Check(dirent string) {
+func (pkglint *Pkglint) Check(dirent Path) {
if trace.Tracing {
- defer trace.Call1(dirent)()
+ defer trace.Call(dirent)()
}
- st, err := os.Lstat(dirent)
+ st, err := dirent.Lstat()
if err != nil {
NewLineWhole(dirent).Errorf("No such file or directory.")
return
@@ -313,7 +312,7 @@ func (pkglint *Pkglint) Check(dirent string) {
pkglint.checkMode(dirent, st.Mode())
}
-func (pkglint *Pkglint) checkMode(dirent string, mode os.FileMode) {
+func (pkglint *Pkglint) checkMode(dirent Path, mode os.FileMode) {
// TODO: merge duplicate code in Package.checkDirent
isDir := mode.IsDir()
isReg := mode.IsRegular()
@@ -324,14 +323,14 @@ func (pkglint *Pkglint) checkMode(dirent string, mode os.FileMode) {
dir := dirent
if !isDir {
- dir = path.Dir(dirent)
+ dir = dirent.Dir()
}
- basename := path.Base(dirent)
+ basename := dirent.Base()
pkgsrcRel := pkglint.Pkgsrc.ToRel(dirent)
- pkglint.Wip = matches(pkgsrcRel, `^wip(/|$)`)
- pkglint.Infrastructure = matches(pkgsrcRel, `^mk(/|$)`)
+ pkglint.Wip = pkgsrcRel.HasPrefixPath("wip")
+ pkglint.Infrastructure = pkgsrcRel.HasPrefixPath("mk")
pkgsrcdir := findPkgsrcTopdir(dir)
if pkgsrcdir == "" {
NewLineWhole(dirent).Errorf("Cannot determine the pkgsrc root directory for %q.", cleanpath(dir))
@@ -339,7 +338,7 @@ func (pkglint *Pkglint) checkMode(dirent string, mode os.FileMode) {
}
if isReg {
- depth := strings.Count(pkgsrcRel, "/")
+ depth := pkgsrcRel.Count() - 1 // FIXME
pkglint.checkExecutable(dirent, mode)
pkglint.checkReg(dirent, basename, depth)
return
@@ -363,9 +362,9 @@ func (pkglint *Pkglint) checkMode(dirent string, mode os.FileMode) {
// checkdirPackage checks a complete pkgsrc package, including each
// of the files individually, and also when seen in combination.
-func (pkglint *Pkglint) checkdirPackage(dir string) {
+func (pkglint *Pkglint) checkdirPackage(dir Path) {
if trace.Tracing {
- defer trace.Call1(dir)()
+ defer trace.Call(dir)()
}
pkglint.Pkg = NewPackage(dir)
@@ -377,9 +376,9 @@ func (pkglint *Pkglint) checkdirPackage(dir string) {
}
// Returns the pkgsrc top-level directory, relative to the given directory.
-func findPkgsrcTopdir(dirname string) string {
- for _, dir := range [...]string{".", "..", "../..", "../../.."} {
- if fileExists(joinPath(dirname, dir, "mk/bsd.pkg.mk")) {
+func findPkgsrcTopdir(dirname Path) Path {
+ for _, dir := range [...]Path{".", "..", "../..", "../../.."} {
+ if joinPath(dirname, dir, "mk/bsd.pkg.mk").IsFile() {
return dir
}
}
@@ -427,9 +426,9 @@ func resolveVariableRefs(mklines *MkLines, text string) (resolved string) {
}
}
-func CheckFileOther(filename string) {
+func CheckFileOther(filename Path) {
if trace.Tracing {
- defer trace.Call1(filename)()
+ defer trace.Call(filename)()
}
if lines := Load(filename, NotEmpty|LogErrors); lines != nil {
@@ -439,7 +438,7 @@ func CheckFileOther(filename string) {
func CheckLinesDescr(lines *Lines) {
if trace.Tracing {
- defer trace.Call1(lines.Filename)()
+ defer trace.Call(lines.Filename)()
}
for _, line := range lines.Lines {
@@ -473,7 +472,7 @@ func CheckLinesDescr(lines *Lines) {
func CheckLinesMessage(lines *Lines) {
if trace.Tracing {
- defer trace.Call1(lines.Filename)()
+ defer trace.Call(lines.Filename)()
}
// For now, skip all checks when the MESSAGE may be built from multiple
@@ -529,9 +528,9 @@ func CheckLinesMessage(lines *Lines) {
SaveAutofixChanges(lines)
}
-func CheckFileMk(filename string) {
+func CheckFileMk(filename Path) {
if trace.Tracing {
- defer trace.Call1(filename)()
+ defer trace.Call(filename)()
}
mklines := LoadMk(filename, NotEmpty|LogErrors)
@@ -547,7 +546,7 @@ func CheckFileMk(filename string) {
mklines.SaveAutofixChanges()
}
-func (pkglint *Pkglint) checkReg(filename, basename string, depth int) {
+func (pkglint *Pkglint) checkReg(filename Path, basename string, depth int) {
if depth == 2 && !pkglint.Wip {
if contains(basename, "README") || contains(basename, "TODO") {
@@ -606,16 +605,16 @@ func (pkglint *Pkglint) checkReg(filename, basename string, depth int) {
CheckLinesPatch(lines)
}
- case matches(filename, `(?:^|/)patches/manual[^/]*$`):
+ case matches(filename.String(), `(?:^|/)patches/manual[^/]*$`):
if trace.Tracing {
- trace.Step1("Unchecked file %q.", filename)
+ trace.Stepf("Unchecked file %q.", filename)
}
- case matches(filename, `(?:^|/)patches/[^/]*$`):
+ case filename.Dir().Base() == "patches":
NewLineWhole(filename).Warnf("Patch files should be named \"patch-\", followed by letters, '-', '_', '.', and digits only.")
case (hasPrefix(basename, "Makefile") || hasSuffix(basename, ".mk")) &&
- !pathContainsDir(filename, "files"):
+ !pathContainsDir(filename, "files"): // FIXME: G.Pkgsrc.Rel(filename) instead of filename
CheckFileMk(filename)
case hasPrefix(basename, "PLIST"):
@@ -627,11 +626,11 @@ func (pkglint *Pkglint) checkReg(filename, basename string, depth int) {
// This only checks the file but doesn't register the changes globally.
_ = pkglint.Pkgsrc.loadDocChangesFromFile(filename)
- case matches(filename, `(?:^|/)files/[^/]*$`):
+ case filename.Dir().Base() == "files":
// Skip files directly in the files/ directory, but not those further down.
case basename == "spec":
- if !hasPrefix(pkglint.Pkgsrc.ToRel(filename), "regress/") {
+ if !pkglint.Pkgsrc.ToRel(filename).HasPrefixPath("regress") {
NewLineWhole(filename).Warnf("Only packages in regress/ may have spec files.")
}
@@ -652,7 +651,7 @@ func (pkglint *Pkglint) matchesLicenseFile(basename string) bool {
return basename == path.Base(licenseFile)
}
-func (pkglint *Pkglint) checkExecutable(filename string, mode os.FileMode) {
+func (pkglint *Pkglint) checkExecutable(filename Path, mode os.FileMode) {
if mode.Perm()&0111 == 0 {
// Not executable at all.
return
@@ -677,7 +676,7 @@ func (pkglint *Pkglint) checkExecutable(filename string, mode os.FileMode) {
fix.Custom(func(showAutofix, autofix bool) {
fix.Describef(0, "Clearing executable bits")
if autofix {
- if err := os.Chmod(filename, mode&^0111); err != nil {
+ if err := filename.Chmod(mode &^ 0111); err != nil {
G.Logger.Errorf(cleanpath(filename), "Cannot clear executable bits: %s", err)
}
}
@@ -732,8 +731,8 @@ func (pkglint *Pkglint) tools(mklines *MkLines) *Tools {
}
}
-func (pkglint *Pkglint) loadCvsEntries(filename string) map[string]CvsEntry {
- dir := path.Dir(filename)
+func (pkglint *Pkglint) loadCvsEntries(filename Path) map[string]CvsEntry {
+ dir := filename.Dir()
if dir == pkglint.cvsEntriesDir {
return pkglint.cvsEntries
}
@@ -798,8 +797,8 @@ func (ip *InterPackage) Enable() {
func (ip *InterPackage) Enabled() bool { return ip.hashes != nil }
-func (ip *InterPackage) Hash(alg, filename string, hashBytes []byte, loc *Location) *Hash {
- key := alg + ":" + filename
+func (ip *InterPackage) Hash(alg string, filename Path, hashBytes []byte, loc *Location) *Hash {
+ key := alg + ":" + filename.String()
if otherHash := ip.hashes[key]; otherHash != nil {
return otherHash
}