-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(libindex): temp files on non-linux
Signed-off-by: RTann <rtannenb@redhat.com>
- Loading branch information
Showing
4 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package libindex | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
type tempFile struct { | ||
*os.File | ||
} | ||
|
||
func openTemp(dir string) (*tempFile, error) { | ||
f, err := os.OpenFile(dir, os.O_WRONLY|unix.O_TMPFILE, 0644) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tempFile{ | ||
File: f, | ||
}, nil | ||
} | ||
|
||
func (t *tempFile) Reopen() (*os.File, error) { | ||
fd := int(t.Fd()) | ||
if fd == -1 { | ||
return nil, errStale | ||
} | ||
p := fmt.Sprintf("/proc/self/fd/%d", fd) | ||
return os.OpenFile(p, os.O_RDONLY, 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build unix && !linux | ||
|
||
package libindex | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
) | ||
|
||
type tempFile struct { | ||
*os.File | ||
} | ||
|
||
func openTemp(dir string) (*tempFile, error) { | ||
f, err := os.CreateTemp(dir, "*.fetch") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tempFile{ | ||
File: f, | ||
}, nil | ||
} | ||
|
||
func (t *tempFile) Reopen() (*os.File, error) { | ||
return os.OpenFile(t.Name(), os.O_RDONLY, 0644) | ||
} | ||
|
||
func (t *tempFile) Close() error { | ||
return errors.Join(os.Remove(t.Name()), t.File.Close()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package libindex | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
_ "unsafe" // linker tricks | ||
) | ||
|
||
// This is a very rough port of src/os/tempfile.go that adds the magic | ||
// autodelete flag. | ||
|
||
//go:linkname fastrand runtime.fastrand | ||
func fastrand() uint32 | ||
|
||
type tempFile struct { | ||
*os.File | ||
} | ||
|
||
func openTemp(dir string) (*tempFile, error) { | ||
// Copied out of golang.org/x/sys/windows | ||
const FILE_FLAG_DELETE_ON_CLOSE = 0x04000000 | ||
for { | ||
fn := fmt.Sprintf("fetch.%d", fastrand()) | ||
f, err := os.OpenFile(filepath.Join(dir, fn), os.O_WRONLY|FILE_FLAG_DELETE_ON_CLOSE, 0644) | ||
switch { | ||
case errors.Is(err, nil): | ||
return &tempFile{ | ||
File: f, | ||
}, nil | ||
case errors.Is(err, fs.ErrExist): | ||
// Continue | ||
default: | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
func (t *tempFile) Reopen() (*os.File, error) { | ||
return os.OpenFile(t.Name(), os.O_RDONLY, 0644) | ||
} |