6 Commits

Author SHA1 Message Date
9a14571c31 fixes (the last) 2025-08-18 22:10:27 +02:00
0f2c0e511f fixes 2025-08-18 21:28:01 +02:00
0d92b6b8a0 multiple fix again 2025-08-18 20:52:06 +02:00
2ff191fecf more fix 2025-08-18 20:04:32 +02:00
b2425d310b fix things 2025-08-18 19:38:42 +02:00
97cd8f065f fix things 2025-08-18 19:38:34 +02:00
14 changed files with 222 additions and 230 deletions

11
.vscode/launch.json vendored
View File

@@ -4,6 +4,15 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "web",
"type": "go",
"request": "launch",
"mode": "auto",
"args": ["-config", "${workspaceFolder}/env/config.json"],
"console": "integratedTerminal",
"program": "${workspaceFolder}/cmd/web"
},
{
"name": "server",
"type": "go",
@@ -18,7 +27,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"args": ["run"],
"args": ["sync"],
"console": "integratedTerminal",
"program": "${workspaceFolder}/cmd/cli"
}

View File

@@ -47,11 +47,11 @@ for platform in "${platforms[@]}"; do
fi
if [ "$MAKE_PACKAGE" == "true" ]; then
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} go build -o build/cloudsave_server$EXT -a ./cmd/server
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} GORISCV64=rva22u64 GOAMD64=v3 GOARM64=v8.2 go build -o build/cloudsave_server$EXT -a ./cmd/server
tar -czf build/server_${platform_split[0]}_${platform_split[1]}.tar.gz build/cloudsave_server$EXT
rm build/cloudsave_server$EXT
else
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} go build -o build/cloudsave_server_${platform_split[0]}_${platform_split[1]}$EXT -a ./cmd/server
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} GORISCV64=rva22u64 GOAMD64=v3 GOARM64=v8.2 go build -o build/cloudsave_server_${platform_split[0]}_${platform_split[1]}$EXT -a ./cmd/server
fi
done

View File

@@ -56,7 +56,7 @@ func (p *AddCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
return subcommands.ExitFailure
}
if err := p.Service.Scan(gameID); err != nil {
if _, err := p.Service.Scan(gameID); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to scan:", err)
return subcommands.ExitFailure
}

View File

@@ -37,15 +37,16 @@ func (p *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
}
for _, metadata := range datastore {
if err := p.Service.MakeBackup(metadata.ID); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to make backup:", err)
return subcommands.ExitFailure
changed, err := p.Service.Scan(metadata.ID)
if err != nil {
fmt.Println("❌", metadata.Name, ":", err.Error())
continue
}
if err := p.Service.Scan(metadata.ID); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to scan:", err)
return subcommands.ExitFailure
if changed {
fmt.Println("✅", metadata.Name, ": backed up")
} else {
fmt.Println("🆗", metadata.Name, ": up to date")
}
fmt.Println("✅", metadata.Name)
}
fmt.Println("done.")

View File

