1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package mainthread
import (
"os"
"path/filepath"
"syscall"
)
// FuncChannel passes functions executed in main thread
var FuncChannel = make(chan func())
// Execute allows to execute a function in the main thread
func Execute(f func()) {
done := make(chan bool)
FuncChannel <- func() {
f()
done <- true
}
<-done
}
// Stat retrieves file stat information from main thread
func Stat(name string) (fi os.FileInfo, err error) {
Execute(func() {
fi, err = os.Stat(name)
})
return
}
// Readlink returns the destination of link name from main thread
func Readlink(name string) (dest string, err error) {
Execute(func() {
dest, err = os.Readlink(name)
})
return
}
// EvalSymlinks returns the evaluated path after link resolution from main thread
func EvalSymlinks(path string) (rpath string, err error) {
Execute(func() {
rpath, err = filepath.EvalSymlinks(path)
})
return
}
// Chdir changes current working directory to the provided directory
func Chdir(dir string) (err error) {
Execute(func() {
err = os.Chdir(dir)
})
return
}
// Fchdir changes current working directory to the directory pointed
func Fchdir(fd int) (err error) {
Execute(func() {
err = syscall.Fchdir(fd)
})
return
}
|