refactor(c/libuv): Adapt libuv.Fs struct

This commit is contained in:
hackerchai
2024-08-01 10:52:59 +08:00
parent f253e4fabe
commit 5dd5494f93

View File

@@ -12,8 +12,8 @@ const BUFFER_SIZE = 1024
var ( var (
loop *libuv.Loop loop *libuv.Loop
openReq *libuv.Fs openReq libuv.Fs
closeReq *libuv.Fs closeReq libuv.Fs
buffer [BUFFER_SIZE]c.Char buffer [BUFFER_SIZE]c.Char
iov libuv.Buf iov libuv.Buf
@@ -27,16 +27,8 @@ func main() {
// Initialize the loop // Initialize the loop
loop = libuv.DefaultLoop() loop = libuv.DefaultLoop()
// Initialize the requests
openReq = libuv.FsNew()
closeReq = libuv.FsNew()
if openReq == nil || closeReq == nil {
c.Fprintf(c.Stderr, c.Str("Error in FsNew\n"))
return
}
// Open the file // Open the file
libuv.FsOpen(loop, openReq, c.Str("example.txt"), os.O_RDONLY, 0, onOpen) libuv.FsOpen(loop, &openReq, c.Str("example.txt"), os.O_RDONLY, 0, onOpen)
// Run the loop // Run the loop
result := libuv.Run(loop, libuv.RUN_DEFAULT) result := libuv.Run(loop, libuv.RUN_DEFAULT)
@@ -69,14 +61,14 @@ func onOpen(req *libuv.Fs) {
} }
func readFile() { func readFile() {
// Initialize the request // Initialize the request every time
readReq := libuv.FsNew() var readReq libuv.Fs
// Read the file // Read the file
readRes := libuv.FsRead(loop, readReq, file, &iov, 1, -1, onRead) readRes := libuv.FsRead(loop, &readReq, file, &iov, 1, -1, onRead)
if readRes != 0 { if readRes != 0 {
c.Printf(c.Str("Error in FsRead: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(readRes)), readRes) c.Printf(c.Str("Error in FsRead: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(readRes)), readRes)
libuv.FsReqCleanup(readReq) libuv.FsReqCleanup(&readReq)
libuv.LoopClose(loop) libuv.LoopClose(loop)
} }
} }
@@ -89,7 +81,7 @@ func onRead(req *libuv.Fs) {
c.Fprintf(c.Stderr, c.Str("Read error: %s\n"), libuv.Strerror(libuv.Errno(libuv.FsGetResult(req)))) c.Fprintf(c.Stderr, c.Str("Read error: %s\n"), libuv.Strerror(libuv.Errno(libuv.FsGetResult(req))))
} else if libuv.FsGetResult(req) == 0 { } else if libuv.FsGetResult(req) == 0 {
// Close the file // Close the file
closeRes := libuv.FsClose(loop, closeReq, libuv.File(libuv.FsGetResult(openReq)), onClose) closeRes := libuv.FsClose(loop, &closeReq, libuv.File(libuv.FsGetResult(&openReq)), onClose)
if closeRes != 0 { if closeRes != 0 {
c.Printf(c.Str("Error in FsClose: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(closeRes)), closeRes) c.Printf(c.Str("Error in FsClose: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(closeRes)), closeRes)
libuv.LoopClose(loop) libuv.LoopClose(loop)
@@ -114,8 +106,8 @@ func onClose(req *libuv.Fs) {
func cleanup() { func cleanup() {
// Cleanup the requests // Cleanup the requests
libuv.FsReqCleanup(openReq) libuv.FsReqCleanup(&openReq)
libuv.FsReqCleanup(closeReq) libuv.FsReqCleanup(&closeReq)
// Close the loop // Close the loop
result := libuv.LoopClose(loop) result := libuv.LoopClose(loop)
if result != 0 { if result != 0 {