fix(c/libuv): Fix async_fs demo return 255 error & pointer not allocated error

Signed-off-by: hackerchai <i@hackerchai.com>

fix(c/libuv): Mv LLGoFiles declaration

Signed-off-by: hackerchai <i@hackerchai.com>

fix(c/libuv/demo): Fix return 255 error & pointer not allocated error

Signed-off-by: hackerchai <i@hackerchai.com>

refactor(c/libuv): Rewrite FsNew() logic

Signed-off-by: hackerchai <i@hackerchai.com>
This commit is contained in:
hackerchai
2024-07-30 15:19:31 +08:00
parent ceac95c81a
commit acd09d24d5
4 changed files with 55 additions and 24 deletions

View File

@@ -12,12 +12,12 @@ const BUFFER_SIZE = 1024
var ( var (
loop *libuv.Loop loop *libuv.Loop
openReq libuv.Fs openReq *libuv.Fs
readReq libuv.Fs closeReq *libuv.Fs
closeReq libuv.Fs
buffer [BUFFER_SIZE]c.Char buffer [BUFFER_SIZE]c.Char
iov libuv.Buf iov libuv.Buf
file libuv.File
) )
func main() { func main() {
@@ -27,11 +27,23 @@ 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
libuv.Run(loop, libuv.RUN_DEFAULT) result := libuv.Run(loop, libuv.RUN_DEFAULT)
if result != 0 {
c.Fprintf(c.Stderr, c.Str("Error in Run: %s\n"), libuv.Strerror(libuv.Errno(result)))
}
// Cleanup // Cleanup
defer cleanup() defer cleanup()
@@ -40,32 +52,45 @@ func main() {
func onOpen(req *libuv.Fs) { func onOpen(req *libuv.Fs) {
// Check for errors // Check for errors
if libuv.FsGetResult(req) < 0 { if libuv.FsGetResult(req) < 0 {
c.Fprintf(c.Stderr, c.Str("Error opening file: %s\n"), libuv.Strerror(libuv.Errno(libuv.LoopClose(loop)))) c.Fprintf(c.Stderr, c.Str("Error opening file: %s\n"), libuv.Strerror(libuv.Errno(libuv.FsGetResult(req))))
libuv.LoopClose(loop) libuv.LoopClose(loop)
return return
} }
// Store the file descriptor
file = libuv.File(libuv.FsGetResult(req))
// Init buffer // Init buffer
iov = libuv.InitBuf((*c.Char)(unsafe.Pointer(&buffer[0])), c.Uint(unsafe.Sizeof(buffer))) iov = libuv.InitBuf((*c.Char)(unsafe.Pointer(&buffer[0])), c.Uint(unsafe.Sizeof(buffer)))
// Read the file // Read the file
readRes := libuv.FsRead(loop, &readReq, libuv.File(libuv.FsGetResult(req)), &iov, 1, -1, onRead) readFile()
}
func readFile() {
// Initialize the request
readReq := libuv.FsNew()
// Read the file
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.LoopClose(loop) libuv.LoopClose(loop)
return
} }
} }
func onRead(req *libuv.Fs) { func onRead(req *libuv.Fs) {
// Cleanup the request
defer libuv.FsReqCleanup(req)
// Check for errors // Check for errors
if libuv.FsGetResult(req) < 0 { if libuv.FsGetResult(req) < 0 {
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))))
libuv.LoopClose(loop)
} else if libuv.FsGetResult(req) == 0 { } else if libuv.FsGetResult(req) == 0 {
c.Printf(c.Str("EOF\n"))
// 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 {
// Print the content
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)
return return
@@ -73,7 +98,8 @@ func onRead(req *libuv.Fs) {
} else { } else {
c.Printf(c.Str("Read %d bytes\n"), libuv.FsGetResult(req)) c.Printf(c.Str("Read %d bytes\n"), libuv.FsGetResult(req))
c.Printf(c.Str("Read content: %.*s\n"), libuv.FsGetResult(req), (*c.Char)(unsafe.Pointer(&buffer[0]))) c.Printf(c.Str("Read content: %.*s\n"), libuv.FsGetResult(req), (*c.Char)(unsafe.Pointer(&buffer[0])))
libuv.LoopClose(loop) // Read the file again
readFile()
} }
} }
@@ -84,14 +110,15 @@ func onClose(req *libuv.Fs) {
} else { } else {
c.Printf(c.Str("\nFile closed successfully.\n")) c.Printf(c.Str("\nFile closed successfully.\n"))
} }
libuv.LoopClose(loop)
} }
func cleanup() { func cleanup() {
// Cleanup the requests // Cleanup the requests
libuv.FsReqCleanup(&openReq) libuv.FsReqCleanup(openReq)
libuv.FsReqCleanup(&readReq) libuv.FsReqCleanup(closeReq)
libuv.FsReqCleanup(&closeReq)
// Close the loop // Close the loop
libuv.LoopClose(loop) result := libuv.LoopClose(loop)
if result != 0 {
c.Fprintf(c.Stderr, c.Str("Error in LoopClose: %s\n"), libuv.Strerror(libuv.Errno(result)))
}
} }

View File

@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <uv.h> #include <uv.h>
uv_fs_t uv_fs_new() { uv_fs_t* uv_fs_new() {
uv_fs_t req; uv_fs_t* req = malloc(sizeof(uv_fs_t));
return req; return req;
} }

View File

@@ -6,6 +6,10 @@ import (
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
) )
const (
LLGoFiles = "$(pkg-config --cflags libuv): _wrap/fs.c"
)
const ( const (
FS_UNKNOWN FsType = -1 FS_UNKNOWN FsType = -1
FS_CUSTOM FsType = 0 FS_CUSTOM FsType = 0
@@ -69,7 +73,7 @@ type File c.Int
/* Handle types. */ /* Handle types. */
type Fs struct { type Fs struct {
Unused [0]byte Unused [440]byte
} }
type FsEvent struct { type FsEvent struct {
@@ -107,7 +111,7 @@ type FsPollCb func(handle *FsPoll, status c.Int, events c.Int)
/* Fs related function and method */ /* Fs related function and method */
//go:linkname FsNew C.uv_fs_new //go:linkname FsNew C.uv_fs_new
func FsNew() Fs func FsNew() *Fs
//go:linkname FsGetType C.uv_fs_get_type //go:linkname FsGetType C.uv_fs_get_type
func FsGetType(req *Fs) FsType func FsGetType(req *Fs) FsType

View File

@@ -9,7 +9,6 @@ import (
const ( const (
LLGoPackage = "link: $(pkg-config --libs libuv); -luv" LLGoPackage = "link: $(pkg-config --libs libuv); -luv"
LLGoFiles = "$(pkg-config --cflags libuv): _wrap/fs.c"
) )
// ---------------------------------------------- // ----------------------------------------------