diff --git a/filesystem.go b/filesystem.go index 5c859c5..d67a651 100644 --- a/filesystem.go +++ b/filesystem.go @@ -24,6 +24,11 @@ type VirtualizationInstance struct { enumerations map[syscall.GUID]*enumerationSession } +type Virtualization interface { + io.Closer + PerformSynchronization() error +} + type enumerationSession struct { searchstr uintptr countget int @@ -76,7 +81,44 @@ func (instance *VirtualizationInstance) start(rootPath string, filesystem afero. ConcurrentThreadCount: 4, } hr = projfs.PrjStartVirtualizing(rootPath, instance.get_callbacks(), instance, options, &instance._instanceHandle) - return projfs.ErrorByCode(hr) + err = projfs.ErrorByCode(hr) + if err != nil { + log.Printf("Error starting virtualization: %s", err) + return err + } + return instance.syncRemoteToLcal() +} + +func (instance *VirtualizationInstance) syncRemoteToLcal() error { + return afero.Walk(instance.fs, "", func(path string, info fs.FileInfo, err error) error { + if os.IsNotExist(err) { + return nil + } + if err != nil { + return err + } + if info.IsDir() { + return nil + } + localpath := instance.rootPath + "\\" + path + var localstate projfs.PRJ_FILE_STATE + hr := projfs.PrjGetOnDiskFileState(localpath, &localstate) + if hr != 0 { + return projfs.ErrorByCode(hr) + } + + if localstate == projfs.PRJ_FILE_STATE_FULL { + // check if remote is newer + localinfo, _ := os.Stat(localpath) + if localinfo.ModTime().UTC().Unix() > info.ModTime().UTC().Unix() { + var placeholderInfo projfs.PRJ_PLACEHOLDER_INFO + placeholderInfo.FileBasicInfo = toBasicInfo(info) + instance.UpdateFileIfNeeded(path, &placeholderInfo, uint32(info.Size()), projfs.PRJ_UPDATE_ALLOW_DIRTY_METADATA, nil) + } + } + + return nil + }) } func (instance *VirtualizationInstance) getVirtualizationInfoFileName() string { diff --git a/filesystem_test.go b/filesystem_test.go index b6f16ff..6da0b3e 100644 --- a/filesystem_test.go +++ b/filesystem_test.go @@ -368,3 +368,38 @@ func TestUpdatedLocallyWhileOffline(t *testing.T) { } instance.stop() } + +func TestRemoveFolder(t *testing.T) { + foldername := "test" + instance := newTestInstance(t) + err := instance.fs.Mkdir(foldername, 0x777) + if err != nil { + t.Fatal(err) + } + instance.start() + defer instance.stop() + + file, err := os.Stat(instance.location + "\\" + foldername) + if err != nil { + t.Fatal(err) + } + if file.IsDir() != true { + t.Error("Not a directory") + } + + err = instance.osRemoveDir(foldername) + if err != nil { + t.Fatal(err) + } + _, err = instance.fs.Stat(foldername) + if err != nil { + if os.IsNotExist(err) { + //ok + return + } + t.Fatal(err) + } else { + t.Error("File exists") + } + +}