13 Commits
fix_sec ... dev

Author SHA1 Message Date
b4811fba4a wip 2026-05-08 14:06:08 +02:00
b314a683c9 fixes 2025-11-19 19:08:21 +01:00
8e18f2ce76 remove useless subcommand 2025-11-19 18:48:35 +01:00
1c89df0673 switch back to qt
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-13 15:32:37 +02:00
1239c5ed6b fix status print
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-13 11:21:42 +02:00
03818e20e5 fix loop sync cli
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-13 09:32:59 +02:00
b36142c309 wip
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-12 19:06:52 +02:00
57fc77755e wip
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-12 01:39:47 +02:00
5f7ca22b8f wip
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-11 17:41:57 +02:00
d15de3c6a1 refactoring sync
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-08 17:40:58 +02:00
f56d3c5857 wip
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-08 01:21:53 +02:00
7e5d8855d4 fix crash
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-07 19:30:05 +02:00
e6ca29a7aa first commit
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
2025-09-07 19:26:18 +02:00
17 changed files with 754 additions and 244 deletions

13
.dockerignore Normal file
View File

@@ -0,0 +1,13 @@
/cli
/server
/web
/gui
/env/
/build/
*.exe
/config.json
/.codex
/.vscode
# for docker-compose.yml
/data

8
.gitignore vendored
View File

@@ -1,7 +1,13 @@
/cli
/server
/web
/gui
/env/
/build/
*.exe
/config.json
/config.json
/.codex
/.vscode
# for docker-compose.yml
/data

260
README.md
View File

