mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-06 01:05:48 +02:00
Allow specifying permissions when creating anonymous temp files
This commit is contained in:
@@ -5,6 +5,7 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
@@ -13,11 +14,15 @@ import (
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func CreateAnonymousTemp(dir string) (*os.File, error) {
|
||||
func CreateAnonymousTemp(dir string, perms ...fs.FileMode) (*os.File, error) {
|
||||
if dir == "" {
|
||||
dir = os.TempDir()
|
||||
}
|
||||
fd, err := unix.Open(dir, unix.O_RDWR|unix.O_TMPFILE|unix.O_CLOEXEC, 0600)
|
||||
var perm fs.FileMode = unix.S_IREAD | unix.S_IWRITE
|
||||
if len(perms) > 0 {
|
||||
perm = perms[0]
|
||||
}
|
||||
fd, err := unix.Open(dir, unix.O_RDWR|unix.O_TMPFILE|unix.O_CLOEXEC, uint32(perm&fs.ModePerm))
|
||||
|
||||
if err == nil {
|
||||
path := "/proc/self/fd/" + strconv.FormatUint(uint64(fd), 10)
|
||||
|
||||
@@ -6,16 +6,33 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func CreateAnonymousTemp(dir string) (*os.File, error) {
|
||||
func CreateAnonymousTemp(dir string, perms ...fs.FileMode) (*os.File, error) {
|
||||
var perm fs.FileMode = unix.S_IREAD | unix.S_IWRITE
|
||||
default_perm := perm
|
||||
if len(perms) > 0 {
|
||||
perm = perms[0]
|
||||
}
|
||||
|
||||
f, err := os.CreateTemp(dir, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if perm != default_perm {
|
||||
err = f.Chmod(perm & fs.ModePerm)
|
||||
if err != nil {
|
||||
os.Remove(f.Name())
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
err = os.Remove(f.Name())
|
||||
if err != nil {
|
||||
f.Close()
|
||||
|
||||
Reference in New Issue
Block a user