You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
package store
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
|
|
"git.phc.dm.unipi.it/phc/storage/utils"
|
|
)
|
|
|
|
const RandomIdSize = 32
|
|
|
|
func split(baseDir string, prefixSize int, id string) (prefix, rest string) {
|
|
return id[:prefixSize], id[prefixSize:]
|
|
}
|
|
|
|
func Create(baseDir string, prefixSize int, r io.Reader) (string, error) {
|
|
id := utils.GenerateRandomString(RandomIdSize)
|
|
prefix, rest := split(baseDir, prefixSize, id)
|
|
|
|
os.MkdirAll(path.Join(baseDir, prefix), os.ModePerm)
|
|
|
|
f, err := os.Create(path.Join(baseDir, prefix, rest))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if _, err := io.Copy(f, r); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
func Read(baseDir string, prefixSize int, id string, w io.Writer) error {
|
|
prefix, rest := split(baseDir, prefixSize, id)
|
|
|
|
f, err := os.Open(path.Join(baseDir, prefix, rest))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := io.Copy(w, f); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func All(baseDir string) ([]string, error) {
|
|
entries, err := os.ReadDir(baseDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
objects := []string{}
|
|
|
|
for _, e := range entries {
|
|
if !e.IsDir() {
|
|
return nil, fmt.Errorf(`invalid store structure`)
|
|
}
|
|
|
|
objectsEntries, err := os.ReadDir(path.Join(baseDir, e.Name()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, obj := range objectsEntries {
|
|
if obj.IsDir() {
|
|
return nil, fmt.Errorf(`invalid store structure`)
|
|
}
|
|
|
|
objects = append(objects, e.Name()+obj.Name())
|
|
}
|
|
}
|
|
|
|
return objects, nil
|
|
}
|
|
|
|
func Update(baseDir string, prefixSize int, id string, r io.Reader) error {
|
|
panic("TODO: Not implemented")
|
|
}
|
|
|
|
func Delete(baseDir string, prefixSize int, id string) error {
|
|
panic("TODO: Not implemented")
|
|
}
|