sys_windows.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package time
  5. import (
  6. "errors"
  7. "syscall"
  8. )
  9. // for testing: whatever interrupts a sleep
  10. func interrupt() {
  11. }
  12. // readFile reads and returns the content of the named file.
  13. // It is a trivial implementation of ioutil.ReadFile, reimplemented
  14. // here to avoid depending on io/ioutil or os.
  15. func readFile(name string) ([]byte, error) {
  16. f, err := syscall.Open(name, syscall.O_RDONLY, 0)
  17. if err != nil {
  18. return nil, err
  19. }
  20. defer syscall.Close(f)
  21. var (
  22. buf [4096]byte
  23. ret []byte
  24. n int
  25. )
  26. for {
  27. n, err = syscall.Read(f, buf[:])
  28. if n > 0 {
  29. ret = append(ret, buf[:n]...)
  30. }
  31. if n == 0 || err != nil {
  32. break
  33. }
  34. }
  35. return ret, err
  36. }
  37. func open(name string) (uintptr, error) {
  38. fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
  39. if err != nil {
  40. return 0, err
  41. }
  42. return uintptr(fd), nil
  43. }
  44. func closefd(fd uintptr) {
  45. syscall.Close(syscall.Handle(fd))
  46. }
  47. func preadn(fd uintptr, buf []byte, off int) error {
  48. whence := 0
  49. if off < 0 {
  50. whence = 2
  51. }
  52. if _, err := syscall.Seek(syscall.Handle(fd), int64(off), whence); err != nil {
  53. return err
  54. }
  55. for len(buf) > 0 {
  56. m, err := syscall.Read(syscall.Handle(fd), buf)
  57. if m <= 0 {
  58. if err == nil {
  59. return errors.New("short read")
  60. }
  61. return err
  62. }
  63. buf = buf[m:]
  64. }
  65. return nil
  66. }