mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-18 06:25:13 +02:00
Function to create symlinks atomically
This commit is contained in:
@@ -3,16 +3,12 @@
|
||||
package shm
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
not_rand "math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"kitty/tools/cli"
|
||||
@@ -48,15 +44,6 @@ func prefix_and_suffix(pattern string) (prefix, suffix string, err error) {
|
||||
return prefix, suffix, nil
|
||||
}
|
||||
|
||||
func next_random() string {
|
||||
b := make([]byte, 8)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
return strconv.FormatUint(uint64(not_rand.Uint32()), 16)
|
||||
}
|
||||
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b)
|
||||
}
|
||||
|
||||
type MMap interface {
|
||||
Close() error
|
||||
Unlink() error
|
||||
|
||||
@@ -8,10 +8,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"kitty/tools/utils"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
@@ -96,7 +97,7 @@ func create_temp(pattern string, size uint64) (ans MMap, err error) {
|
||||
var f *os.File
|
||||
try := 0
|
||||
for {
|
||||
name := prefix + next_random() + suffix
|
||||
name := prefix + utils.RandomFilename() + suffix
|
||||
path := file_path_from_name(name)
|
||||
f, err = os.OpenFile(path, os.O_EXCL|os.O_CREATE|os.O_RDWR, 0600)
|
||||
if err != nil {
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"kitty/tools/utils"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
@@ -127,7 +129,7 @@ func create_temp(pattern string, size uint64) (ans MMap, err error) {
|
||||
var f *os.File
|
||||
try := 0
|
||||
for {
|
||||
name := prefix + next_random() + suffix
|
||||
name := prefix + utils.RandomFilename() + suffix
|
||||
if len(name) > SHM_NAME_MAX {
|
||||
return nil, ErrPatternTooLong
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user