library: c/syscall

This commit is contained in:
xushiwei
2024-07-11 19:11:54 +08:00
parent b81638794f
commit a5ff25b0fe
38 changed files with 46 additions and 13 deletions

View File

@@ -664,8 +664,6 @@ func canSkipToBuild(pkgPath string) bool {
switch pkgPath { switch pkgPath {
case "unsafe": case "unsafe":
return true return true
case "internal/oserror":
return false
default: default:
return strings.HasPrefix(pkgPath, "internal/") || return strings.HasPrefix(pkgPath, "internal/") ||
strings.HasPrefix(pkgPath, "runtime/internal/") strings.HasPrefix(pkgPath, "runtime/internal/")
@@ -679,6 +677,7 @@ var hasAltPkg = map[string]none{
"fmt": {}, "fmt": {},
"internal/abi": {}, "internal/abi": {},
"internal/bytealg": {}, "internal/bytealg": {},
"internal/oserror": {},
"internal/reflectlite": {}, "internal/reflectlite": {},
//"io": {}, //"io": {},
//"io/fs": {}, //"io/fs": {},

View File

@@ -2,11 +2,14 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package syscall // Package oserror defines errors values used in the os package.
//
// These types are defined here to permit the syscall package to reference them.
package oserror
// llgo:skipall
import "errors" import "errors"
// from internal/oserror
var ( var (
ErrInvalid = errors.New("invalid argument") ErrInvalid = errors.New("invalid argument")
ErrPermission = errors.New("permission denied") ErrPermission = errors.New("permission denied")

View File

@@ -6,6 +6,8 @@
package os package os
import "syscall"
// Stat returns the FileInfo structure describing file. // Stat returns the FileInfo structure describing file.
// If there is an error, it will be of type *PathError. // If there is an error, it will be of type *PathError.
func (f *File) Stat() (FileInfo, error) { func (f *File) Stat() (FileInfo, error) {
@@ -26,7 +28,6 @@ func (f *File) Stat() (FileInfo, error) {
// statNolog stats a file with no test logging. // statNolog stats a file with no test logging.
func statNolog(name string) (FileInfo, error) { func statNolog(name string) (FileInfo, error) {
/* TODO(xsw):
var fs fileStat var fs fileStat
err := ignoringEINTR(func() error { err := ignoringEINTR(func() error {
return syscall.Stat(name, &fs.sys) return syscall.Stat(name, &fs.sys)
@@ -36,13 +37,10 @@ func statNolog(name string) (FileInfo, error) {
} }
fillFileStatFromSys(&fs, name) fillFileStatFromSys(&fs, name)
return &fs, nil return &fs, nil
*/
panic("todo: os.statNolog")
} }
// lstatNolog lstats a file with no test logging. // lstatNolog lstats a file with no test logging.
func lstatNolog(name string) (FileInfo, error) { func lstatNolog(name string) (FileInfo, error) {
/* TODO(xsw):
var fs fileStat var fs fileStat
err := ignoringEINTR(func() error { err := ignoringEINTR(func() error {
return syscall.Lstat(name, &fs.sys) return syscall.Lstat(name, &fs.sys)
@@ -52,6 +50,4 @@ func lstatNolog(name string) (FileInfo, error) {
} }
fillFileStatFromSys(&fs, name) fillFileStatFromSys(&fs, name)
return &fs, nil return &fs, nil
*/
panic("todo: os.lstatNolog")
} }

View File

@@ -22,8 +22,12 @@ import (
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/os" "github.com/goplus/llgo/c/os"
"github.com/goplus/llgo/c/syscall"
) )
type Timespec syscall.Timespec
type Timeval syscall.Timeval
// Unix returns the time stored in ts as seconds plus nanoseconds. // Unix returns the time stored in ts as seconds plus nanoseconds.
func (ts *Timespec) Unix() (sec int64, nsec int64) { func (ts *Timespec) Unix() (sec int64, nsec int64) {
return int64(ts.Sec), int64(ts.Nsec) return int64(ts.Sec), int64(ts.Nsec)

View File

@@ -11,6 +11,7 @@ import (
"unsafe" "unsafe"
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
"github.com/goplus/llgo/internal/lib/internal/oserror"
) )
var ( var (
@@ -28,11 +29,11 @@ func (e Errno) Error() string {
func (e Errno) Is(target error) bool { func (e Errno) Is(target error) bool {
switch target { switch target {
case ErrPermission: case oserror.ErrPermission:
return e == EACCES || e == EPERM return e == EACCES || e == EPERM
case ErrExist: case oserror.ErrExist:
return e == EEXIST || e == ENOTEMPTY return e == EEXIST || e == ENOTEMPTY
case ErrNotExist: case oserror.ErrNotExist:
return e == ENOENT return e == ENOENT
case errors.ErrUnsupported: case errors.ErrUnsupported:
return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP

View File

@@ -1021,6 +1021,36 @@ func unixTime(sec int64, nsec int32) Time {
return Time{uint64(nsec), sec + unixToInternal, Local} return Time{uint64(nsec), sec + unixToInternal, Local}
} }
// Unix returns the local Time corresponding to the given Unix time,
// sec seconds and nsec nanoseconds since January 1, 1970 UTC.
// It is valid to pass nsec outside the range [0, 999999999].
// Not all sec values have a corresponding time value. One such
// value is 1<<63-1 (the largest int64 value).
func Unix(sec int64, nsec int64) Time {
if nsec < 0 || nsec >= 1e9 {
n := nsec / 1e9
sec += n
nsec -= n * 1e9
if nsec < 0 {
nsec += 1e9
sec--
}
}
return unixTime(sec, int32(nsec))
}
// UnixMilli returns the local Time corresponding to the given Unix time,
// msec milliseconds since January 1, 1970 UTC.
func UnixMilli(msec int64) Time {
return Unix(msec/1e3, (msec%1e3)*1e6)
}
// UnixMicro returns the local Time corresponding to the given Unix time,
// usec microseconds since January 1, 1970 UTC.
func UnixMicro(usec int64) Time {
return Unix(usec/1e6, (usec%1e6)*1e3)
}
func isLeap(year int) bool { func isLeap(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0) return year%4 == 0 && (year%100 != 0 || year%400 == 0)
} }