Skip to content

Commit

Permalink
Starting on bg threads because of RoInitialize
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgrill committed Aug 19, 2024
1 parent b65069d commit 85f8767
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
15 changes: 9 additions & 6 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
6 changes: 5 additions & 1 deletion cmd/main/mgr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
40 changes: 30 additions & 10 deletions win/cfapi/filesystem/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"runtime"
"unsafe"

"github.com/go-ole/go-ole"
"github.com/saltosystems/winrt-go"

Check failure on line 8 in win/cfapi/filesystem/register.go

View workflow job for this annotation

GitHub Actions / build

github.com/saltosystems/[email protected]: replacement directory ../winrt-go does not exist
"github.com/saltosystems/winrt-go/windows/foundation"

Check failure on line 9 in win/cfapi/filesystem/register.go

View workflow job for this annotation

GitHub Actions / build

github.com/saltosystems/[email protected]: replacement directory ../winrt-go does not exist
"github.com/saltosystems/winrt-go/windows/storage"

Check failure on line 10 in win/cfapi/filesystem/register.go

View workflow job for this annotation

GitHub Actions / build

github.com/saltosystems/[email protected]: replacement directory ../winrt-go does not exist
"github.com/saltosystems/winrt-go/windows/storage/provider"

Check failure on line 11 in win/cfapi/filesystem/register.go

View workflow job for this annotation

GitHub Actions / build

github.com/saltosystems/[email protected]: replacement directory ../winrt-go does not exist
)
Expand All @@ -13,14 +16,31 @@ 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
}

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
Expand All @@ -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)
}

0 comments on commit 85f8767

Please sign in to comment.