libgocryptfs/tests/symlink_race/main.go
Jakob Unterwurzacher be2bd4eec7 golangci-lint: fix issues found by "unused" and "deadcode"
Except xattrSupported, this is a false positive.

$ golangci-lint run --disable-all --enable unused --enable deadcode
gocryptfs-xray/xray_main.go:24:5: `GitVersionFuse` is unused (deadcode)
var GitVersionFuse = "[GitVersionFuse not set - please compile using ./build.bash]"
    ^
tests/symlink_race/main.go:47:6: `chmodLoop` is unused (deadcode)
func chmodLoop() {
     ^
internal/readpassword/extpass_test.go:11:5: `testPw` is unused (deadcode)
var testPw = []byte("test")
    ^
tests/reverse/xattr_test.go:13:6: func `xattrSupported` is unused (unused)
func xattrSupported(path string) bool {
     ^
internal/fusefrontend_reverse/rpath.go:20:22: func `(*RootNode).abs` is unused (unused)
func (rfs *RootNode) abs(relPath string, err error) (string, error) {
                     ^
tests/matrix/matrix_test.go:310:6: `sContains` is unused (deadcode)
func sContains(haystack []string, needle string) bool {
2021-08-19 08:34:49 +02:00

78 lines
1.3 KiB
Go

package main
import (
"fmt"
"os"
"syscall"
)
const (
testFile = "symlink_race.test_file"
testFileTmp = testFile + ".tmp"
)
func renameLoop() {
// May be left behind from an earlier run
syscall.Unlink(testFileTmp)
var err error
var fd *os.File
for {
err = syscall.Symlink("/root/chmod_me", testFileTmp)
if err != nil {
fmt.Printf("Symlink() failed: %v\n", err)
continue
}
err = syscall.Rename(testFileTmp, testFile)
if err != nil {
fmt.Printf("Rename() 1 failed: %v\n", err)
continue
}
fd, err = os.Create(testFileTmp)
if err != nil {
fmt.Printf("Create() failed: %v\n", err)
continue
}
fd.Close()
err = syscall.Rename(testFileTmp, testFile)
if err != nil {
fmt.Printf("Rename() 2 failed: %v\n", err)
continue
}
fmt.Printf(".")
}
}
func openLoop() {
var err error
var f *os.File
buf := make([]byte, 100)
owned := []byte("owned")
var n int
for {
f, err = os.OpenFile(testFile, os.O_RDWR, 0777)
if err != nil {
fmt.Printf("Open() failed: %v\n", err)
continue
}
_, err = f.Write(owned)
if err != nil {
fmt.Printf("Write() failed: %v\n", err)
}
n, err = f.Read(buf)
if err != nil {
fmt.Printf("Read() failed: %v\n", err)
continue
}
if n > 0 {
fmt.Printf("Content: %q\n", string(buf[:n]))
os.Exit(1)
}
}
}
func main() {
go openLoop()
renameLoop()
}