diff --git a/c/libuv/_demo/libuv/fs.go b/c/libuv/_demo/libuv/fs.go new file mode 100644 index 00000000..7d50f2e9 --- /dev/null +++ b/c/libuv/_demo/libuv/fs.go @@ -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) +} diff --git a/c/libuv/fs.go b/c/libuv/fs.go index 3381e5bb..555b5b0f 100644 --- a/c/libuv/fs.go +++ b/c/libuv/fs.go @@ -5,6 +5,47 @@ import ( _ "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 ( DirentUnknown DirentType = iota DirentFile @@ -16,8 +57,12 @@ const ( DirentBlock ) +type FsType int + type DirentType int +// ---------------------------------------------- + /* Handle types. */ type Fs struct { @@ -42,6 +87,14 @@ type File struct { Req *Fs } +type Stat struct { + Unused [0]byte +} + +// ---------------------------------------------- + +/* Function type */ + // llgo:type C 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 type FsPollCb func(handle *FsPoll, status c.Int, events c.Int) -/* Request types. */ - -/* None of the above. */ - // ---------------------------------------------- /* 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 func FsReqCleanup(req *Fs) diff --git a/c/libuv/libuv.go b/c/libuv/libuv.go index 26a6b6c8..7ca9e964 100644 --- a/c/libuv/libuv.go +++ b/c/libuv/libuv.go @@ -27,10 +27,6 @@ type Loop struct { Unused [0]byte } -type Handle struct { - Unused [0]byte -} - type Buf struct { Base *c.Char 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 func LoopSize() uintptr @@ -122,20 +120,9 @@ func (l *Loop) BackendTimeout() int { return 0 } -// llgo:link (*Handle).Ref C.uv_ref -func (h *Handle) Ref() { - return -} +// ---------------------------------------------- -// llgo:link (*Handle).Unref C.uv_unref -func (h *Handle) Unref() { - return -} - -// llgo:link (*Handle).HasRef C.uv_has_ref -func (h *Handle) HasRef() int { - return 0 -} +/* Buf related functions and method. */ //go:linkname InitBuf C.uv_buf_init func InitBuf(base *c.Char, len c.Uint) Buf diff --git a/c/libuv/signal.go b/c/libuv/signal.go index 46b6eb9d..97847d1c 100644 --- a/c/libuv/signal.go +++ b/c/libuv/signal.go @@ -5,10 +5,16 @@ import ( _ "unsafe" ) +/* Handle types. */ + type Signal struct { Unused [0]byte } +// ---------------------------------------------- + +/* Signal related functions and method. */ + // llgo:type C type SignalCb func(handle *Signal, sigNum c.Int)