@@ -66,7 +66,6 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
pg.Finish()
pg.Clear()
pg.Close()
}
pg.Describe(fmt.Sprintf("[%s] Checking status...", g.Name))
@@ -88,19 +87,13 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
destroyPg()
slog.Warn("failed to push backup files", "err", err)
}
destroyPg()
fmt.Println(g.Name + ": pushed")
continue
}
pg.Describe(fmt.Sprintf("[%s] Fetching metadata...", g.Name))
hremote, err := cli.Hash(r.GameID)
if err != nil {
destroyPg()
fmt.Fprintln(os.Stderr, "error: failed to get the file hash from the remote:", err)
continue
}
remoteMetadata, err := cli.Metadata(r.GameID)
if err != nil {
destroyPg()
@@ -118,7 +111,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
slog.Warn("failed to push backup files", "err", err)
}
if g.MD5 == hremote {
if g.MD5 == remoteMetadata.MD5 {
destroyPg()
if g.Version != remoteMetadata.Version {
slog.Debug("version is not the same, but the hash is equal. Updating local database")

View File

@@ -3,7 +3,6 @@ package api
import (
"cloudsave/pkg/data"
"cloudsave/pkg/repository"
"encoding/json"
"errors"
"fmt"
"log/slog"
@@ -36,7 +35,7 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
}
router := chi.NewRouter()
router.NotFound(func(writer http.ResponseWriter, request *http.Request) {
notFound("This route does not exist", writer, request)
notFound("id not found", writer, request)
})
router.MethodNotAllowed(func(writer http.ResponseWriter, request *http.Request) {
methodNotAllowed(writer, request)
@@ -61,7 +60,6 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
gamesRouter.Group(func(saveRouter chi.Router) {
saveRouter.Post("/{id}/data", s.upload)
saveRouter.Get("/{id}/data", s.download)
saveRouter.Get("/{id}/hash", s.hash)
saveRouter.Get("/{id}/metadata", s.metadata)
saveRouter.Get("/{id}/hist", s.allHist)
@@ -81,43 +79,13 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
}
func (s HTTPServer) all(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(s.documentRoot, "data")
datastore := make([]repository.Metadata, 0)
if _, err := os.Stat(path); err != nil {
if errors.Is(err, os.ErrNotExist) {
ok(datastore, w, r)
return
}
fmt.Fprintln(os.Stderr, "failed to open datastore (", s.documentRoot, "):", err)
datastore, err := s.Service.AllGames()
if err != nil {
slog.Error(err.Error())
internalServerError(w, r)
return
}
ds, err := os.ReadDir(path)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to open datastore (", s.documentRoot, "):", err)
internalServerError(w, r)
return
}
for _, d := range ds {
content, err := os.ReadFile(filepath.Join(path, d.Name(), "metadata.json"))
if err != nil {
slog.Error("error: failed to load metadata.json", "err", err)
continue
}
var m repository.Metadata
err = json.Unmarshal(content, &m)
if err != nil {
fmt.Fprintf(os.Stderr, "corrupted datastore: failed to parse %s/metadata.json: %s", d.Name(), err)
internalServerError(w, r)
}
datastore = append(datastore, m)
}
ok(datastore, w, r)
}
@@ -125,32 +93,19 @@ func (s HTTPServer) download(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id))
sdir, err := os.Stat(path)
fi, err := os.Stat(filepath.Join(path, "data.tar.gz"))
if err != nil {
notFound("id not found", w, r)
return
}
if !sdir.IsDir() {
notFound("id not found", w, r)
return
}
path = filepath.Join(path, "data.tar.gz")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
f, err := s.Service.Repository().ReadBlob(repository.NewGameIdentifier(id))
if err != nil {
notFound("id not found", w, r)
return
}
defer f.Close()
// Get file info to set headers
fi, err := f.Stat()
if err != nil || fi.IsDir() {
slog.Error(err.Error())
internalServerError(w, r)
return
}
defer f.Close()
// Set headers
w.Header().Set("Content-Disposition", "attachment; filename=\"data.tar.gz\"")
@@ -209,6 +164,12 @@ func (s HTTPServer) upload(w http.ResponseWriter, r *http.Request) {
return
}
if err := s.Service.ReloadCache(id); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to reload data from the disk:", err)
internalServerError(w, r)
return
}
// Respond success
w.WriteHeader(http.StatusCreated)
}
@@ -260,7 +221,13 @@ func (s HTTPServer) histUpload(w http.ResponseWriter, r *http.Request) {
defer file.Close()
if err := s.Service.CopyBackup(gameID, uuid, file); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to write data to disk:", err)
fmt.Fprintln(os.Stderr, "error: failed to write data to the disk:", err)
internalServerError(w, r)
return
}
if err := s.Service.ReloadCache(gameID); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to reload data from the disk:", err)
internalServerError(w, r)
return
}
@@ -274,32 +241,19 @@ func (s HTTPServer) histDownload(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id, "hist", uuid))
sdir, err := os.Stat(path)
fi, err := os.Stat(filepath.Join(path, "data.tar.gz"))
if err != nil {
notFound("id not found", w, r)
return
}
if !sdir.IsDir() {
notFound("id not found", w, r)
return
}
path = filepath.Join(path, "data.tar.gz")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
f, err := s.Service.Repository().ReadBlob(repository.NewBackupIdentifier(id, uuid))
if err != nil {
notFound("id not found", w, r)
return
}
defer f.Close()
// Get file info to set headers
fi, err := f.Stat()
if err != nil || fi.IsDir() {
slog.Error(err.Error())
internalServerError(w, r)
return
}
defer f.Close()
// Set headers
w.Header().Set("Content-Disposition", "attachment; filename=\"data.tar.gz\"")
@@ -329,56 +283,18 @@ func (s HTTPServer) histExists(w http.ResponseWriter, r *http.Request) {
ok(finfo, w, r)
}
func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
m, err := s.Service.One(id)
if err != nil {
if errors.Is(err, repository.ErrNotFound) {
notFound("not found", w, r)
return
}
fmt.Fprintln(os.Stderr, "error: an error occured while calculating the hash:", err)
internalServerError(w, r)
return
}
ok(m.MD5, w, r)
}
func (s HTTPServer) metadata(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id))
sdir, err := os.Stat(path)
metadata, err := s.Service.One(id)
if err != nil {
if errors.Is(err, repository.ErrNotFound) {
notFound("id not found", w, r)
return
}
if !sdir.IsDir() {
notFound("id not found", w, r)
return
}
path = filepath.Join(path, "metadata.json")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
notFound("id not found", w, r)
return
}
defer f.Close()
var metadata repository.Metadata
d := json.NewDecoder(f)
err = d.Decode(&metadata)
if err != nil {
fmt.Fprintln(os.Stderr, "error: an error occured while reading data:", err)
slog.Error(err.Error())
internalServerError(w, r)
return
}
ok(metadata, w, r)
}

