Dont use os.Remove on failures in syscall_shm

Use shm_unlink instead
This commit is contained in:
Kovid Goyal
2023-09-23 11:16:30 +05:30
parent 24598b846c
commit c280a28155
3 changed files with 7 additions and 7 deletions

View File

@@ -92,7 +92,7 @@ func CreateTemp(pattern string, size uint64) (MMap, error) {
return create_temp(pattern, size)
}
func truncate_or_unlink(ans *os.File, size uint64) (err error) {
func truncate_or_unlink(ans *os.File, size uint64, unlink func(string) error) (err error) {
fd := int(ans.Fd())
sz := int64(size)
if err = Fallocate_simple(fd, sz); err != nil {
@@ -106,8 +106,8 @@ func truncate_or_unlink(ans *os.File, size uint64) (err error) {
}
}
if err != nil {
ans.Close()
os.Remove(ans.Name())
_ = ans.Close()
_ = unlink(ans.Name())
return fmt.Errorf("Failed to ftruncate() SHM file %s to size: %d with error: %w", ans.Name(), size, err)
}
return

View File

@@ -30,7 +30,7 @@ type file_based_mmap struct {
func file_mmap(f *os.File, size uint64, access AccessFlags, truncate bool, special_name string) (MMap, error) {
if truncate {
err := truncate_or_unlink(f, size)
err := truncate_or_unlink(f, size, os.Remove)
if err != nil {
return nil, err
}

View File

@@ -73,15 +73,15 @@ type syscall_based_mmap struct {
func syscall_mmap(f *os.File, size uint64, access AccessFlags, truncate bool) (MMap, error) {
if truncate {
err := truncate_or_unlink(f, size)
err := truncate_or_unlink(f, size, shm_unlink)
if err != nil {
return nil, fmt.Errorf("truncate failed with error: %w", err)
}
}
region, err := mmap(int(size), access, int(f.Fd()), 0)
if err != nil {
f.Close()
os.Remove(f.Name())
_ = f.Close()
_ = shm_unlink(f.Name())
return nil, fmt.Errorf("mmap failed with error: %w", err)
}
return &syscall_based_mmap{f: f, region: region}, nil