Skip to content

Commit

Permalink
feat: reconstruct works
Browse files Browse the repository at this point in the history
  • Loading branch information
bsach64 committed Dec 8, 2024
1 parent 150677e commit 695f4ff
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 26 deletions.
1 change: 0 additions & 1 deletion Directory.json

This file was deleted.

26 changes: 1 addition & 25 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ func ClientLoop(cmd *cobra.Command, args []string) {
}

case "Add Directory to Sync":
dir, err := promptForDirectory()
if err != nil {
log.Error("Could not get directory for sync", "err", err)
continue
}
err = watchAndUpload(dir, sshC, worker)
err = watchAndUpload("./files", sshC, worker)
if err != nil {
log.Error("Watcher with error", "err", err)
}
Expand Down Expand Up @@ -255,25 +250,6 @@ func SendWorkerDetails(worker server.Worker, sshC *ssh.Client) error {
return nil
}

func promptForDirectory() (string, error) {
var directory string
directoryPrompt := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Enter Directory to Watch (If it does not exist it will be create)").
Prompt("? ").
Placeholder("./files").
Suggestions([]string{"./files"}).
Value(&directory),
),
)
err := directoryPrompt.Run()
if err != nil {
return "", err
}
return directory, nil
}

func promptForIP() (string, error) {
var ip string
ipPrompt := huh.NewForm(
Expand Down
95 changes: 95 additions & 0 deletions server/worker.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package server

import (
"encoding/json"
"os"
"path/filepath"
"time"

"github.com/bsach64/goback/utils"
"github.com/charmbracelet/log"
"github.com/fsnotify/fsnotify"
)

// Worker is just a usual SFTP server that handles the file request
Expand All @@ -18,6 +22,9 @@ type Worker struct {
}

func (w *Worker) StartSFTPServer() {
fileWrites := make(map[string]time.Time)
delay := 10 * time.Millisecond

wd, err := os.Getwd()
if err != nil {
log.Errorf("Cannot get the working directory: %v", err)
Expand All @@ -34,6 +41,94 @@ func (w *Worker) StartSFTPServer() {
}
}()

watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}

go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
log.Error("Watcher events channel closed")
return
}
if event.Has(fsnotify.Create | fsnotify.Write) {
fileWrites[event.Name] = time.Now()
}
case err, ok := <-watcher.Errors:
if !ok {
log.Error("Watcher errors channel closed")
return
}
log.Errorf("Watcher error: %v", err)
}

for filename, t := range fileWrites {
if time.Since(t) > delay {
time.Sleep(delay)
}
err := reconstruct(wd, filename)
if err != nil {
log.Error("Could not reconstruct file", filename, "err", err)
continue
}
log.Info("Reconstructed file", filename)
delete(fileWrites, filename)
}
}
}()

err = os.MkdirAll(filepath.Join(wd, ".data", "snapshots"), 0755)
if err != nil {
watcher.Close()
log.Fatalf("failed to create directory for watcher: %v", err)
}

err = watcher.Add(filepath.Join(wd, ".data", "snapshots"))
if err != nil {
watcher.Close() // Clean up if watcher.Add fails
log.Fatalf("failed to add directory to watcher: %v", err)
}

// Keep the worker process alive
select {}
}

func reconstruct(wd, filename string) error {
dat, err := os.ReadFile(filename)
if err != nil {
return err
}

var snapshot utils.Snapshot
err = json.Unmarshal(dat, &snapshot)
if err != nil {
return err
}

byteData, err := utils.Reconstruct(snapshot)
if err != nil {
return err
}

log.Info("BYTES", string(byteData))

err = os.MkdirAll(filepath.Join(wd, "files"), 0755)
if err != nil {
return err
}

file, err := os.Create(filepath.Join(wd, snapshot.Filename))
if err != nil {
return err
}

_, err = file.Write(byteData)
if err != nil {
return err
}
log.Info("Recreated: ", "file", filename)
return nil
}

0 comments on commit 695f4ff

Please sign in to comment.