github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/time/sys_unix.go (about)

     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  // It returns an error if name exceeds maxFileSize bytes.
    23  func readFile(name string) ([]byte, error) {
    24  	f, err := syscall.Open(name, syscall.O_RDONLY, 0)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	defer syscall.Close(f)
    29  	var (
    30  		buf [4096]byte
    31  		ret []byte
    32  		n   int
    33  	)
    34  	for {
    35  		n, err = syscall.Read(f, buf[:])
    36  		if n > 0 {
    37  			ret = append(ret, buf[:n]...)
    38  		}
    39  		if n == 0 || err != nil {
    40  			break
    41  		}
    42  		if len(ret) > maxFileSize {
    43  			return nil, fileSizeError(name)
    44  		}
    45  	}
    46  	return ret, err
    47  }
    48  
    49  func open(name string) (uintptr, error) {
    50  	fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
    51  	if err != nil {
    52  		return 0, err
    53  	}
    54  	return uintptr(fd), nil
    55  }
    56  
    57  func closefd(fd uintptr) {
    58  	syscall.Close(int(fd))
    59  }
    60  
    61  func preadn(fd uintptr, buf []byte, off int) error {
    62  	whence := seekStart
    63  	if off < 0 {
    64  		whence = seekEnd
    65  	}
    66  	if _, err := syscall.Seek(int(fd), int64(off), whence); err != nil {
    67  		return err
    68  	}
    69  	for len(buf) > 0 {
    70  		m, err := syscall.Read(int(fd), buf)
    71  		if m <= 0 {
    72  			if err == nil {
    73  				return errors.New("short read")
    74  			}
    75  			return err
    76  		}
    77  		buf = buf[m:]
    78  	}
    79  	return nil
    80  }
    81  
    82  func isNotExist(err error) bool { return err == syscall.ENOENT }