github.com/criyle/go-sandbox@v0.10.3/container/utils.go (about)

     1  package container
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"syscall"
     7  )
     8  
     9  func intSliceToUintptr(s []int) []uintptr {
    10  	var r []uintptr
    11  	if len(s) > 0 {
    12  		r = make([]uintptr, len(s))
    13  		for i, x := range s {
    14  			r[i] = uintptr(x)
    15  		}
    16  	}
    17  	return r
    18  }
    19  
    20  func uintptrSliceToInt(s []uintptr) []int {
    21  	var r []int
    22  	if len(s) > 0 {
    23  		r = make([]int, len(s))
    24  		for i, x := range s {
    25  			r[i] = int(x)
    26  		}
    27  	}
    28  	return r
    29  }
    30  
    31  func closeOnExecFds(s []int) {
    32  	for _, f := range s {
    33  		syscall.CloseOnExec(f)
    34  	}
    35  }
    36  
    37  func closeFds(s []int) {
    38  	for _, f := range s {
    39  		syscall.Close(f)
    40  	}
    41  }
    42  
    43  // removeContents delete content of a directory
    44  func removeContents(dir string) error {
    45  	d, err := os.Open(dir)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	defer d.Close()
    50  
    51  	names, err := d.Readdirnames(-1)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	for _, name := range names {
    57  		err = os.RemoveAll(path.Join(dir, name))
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  	return nil
    63  }