github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/mainthread/mainthread.go (about)

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