Skip to content
Snippets Groups Projects
Select Git revision
  • 68a3d9c3d97d2ab313daa613dbaed2b6e6966079
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

storage.go

Blame
  • storage.go 4.26 KiB
    ///////////////////////////////////////////////////////////////////////////////
    // Copyright © 2020 xx network SEZC                                          //
    //                                                                           //
    // Use of this source code is governed by a license that can be found in the //
    // LICENSE file                                                              //
    ///////////////////////////////////////////////////////////////////////////////
    
    package globals
    
    import (
    	"os"
    	"sync"
    )
    
    const (
    	NoSave uint8 = iota
    	LocationA
    	LocationB
    )
    
    type Storage interface {
    	SetLocation(string, string) error
    	GetLocation() (string, string)
    	SaveA([]byte) error
    	SaveB([]byte) error
    	LoadA() []byte
    	LoadB() []byte
    	IsEmpty() bool
    }
    
    type DefaultStorage struct {
    	locationA string
    	locationB string
    	sync.Mutex
    }
    
    func (ds *DefaultStorage) SetLocation(locationA, locationB string) error {
    	ds.Lock()
    	ds.locationA = locationA
    	ds.locationB = locationB
    	ds.Unlock()
    	return nil
    }
    
    func (ds *DefaultStorage) GetLocation() (string, string) {
    	ds.Lock()
    	defer ds.Unlock()
    	return ds.locationA, ds.locationB
    }
    
    func (ds *DefaultStorage) IsEmpty() bool {
    	_, err := os.Stat(ds.locationA)
    	firstEmpty := err != nil && os.IsNotExist(err)
    	_, err = os.Stat(ds.locationB)
    	secondEmpty := err != nil && os.IsNotExist(err)
    	return firstEmpty && secondEmpty
    }
    
    func (ds *DefaultStorage) SaveA(data []byte) error {
    	return dsSaveHelper(ds.locationA, data)
    }
    
    func (ds *DefaultStorage) LoadA() []byte {
    	return dsLoadHelper(ds.locationA)
    }
    
    func (ds *DefaultStorage) SaveB(data []byte) error {
    	return dsSaveHelper(ds.locationB, data)
    }
    
    func (ds *DefaultStorage) LoadB() []byte {
    	return dsLoadHelper(ds.locationB)
    }
    
    type RamStorage struct {
    	DataA []byte
    	DataB []byte
    }
    
    func (rs *RamStorage) SetLocation(string, string) error {
    	return nil
    }
    
    func (rs *RamStorage) GetLocation() (string, string) {
    	return "", ""
    }
    
    func (rs *RamStorage) SaveA(data []byte) error {
    	rs.DataA = make([]byte, len(data))
    	copy(rs.DataA, data)
    	return nil
    }
    
    func (rs *RamStorage) SaveB(data []byte) error {
    	rs.DataB = make([]byte, len(data))
    	copy(rs.DataB, data)
    	return nil
    }
    
    func (rs *RamStorage) LoadA() []byte {
    	b := make([]byte, len(rs.DataA))
    	copy(b, rs.DataA)
    
    	return b
    }
    
    func (rs *RamStorage) LoadB() []byte {
    	b := make([]byte, len(rs.DataB))
    	copy(b, rs.DataB)
    
    	return b
    }
    
    func (rs *RamStorage) IsEmpty() bool {
    	return (rs.DataA == nil || len(rs.DataA) == 0) && (rs.DataB == nil || len(rs.DataB) == 0)
    }
    
    func dsLoadHelper(loc string) []byte {
    	// Check if the file exists, return nil if it does not
    	finfo, err1 := os.Stat(loc)
    
    	if err1 != nil {
    		Log.ERROR.Printf("Default Storage Load: Unknown Error Occurred on"+
    			" file check: \n  %v", err1.Error())
    		return nil
    	}
    
    	b := make([]byte, finfo.Size())
    
    	// Open the file, return nil if it cannot be opened
    	f, err2 := os.Open(loc)
    
    	defer func() {
    		if f != nil {
    			f.Close()
    		} else {
    			Log.WARN.Println("Could not close file, file is nil")
    		}
    	}()
    
    	if err2 != nil {
    		Log.ERROR.Printf("Default Storage Load: Unknown Error Occurred on"+
    			" file open: \n  %v", err2.Error())
    		return nil
    	}
    
    	// Read the data from the file, return nil if read fails
    	_, err3 := f.Read(b)
    
    	if err3 != nil {
    		Log.ERROR.Printf("Default Storage Load: Unknown Error Occurred on"+
    			" file read: \n  %v", err3.Error())
    		return nil
    	}
    
    	return b
    
    }
    
    func dsSaveHelper(loc string, data []byte) error {
    	//check if the file exists, delete if it does
    	_, err1 := os.Stat(loc)
    
    	if err1 == nil {
    		errRmv := os.Remove(loc)
    		if errRmv != nil {
    			Log.WARN.Printf("Could not remove Storage File B: %s", errRmv)
    		}
    	} else if !os.IsNotExist(err1) {
    		Log.ERROR.Printf("Default Storage Save: Unknown Error Occurred on"+
    			" file check: \n  %v",
    			err1.Error())
    		return err1
    	}
    
    	//create new file
    	f, err2 := os.Create(loc)
    
    	defer func() {
    		if f != nil {
    			f.Close()
    		} else {
    			Log.WARN.Println("Could not close file, file is nil")
    		}
    	}()
    
    	if err2 != nil {
    		Log.ERROR.Printf("Default Storage Save: Unknown Error Occurred on"+
    			" file creation: \n %v", err2.Error())
    		return err2
    	}
    
    	//Save to file
    	_, err3 := f.Write(data)
    
    	if err3 != nil {
    		Log.ERROR.Printf("Default Storage Save: Unknown Error Occurred on"+
    			" file write: \n %v", err3.Error())
    		return err3
    	}
    
    	return nil
    }