Home | History | Annotate | Download | only in time
      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 
      5 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
      6 
      7 package time
      8 
      9 import (
     10 	"errors"
     11 	"syscall"
     12 )
     13 
     14 // for testing: whatever interrupts a sleep
     15 func interrupt() {
     16 	syscall.Kill(syscall.Getpid(), syscall.SIGCHLD)
     17 }
     18 
     19 // readFile reads and returns the content of the named file.
     20 // It is a trivial implementation of ioutil.ReadFile, reimplemented
     21 // here to avoid depending on io/ioutil or os.
     22 func readFile(name string) ([]byte, error) {
     23 	f, err := syscall.Open(name, syscall.O_RDONLY, 0)
     24 	if err != nil {
     25 		return nil, err
     26 	}
     27 	defer syscall.Close(f)
     28 	var (
     29 		buf [4096]byte
     30 		ret []byte
     31 		n   int
     32 	)
     33 	for {
     34 		n, err = syscall.Read(f, buf[:])
     35 		if n > 0 {
     36 			ret = append(ret, buf[:n]...)
     37 		}
     38 		if n == 0 || err != nil {
     39 			break
     40 		}
     41 	}
     42 	return ret, err
     43 }
     44 
     45 func open(name string) (uintptr, error) {
     46 	fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
     47 	if err != nil {
     48 		return 0, err
     49 	}
     50 	return uintptr(fd), nil
     51 }
     52 
     53 func closefd(fd uintptr) {
     54 	syscall.Close(int(fd))
     55 }
     56 
     57 func preadn(fd uintptr, buf []byte, off int) error {
     58 	whence := seekStart
     59 	if off < 0 {
     60 		whence = seekEnd
     61 	}
     62 	if _, err := syscall.Seek(int(fd), int64(off), whence); err != nil {
     63 		return err
     64 	}
     65 	for len(buf) > 0 {
     66 		m, err := syscall.Read(int(fd), buf)
     67 		if m <= 0 {
     68 			if err == nil {
     69 				return errors.New("short read")
     70 			}
     71 			return err
     72 		}
     73 		buf = buf[m:]
     74 	}
     75 	return nil
     76 }
     77 
     78 func isNotExist(err error) bool { return err == syscall.ENOENT }
     79