diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..bbcd6ef --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch potatodrive", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "./cmd/main" + } + ] +} \ No newline at end of file diff --git a/cmd/main/main.go b/cmd/main/main.go index 10c86c5..9821b59 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -17,16 +17,19 @@ func main() { keys, _ := mgr.InstanceList() for _, keyname := range keys { - err := mgr.StartInstance(keyname, ui.Logger, func(err error) { + go func(keyname string) { + err := mgr.StartInstance(keyname, ui.Logger, func(err error) { + if err != nil { + ui.Logger.Err(err).Msgf("%s is offline %v", keyname, err) + } + }) if err != nil { - ui.Logger.Err(err).Msgf("%s is offline %v", keyname, err) + ui.Logger.Err(err).Msgf("Failed to start %s", keyname) } - }) - if err != nil { - ui.Logger.Err(err).Msgf("Failed to start %s", keyname) - } + }(keyname) } go bindings.CloseOnSigTerm(mgr) ui.Run() + //select {} } diff --git a/cmd/main/mgr.go b/cmd/main/mgr.go index 7a260fc..42e2f02 100644 --- a/cmd/main/mgr.go +++ b/cmd/main/mgr.go @@ -1,6 +1,7 @@ package main import ( + "errors" "io" "os" "path/filepath" @@ -69,7 +70,7 @@ func startInstance(parentkey registry.Key, keyname string, logger zerolog.Logger log.Printf("Starting %s on %s", keyname, basec.LocalPath) c, err := bindings.BindVirtualizationInstance(keyname, basec.LocalPath, fs, logger.With().Str("instance", keyname).Logger(), statecallback) if err != nil { - logger.Print(err) + return nil, err } logger.Info().Msgf("%s started", keyname) return c, nil @@ -113,6 +114,9 @@ func (m *Manager) StartInstance(id string, logger zerolog.Logger, statecallback if err != nil { return err } + if instance == nil { + return errors.New("instance is nil") + } m.instances[id] = instance return nil } diff --git a/win/cfapi/filesystem/register.go b/win/cfapi/filesystem/register.go index f8c7d6a..abc61cb 100644 --- a/win/cfapi/filesystem/register.go +++ b/win/cfapi/filesystem/register.go @@ -4,6 +4,9 @@ import ( "runtime" "unsafe" + "github.com/go-ole/go-ole" + "github.com/saltosystems/winrt-go" + "github.com/saltosystems/winrt-go/windows/foundation" "github.com/saltosystems/winrt-go/windows/storage" "github.com/saltosystems/winrt-go/windows/storage/provider" ) @@ -13,6 +16,16 @@ func getFolder(folder string) (*storage.IStorageFolder, error) { if err != nil { return nil, err } + semaphore := make(chan bool) + iid := winrt.ParameterizedInstanceGUID(foundation.GUIDAsyncOperationCompletedHandler, storage.SignatureStorageFolder) + handler := foundation.NewAsyncOperationCompletedHandler(ole.NewGUID(iid), func(instance *foundation.AsyncOperationCompletedHandler, asyncInfo *foundation.IAsyncOperation, asyncStatus foundation.AsyncStatus) { + semaphore <- true + }) + err = op.SetCompleted(handler) + if err != nil { + return nil, err + } + <-semaphore ptr, err := op.GetResults() return (*storage.IStorageFolder)(unsafe.Pointer(ptr)), err } @@ -20,7 +33,14 @@ func getFolder(folder string) (*storage.IStorageFolder, error) { func RegisterRootPath(id string, rootPath string) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - var info provider.StorageProviderSyncRootInfo + err := ole.RoInitialize(1) + if err != nil { + return err + } + info, err := provider.NewStorageProviderSyncRootInfo() + if err != nil { + return err + } folder, err := getFolder(rootPath) if err != nil { return err @@ -30,28 +50,28 @@ func RegisterRootPath(id string, rootPath string) error { if err == nil && existing != nil { // Already registered eid, _ := existing.GetId() - if eid == id { + /*if eid == id { // No need to register again return nil - } else { - // unregister first - err = provider.StorageProviderSyncRootManagerUnregister(eid) - if err != nil { - return err - } + } else {*/ + // unregister first + err = provider.StorageProviderSyncRootManagerUnregister(eid) + if err != nil { + return err } + //} } info.SetAllowPinning(true) info.SetId(id) info.SetPath(folder) info.SetDisplayNameResource("PotatoDrive " + rootPath) - info.SetIconResource("potato.ico") + info.SetIconResource("C:\\git\\potatodrive\\potato.ico") info.SetVersion("1") info.SetHydrationPolicy(provider.StorageProviderHydrationPolicyFull) info.SetHydrationPolicyModifier(provider.StorageProviderHydrationPolicyModifierAutoDehydrationAllowed) info.SetPopulationPolicy(provider.StorageProviderPopulationPolicyAlwaysFull) info.SetInSyncPolicy(provider.StorageProviderInSyncPolicyDirectoryLastWriteTime | provider.StorageProviderInSyncPolicyFileLastWriteTime) info.SetHardlinkPolicy(provider.StorageProviderHardlinkPolicyNone) - return provider.StorageProviderSyncRootManagerRegister(&info) + return provider.StorageProviderSyncRootManagerRegister(info) }