Skip to content

Commit

Permalink
renamed core package
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgrill committed Sep 6, 2024
1 parent 639b5e0 commit 76ddee6
Show file tree
Hide file tree
Showing 35 changed files with 364 additions and 128 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"request": "launch",
"mode": "auto",
"program": "./cmd/main"
},
{
"name": "Test statuswindow",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "./test/statuswindow"
}
]
}
16 changes: 8 additions & 8 deletions bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

"github.com/balazsgrill/potatodrive/bindings/s3"
"github.com/balazsgrill/potatodrive/bindings/sftp"
"github.com/balazsgrill/potatodrive/win"
cfapi "github.com/balazsgrill/potatodrive/win/cfapi/filesystem"
prjfs "github.com/balazsgrill/potatodrive/win/projfs/filesystem"
"github.com/balazsgrill/potatodrive/core"
cfapi "github.com/balazsgrill/potatodrive/core/cfapi/filesystem"
prjfs "github.com/balazsgrill/potatodrive/core/projfs/filesystem"
"github.com/spf13/afero"
"golang.org/x/sys/windows/registry"
)
Expand Down Expand Up @@ -116,8 +116,8 @@ func (f closerFunc) Close() error {
return f()
}

func BindVirtualizationInstance(id string, localpath string, remotefs afero.Fs, logger zerolog.Logger, statecallback func(win.ConnectionState)) (io.Closer, error) {
var closer win.Virtualization
func BindVirtualizationInstance(id string, localpath string, remotefs afero.Fs, logger zerolog.Logger, statecallback func(core.ConnectionState)) (io.Closer, error) {
var closer core.Virtualization
var err error
if UseCFAPI {
err = cfapi.RegisterRootPath(id, localpath)
Expand All @@ -133,21 +133,21 @@ func BindVirtualizationInstance(id string, localpath string, remotefs afero.Fs,
}

internalSynchronize := func() {
statecallback(win.ConnectionState{
statecallback(core.ConnectionState{
ID: id,
SyncInProgress: true,
LastSyncError: nil,
})
err = closer.PerformSynchronization()
if err != nil {
logger.Err(err).Send()
statecallback(win.ConnectionState{
statecallback(core.ConnectionState{
ID: id,
SyncInProgress: false,
LastSyncError: err,
})
} else {
statecallback(win.ConnectionState{
statecallback(core.ConnectionState{
ID: id,
SyncInProgress: false,
LastSyncError: nil,
Expand Down
6 changes: 3 additions & 3 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/balazsgrill/potatodrive/bindings"
"github.com/balazsgrill/potatodrive/win"
"github.com/balazsgrill/potatodrive/core"
)

var Version string = "0.0.0-dev"
Expand All @@ -23,7 +23,7 @@ func main() {
})
defer ui.ni.Dispose()

err = win.CheckAlreadyRunning()
err = core.CheckAlreadyRunning()
if err != nil {
ui.NotificationInfo("Can't start PotatoDrive", "Already running")
return
Expand All @@ -32,7 +32,7 @@ func main() {
keys, _ := mgr.InstanceList()
for _, keyname := range keys {
go func(keyname string) {
err := mgr.StartInstance(keyname, ui.Logger, func(state win.ConnectionState) {
err := mgr.StartInstance(keyname, ui.Logger, func(state core.ConnectionState) {
if state.LastSyncError != nil {
ui.Logger.Err(err).Msgf("%s is offline %v", keyname, err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/main/mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"

"github.com/balazsgrill/potatodrive/bindings"
"github.com/balazsgrill/potatodrive/win"
"github.com/balazsgrill/potatodrive/core"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sys/windows/registry"
Expand Down Expand Up @@ -44,7 +44,7 @@ func initLogger() (string, zerolog.Logger, io.Closer) {
return logfilepath, log.Output(zerolog.MultiLevelWriter(logf, zerolog.NewConsoleWriter())).With().Timestamp().Logger(), logf
}

func startInstance(parentkey registry.Key, keyname string, logger zerolog.Logger, statecallback func(win.ConnectionState)) (io.Closer, error) {
func startInstance(parentkey registry.Key, keyname string, logger zerolog.Logger, statecallback func(core.ConnectionState)) (io.Closer, error) {
key, err := registry.OpenKey(parentkey, keyname, registry.QUERY_VALUE)
if err != nil {
logger.Printf("Open key: %v", err)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (m *Manager) InstanceList() ([]string, error) {
return m.keylist, nil
}

func (m *Manager) StartInstance(id string, logger zerolog.Logger, statecallback func(win.ConnectionState)) error {
func (m *Manager) StartInstance(id string, logger zerolog.Logger, statecallback func(core.ConnectionState)) error {
instance, err := startInstance(m.parentkey, id, logger, statecallback)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/main/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
_ "image/png"

"github.com/balazsgrill/potatodrive/assets"
"github.com/balazsgrill/potatodrive/win"
"github.com/balazsgrill/potatodrive/core"
"github.com/lxn/walk"
"github.com/rs/zerolog"
"golang.org/x/sys/windows"
Expand Down Expand Up @@ -89,7 +89,7 @@ func createUI(context UIContext) *UI {
logger.Fatal().Err(err).Send()
}
openLogAction.Triggered().Attach(func() {
win.OpenFile(windows.Handle(ui.MainWindow.Handle()), context.LogFile)
core.OpenFile(windows.Handle(ui.MainWindow.Handle()), context.LogFile)
})
if err := ui.ni.ContextMenu().Actions().Add(openLogAction); err != nil {
logger.Fatal().Err(err).Send()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion win/cfapi/cfapi_test.go → core/cfapi/cfapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cfapi_test
import (
"testing"

"github.com/balazsgrill/potatodrive/win/cfapi"
"github.com/balazsgrill/potatodrive/core/cfapi"
)

func Test_PlatformVersion(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"syscall"
"unsafe"

"github.com/balazsgrill/potatodrive/win"
"github.com/balazsgrill/potatodrive/win/cfapi"
"github.com/balazsgrill/potatodrive/core"
"github.com/balazsgrill/potatodrive/core/cfapi"
)

const BUFFER_SIZE int64 = 1024 * 1024

func (instance *VirtualizationInstance) callback_getFilePath(info *cfapi.CF_CALLBACK_INFO) string {
return win.GetString(info.VolumeDosName) + win.GetString(info.NormalizedPath)
return core.GetString(info.VolumeDosName) + core.GetString(info.NormalizedPath)
}

func (instance *VirtualizationInstance) callback_getRemoteFilePath(info *cfapi.CF_CALLBACK_INFO) string {
Expand Down Expand Up @@ -48,14 +48,14 @@ func (tb *transferBuffer) send(updatehash hash.Hash) error {
tb.byteOffset += tb.count
tb.count = 0

return win.ErrorByCode(hr)
return core.ErrorByCode(hr)
}

func (instance *VirtualizationInstance) fetchData(info *cfapi.CF_CALLBACK_INFO, data *cfapi.CF_CALLBACK_PARAMETERS_FetchData) uintptr {
instance.lock.Lock()
defer instance.lock.Unlock()
localpath := instance.callback_getFilePath(info)
instance.NotifyFileState(localpath, win.FileSyncStateDownloading)
instance.NotifyFileState(localpath, core.FileSyncStateDownloading)

filename := instance.path_localToRemote(localpath)
length := data.RequiredLength
Expand Down Expand Up @@ -129,7 +129,7 @@ func (instance *VirtualizationInstance) fetchData(info *cfapi.CF_CALLBACK_INFO,
instance.Logger.Warn().Msgf("Error updating state cache %s: %s", filename, err)
}
}
instance.NotifyFileState(localpath, win.FileSyncStateDone)
instance.NotifyFileState(localpath, core.FileSyncStateDone)

return 0
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package filesystem

import "github.com/balazsgrill/potatodrive/win"
import "github.com/balazsgrill/potatodrive/core"

func (instance *VirtualizationInstance) NotifyFileState(path string, state win.FileSyncStateEnum) {
func (instance *VirtualizationInstance) NotifyFileState(path string, state core.FileSyncStateEnum) {
if instance.handler == nil {
return
}
instance.handler(win.FileSyncState{
instance.handler(core.FileSyncState{
Path: path,
State: state,
})
Expand All @@ -16,9 +16,9 @@ func (instance *VirtualizationInstance) NotifyFileError(path string, err error)
if instance.handler == nil {
return
}
instance.handler(win.FileSyncState{
instance.handler(core.FileSyncState{
Path: path,
State: win.FileSyncStateError,
State: core.FileSyncStateError,
LastError: err,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/rs/zerolog"

"github.com/balazsgrill/potatodrive/win"
"github.com/balazsgrill/potatodrive/win/cfapi"
"github.com/balazsgrill/potatodrive/core"
"github.com/balazsgrill/potatodrive/core/cfapi"
"github.com/fsnotify/fsnotify"
"github.com/spf13/afero"
"golang.org/x/sys/windows"
Expand All @@ -27,28 +27,28 @@ type VirtualizationInstance struct {
shortprefix string
longprefix string
fs afero.Fs
remoteCacheState win.RemoteStateCache
remoteCacheState core.RemoteStateCache

connectionKey cfapi.CF_CONNECTION_KEY
lock sync.Mutex
watcher *fsnotify.Watcher
handler func(state win.FileSyncState)
handler func(state core.FileSyncState)
}

func (instance *VirtualizationInstance) SetFileStateHandler(handler func(state win.FileSyncState)) {
func (instance *VirtualizationInstance) SetFileStateHandler(handler func(state core.FileSyncState)) {
instance.handler = handler
}

func StartProjecting(rootPath string, filesystem afero.Fs, logger zerolog.Logger) (win.Virtualization, error) {
func StartProjecting(rootPath string, filesystem afero.Fs, logger zerolog.Logger) (core.Virtualization, error) {
instance := &VirtualizationInstance{
Logger: logger,
rootPath: rootPath,
fs: filesystem,
remoteCacheState: win.HashFilesRemotely(filesystem),
remoteCacheState: core.HashFilesRemotely(filesystem),
}

instance.longprefix = win.ToLongPath(rootPath)
instance.shortprefix = win.ToShortPath(rootPath)
instance.longprefix = core.ToLongPath(rootPath)
instance.shortprefix = core.ToShortPath(rootPath)

return instance, instance.start()
}
Expand All @@ -61,9 +61,9 @@ func (instance *VirtualizationInstance) start() error {
}

instance.Logger.Print("Connecting sync root")
hr := cfapi.CfConnectSyncRoot(win.GetPointer(instance.rootPath), callbacks.CreateCallbackTable(), uintptr(unsafe.Pointer(instance)), cfapi.CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH, &instance.connectionKey)
hr := cfapi.CfConnectSyncRoot(core.GetPointer(instance.rootPath), callbacks.CreateCallbackTable(), uintptr(unsafe.Pointer(instance)), cfapi.CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH, &instance.connectionKey)

err := win.ErrorByCode(hr)
err := core.ErrorByCode(hr)
if err != nil {
return err
}
Expand All @@ -86,7 +86,7 @@ func getFileNameFromIdentity(info *cfapi.CF_CALLBACK_INFO) string {
func getPlaceholder(f fs.FileInfo) cfapi.CF_PLACEHOLDER_CREATE_INFO {
var placeholder cfapi.CF_PLACEHOLDER_CREATE_INFO
filename := f.Name()
placeholder.RelativeFileName = win.GetPointer(filename)
placeholder.RelativeFileName = core.GetPointer(filename)
placeholder.FsMetadata.BasicInfo = toBasicInfo(f)
identity := []byte(filename)
placeholder.FileIdentity = uintptr(unsafe.Pointer(&identity[0]))
Expand Down Expand Up @@ -147,7 +147,7 @@ func (instance *VirtualizationInstance) Close() error {
instance.watcher.Close()
hr := cfapi.CfDisconnectSyncRoot(instance.connectionKey)
if hr != 0 {
return win.ErrorByCode(hr)
return core.ErrorByCode(hr)
}

return nil
Expand All @@ -168,9 +168,9 @@ func (instance *VirtualizationInstance) setInSync(localpath string) error {
}

var handle syscall.Handle
hr := cfapi.CfOpenFileWithOplock(win.GetPointer(localpath), cfapi.CF_OPEN_FILE_FLAG_WRITE_ACCESS|cfapi.CF_OPEN_FILE_FLAG_EXCLUSIVE, &handle)
hr := cfapi.CfOpenFileWithOplock(core.GetPointer(localpath), cfapi.CF_OPEN_FILE_FLAG_WRITE_ACCESS|cfapi.CF_OPEN_FILE_FLAG_EXCLUSIVE, &handle)
if hr != 0 {
return win.ErrorByCode(hr)
return core.ErrorByCode(hr)
}
defer cfapi.CfCloseHandle(handle)

Expand All @@ -187,14 +187,14 @@ func (instance *VirtualizationInstance) setInSync(localpath string) error {
// setting in-sync staate only works if it's a placeholder
hr = cfapi.CfConvertToPlaceholder(handle, placeholder.FileIdentity, placeholder.FileIdentityLength, cfapi.CF_CONVERT_FLAG_NONE, 0, 0)
if hr != 0 {
return win.ErrorByCode(hr)
return core.ErrorByCode(hr)
}
}
if !insync {
// updating a placeholder only works if it is marked as in-sync
hr = cfapi.CfSetInSyncState(handle, cfapi.CF_IN_SYNC_STATE_IN_SYNC, cfapi.CF_SET_IN_SYNC_FLAG_NONE, nil)
if hr != 0 {
return win.ErrorByCode(hr)
return core.ErrorByCode(hr)
}
}
return nil
Expand Down Expand Up @@ -274,7 +274,7 @@ func getPlaceholderInfo(localpath string) (*cfapi.CF_PLACEHOLDER_BASIC_INFO, err
var placeholderInfo cfapi.CF_PLACEHOLDER_BASIC_INFO
var ReturnedLength uint32
hr := cfapi.CfGetPlaceholderInfo(fileHandle, cfapi.CF_PLACEHOLDER_INFO_BASIC, uintptr(unsafe.Pointer(&placeholderInfo)), uint32(unsafe.Sizeof(placeholderInfo)), &ReturnedLength)
return &placeholderInfo, win.ErrorByCode(hr)
return &placeholderInfo, core.ErrorByCode(hr)
}

const FileAttributeTagInfo uint32 = 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/balazsgrill/potatodrive/win"
"github.com/balazsgrill/potatodrive/win/cfapi/filesystem"
"github.com/balazsgrill/potatodrive/core"
"github.com/balazsgrill/potatodrive/core/cfapi/filesystem"
"github.com/spf13/afero"
)

type testInstance struct {
t *testing.T
location string
fs afero.Fs
closer win.Virtualization
closer core.Virtualization
closechan chan bool
}

Expand All @@ -31,7 +31,7 @@ func newTestInstance(t *testing.T) *testInstance {
os.RemoveAll(location)
os.MkdirAll(location, 0x777)
uid := uuid.NewMD5(uuid.UUID{}, []byte(location))
id := win.BytesToGuid(uid[:])
id := core.BytesToGuid(uid[:])
err := filesystem.RegisterRootPathSimple(*id, location)
if err != nil {
t.Fatal(err)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"path/filepath"
"strings"

"github.com/balazsgrill/potatodrive/win"
"github.com/balazsgrill/potatodrive/core"
)

func (instance *VirtualizationInstance) path_localToRemote(path string) string {
p := win.ToLongPath(path)
p := core.ToLongPath(path)
p = strings.TrimPrefix(p, instance.shortprefix)
p = strings.TrimPrefix(p, instance.longprefix)
p = strings.ReplaceAll(p, "\\", "/")
Expand Down
Loading

0 comments on commit 76ddee6

Please sign in to comment.