github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/safeio/file.go (about)

     1  package safeio
     2  
     3  import (
     4  	"errors"
     5  	"io/fs"
     6  	"io/ioutil"
     7  	"os"
     8  
     9  	rt "github.com/arnodel/golua/runtime"
    10  )
    11  
    12  func OpenFile(r *rt.Runtime, name string, flag int, perm fs.FileMode) (*os.File, error) {
    13  	if r.RequiredFlags()&rt.ComplyIoSafe != 0 {
    14  		return nil, ErrNotAllowed
    15  	}
    16  	return os.OpenFile(name, flag, perm)
    17  }
    18  
    19  func TempFile(r *rt.Runtime, dir string, pattern string) (*os.File, error) {
    20  	if r.RequiredFlags()&rt.ComplyIoSafe != 0 {
    21  		return nil, ErrNotAllowed
    22  	}
    23  	return ioutil.TempFile(dir, pattern)
    24  }
    25  
    26  func RemoveFile(r *rt.Runtime, name string) error {
    27  	if r.RequiredFlags()&rt.ComplyIoSafe != 0 {
    28  		return ErrNotAllowed
    29  	}
    30  	return os.Remove(name)
    31  }
    32  
    33  func RenameFile(r *rt.Runtime, oldName, newName string) error {
    34  	if r.RequiredFlags()&rt.ComplyIoSafe != 0 {
    35  		return ErrNotAllowed
    36  	}
    37  	return os.Rename(oldName, newName)
    38  }
    39  
    40  var ErrNotAllowed = errors.New("safeio: operation not allowed")