2023-01-09 22:28:54 +01:00
|
|
|
package zrokdir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/tui"
|
2023-01-09 22:28:54 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"os"
|
2023-01-09 23:57:57 +01:00
|
|
|
"path/filepath"
|
2023-01-09 22:28:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const V = "v0.3"
|
|
|
|
|
|
|
|
type Metadata struct {
|
|
|
|
V string `json:"v"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkMetadata() error {
|
|
|
|
mf, err := metadataFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := os.ReadFile(mf)
|
|
|
|
if err != nil {
|
2023-01-09 23:56:16 +01:00
|
|
|
tui.Warning("unable to open zrokdir metadata; ignoring\n")
|
2023-01-09 23:31:45 +01:00
|
|
|
return nil
|
2023-01-09 22:28:54 +01:00
|
|
|
}
|
|
|
|
m := &Metadata{}
|
|
|
|
if err := json.Unmarshal(data, m); err != nil {
|
|
|
|
return errors.Wrapf(err, "error unmarshaling metadata file '%v'", mf)
|
|
|
|
}
|
|
|
|
if m.V != V {
|
|
|
|
return errors.Errorf("invalid zrokdir metadata version '%v'", m.V)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeMetadata() error {
|
|
|
|
mf, err := metadataFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(&Metadata{V: V})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-09 23:57:57 +01:00
|
|
|
if err := os.MkdirAll(filepath.Dir(mf), os.FileMode(0700)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-10 22:39:08 +01:00
|
|
|
if err := os.WriteFile(mf, data, os.FileMode(0600)); err != nil {
|
2023-01-09 22:28:54 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|