feat(c/libuv/demo): Add libuv async_fs demo
Signed-off-by: hackerchai <i@hackerchai.com> fix(c/libuv): fix fs demo Signed-off-by: hackerchai <i@hackerchai.com> refactor(c/libuv): neat comment and adapt merge Signed-off-by: hackerchai <i@hackerchai.com>
This commit is contained in:
70
c/libuv/_demo/libuv/fs.go
Normal file
70
c/libuv/_demo/libuv/fs.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
"github.com/goplus/llgo/c/libuv"
|
||||||
|
"github.com/goplus/llgo/c/os"
|
||||||
|
"golang.org/x/tools/container/intsets"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const bufferSize = 1024
|
||||||
|
|
||||||
|
var buffer []c.Char
|
||||||
|
var iov libuv.Buf
|
||||||
|
|
||||||
|
var loop *libuv.Loop
|
||||||
|
var readReq *libuv.Fs
|
||||||
|
var closeReq *libuv.Fs
|
||||||
|
var openReq *libuv.Fs
|
||||||
|
|
||||||
|
func initBuffer() {
|
||||||
|
buffer = make([]c.Char, bufferSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
loop = libuv.DefaultLoop()
|
||||||
|
initBuffer()
|
||||||
|
|
||||||
|
file := libuv.NewFile(loop, openReq)
|
||||||
|
|
||||||
|
path := c.Str("example.txt")
|
||||||
|
|
||||||
|
file.Open(path, os.O_RDONLY, 0, onOpen)
|
||||||
|
|
||||||
|
loop.Run(libuv.RUN_DEFAULT)
|
||||||
|
|
||||||
|
libuv.FsReqCleanup(openReq)
|
||||||
|
loop.Close()
|
||||||
|
c.Free(unsafe.Pointer(loop))
|
||||||
|
}
|
||||||
|
|
||||||
|
func onOpen(req *libuv.Fs) {
|
||||||
|
if req.GetResult() < 0 {
|
||||||
|
c.Fprintf(c.Stderr, c.Str("Error opening file: %s\n"), libuv.Strerror(req.GetResult()))
|
||||||
|
} else {
|
||||||
|
iov = libuv.InitBuf(unsafe.SliceData(buffer), c.Uint(unsafe.Sizeof(buffer)))
|
||||||
|
libuv.FsRead(loop, readReq, req.GetResult(), []libuv.Buf{iov}, 1, -1, onRead)
|
||||||
|
}
|
||||||
|
libuv.FsReqCleanup(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func onRead(req *libuv.Fs) {
|
||||||
|
if req.GetResult() < 0 {
|
||||||
|
c.Fprintf(c.Stderr, c.Str("Read error: %s\n"), libuv.Strerror(req.GetResult()))
|
||||||
|
} else if req.GetResult() == 0 {
|
||||||
|
libuv.FsClose(loop, closeReq, req.GetResult(), onClose)
|
||||||
|
} else {
|
||||||
|
if req.GetResult() > intsets.MaxInt {
|
||||||
|
c.Fprintf(c.Stderr, c.Str("Too big file.\n"))
|
||||||
|
}
|
||||||
|
c.Printf(c.Str("%.*s"), c.Int(req.GetResult()), buffer)
|
||||||
|
libuv.FsRead(loop, req, req.GetResult(), []libuv.Buf{iov}, 1, -1, onRead)
|
||||||
|
}
|
||||||
|
libuv.FsReqCleanup(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func onClose(req *libuv.Fs) {
|
||||||
|
c.Printf(c.Str("\nFile closed.\n"))
|
||||||
|
libuv.FsReqCleanup(req)
|
||||||
|
}
|
||||||
@@ -5,6 +5,47 @@ import (
|
|||||||
_ "unsafe"
|
_ "unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
FS_UNKNOWN FsType = -1
|
||||||
|
FS_CUSTOM FsType = 0
|
||||||
|
FS_OPEN FsType = 1
|
||||||
|
FS_CLOSE FsType = 2
|
||||||
|
FS_READ FsType = 3
|
||||||
|
FS_WRITE FsType = 4
|
||||||
|
FS_SENDFILE FsType = 5
|
||||||
|
FS_STAT FsType = 6
|
||||||
|
FS_LSTAT FsType = 7
|
||||||
|
FS_FSTAT FsType = 8
|
||||||
|
FS_FTRUNCATE FsType = 9
|
||||||
|
FS_UTIME FsType = 10
|
||||||
|
FS_FUTIME FsType = 11
|
||||||
|
FS_ACCESS FsType = 12
|
||||||
|
FS_CHMOD FsType = 13
|
||||||
|
FS_FCHMOD FsType = 14
|
||||||
|
FS_FSYNC FsType = 15
|
||||||
|
FS_FDATASYNC FsType = 16
|
||||||
|
FS_UNLINK FsType = 17
|
||||||
|
FS_RMDIR FsType = 18
|
||||||
|
FS_MKDIR FsType = 19
|
||||||
|
FS_MKDTEMP FsType = 20
|
||||||
|
FS_RENAME FsType = 21
|
||||||
|
FS_SCANDIR FsType = 22
|
||||||
|
FS_LINK FsType = 23
|
||||||
|
FS_SYMLINK FsType = 24
|
||||||
|
FS_READLINK FsType = 25
|
||||||
|
FS_CHOWN FsType = 26
|
||||||
|
FS_FCHOWN FsType = 27
|
||||||
|
FS_REALPATH FsType = 28
|
||||||
|
FS_COPYFILE FsType = 29
|
||||||
|
FS_LCHOWN FsType = 30
|
||||||
|
FS_OPENDIR FsType = 31
|
||||||
|
FS_READDIR FsType = 32
|
||||||
|
FS_CLOSEDIR FsType = 33
|
||||||
|
FS_STATFS FsType = 34
|
||||||
|
FS_MKSTEMP FsType = 35
|
||||||
|
FS_LUTIME FsType = 36
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DirentUnknown DirentType = iota
|
DirentUnknown DirentType = iota
|
||||||
DirentFile
|
DirentFile
|
||||||
@@ -16,8 +57,12 @@ const (
|
|||||||
DirentBlock
|
DirentBlock
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type FsType int
|
||||||
|
|
||||||
type DirentType int
|
type DirentType int
|
||||||
|
|
||||||
|
// ----------------------------------------------
|
||||||
|
|
||||||
/* Handle types. */
|
/* Handle types. */
|
||||||
|
|
||||||
type Fs struct {
|
type Fs struct {
|
||||||
@@ -42,6 +87,14 @@ type File struct {
|
|||||||
Req *Fs
|
Req *Fs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Stat struct {
|
||||||
|
Unused [0]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------
|
||||||
|
|
||||||
|
/* Function type */
|
||||||
|
|
||||||
// llgo:type C
|
// llgo:type C
|
||||||
type FsCb func(req *Fs)
|
type FsCb func(req *Fs)
|
||||||
|
|
||||||
@@ -51,14 +104,40 @@ type FsEventCb func(handle *FsEvent, filename *c.Char, events c.Int, status c.In
|
|||||||
// llgo:type C
|
// llgo:type C
|
||||||
type FsPollCb func(handle *FsPoll, status c.Int, events c.Int)
|
type FsPollCb func(handle *FsPoll, status c.Int, events c.Int)
|
||||||
|
|
||||||
/* Request types. */
|
|
||||||
|
|
||||||
/* None of the above. */
|
|
||||||
|
|
||||||
// ----------------------------------------------
|
// ----------------------------------------------
|
||||||
|
|
||||||
/* Fs related function and method */
|
/* Fs related function and method */
|
||||||
|
|
||||||
|
// llgo:link (*Fs).GetType C.uv_fs_get_type
|
||||||
|
func (f *Fs) GetType() *FsType {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// llgo:link (*Fs).GetPath C.uv_fs_get_path
|
||||||
|
func (f *Fs) GetPath() *c.Char {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// llgo:link (*Fs).GetResult C.uv_fs_get_result
|
||||||
|
func (f *Fs) GetResult() c.Int {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// llgo:link (*Fs).GetPtr C.uv_fs_get_ptr
|
||||||
|
func (f *Fs) GetPtr() c.Pointer {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// llgo:link (*Fs).GetSystemError C.uv_fs_get_system_error
|
||||||
|
func (f *Fs) GetSystemError() c.Int {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// llgo:link (*Fs).GetStatBuf C.uv_fs_get_statbuf
|
||||||
|
func (f *Fs) GetStatBuf() *Stat {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//go:linkname FsReqCleanup C.uv_fs_req_cleanup
|
//go:linkname FsReqCleanup C.uv_fs_req_cleanup
|
||||||
func FsReqCleanup(req *Fs)
|
func FsReqCleanup(req *Fs)
|
||||||
|
|
||||||
|
|||||||
@@ -27,10 +27,6 @@ type Loop struct {
|
|||||||
Unused [0]byte
|
Unused [0]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handle struct {
|
|
||||||
Unused [0]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type Buf struct {
|
type Buf struct {
|
||||||
Base *c.Char
|
Base *c.Char
|
||||||
Len uintptr
|
Len uintptr
|
||||||
@@ -45,6 +41,8 @@ type WalkCb func(handle *Handle, arg c.Pointer)
|
|||||||
|
|
||||||
// ----------------------------------------------
|
// ----------------------------------------------
|
||||||
|
|
||||||
|
/* Loop related functions and method. */
|
||||||
|
|
||||||
//go:linkname LoopSize C.uv_loop_size
|
//go:linkname LoopSize C.uv_loop_size
|
||||||
func LoopSize() uintptr
|
func LoopSize() uintptr
|
||||||
|
|
||||||
@@ -122,20 +120,9 @@ func (l *Loop) BackendTimeout() int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// llgo:link (*Handle).Ref C.uv_ref
|
// ----------------------------------------------
|
||||||
func (h *Handle) Ref() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// llgo:link (*Handle).Unref C.uv_unref
|
/* Buf related functions and method. */
|
||||||
func (h *Handle) Unref() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// llgo:link (*Handle).HasRef C.uv_has_ref
|
|
||||||
func (h *Handle) HasRef() int {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:linkname InitBuf C.uv_buf_init
|
//go:linkname InitBuf C.uv_buf_init
|
||||||
func InitBuf(base *c.Char, len c.Uint) Buf
|
func InitBuf(base *c.Char, len c.Uint) Buf
|
||||||
|
|||||||
@@ -5,10 +5,16 @@ import (
|
|||||||
_ "unsafe"
|
_ "unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/* Handle types. */
|
||||||
|
|
||||||
type Signal struct {
|
type Signal struct {
|
||||||
Unused [0]byte
|
Unused [0]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------
|
||||||
|
|
||||||
|
/* Signal related functions and method. */
|
||||||
|
|
||||||
// llgo:type C
|
// llgo:type C
|
||||||
type SignalCb func(handle *Signal, sigNum c.Int)
|
type SignalCb func(handle *Signal, sigNum c.Int)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user