github.com/iceber/iouring-go@v0.0.0-20230403020409-002cfd2e2a90/examples/rm/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/iceber/iouring-go"
     9  	"golang.org/x/sys/unix"
    10  )
    11  
    12  func unlinkPath(iour *iouring.IOURing, path string) {
    13  	pathDir, err := os.Open(filepath.Dir(path))
    14  	if err != nil {
    15  		fmt.Printf("open path error: %v\n", err)
    16  		return
    17  	}
    18  	defer pathDir.Close()
    19  
    20  	// checking if the path is a directory or a path, to set the good flags
    21  	fileInfo, err := os.Stat(path)
    22  	if err != nil {
    23  		fmt.Printf("file stat error: %v\n", err)
    24  		return
    25  	}
    26  	var flags int32 = 0 // no flags for files
    27  	if fileInfo.IsDir() {
    28  		flags = unix.AT_REMOVEDIR
    29  	}
    30  
    31  	pr, err := iouring.Unlinkat(int(pathDir.Fd()), filepath.Base(path), flags)
    32  	if err != nil {
    33  		fmt.Printf("prep request error: %v\n", err)
    34  		return
    35  	}
    36  	request, err := iour.SubmitRequest(pr, nil)
    37  	<-request.Done()
    38  	if err := request.Err(); err != nil {
    39  		fmt.Printf("submited iouring request error: %v\n", err)
    40  		return
    41  	}
    42  }
    43  
    44  func main() {
    45  	if len(os.Args) < 2 {
    46  		fmt.Printf("Usage: %s path [path2] [path3] ...\n", os.Args[0])
    47  		return
    48  	}
    49  
    50  	iour, err := iouring.New(1)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	defer iour.Close()
    55  
    56  	for _, filename := range os.Args[1:] {
    57  		unlinkPath(iour, filename)
    58  	}
    59  }