@@ -1,58 +1,270 @@
# CloudSave
The software is still in alpha.
CloudSave is a small client/server tool to keep save folders in sync across multiple computers.
It is aimed at games that do not provide their own cloud sync, such as emulators, old games, or any title that stores progress in a local directory.
A client/server that allows unsynchronized games (such as emulators, old games, etc.) to be kept up to date on multiple computers.
The project is still in alpha.
## What Is In The Repository
This repository currently contains three Go binaries:
- `cmd/cli`: the end-user CLI (`cloudsave`)
- `cmd/server`: the HTTP API server
- `cmd/web`: a small read-only web UI that talks to the API server
## Build
You need go1.24
The module targets Go `1.24` in [go.mod](/home/aurelie/src/cloudsave/go.mod), while the container image builds with Go `1.26.3` from [dockerfile](/home/aurelie/src/cloudsave/dockerfile). In practice, using a recent Go toolchain is recommended.
After downloading the go toolchain, just run the script `./build.sh`
To build all binaries for the platforms configured in the project:
## Usage
### Server
The server needs an empty directory. After creating this directory, you need to make a file that contains your credential. The format is "username:password". The server only understand bcrypt password hash for now.
e.g.:
```bash
./build.sh
```
Artifacts are written to `./build`.
If you only want one binary, you can also build it directly:
```bash
go build -o cloudsave ./cmd/cli
go build -o cloudsave_server ./cmd/server
go build -o cloudsave_web ./cmd/web
```
## Server
The server exposes an authenticated HTTP API on port `8080` by default.
### Data Directory
By default, the server uses:
```text
/var/lib/cloudsave
```
You can override it with:
```bash
cloudsave_server -document-root /path/to/cloudsave-data
```
Inside this directory, the server expects:
- `.htpasswd`: credentials file
- `data/`: stored save archives and metadata
### Authentication
The API uses HTTP Basic Auth. Credentials are read from `.htpasswd`.
Example:
```text
test:$2y$10$uULsuyROe3LVdTzFoBH7HO0zhvyKp6CX2FDNl7quXMFYqzitU0kc.
```
To generate bcrypt password, I recommand [hash_utils](https://git.thelilfrog.com/thelilfrog/hash_utils), which is offline and secure
The code currently expects bcrypt hashes when validating passwords.
The default path to this directory is `/var/lib/cloudsave`, this can be changed with the `-document-root` argument
### Start The Server
### Client
```bash
cloudsave_server
```
#### Register a game
Useful flags from [cmd/server/runner.go](/home/aurelie/src/cloudsave/cmd/server/runner.go):
- `-document-root`: change the storage directory
- `-port`: change the listening port
- `-no-cache`: use the lazy repository instead of the eager cache
- `-verbose`: enable more logs
On non-Windows systems, sending `SIGHUP` reloads the eager cache and the `.htpasswd` file.
## Docker
The repository contains a server-only container setup:
- [dockerfile](/home/aurelie/src/cloudsave/dockerfile)
- [docker-compose.yml](/home/aurelie/src/cloudsave/docker-compose.yml)
Run it with:
```bash
docker compose up --build
```
This maps:
- port `8080`
- local `./data` to `/var/lib/cloudsave`
Before starting the container, you still need to create `./data/.htpasswd`.
## Client
The CLI stores its local database in the user config directory under `cloudsave/data`.
It keeps:
- per-game metadata
- current local archive
- backup archives
- `remote.json` per game when a remote is configured
Saved credentials are stored separately in `credential.json`.
Important: the `login` command stores credentials in plain text. This is also stated in the code.
## Typical Workflow
### 1. Register a game
You can register a game with the verb `add`
```bash
cloudsave add /home/user/gamedata
```
You can also change the name of the registration and add a remote
You can override the displayed name:
```bash
cloudsave add -name "My Game" -remote "http://localhost:8080" /home/user/gamedata
cloudsave add -name "My Game" /home/user/gamedata
```
#### Make an archive of the current state
Note: the `-remote` flag exists on `add`, but the current implementation does not persist it. Use `cloudsave remote -set` after `add`.
This is a command line tool, it cannot auto detect changes.
Run this command to start the scan, if needed, the tool will create a new archive
### 2. List registered games
```bash
cloudsave list
```
To include local backup IDs:
```bash
cloudsave list -include-backup
```
### 3. Create or refresh the local archive
```bash
cloudsave scan
```
#### Send everything on the server
This will pull and push data to the server.
This scans all registered folders. If a folder changed since the last scan, the current archive is moved to the backup history and a new `data.tar.gz` archive is created.
Note: If multiple computers are pushing to this server, a conflict may be generated. If so, the tool will ask for the version to keep
### 4. Configure the remote server for a game
```bash
cloudsave remote -set GAME_ID http://localhost:8080
```
To list configured remotes:
```bash
cloudsave remote -list
```
### 5. Save credentials locally
```bash
cloudsave login http://localhost:8080
```
This verifies the credentials against the server and then stores them locally in plain text.
### 6. Synchronize with the server
```bash
cloudsave sync
```
The sync command:
- groups games by remote URL
- authenticates once per remote
- compares local and remote metadata
- pushes or pulls as needed
- asks for a resolution if versions conflict
### 7. Restore a save locally
Apply the latest local archive for a game:
```bash
cloudsave apply GAME_ID
```
Apply a specific backup:
```bash
cloudsave apply GAME_ID BACKUP_ID
```
## Other CLI Commands
Show metadata for one game:
```bash
cloudsave show GAME_ID
```
Pull one game and its backups from a remote into a local path:
```bash
cloudsave pull http://localhost:8080 GAME_ID /path/to/restore
```
Show local version information:
```bash
cloudsave version
```
Show remote version information:
```bash
cloudsave version -a http://localhost:8080
```
Remove a registered game and its local backups:
```bash
cloudsave remove GAME_ID
```
## Web UI
The repository also contains a small web frontend in `cmd/web`.
It uses a JSON config file, for example:
```json
{
"server": {
"port": 8081
},
"remote": {
"url": "http://localhost:8080"
}
}
```
Then start it with:
```bash
cloudsave_web -config /path/to/config.json
```
The web UI itself does not manage users. It forwards HTTP Basic Auth credentials to the configured API server.
## Current Caveats
These points are worth knowing in the current state of the project:
- the software is still alpha
- credentials saved by `cloudsave login` are stored in plain text
- the Docker setup only runs the API server, not the web UI
- `add -remote` is exposed by the CLI but is not currently persisted by the service layer

24
api.Dockerfile Normal file
View File

@@ -0,0 +1,24 @@
FROM golang:1.26.3-trixie AS build
ENV GOOS=linux
ENV CGO_ENABLED=0
ENV GOAMD64=v3
ENV GORISCV64=rva22u64
ENV GOARM64=v8.2
COPY . /src
RUN cd /src \
&& go build -ldflags="-s -w" -o server ./cmd/server \
&& chown 0:0 server \
&& chmod ugo+x server
FROM busybox:1.37.0 AS prod
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /etc/shadow /etc/shadow
COPY --from=build /src/server /server
VOLUME [ "/var/lib/cloudsave" ]
ENTRYPOINT [ "/server" ]

View File

@@ -32,7 +32,8 @@ func (p *LoginCmd) SetFlags(f *flag.FlagSet) {
func (p *LoginCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if f.NArg() != 1 {
fmt.Fprintf(os.Stderr, "error: this command take 1 argument")
fmt.Fprintln(os.Stderr, "error: this command take 1 argument")
return subcommands.ExitUsageError
}
@@ -46,12 +47,12 @@ func (p *LoginCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
cli := client.New(server, username, password)
if _, err := cli.Version(); err != nil {
fmt.Fprintf(os.Stderr, "error: failed to login: %s", err)
fmt.Fprintf(os.Stderr, "error: failed to login: %s\n", err)
return subcommands.ExitFailure
}
if err := credentials.Login(username, password, server); err != nil {
fmt.Fprintf(os.Stderr, "error: failed to save login: %s", err)
fmt.Fprintf(os.Stderr, "error: failed to save login: %s\n", err)
return subcommands.ExitFailure
}

View File

@@ -27,9 +27,11 @@ func (*RemoteCmd) Usage() string {
The -list argument lists all remotes for each registered game.
This command performs a connection test.
Usage: cloudsave remote -list
The -set argument allow you to set (create or update)
the URL to the remote for a game
Usage: cloudsave remote -set GAME_ID REMOTE_URL
Options
`

View File

@@ -7,11 +7,10 @@ import (
"cloudsave/pkg/remote"
"cloudsave/pkg/remote/client"
"cloudsave/pkg/repository"
"cloudsave/pkg/sync"
"context"
"errors"
"flag"
"fmt"
"log/slog"
"os"
"time"
@@ -38,247 +37,92 @@ func (p *SyncCmd) SetFlags(f *flag.FlagSet) {
}
func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
games, err := p.Service.AllGames()
remoteCred := make(map[string]map[string]string)
rs, err := remote.All()
if err != nil {
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
fmt.Fprintln(os.Stderr, "error: failed to connect to the remote:", err)
return subcommands.ExitFailure
}
remoteCred := make(map[string]map[string]string)
for _, g := range games {
r, err := remote.One(g.ID)
if err != nil {
if errors.Is(err, remote.ErrNoRemote) {
fmt.Println("⬛", g.Name+": no remote configured")
continue
}
fmt.Fprintln(os.Stderr, "error: failed to load datastore:", err)
return subcommands.ExitFailure
done := make(map[string]struct{})
for _, r := range rs {
if _, ok := done[r.URL]; ok {
continue
}
cli, err := connect(remoteCred, r)
if err != nil {
fmt.Fprintln(os.Stderr, "error: failed to connect to the remote:", err)
return subcommands.ExitFailure
}
pg := progressbar.New(-1)
fmt.Println()
done[r.URL] = struct{}{}
syncer := sync.NewSyncer(cli, p.Service)
var pg *progressbar.ProgressBar
destroyPg := func() {
if err := pg.Finish(); err != nil {
slog.Error("failed to finish progressbar", "err", err)
}
if err := pg.Clear(); err != nil {
slog.Error("failed to clear progressbar", "err", err)
}
if err := pg.Close(); err != nil {
slog.Error("failed to close progressbar", "err", err)
}
pg.Finish()
pg.Clear()
pg.Close()
}
pg.Describe(fmt.Sprintf("[%s] Checking status...", g.Name))
exists, err := cli.Exists(r.GameID)
if err != nil {
slog.Error(err.Error())
continue
}
if !exists {
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
if err := p.push(g, cli); err != nil {
syncer.SetStateCallback(func(s sync.State, g repository.Metadata) {
switch s {
case sync.FetchingMetdata:
pg = progressbar.New(-1)
pg.Describe(fmt.Sprintf("%s: fetching metadata from repository", g.Name))
case sync.Pushing:
pg.Describe(fmt.Sprintf("%s: pushing data to the server", g.Name))
case sync.Pulling:
pg.Describe(fmt.Sprintf("%s: pull data from the server", g.Name))
case sync.UpToDate:
destroyPg()
fmt.Fprintln(os.Stderr, "failed to push:", err)
return subcommands.ExitFailure
}
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
if err := p.pushBackup(g, cli); err != nil {
fmt.Println("🆗", g.Name+": already up-to-date")
case sync.Pushed:
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))
remoteMetadata, err := cli.Metadata(r.GameID)
if err != nil {
destroyPg()
fmt.Fprintln(os.Stderr, "error: failed to get the game metadata from the remote:", err)
continue
}
pg.Describe(fmt.Sprintf("[%s] Pulling backup...", g.Name))
if err := p.pullBackup(g, cli); err != nil {
slog.Warn("failed to pull backup files", "err", err)
}
pg.Describe(fmt.Sprintf("[%s] Pushing backup...", g.Name))
if err := p.pushBackup(g, cli); err != nil {
slog.Warn("failed to push backup files", "err", err)
}
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")
if err := p.Service.SetVersion(r.GameID, remoteMetadata.Version); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to synchronize version number:", err)
continue
}
}
fmt.Println("🆗", g.Name+": already up-to-date")
continue
}
if g.Version > remoteMetadata.Version {
pg.Describe(fmt.Sprintf("[%s] Pushing data...", g.Name))
if err := p.push(g, cli); err != nil {
fmt.Println("⬆️", g.Name+": pushed")
case sync.Pulled:
destroyPg()
fmt.Fprintln(os.Stderr, "failed to push:", err)
return subcommands.ExitFailure
fmt.Println("⬇️", g.Name+": pulled")
}
})
syncer.SetErrorCallback(func(err error, g repository.Metadata) {
destroyPg()
fmt.Println("⬆️", g.Name+": pushed")
continue
}
fmt.Println("", g.Name+": "+err.Error())
})
if g.Version < remoteMetadata.Version {
destroyPg()
if err := p.pull(g, cli); err != nil {
destroyPg()
fmt.Fprintln(os.Stderr, "failed to push:", err)
return subcommands.ExitFailure
syncer.SetConflictCallback(func(a, b repository.Metadata) sync.ConflictResolution {
fmt.Println()
fmt.Println("--- ⚠️ CONFLICT ---")
fmt.Println(a.Name, "(", a.Path, ")")
fmt.Println("----")
fmt.Println("Your version:", a.Date.Format(time.RFC1123))
fmt.Println("Their version:", b.Date.Format(time.RFC1123))
fmt.Println()
res := prompt.Conflict()
switch res {
case prompt.Their:
return sync.Their
case prompt.My:
return sync.Mine
}
g.Version = remoteMetadata.Version
g.Date = remoteMetadata.Date
return sync.None
})
if err := p.Service.UpdateMetadata(g.ID, g); err != nil {
destroyPg()
fmt.Fprintln(os.Stderr, "failed to push:", err)
return subcommands.ExitFailure
}
fmt.Println("⬇️", g.Name+": pulled")
continue
}
destroyPg()
if g.Version == remoteMetadata.Version {
if err := p.conflict(r.GameID, g, remoteMetadata, cli); err != nil {
fmt.Fprintln(os.Stderr, "error: failed to resolve conflict:", err)
continue
}
continue
}
syncer.Sync()
}
fmt.Println("done.")
return subcommands.ExitSuccess
}
func (p *SyncCmd) conflict(gameID string, m, remoteMetadata repository.Metadata, cli *client.Client) error {
g, err := p.Service.One(gameID)
if err != nil {
slog.Warn("a conflict was found but the game is not found in the database")
slog.Debug("debug info", "gameID", gameID)
return nil
}
fmt.Println()
fmt.Println("--- ⚠️ CONFLICT ---")
fmt.Println(g.Name, "(", g.Path, ")")
fmt.Println("----")
fmt.Println("Your version:", g.Date.Format(time.RFC1123))
fmt.Println("Their version:", remoteMetadata.Date.Format(time.RFC1123))
fmt.Println()
res := prompt.Conflict()
switch res {
case prompt.My:
{
if err := p.push(m, cli); err != nil {
return fmt.Errorf("failed to push: %w", err)
}
}
case prompt.Their:
{
if err := p.pull(g, cli); err != nil {
return fmt.Errorf("failed to push: %w", err)
}
g.Version = remoteMetadata.Version
g.Date = remoteMetadata.Date
if err := p.Service.UpdateMetadata(g.ID, g); err != nil {
return fmt.Errorf("failed to push: %w", err)
}
}
}
return nil
}
func (p *SyncCmd) push(m repository.Metadata, cli *client.Client) error {
return p.Service.PushArchive(m.ID, "", cli)
}
func (p *SyncCmd) pushBackup(m repository.Metadata, cli *client.Client) error {
bs, err := p.Service.AllBackups(m.ID)
if err != nil {
return err
}
for _, b := range bs {
binfo, err := cli.ArchiveInfo(m.ID, b.UUID)
if err != nil {
if !errors.Is(err, client.ErrNotFound) {
return fmt.Errorf("failed to get remote information about the backup file: %w", err)
}
}
if binfo.MD5 != b.MD5 {
if err := cli.PushBackup(b, m); err != nil {
return fmt.Errorf("failed to push backup: %w", err)
}
}
}
return nil
}
func (p *SyncCmd) pullBackup(m repository.Metadata, cli *client.Client) error {
bs, err := cli.ListArchives(m.ID)
if err != nil {
return err
}
for _, uuid := range bs {
rinfo, err := cli.ArchiveInfo(m.ID, uuid)
if err != nil {
return err
}
linfo, err := p.Service.Backup(m.ID, uuid)
if err != nil {
return err
}
if linfo.MD5 != rinfo.MD5 {
if err := p.Service.PullBackup(m.ID, uuid, cli); err != nil {
return err
}
}
}
return nil
}
func (p *SyncCmd) pull(g repository.Metadata, cli *client.Client) error {
if err := p.Service.PullArchive(g.ID, "", cli); err != nil {
return err
}
return p.Service.ApplyCurrent(g.ID)
}
func connect(remoteCred map[string]map[string]string, r remote.Remote) (*client.Client, error) {
var cli *client.Client

View File

@@ -44,7 +44,6 @@ func main() {
subcommands.Register(subcommands.HelpCommand(), "help")
subcommands.Register(subcommands.FlagsCommand(), "help")
subcommands.Register(subcommands.CommandsCommand(), "help")
subcommands.Register(&version.VersionCmd{}, "help")
subcommands.Register(&add.AddCmd{Service: s}, "management")

View File

@@ -35,6 +35,17 @@ func init() {
datastorePath = filepath.Join(roaming, "cloudsave")
}
func Get(server string) (string, string, error) {
var err error
store, err := load()
if err == nil {
if c, ok := store[server]; ok {
return c.Username, c.Password, nil
}
}
return "","",fmt.Errorf("not found")
}
func Read(server string) (string, string, error) {
var err error
store, err := load()

View File

@@ -30,6 +30,14 @@ func run(updateChan <-chan struct{}) {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
if !filepath.IsAbs(documentRoot) {
if v, err := filepath.Abs(documentRoot); err == nil {
documentRoot = v
} else {
fatal("failed to get absolute path from document-root flag: "+err.Error(), 2)
}
}
slog.Info("loading .htpasswd")
h, err := htpasswd.Open(filepath.Join(documentRoot, ".htpasswd"))
if err != nil {

63
docker-compose.yml Normal file
View File

@@ -0,0 +1,63 @@
services:
api:
build:
context: .
dockerfile: api.Dockerfile
volumes:
- "./data:/var/lib/cloudsave"
networks:
- cloudsave_net
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:8080/heartbeat || exit 1
interval: 3s
timeout: 2s
retries: 3
start_period: 10s
labels:
- "traefik.enable=true"
- "traefik.http.routers.api-router.rule=Host(`${DOMAIN:-localhost}`) && PathPrefix(`/api`)"
- "traefik.http.routers.api-router.entrypoints=web"
- "traefik.http.services.cloudsave-api.loadbalancer.server.port=8080"
web:
build:
context: .
dockerfile: web.Dockerfile
volumes:
- "./config.json:/var/lib/cloudsave/config.json"
networks:
- cloudsave_net
depends_on:
api:
condition: service_healthy
labels:
- "traefik.enable=true"
- "traefik.http.routers.web-router.rule=Host(`${DOMAIN:-localhost}`) && PathPrefix(`/web`)"
- "traefik.http.routers.web-router.entrypoints=web"
- "traefik.http.services.cloudsave-web.loadbalancer.server.port=8080"
proxy:
image: traefik:3.7.0
ports:
- 127.0.0.1:80:80
- 127.0.0.1:8080:8080
networks:
- cloudsave_net
depends_on:
api:
condition: service_healthy
web:
condition: service_started
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
command:
- --api.dashboard=true
- --api.insecure=true
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --entrypoints.web.address=:80
- --log.level=DEBUG
- --accesslog=true
networks:
cloudsave_net:

View File

@@ -1,5 +1,5 @@
package constants
const Version = "0.0.4e"
const Version = "0.0.5"
const ApiVersion = 1

View File

@@ -384,6 +384,10 @@ func (c *Client) All() ([]repository.Metadata, error) {
return nil, errors.New("invalid payload sent by the server")
}
func (c *Client) BaseURL() string {
return c.baseURL
}
func (c *Client) get(url string) (obj.HTTPObject, error) {
cli := http.Client{}

View File

@@ -57,6 +57,28 @@ func One(gameID string) (Remote, error) {
return r, nil
}
func All() ([]Remote, error) {
d, err := os.ReadDir(filepath.Clean(datastorepath))
if err != nil {
return nil, fmt.Errorf("failed to load datastore: %w", err)
}
var res []Remote
for _, g := range d {
r, err := One(g.Name())
if err != nil {
if errors.Is(err, ErrNoRemote) {
continue
}
return nil, fmt.Errorf("failed to load remote: %w", err)
}
res = append(res, r)
}
return res, nil
}
func Set(gameID, url string) error {
r := Remote{
URL: url,

237
pkg/sync/sync.go Normal file
View File

@@ -0,0 +1,237 @@
package sync
import (
"cloudsave/pkg/data"
"cloudsave/pkg/remote"
"cloudsave/pkg/remote/client"
"cloudsave/pkg/repository"
"errors"
"fmt"
)
type (
ConflictResolution int
State int
decision int
Syncer struct {
cli *client.Client
service *data.Service
stateCallback func(s State, g repository.Metadata)
errorCallback func(err error, g repository.Metadata)
conflictCallback func(a, b repository.Metadata) ConflictResolution
}
)
const (
Their ConflictResolution = iota
Mine
None
)
const (
ignore decision = iota
push
pull
)
const (
FetchingMetdata State = iota
Pushing
Pulling
Pushed
Pulled
UpToDate
Done
)
var (
ErrFetching error = errors.New("failed to fetch the metadata")
ErrPushing error = errors.New("failed to push data")
ErrPulling error = errors.New("failed to pull data")
ErrDatastore error = errors.New("failed to get data from local datastore")
)
func NewSyncer(cli *client.Client, service *data.Service) *Syncer {
return &Syncer{
cli: cli,
service: service,
}
}
func (s *Syncer) SetStateCallback(fn func(s State, g repository.Metadata)) {
s.stateCallback = fn
}
func (s *Syncer) SetErrorCallback(fn func(err error, g repository.Metadata)) {
s.errorCallback = fn
}
func (s *Syncer) SetConflictCallback(fn func(a, b repository.Metadata) ConflictResolution) {
s.conflictCallback = fn
}
func (s *Syncer) Sync() {
games, err := s.service.AllGames()
if err != nil {
s.errorCallback(fmt.Errorf("failed to get all games: %w", err), repository.Metadata{})
return
}
for _, g := range games {
r, err := remote.One(g.ID)
if err != nil {
continue
}
if r.URL != s.cli.BaseURL() {
continue
}
if err := s.sync(g); err != nil {
s.errorCallback(err, g)
}
}
s.stateCallback(Done, repository.Metadata{})
}
func (s *Syncer) sync(g repository.Metadata) error {
s.stateCallback(FetchingMetdata, g)
remoteMetadata, err := s.cli.Metadata(g.ID)
if err != nil {
if errors.Is(err, client.ErrNotFound) {
s.stateCallback(Pushing, g)
if err := s.push(g); err != nil {
return fmt.Errorf("%w: %s", ErrPushing, err)
}
s.stateCallback(Pushed, g)
return nil
}
return fmt.Errorf("%w: %s", ErrFetching, err)
}
if g.MD5 == remoteMetadata.MD5 {
s.stateCallback(UpToDate, g)
return nil
}
d := ignore
if g.Version > remoteMetadata.Version {
d = push
}
if g.Version < remoteMetadata.Version {
d = pull
}
if g.Version == remoteMetadata.Version {
r := s.conflictCallback(g, remoteMetadata)
switch r {
case Mine:
{
d = push
}
case Their:
{
d = pull
}
}
return nil
}
switch d {
case push:
{
s.stateCallback(Pushing, g)
if err := s.push(g); err != nil {
return fmt.Errorf("%w: %s", ErrPushing, err)
}
s.stateCallback(Pushed, g)
return nil
}
case pull:
{
s.stateCallback(Pulling, g)
if err := s.pull(g, remoteMetadata); err != nil {
return fmt.Errorf("%w: %s", ErrPulling, err)
}
s.stateCallback(Pulled, g)
return nil
}
}
return nil
}
func (s *Syncer) push(g repository.Metadata) error {
if err := s.service.PushArchive(g.ID, "", s.cli); err != nil {
return err
}
// manage backup
bs, err := s.service.AllBackups(g.ID)
if err != nil {
return err
}
for _, b := range bs {
binfo, err := s.cli.ArchiveInfo(g.ID, b.UUID)
if err != nil {
if !errors.Is(err, client.ErrNotFound) {
return fmt.Errorf("failed to get remote information about the backup file: %w", err)
}
}
if binfo.MD5 != b.MD5 {
if err := s.cli.PushBackup(b, g); err != nil {
return fmt.Errorf("failed to push backup: %w", err)
}
}
}
return nil
}
func (s *Syncer) pull(g, r repository.Metadata) error {
g.Version = r.Version
g.Date = r.Date
if err := s.service.UpdateMetadata(g.ID, g); err != nil {
return err
}
if err := s.service.PullArchive(g.ID, "", s.cli); err != nil {
return err
}
if err := s.service.ApplyCurrent(g.ID); err != nil {
return err
}
// manage backup
bs, err := s.cli.ListArchives(g.ID)
if err != nil {
return err
}
for _, uuid := range bs {
rinfo, err := s.cli.ArchiveInfo(g.ID, uuid)
if err != nil {
return err
}
linfo, err := s.service.Backup(g.ID, uuid)
if err != nil {
return err
}
if linfo.MD5 != rinfo.MD5 {
if err := s.service.PullBackup(g.ID, uuid, s.cli); err != nil {
return err
}
}
}
return nil
}

View File

@@ -0,0 +1,40 @@
package iterator
type (
Iterator[T any] struct {
index int
values []T
}
)
func New[T any](values []T) *Iterator[T] {
return &Iterator[T]{
values: values,
}
}
func (it *Iterator[T]) Next() bool {
if len(it.values) == it.index {
return false
}
it.index += 1
return true
}
func (it *Iterator[T]) Value() T {
if len(it.values) == 0 {
var zero T
return zero
}
if len(it.values) == it.index {
var zero T
return zero
}
return it.values[it.index]
}
func (it *Iterator[T]) IsEmpty() bool {
return len(it.values) == 0
}

24
web.Dockerfile Normal file
View File

@@ -0,0 +1,24 @@
FROM golang:1.26.3-trixie AS build
ENV GOOS=linux
ENV CGO_ENABLED=0
ENV GOAMD64=v3
ENV GORISCV64=rva22u64
ENV GOARM64=v8.2
COPY . /src
RUN cd /src \
&& go build -ldflags="-s -w" -o web ./cmd/web \
&& chown 0:0 web \
&& chmod ugo+x web
FROM scratch AS prod
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /etc/shadow /etc/shadow
COPY --from=build /src/web /web
VOLUME [ "/var/lib/cloudsave" ]
ENTRYPOINT [ "/web" ]