View File

@@ -3,13 +3,13 @@ package api
import (
"cloudsave/pkg/remote/obj"
"encoding/json"
"log"
"log/slog"
"net/http"
"time"
)
func internalServerError(w http.ResponseWriter, r *http.Request) {
e := obj.HTTPError{
payload := obj.HTTPError{
HTTPCore: obj.HTTPCore{
Status: http.StatusInternalServerError,
Path: r.RequestURI,
@@ -19,20 +19,16 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
Message: "The server encountered an unexpected condition that prevented it from fulfilling the request.",
}
payload, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
e := json.NewEncoder(w)
if err := e.Encode(payload); err != nil {
slog.Error(err.Error())
}
}
func notFound(message string, w http.ResponseWriter, r *http.Request) {
e := obj.HTTPError{
payload := obj.HTTPError{
HTTPCore: obj.HTTPCore{
Status: http.StatusNotFound,
Path: r.RequestURI,
@@ -42,20 +38,16 @@ func notFound(message string, w http.ResponseWriter, r *http.Request) {
Message: message,
}
payload, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
e := json.NewEncoder(w)
if err := e.Encode(payload); err != nil {
slog.Error(err.Error())
}
}
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
e := obj.HTTPError{
payload := obj.HTTPError{
HTTPCore: obj.HTTPCore{
Status: http.StatusMethodNotAllowed,
Path: r.RequestURI,
@@ -65,20 +57,16 @@ func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
Message: "The server knows the request method, but the target resource doesn't support this method",
}
payload, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
e := json.NewEncoder(w)
if err := e.Encode(payload); err != nil {
slog.Error(err.Error())
}
}
func unauthorized(w http.ResponseWriter, r *http.Request) {
e := obj.HTTPError{
payload := obj.HTTPError{
HTTPCore: obj.HTTPCore{
Status: http.StatusUnauthorized,
Path: r.RequestURI,
@@ -88,21 +76,17 @@ func unauthorized(w http.ResponseWriter, r *http.Request) {
Message: "The request has not been completed because it lacks valid authentication credentials for the requested resource.",
}
payload, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.Header().Add("WWW-Authenticate", "Custom realm=\"loginUserHandler via /api/login\"")
w.WriteHeader(http.StatusUnauthorized)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
e := json.NewEncoder(w)
if err := e.Encode(payload); err != nil {
slog.Error(err.Error())
}
}
func ok(o interface{}, w http.ResponseWriter, r *http.Request) {
e := obj.HTTPObject{
payload := obj.HTTPObject{
HTTPCore: obj.HTTPCore{
Status: http.StatusOK,
Path: r.RequestURI,
@@ -110,20 +94,15 @@ func ok(o interface{}, w http.ResponseWriter, r *http.Request) {
},
Data: o,
}
payload, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(payload)
if err != nil {
log.Println(err)
e := json.NewEncoder(w)
if err := e.Encode(payload); err != nil {
slog.Error(err.Error())
}
}
func badRequest(message string, w http.ResponseWriter, r *http.Request) {
e := obj.HTTPError{
payload := obj.HTTPError{
HTTPCore: obj.HTTPCore{
Status: http.StatusBadRequest,
Path: r.RequestURI,
@@ -133,14 +112,10 @@ func badRequest(message string, w http.ResponseWriter, r *http.Request) {
Message: message,
}
payload, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
e := json.NewEncoder(w)
if err := e.Encode(payload); err != nil {
slog.Error(err.Error())
}
}

View File

@@ -8,6 +8,7 @@ import (
"cloudsave/pkg/repository"
"flag"
"fmt"
"log/slog"
"path/filepath"
"runtime"
"strconv"
@@ -18,18 +19,27 @@ func run() {
var documentRoot string
var port int
var noCache bool
var noCache, verbose bool
flag.StringVar(&documentRoot, "document-root", defaultDocumentRoot, "Define the path to the document root")
flag.IntVar(&port, "port", 8080, "Define the port of the server")
flag.BoolVar(&noCache, "no-cache", false, "Disable the cache")
flag.BoolVar(&verbose, "verbose", false, "Show more logs")
flag.Parse()
if verbose {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
slog.Info("loading .htpasswd")
h, err := htpasswd.Open(filepath.Join(documentRoot, ".htpasswd"))
if err != nil {
fatal("failed to load .htpasswd: "+err.Error(), 1)
}
slog.Info("users loaded: " + strconv.Itoa(len(h.Content())) + " user(s) loaded")
var repo repository.Repository
if noCache {
if !noCache {
slog.Info("loading eager repository...")
r, err := repository.NewEagerRepository(filepath.Join(documentRoot, "data"))
if err != nil {
fatal("failed to load datastore: "+err.Error(), 1)
@@ -39,17 +49,19 @@ func run() {
}
repo = r
} else {
slog.Info("loading lazy repository...")
repo, err = repository.NewLazyRepository(filepath.Join(documentRoot, "data"))
if err != nil {
fatal("failed to load datastore: "+err.Error(), 1)
}
}
slog.Info("repository loaded")
s := data.NewService(repo)
server := api.NewServer(documentRoot, s, h.Content(), port)
fmt.Println("starting server at :" + strconv.Itoa(port))
fmt.Println("server started at :" + strconv.Itoa(port))
if err := server.Server.ListenAndServe(); err != nil {
fatal("failed to start server: "+err.Error(), 1)
}

View File

@@ -162,9 +162,8 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
}
var wg sync.WaitGroup
var err1, err2, err3 error
var err1, err2 error
var save repository.Metadata
var h string
var ids []string
wg.Add(1)
@@ -175,24 +174,18 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
wg.Add(1)
go func() {
h, err2 = cli.Hash(id)
wg.Done()
}()
wg.Add(1)
go func() {
ids, err3 = cli.ListArchives(id)
ids, err2 = cli.ListArchives(id)
wg.Done()
}()
wg.Wait()
if err1 != nil || err2 != nil || err3 != nil {
if err1 != nil || err2 != nil {
if errors.Is(err1, client.ErrUnauthorized) {
unauthorized("Unable to access resources", w, r)
return
}
slog.Error("unable to connect to the remote", "err", err1)
slog.Error("failed to get metadata: unable to connect to the remote", "err", err1)
return
}
@@ -205,7 +198,7 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
defer wg.Done()
b, err := cli.ArchiveInfo(id, i)
if err != nil {
slog.Error("unable to connect to the remote", "err", err)
slog.Error("failed to get backup: unable to connect to the remote", "err", err)
return
}
bm = append(bm, b)
@@ -216,7 +209,6 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
payload := DetaillePayload{
Save: save,
Hash: h,
BackupMetadata: bm,
Version: constants.Version,
}

View File

@@ -30,7 +30,7 @@
<ul class="list-group list-group-flush">
<li class="list-group-item">UUID: {{.Save.ID}}</li>
<li class="list-group-item">Last Upload: {{.Save.Date}}</li>
<li class="list-group-item">Hash (MD5): {{.Hash}}</li>
<li class="list-group-item">Hash (MD5): {{.Save.MD5}}</li>
</ul>
<hr />

View File

@@ -1,5 +1,5 @@
package constants
const Version = "0.0.4a"
const Version = "0.0.4b"
const ApiVersion = 1

View File

@@ -82,47 +82,51 @@ func (s *Service) UpdateMetadata(gameID string, m repository.Metadata) error {
return nil
}
func (s *Service) Scan(gameID string) error {
func (s *Service) Scan(gameID string) (bool, error) {
id := repository.NewGameIdentifier(gameID)
lastRun, err := s.repo.LastScan(id)
if err != nil {
return fmt.Errorf("failed to get last scan time: %w", err)
return false, fmt.Errorf("failed to get last scan time: %w", err)
}
m, err := s.repo.Metadata(id)
if err != nil {
return fmt.Errorf("failed to get game metadata: %w", err)
return false, fmt.Errorf("failed to get game metadata: %w", err)
}
if !IsDirectoryChanged(m.Path, lastRun) {
return nil
return false, nil
}
if err := s.MakeBackup(gameID); err != nil {
return false, fmt.Errorf("failed to make the backup: %w", err)
}
f, err := s.repo.WriteBlob(id)
if err != nil {
return fmt.Errorf("failed to get datastore stream: %w", err)
return false, fmt.Errorf("failed to get datastore stream: %w", err)
}
if v, ok := f.(io.Closer); ok {
defer v.Close()
}
if err := archive.Tar(f, m.Path); err != nil {
return fmt.Errorf("failed to make archive: %w", err)
return false, fmt.Errorf("failed to make archive: %w", err)
}
if err := s.repo.ResetLastScan(id); err != nil {
return fmt.Errorf("failed to reset scan date: %w", err)
return false, fmt.Errorf("failed to reset scan date: %w", err)
}
m.Date = time.Now()
m.Version += 1
if err := s.repo.WriteMetadata(id, m); err != nil {
return fmt.Errorf("failed to update metadata: %w", err)
return false, fmt.Errorf("failed to update metadata: %w", err)
}
return nil
return true, nil
}
func (s *Service) MakeBackup(gameID string) error {
@@ -268,8 +272,6 @@ func (l Service) PullBackup(gameID, backupID string, cli *client.Client) error {
return fmt.Errorf("failed to pull backup: %w", err)
}
return nil
}
@@ -372,6 +374,17 @@ func (l Service) ApplyBackup(gameID, backupID string) error {
return l.apply(filepath.Join(path, "data.tar.gz"), g.Path)
}
func (l Service) Repository() repository.Repository {
return l.repo
}
func (l Service) ReloadCache(gameID string) error {
if er, ok := l.repo.(*repository.EagerRepository); ok {
return er.ReloadMetadata(repository.NewGameIdentifier(gameID))
}
return nil
}
func (l Service) apply(src, dst string) error {
if err := os.RemoveAll(dst); err != nil {
return fmt.Errorf("failed to remove old save: %w", err)

View File

@@ -49,7 +49,7 @@ func New(baseURL, username, password string) *Client {
}
func (c *Client) Exists(gameID string) (bool, error) {
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "hash")
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "metadata")
if err != nil {
return false, err
}
@@ -104,22 +104,13 @@ func (c *Client) Version() (Information, error) {
return Information{}, errors.New("invalid payload sent by the server")
}
// Deprecated: use c.Metadata instead
func (c *Client) Hash(gameID string) (string, error) {
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "hash")
m, err := c.Metadata(gameID)
if err != nil {
return "", err
}
o, err := c.get(u)
if err != nil {
return "", err
}
if h, ok := (o.Data).(string); ok {
return h, nil
}
return "", errors.New("invalid payload sent by the server")
return m.MD5, nil
}
func (c *Client) Metadata(gameID string) (repository.Metadata, error) {
@@ -139,6 +130,7 @@ func (c *Client) Metadata(gameID string) (repository.Metadata, error) {
Name: m["name"].(string),
Version: int(m["version"].(float64)),
Date: customtime.MustParse(time.RFC3339, m["date"].(string)),
MD5: m["md5"].(string),
}
return gm, nil
}
@@ -175,6 +167,10 @@ func (c *Client) ListArchives(gameID string) ([]string, error) {
return nil, err
}
if o.Data == nil {
return nil, nil
}
if m, ok := (o.Data).([]any); ok {
var res []string
for _, uuid := range m {
@@ -346,6 +342,10 @@ func (c *Client) All() ([]repository.Metadata, error) {
return nil, err
}
if o.Data == nil {
return nil, nil
}
if games, ok := (o.Data).([]any); ok {
var res []repository.Metadata
for _, g := range games {
@@ -355,6 +355,7 @@ func (c *Client) All() ([]repository.Metadata, error) {
Name: v["name"].(string),
Version: int(v["version"].(float64)),
Date: customtime.MustParse(time.RFC3339, v["date"].(string)),
MD5: v["md5"].(string),
}
res = append(res, gm)
}

View File

@@ -6,8 +6,10 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"sync"
"time"
)
@@ -18,7 +20,7 @@ type (
Path string `json:"path"`
Version int `json:"version"`
Date time.Time `json:"date"`
MD5 string `json:"-"`
MD5 string `json:"md5,omitempty"`
}
Remote struct {
@@ -60,6 +62,7 @@ type (
EagerRepository struct {
Repository
mu sync.RWMutex
data map[string]Data
}
@@ -74,7 +77,7 @@ type (
Metadata(gameID GameIdentifier) (Metadata, error)
LastScan(gameID GameIdentifier) (time.Time, error)
ReadBlob(gameID Identifier) (io.Reader, error)
ReadBlob(gameID Identifier) (io.ReadSeekCloser, error)
Backup(id BackupIdentifier) (Backup, error)
Remote(id GameIdentifier) (*Remote, error)
@@ -132,10 +135,16 @@ func NewLazyRepository(dataRootPath string) (*LazyRepository, error) {
}
func (l *LazyRepository) Mkdir(id Identifier) error {
return os.MkdirAll(l.DataPath(id), 0740)
path := l.DataPath(id)
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
slog.Debug("making directory", "path", path, "id", id, "perm", "0740")
return os.MkdirAll(path, 0740)
}
return nil
}
func (l *LazyRepository) All() ([]string, error) {
slog.Debug("loading all current data...")
dir, err := os.ReadDir(l.dataRoot)
if err != nil {
return nil, fmt.Errorf("failed to open directory: %w", err)
@@ -152,6 +161,7 @@ func (l *LazyRepository) All() ([]string, error) {
func (l *LazyRepository) AllHist(id GameIdentifier) ([]string, error) {
path := l.DataPath(id)
slog.Debug("loading hist data...", "id", id)
dir, err := os.ReadDir(filepath.Join(path, "hist"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@@ -171,6 +181,7 @@ func (l *LazyRepository) AllHist(id GameIdentifier) ([]string, error) {
func (l *LazyRepository) WriteBlob(ID Identifier) (io.Writer, error) {
path := l.DataPath(ID)
slog.Debug("loading write buffer...", "id", ID)
dst, err := os.OpenFile(filepath.Join(path, "data.tar.gz"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
if err != nil {
return nil, fmt.Errorf("failed to open destination file: %w", err)
@@ -180,8 +191,10 @@ func (l *LazyRepository) WriteBlob(ID Identifier) (io.Writer, error) {
}
func (l *LazyRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
m.MD5 = ""
path := l.DataPath(id)
slog.Debug("writing metadata", "id", id, "metadata", m)
dst, err := os.OpenFile(filepath.Join(path, "metadata.json"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
if err != nil {
return fmt.Errorf("failed to open destination file: %w", err)
@@ -199,6 +212,7 @@ func (l *LazyRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
func (l *LazyRepository) Metadata(id GameIdentifier) (Metadata, error) {
path := l.DataPath(id)
slog.Debug("loading metadata", "id", id)
src, err := os.OpenFile(filepath.Join(path, "metadata.json"), os.O_RDONLY, 0)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@@ -220,6 +234,7 @@ func (l *LazyRepository) Metadata(id GameIdentifier) (Metadata, error) {
return Metadata{}, fmt.Errorf("failed to open archive: %w", err)
}
slog.Debug("loading md5 hash", "id", id)
m.MD5, err = hash.FileMD5(filepath.Join(path, "data.tar.gz"))
if err != nil {
return Metadata{}, fmt.Errorf("failed to calculate md5: %w", err)
@@ -231,6 +246,7 @@ func (l *LazyRepository) Metadata(id GameIdentifier) (Metadata, error) {
func (l *LazyRepository) Backup(id BackupIdentifier) (Backup, error) {
path := l.DataPath(id)
slog.Debug("loading hist metadata", "id", id)
fs, err := os.Stat(filepath.Join(path, "data.tar.gz"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@@ -239,6 +255,7 @@ func (l *LazyRepository) Backup(id BackupIdentifier) (Backup, error) {
return Backup{}, fmt.Errorf("corrupted datastore: failed to open metadata: %w", err)
}
slog.Debug("loading md5 hash", "id", id)
h, err := hash.FileMD5(filepath.Join(path, "data.tar.gz"))
if err != nil {
return Backup{}, fmt.Errorf("corrupted datastore: failed to open metadata: %w", err)
@@ -274,6 +291,7 @@ func (l *LazyRepository) LastScan(id GameIdentifier) (time.Time, error) {
func (l *LazyRepository) ResetLastScan(id GameIdentifier) error {
path := l.DataPath(id)
slog.Debug("resetting last scan datetime for", "id", id)
f, err := os.OpenFile(filepath.Join(path, ".last_run"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
@@ -289,9 +307,10 @@ func (l *LazyRepository) ResetLastScan(id GameIdentifier) error {
return nil
}
func (l *LazyRepository) ReadBlob(id Identifier) (io.Reader, error) {
func (l *LazyRepository) ReadBlob(id Identifier) (io.ReadSeekCloser, error) {
path := l.DataPath(id)
slog.Debug("loading read buffer...", "id", id)
dst, err := os.OpenFile(filepath.Join(path, "data.tar.gz"), os.O_RDONLY, 0)
if err != nil {
return nil, fmt.Errorf("failed to open blob: %w", err)
@@ -344,6 +363,7 @@ func (l *LazyRepository) Remote(id GameIdentifier) (*Remote, error) {
func (l *LazyRepository) Remove(id GameIdentifier) error {
path := l.DataPath(id)
slog.Debug("removing data", "id", id)
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("failed to remove game folder from the datastore: %w", err)
}
@@ -375,6 +395,9 @@ func NewEagerRepository(dataRootPath string) (*EagerRepository, error) {
}
func (r *EagerRepository) Preload() error {
r.mu.Lock()
defer r.mu.Unlock()
games, err := r.Repository.All()
if err != nil {
return fmt.Errorf("failed to load all data: %w", err)
@@ -418,6 +441,9 @@ func (r *EagerRepository) Preload() error {
}
func (r *EagerRepository) All() ([]string, error) {
r.mu.RLock()
defer r.mu.RUnlock()
var res []string
for _, g := range r.data {
res = append(res, g.Metadata.ID)
@@ -427,6 +453,9 @@ func (r *EagerRepository) All() ([]string, error) {
}
func (r *EagerRepository) AllHist(id GameIdentifier) ([]string, error) {
r.mu.RLock()
defer r.mu.RUnlock()
var res []string
if d, ok := r.data[id.gameID]; ok {
for _, b := range d.Backup {
@@ -437,6 +466,9 @@ func (r *EagerRepository) AllHist(id GameIdentifier) ([]string, error) {
}
func (r *EagerRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
r.mu.Lock()
defer r.mu.Unlock()
err := r.Repository.WriteMetadata(id, m)
if err != nil {
return err
@@ -450,6 +482,9 @@ func (r *EagerRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
}
func (r *EagerRepository) Metadata(id GameIdentifier) (Metadata, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if d, ok := r.data[id.gameID]; ok {
return d.Metadata, nil
}
@@ -457,6 +492,9 @@ func (r *EagerRepository) Metadata(id GameIdentifier) (Metadata, error) {
}
func (r *EagerRepository) Backup(id BackupIdentifier) (Backup, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if d, ok := r.data[id.gameID]; ok {
if b, ok := d.Backup[id.backupID]; ok {
return b, nil
@@ -466,6 +504,9 @@ func (r *EagerRepository) Backup(id BackupIdentifier) (Backup, error) {
}
func (r *EagerRepository) SetRemote(id GameIdentifier, url string) error {
r.mu.Lock()
defer r.mu.Unlock()
err := r.Repository.SetRemote(id, url)
if err != nil {
return err
@@ -482,6 +523,9 @@ func (r *EagerRepository) SetRemote(id GameIdentifier, url string) error {
}
func (r *EagerRepository) Remove(id GameIdentifier) error {
r.mu.Lock()
defer r.mu.Unlock()
if err := r.Repository.Remove(id); err != nil {
return err
}
@@ -489,3 +533,39 @@ func (r *EagerRepository) Remove(id GameIdentifier) error {
delete(r.data, id.gameID)
return nil
}
func (r *EagerRepository) ReloadMetadata(id GameIdentifier) error {
backup, err := r.Repository.AllHist(id)
if err != nil {
return fmt.Errorf("[%s] failed to load hist data: %w", id, err)
}
remote, err := r.Repository.Remote(id)
if err != nil {
return fmt.Errorf("[%s] failed to load remote metadata: %w", id, err)
}
m, err := r.Repository.Metadata(id)
if err != nil {
return fmt.Errorf("[%s] failed to load metadata: %w", id, err)
}
backups := make(map[string]Backup)
for _, b := range backup {
info, err := r.Repository.Backup(NewBackupIdentifier(id.gameID, b))
if err != nil {
return fmt.Errorf("[%s] failed to get backup information: %w", id, err)
}
backups[b] = info
}
r.data[id.gameID] = Data{
Metadata: m,
Remote: remote,
DataPath: r.DataPath(id),
Backup: backups,
}
return nil
}