Initial commit
This commit is contained in:
63
fstore/paths.go
Normal file
63
fstore/paths.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package fstore
|
||||
|
||||
import (
|
||||
"git.crumpington.com/public/jldb/lib/errs"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func filesRootPath(rootDir string) string {
|
||||
return filepath.Clean(filepath.Join(rootDir, "files"))
|
||||
}
|
||||
|
||||
func repDirPath(rootDir string) string {
|
||||
return filepath.Clean(filepath.Join(rootDir, "rep"))
|
||||
}
|
||||
|
||||
func tempDirPath(rootDir string) string {
|
||||
return filepath.Clean(filepath.Join(rootDir, "tmp"))
|
||||
}
|
||||
|
||||
func tempFilePath(inDir string, id uint64) string {
|
||||
return filepath.Join(inDir, strconv.FormatUint(id, 10))
|
||||
}
|
||||
|
||||
func validatePath(p string) error {
|
||||
if len(p) == 0 {
|
||||
return errs.InvalidPath.WithMsg("empty path")
|
||||
}
|
||||
|
||||
if p[0] != '/' {
|
||||
return errs.InvalidPath.WithMsg("path must be absolute")
|
||||
}
|
||||
|
||||
for _, c := range p {
|
||||
switch c {
|
||||
case '/', '-', '_', '.':
|
||||
continue
|
||||
default:
|
||||
}
|
||||
|
||||
if c >= 'a' && c <= 'z' {
|
||||
continue
|
||||
}
|
||||
|
||||
if c >= '0' && c <= '9' {
|
||||
continue
|
||||
}
|
||||
|
||||
return errs.InvalidPath.WithMsg("invalid character in path: %s", string([]rune{c}))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanPath(p string) string {
|
||||
if len(p) == 0 {
|
||||
return "/"
|
||||
}
|
||||
|
||||
if p[0] != '/' {
|
||||
p = "/" + p
|
||||
}
|
||||
return filepath.Clean(p)
|
||||
}
|
||||
Reference in New Issue
Block a user