initial import
This commit is contained in:
8
core/fs/files+polyfill.go
Normal file
8
core/fs/files+polyfill.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build windows
|
||||
|
||||
package fs
|
||||
|
||||
import "os"
|
||||
|
||||
func CloseOnExec(*os.File) {
|
||||
}
|
||||
14
core/fs/files.go
Normal file
14
core/fs/files.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// +build linux darwin
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func CloseOnExec(file *os.File) {
|
||||
if file != nil {
|
||||
syscall.CloseOnExec(int(file.Fd()))
|
||||
}
|
||||
}
|
||||
42
core/fs/temps.go
Normal file
42
core/fs/temps.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"zero/core/hash"
|
||||
)
|
||||
|
||||
// TempFileWithText creates the temporary file with the given content,
|
||||
// and returns the opened *os.File instance.
|
||||
// The file is kept as open, the caller should close the file handle,
|
||||
// and remove the file by name.
|
||||
func TempFileWithText(text string) (*os.File, error) {
|
||||
tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(text)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tmpfile, nil
|
||||
}
|
||||
|
||||
// TempFilenameWithText creates the file with the given content,
|
||||
// and returns the filename (full path).
|
||||
// The caller should remove the file after use.
|
||||
func TempFilenameWithText(text string) (string, error) {
|
||||
tmpfile, err := TempFileWithText(text)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
filename := tmpfile.Name()
|
||||
if err = tmpfile.Close(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filename, nil
|
||||
}
|
||||
Reference in New Issue
Block a user