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"
|
|
|
|
)
|
|
|
|
|
2022-02-19 16:42:32 +00:00
|
|
|
// hyphaNamePattern is a pattern which all hyphae names must match.
|
2022-08-21 13:18:28 +00:00
|
|
|
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 {
|
2022-02-19 16:42:32 +00:00
|
|
|
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
|
|
|
|
2022-02-19 16:42:32 +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
|
|
|
|
|
2022-02-19 16:42:32 +00:00
|
|
|
// 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) {
|
2022-02-04 14:56:28 +00:00
|
|
|
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
|
|
|
}
|