Skip to content

Commit

Permalink
Merge pull request goplus#613 from hackerchai/fix/c-libuv-struct
Browse files Browse the repository at this point in the history
fix(c/libuv): Add libuv fs struct new func & fix async_fs demo
  • Loading branch information
xushiwei authored Aug 1, 2024
2 parents d297547 + 5dd5494 commit acedf4d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 31 additions & 12 deletions c/libuv/_demo/async_fs/async_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const BUFFER_SIZE = 1024
var (
loop *libuv.Loop
openReq libuv.Fs
readReq libuv.Fs
closeReq libuv.Fs

buffer [BUFFER_SIZE]c.Char
iov libuv.Buf
file libuv.File
)

func main() {
Expand All @@ -31,7 +31,11 @@ func main() {
libuv.FsOpen(loop, &openReq, c.Str("example.txt"), os.O_RDONLY, 0, onOpen)

// 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
defer cleanup()
Expand All @@ -40,40 +44,54 @@ func main() {
func onOpen(req *libuv.Fs) {
// Check for errors
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)
return
}

// Store the file descriptor
file = libuv.File(libuv.FsGetResult(req))

// Init buffer
iov = libuv.InitBuf((*c.Char)(unsafe.Pointer(&buffer[0])), c.Uint(unsafe.Sizeof(buffer)))

// Read the file
readFile()

}

func readFile() {
// Initialize the request every time
var readReq libuv.Fs

// Read the file
readRes := libuv.FsRead(loop, &readReq, libuv.File(libuv.FsGetResult(req)), &iov, 1, -1, onRead)
readRes := libuv.FsRead(loop, &readReq, file, &iov, 1, -1, onRead)
if readRes != 0 {
c.Printf(c.Str("Error in FsRead: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(readRes)), readRes)
libuv.FsReqCleanup(&readReq)
libuv.LoopClose(loop)
return
}
}

func onRead(req *libuv.Fs) {
// Cleanup the request
defer libuv.FsReqCleanup(req)
// Check for errors
if libuv.FsGetResult(req) < 0 {
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 {
c.Printf(c.Str("EOF\n"))
// Close the file
closeRes := libuv.FsClose(loop, &closeReq, libuv.File(libuv.FsGetResult(&openReq)), onClose)
if closeRes != 0 {
// Print the content
c.Printf(c.Str("Error in FsClose: %s (code: %d)\n"), libuv.Strerror(libuv.Errno(closeRes)), closeRes)
libuv.LoopClose(loop)
return
}
} else {
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])))
libuv.LoopClose(loop)
// Read the file again
readFile()
}
}

Expand All @@ -84,14 +102,15 @@ func onClose(req *libuv.Fs) {
} else {
c.Printf(c.Str("\nFile closed successfully.\n"))
}
libuv.LoopClose(loop)
}

func cleanup() {
// Cleanup the requests
libuv.FsReqCleanup(&openReq)
libuv.FsReqCleanup(&readReq)
libuv.FsReqCleanup(&closeReq)
// 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)))
}
}
2 changes: 1 addition & 1 deletion c/libuv/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type File c.Int
/* Handle types. */

type Fs struct {
Unused [0]byte
Unused [440]byte
}

type FsEvent struct {
Expand Down

0 comments on commit acedf4d

Please sign in to comment.