Skip to content

Commit c6c507a

Browse files
committed
Manual file creation, not depend on os.CreateTemp
1 parent b769aa6 commit c6c507a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

queue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (q *queue) enqueueBatch(b entry.Batch) error {
316316
}
317317

318318
func (q *queue) newSegment() (*segment, error) {
319-
f, err := os.CreateTemp(q.settings.DataDir, segPrefix)
319+
f, err := createFile(q.settings.DataDir, segPrefix)
320320
if err != nil {
321321
return nil, err
322322
}

utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package pqueue
22

33
import (
44
"container/list"
5+
"fmt"
56
"os"
7+
"path"
68
"path/filepath"
79
"sort"
10+
"strconv"
811
"strings"
912
"time"
1013
)
@@ -79,3 +82,19 @@ func loadFileInfos(dir string, infoExtractor func(os.DirEntry) (os.FileInfo, err
7982
func fileInfoExtractor(f os.DirEntry) (os.FileInfo, error) {
8083
return f.Info()
8184
}
85+
86+
func createFile(dir, prefix string) (f *os.File, err error) {
87+
prefix = path.Join(dir, prefix)
88+
89+
for attempt := 0; attempt < 10_000; attempt++ {
90+
name := prefix + strconv.FormatInt(time.Now().UnixNano(), 10)
91+
92+
f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
93+
if !os.IsExist(err) {
94+
return
95+
}
96+
}
97+
98+
err = fmt.Errorf("creating file but fail. path: %s", dir)
99+
return
100+
}

0 commit comments

Comments
 (0)