Makefile, server entry point prototype, compareFiles function + other minor changes

This commit is contained in:
Gu://em_ 2025-04-13 12:50:35 +02:00
parent 2efb8c4130
commit c1ccb61346
10 changed files with 139 additions and 56 deletions

View file

@ -7,14 +7,11 @@ import (
"github.com/spf13/viper"
)
// Config will follow this model
// server : string
// port : int
var config_loaded bool = false
// Load config file
func LoadConfig() error {
@ -43,6 +40,7 @@ func LoadConfig() error {
// return err
}
config_loaded = true
return nil
}
@ -57,7 +55,16 @@ func LoadSyncList() error {
}
// returns all config variables
// ! WARNING: Do not use this function without executing LoadConfig prior
func GetConfig() map[string]any {
return viper.AllSettings()
if config_loaded {
return viper.AllSettings()
} else {
err := LoadConfig()
if err != nil {
panic("GetConfig: Failed to load config file -> " + err.Error())
}
return viper.AllSettings()
}
}

View file

@ -5,10 +5,10 @@ import (
)
type File struct {
ID string
ParentDirectory string
Metadata FileMetadata
State SynchronizationState
ID string
ParentDirectory Folder
Metadata FileMetadata
State SynchronizationState
}
type FileMetadata struct {
@ -22,4 +22,13 @@ type SynchronizationState struct {
Status string // "pending", "in_progress", "completed", "error"
Progress float64
LastError error
}
type Folder struct {
ID string
Files []File
Subfolders []Folder
Name string
Path string
}

View file

@ -1,8 +1,36 @@
package core
import (
"crypto/sha256"
"io"
"os"
)
//! Not implemented
func CompareFiles(file1 File, file2 File) bool {
return false
// Panic if e contains an error
func Check(e error) {
if e != nil {
panic("Check: A fatal error occured during execution -> " + e.Error())
}
}
// Returns true if files correspond
func CompareFiles(file1 File, file2 File) bool {
return file1.Metadata.Hash == file2.Metadata.Hash
}
// Gets the sha256 hash of a file's content
func GetHash(file File) ([]byte, error) {
// init
hasher := sha256.New()
file_path := "" //TODO: missing
// Open file
f, err := os.Open(file_path)
if err != nil { // Path error
return nil, err
}
// Compute hash
io.Copy(hasher, f)
sum := hasher.Sum(nil)
return sum, nil
}

View file

@ -27,7 +27,7 @@ func Run() {
serverPort := strconv.Itoa(config["port"].(int))
// Listening
listener, err := net.Listen("tcp", ":" + serverPort)
listener, err := net.Listen("tcp", ":"+serverPort)
if err != nil {
panic(err)
}
@ -65,7 +65,7 @@ func handleConnection(conn net.Conn) {
log.Println("Error reading from ", clientAddress, ": ", err)
return
}
// to rm
println(message)
// fmt.Println("Received from client:", message)
@ -76,7 +76,7 @@ func handleConnection(conn net.Conn) {
// Send a response to the client
_, err = conn.Write([]byte("Server received your message\n"))
if err != nil {
log.Println("Error replying to", clientAddress,": ", err)
log.Println("Error replying to", clientAddress, ": ", err)
return
}
}