1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-02-07 14:40:16 +00:00
mycorrhiza/hyphae/hypha.go

48 lines
1.1 KiB
Go
Raw Normal View History

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