diff --git a/internal/lib/syscall/ztypes_aix_ppc64.go b/c/syscall/ztypes_aix_ppc64.go similarity index 100% rename from internal/lib/syscall/ztypes_aix_ppc64.go rename to c/syscall/ztypes_aix_ppc64.go diff --git a/internal/lib/syscall/ztypes_darwin_amd64.go b/c/syscall/ztypes_darwin_amd64.go similarity index 100% rename from internal/lib/syscall/ztypes_darwin_amd64.go rename to c/syscall/ztypes_darwin_amd64.go diff --git a/internal/lib/syscall/ztypes_darwin_arm64.go b/c/syscall/ztypes_darwin_arm64.go similarity index 100% rename from internal/lib/syscall/ztypes_darwin_arm64.go rename to c/syscall/ztypes_darwin_arm64.go diff --git a/internal/lib/syscall/ztypes_dragonfly_amd64.go b/c/syscall/ztypes_dragonfly_amd64.go similarity index 100% rename from internal/lib/syscall/ztypes_dragonfly_amd64.go rename to c/syscall/ztypes_dragonfly_amd64.go diff --git a/internal/lib/syscall/ztypes_freebsd_386.go b/c/syscall/ztypes_freebsd_386.go similarity index 100% rename from internal/lib/syscall/ztypes_freebsd_386.go rename to c/syscall/ztypes_freebsd_386.go diff --git a/internal/lib/syscall/ztypes_freebsd_amd64.go b/c/syscall/ztypes_freebsd_amd64.go similarity index 100% rename from internal/lib/syscall/ztypes_freebsd_amd64.go rename to c/syscall/ztypes_freebsd_amd64.go diff --git a/internal/lib/syscall/ztypes_freebsd_arm.go b/c/syscall/ztypes_freebsd_arm.go similarity index 100% rename from internal/lib/syscall/ztypes_freebsd_arm.go rename to c/syscall/ztypes_freebsd_arm.go diff --git a/internal/lib/syscall/ztypes_freebsd_arm64.go b/c/syscall/ztypes_freebsd_arm64.go similarity index 100% rename from internal/lib/syscall/ztypes_freebsd_arm64.go rename to c/syscall/ztypes_freebsd_arm64.go diff --git a/internal/lib/syscall/ztypes_freebsd_riscv64.go b/c/syscall/ztypes_freebsd_riscv64.go similarity index 100% rename from internal/lib/syscall/ztypes_freebsd_riscv64.go rename to c/syscall/ztypes_freebsd_riscv64.go diff --git a/internal/lib/syscall/ztypes_linux_386.go b/c/syscall/ztypes_linux_386.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_386.go rename to c/syscall/ztypes_linux_386.go diff --git a/internal/lib/syscall/ztypes_linux_amd64.go b/c/syscall/ztypes_linux_amd64.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_amd64.go rename to c/syscall/ztypes_linux_amd64.go diff --git a/internal/lib/syscall/ztypes_linux_arm.go b/c/syscall/ztypes_linux_arm.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_arm.go rename to c/syscall/ztypes_linux_arm.go diff --git a/internal/lib/syscall/ztypes_linux_arm64.go b/c/syscall/ztypes_linux_arm64.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_arm64.go rename to c/syscall/ztypes_linux_arm64.go diff --git a/internal/lib/syscall/ztypes_linux_loong64.go b/c/syscall/ztypes_linux_loong64.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_loong64.go rename to c/syscall/ztypes_linux_loong64.go diff --git a/internal/lib/syscall/ztypes_linux_mips.go b/c/syscall/ztypes_linux_mips.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_mips.go rename to c/syscall/ztypes_linux_mips.go diff --git a/internal/lib/syscall/ztypes_linux_mips64.go b/c/syscall/ztypes_linux_mips64.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_mips64.go rename to c/syscall/ztypes_linux_mips64.go diff --git a/internal/lib/syscall/ztypes_linux_mips64le.go b/c/syscall/ztypes_linux_mips64le.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_mips64le.go rename to c/syscall/ztypes_linux_mips64le.go diff --git a/internal/lib/syscall/ztypes_linux_mipsle.go b/c/syscall/ztypes_linux_mipsle.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_mipsle.go rename to c/syscall/ztypes_linux_mipsle.go diff --git a/internal/lib/syscall/ztypes_linux_ppc64.go b/c/syscall/ztypes_linux_ppc64.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_ppc64.go rename to c/syscall/ztypes_linux_ppc64.go diff --git a/internal/lib/syscall/ztypes_linux_ppc64le.go b/c/syscall/ztypes_linux_ppc64le.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_ppc64le.go rename to c/syscall/ztypes_linux_ppc64le.go diff --git a/internal/lib/syscall/ztypes_linux_riscv64.go b/c/syscall/ztypes_linux_riscv64.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_riscv64.go rename to c/syscall/ztypes_linux_riscv64.go diff --git a/internal/lib/syscall/ztypes_linux_s390x.go b/c/syscall/ztypes_linux_s390x.go similarity index 100% rename from internal/lib/syscall/ztypes_linux_s390x.go rename to c/syscall/ztypes_linux_s390x.go diff --git a/internal/lib/syscall/ztypes_netbsd_386.go b/c/syscall/ztypes_netbsd_386.go similarity index 100% rename from internal/lib/syscall/ztypes_netbsd_386.go rename to c/syscall/ztypes_netbsd_386.go diff --git a/internal/lib/syscall/ztypes_netbsd_amd64.go b/c/syscall/ztypes_netbsd_amd64.go similarity index 100% rename from internal/lib/syscall/ztypes_netbsd_amd64.go rename to c/syscall/ztypes_netbsd_amd64.go diff --git a/internal/lib/syscall/ztypes_netbsd_arm.go b/c/syscall/ztypes_netbsd_arm.go similarity index 100% rename from internal/lib/syscall/ztypes_netbsd_arm.go rename to c/syscall/ztypes_netbsd_arm.go diff --git a/internal/lib/syscall/ztypes_netbsd_arm64.go b/c/syscall/ztypes_netbsd_arm64.go similarity index 100% rename from internal/lib/syscall/ztypes_netbsd_arm64.go rename to c/syscall/ztypes_netbsd_arm64.go diff --git a/internal/lib/syscall/ztypes_openbsd_386.go b/c/syscall/ztypes_openbsd_386.go similarity index 100% rename from internal/lib/syscall/ztypes_openbsd_386.go rename to c/syscall/ztypes_openbsd_386.go diff --git a/internal/lib/syscall/ztypes_openbsd_amd64.go b/c/syscall/ztypes_openbsd_amd64.go similarity index 100% rename from internal/lib/syscall/ztypes_openbsd_amd64.go rename to c/syscall/ztypes_openbsd_amd64.go diff --git a/internal/lib/syscall/ztypes_openbsd_arm.go b/c/syscall/ztypes_openbsd_arm.go similarity index 100% rename from internal/lib/syscall/ztypes_openbsd_arm.go rename to c/syscall/ztypes_openbsd_arm.go diff --git a/internal/lib/syscall/ztypes_openbsd_arm64.go b/c/syscall/ztypes_openbsd_arm64.go similarity index 100% rename from internal/lib/syscall/ztypes_openbsd_arm64.go rename to c/syscall/ztypes_openbsd_arm64.go diff --git a/internal/lib/syscall/ztypes_openbsd_mips64.go b/c/syscall/ztypes_openbsd_mips64.go similarity index 100% rename from internal/lib/syscall/ztypes_openbsd_mips64.go rename to c/syscall/ztypes_openbsd_mips64.go diff --git a/internal/lib/syscall/ztypes_solaris_amd64.go b/c/syscall/ztypes_solaris_amd64.go similarity index 100% rename from internal/lib/syscall/ztypes_solaris_amd64.go rename to c/syscall/ztypes_solaris_amd64.go diff --git a/internal/build/build.go b/internal/build/build.go index 363598c5..15b687a6 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -664,8 +664,6 @@ func canSkipToBuild(pkgPath string) bool { switch pkgPath { case "unsafe": return true - case "internal/oserror": - return false default: return strings.HasPrefix(pkgPath, "internal/") || strings.HasPrefix(pkgPath, "runtime/internal/") @@ -679,6 +677,7 @@ var hasAltPkg = map[string]none{ "fmt": {}, "internal/abi": {}, "internal/bytealg": {}, + "internal/oserror": {}, "internal/reflectlite": {}, //"io": {}, //"io/fs": {}, diff --git a/internal/lib/syscall/errors.go b/internal/lib/internal/oserror/errors.go similarity index 70% rename from internal/lib/syscall/errors.go rename to internal/lib/internal/oserror/errors.go index e35f780d..b9124224 100644 --- a/internal/lib/syscall/errors.go +++ b/internal/lib/internal/oserror/errors.go @@ -2,11 +2,14 @@ // Use of this source code is governed by a BSD-style // 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" -// from internal/oserror var ( ErrInvalid = errors.New("invalid argument") ErrPermission = errors.New("permission denied") diff --git a/internal/lib/os/stat_unix.go b/internal/lib/os/stat_unix.go index 1c826576..04ae7304 100644 --- a/internal/lib/os/stat_unix.go +++ b/internal/lib/os/stat_unix.go @@ -6,6 +6,8 @@ package os +import "syscall" + // Stat returns the FileInfo structure describing file. // If there is an error, it will be of type *PathError. func (f *File) Stat() (FileInfo, error) { @@ -26,7 +28,6 @@ func (f *File) Stat() (FileInfo, error) { // statNolog stats a file with no test logging. func statNolog(name string) (FileInfo, error) { - /* TODO(xsw): var fs fileStat err := ignoringEINTR(func() error { return syscall.Stat(name, &fs.sys) @@ -36,13 +37,10 @@ func statNolog(name string) (FileInfo, error) { } fillFileStatFromSys(&fs, name) return &fs, nil - */ - panic("todo: os.statNolog") } // lstatNolog lstats a file with no test logging. func lstatNolog(name string) (FileInfo, error) { - /* TODO(xsw): var fs fileStat err := ignoringEINTR(func() error { return syscall.Lstat(name, &fs.sys) @@ -52,6 +50,4 @@ func lstatNolog(name string) (FileInfo, error) { } fillFileStatFromSys(&fs, name) return &fs, nil - */ - panic("todo: os.lstatNolog") } diff --git a/internal/lib/syscall/syscall.go b/internal/lib/syscall/syscall.go index 045dc87b..6dffaf47 100644 --- a/internal/lib/syscall/syscall.go +++ b/internal/lib/syscall/syscall.go @@ -22,8 +22,12 @@ import ( "github.com/goplus/llgo/c" "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. func (ts *Timespec) Unix() (sec int64, nsec int64) { return int64(ts.Sec), int64(ts.Nsec) diff --git a/internal/lib/syscall/syscall_unix.go b/internal/lib/syscall/syscall_unix.go index b0b2a72f..30d66e4f 100644 --- a/internal/lib/syscall/syscall_unix.go +++ b/internal/lib/syscall/syscall_unix.go @@ -11,6 +11,7 @@ import ( "unsafe" "github.com/goplus/llgo/c" + "github.com/goplus/llgo/internal/lib/internal/oserror" ) var ( @@ -28,11 +29,11 @@ func (e Errno) Error() string { func (e Errno) Is(target error) bool { switch target { - case ErrPermission: + case oserror.ErrPermission: return e == EACCES || e == EPERM - case ErrExist: + case oserror.ErrExist: return e == EEXIST || e == ENOTEMPTY - case ErrNotExist: + case oserror.ErrNotExist: return e == ENOENT case errors.ErrUnsupported: return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP diff --git a/internal/lib/time/time.go b/internal/lib/time/time.go index c20799ab..7f748569 100644 --- a/internal/lib/time/time.go +++ b/internal/lib/time/time.go @@ -1021,6 +1021,36 @@ func unixTime(sec int64, nsec int32) Time { 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 { return year%4 == 0 && (year%100 != 0 || year%400 == 0) }