Fixing updates

This commit is contained in:
alex
2025-07-10 18:03:30 +03:00
parent e71d69f3f1
commit 5bc334fd2c
21 changed files with 617 additions and 418 deletions

View File

@@ -28,7 +28,7 @@ func readNodeUUIDRaw(p string) ([]byte, error) {
if err != nil {
return data, err
}
if len(data) != config.GetInternalConsts().GetUUIDLength() {
if len(data) != config.UUIDLength {
return data, errors.New("decoded UUID length mismatch")
}
return data, nil

View File

@@ -1,93 +0,0 @@
package corestate
import (
"context"
"fmt"
"os"
"syscall"
"time"
)
func (r *RunManager) File(index string) RunFileManagerContract {
value, ok := (*r.indexedPaths)[index]
if !ok {
err := r.indexPaths()
if err != nil {
return &RunFileManager{
err: err,
}
}
value, ok = (*r.indexedPaths)[index]
if !ok {
return &RunFileManager{
err: fmt.Errorf("cannot detect file under index %s", index),
}
}
}
return &RunFileManager{
indexedPath: value,
}
}
func (r *RunFileManager) Open() (*os.File, error) {
if r.err != nil {
return nil, r.err
}
file, err := os.OpenFile(r.indexedPath, os.O_RDWR, 0)
if err != nil {
return nil, err
}
r.file = file
return file, nil
}
func (r *RunFileManager) Close() error {
return r.file.Close()
}
func (r *RunFileManager) Watch(ctx context.Context, callback func()) error {
if r.err != nil {
return r.err
}
if r.file == nil {
return fmt.Errorf("file is not opened")
}
info, err := r.file.Stat()
if err != nil {
return err
}
origStat := info.Sys().(*syscall.Stat_t)
origIno := origStat.Ino
origModTime := info.ModTime()
go func() {
for {
select {
case <-ctx.Done():
return
default:
newInfo, err := os.Stat(r.indexedPath)
if err != nil {
if os.IsNotExist(err) {
callback()
return
}
} else {
newStat := newInfo.Sys().(*syscall.Stat_t)
if newStat.Ino != origIno {
callback()
return
}
if !newInfo.ModTime().Equal(origModTime) {
callback()
return
}
}
time.Sleep(1 * time.Second)
}
}
}()
return nil
}

View File

@@ -1,115 +0,0 @@
package corestate
import (
"fmt"
"os"
"path/filepath"
"github.com/akyaiy/GoSally-mvp/core/utils"
)
func NewRM() *RunManager {
return &RunManager{
indexedPaths: func() *map[string]string { m := make(map[string]string); return &m }(),
created: false,
}
}
func (c *CoreState) RuntimeDir() RunManagerContract {
return c.RM
}
// Create creates a temp directory
func (r *RunManager) Create(uuid32 string) (string, error) {
if r.created {
return r.runDir, fmt.Errorf("runtime directory is already created")
}
path, err := os.MkdirTemp("", fmt.Sprintf("*-%s-%s", uuid32, "gosally-runtime"))
if err != nil {
return "", err
}
r.runDir = path
r.created = true
return path, nil
}
func (r *RunManager) Clean() error {
return utils.CleanTempRuntimes(r.runDir)
}
// Quite dangerous and goofy.
// TODO: implement a better variant of runDir indexing on the second stage of initialization
func (r *RunManager) Toggle() string {
r.runDir = filepath.Dir(os.Args[0])
r.created = true
return r.runDir
}
func (r *RunManager) Get(index string) (string, error) {
if !r.created {
return "", fmt.Errorf("runtime directory is not created")
}
if r.indexedPaths == nil {
err := r.indexPaths()
if err != nil {
return "", nil
}
}
if r.indexedPaths == nil {
return "", fmt.Errorf("indexedPaths is nil")
}
value, ok := (*r.indexedPaths)[index]
if !ok {
err := r.indexPaths()
if err != nil {
return "", err
}
value, ok = (*r.indexedPaths)[index]
if !ok {
return "", fmt.Errorf("cannot detect file under index %s", index)
}
}
return value, nil
}
func (r *RunManager) Set(index string) error {
if !r.created {
return fmt.Errorf("runtime directory is not created")
}
fullPath := filepath.Join(r.runDir, index)
dir := filepath.Dir(fullPath)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
f, err := os.OpenFile(fullPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
if r.indexedPaths == nil {
err = r.indexPaths()
if err != nil {
return err
}
} else {
(*r.indexedPaths)[index] = fullPath
}
return nil
}
func (r *RunManager) indexPaths() error {
if !r.created {
return fmt.Errorf("runtime directory is not created")
}
i, err := utils.IndexPaths(r.runDir)
if err != nil {
return err
}
r.indexedPaths = i
return nil
}

View File

@@ -1,16 +1,10 @@
package corestate
import (
"context"
"os"
)
// CoreStateContract is interface for CoreState.
// CoreState is a structure that contains the basic meta-information vital to the node.
// The interface contains functionality for working with the Runtime directory and its files,
// and access to low-level logging in stdout
type CoreStateContract interface {
RuntimeDir() RunManagerContract
}
type CoreState struct {
@@ -27,36 +21,4 @@ type CoreState struct {
NodePath string
MetaDir string
RunDir string
RM *RunManager
}
type RunManagerContract interface {
Get(index string) (string, error)
// Set recursively creates a file in runDir
Set(index string) error
File(index string) RunFileManagerContract
indexPaths() error
}
type RunManager struct {
created bool
runDir string
// I obviously keep it with a pointer because it makes me feel calmer
indexedPaths *map[string]string
}
type RunFileManagerContract interface {
Open() (*os.File, error)
Close() error
Watch(ctx context.Context, callback func()) error
}
type RunFileManager struct {
err error
indexedPath string
file *os.File
}