1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00
mycorrhiza/hyphae/hypha.go

48 lines
1.1 KiB
Go
Raw Normal View History

2022-06-06 14:41:33 +00:00
// Package hyphae manages hypha storage and hypha types.
2022-02-03 21:26:08 +00:00
package hyphae
2022-02-04 11:14:53 +00:00
import (
"regexp"
"strings"
"sync"
)
// hyphaNamePattern is a pattern which all hyphae names must match.
var hyphaNamePattern = regexp.MustCompile(`^[^?!:#@><*|"'&%{}]+$`)
2022-02-04 11:14:53 +00:00
// IsValidName checks for invalid characters and path traversals.
func IsValidName(hyphaName string) bool {
if !hyphaNamePattern.MatchString(hyphaName) {
2022-02-04 11:14:53 +00:00
return false
}
for _, segment := range strings.Split(hyphaName, "/") {
if segment == ".git" || segment == ".." {
return false
}
}
return true
}
2022-02-03 21:26:08 +00:00
// Hypha is the hypha you know and love.
2022-02-19 08:26:38 +00:00
type Hypha interface {
2022-02-03 21:26:08 +00:00
sync.Locker
// CanonicalName returns the canonical name of the hypha.
//
// util.CanonicalName(h.CanonicalName()) == h.CanonicalName()
2022-02-03 21:26:08 +00:00
CanonicalName() string
}
2022-02-04 17:04:39 +00:00
// ByName returns a hypha by name. It returns an *EmptyHypha if there is no such hypha. This function is the only source of empty hyphae.
2022-02-19 08:26:38 +00:00
func ByName(hyphaName string) (h Hypha) {
byNamesMutex.Lock()
defer byNamesMutex.Unlock()
2022-02-04 11:14:53 +00:00
h, recorded := byNames[hyphaName]
if recorded {
return h
}
2022-02-19 08:26:38 +00:00
return &EmptyHypha{
canonicalName: hyphaName,
}
2022-02-04 11:14:53 +00:00
}