github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/time/sys_windows.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  package time
     6  
     7  import (
     8  	"errors"
     9  	"syscall"
    10  )
    11  
    12  // for testing: whatever interrupts a sleep
    13  func interrupt() {
    14  }
    15  
    16  // readFile reads and returns the content of the named file.
    17  // It is a trivial implementation of ioutil.ReadFile, reimplemented
    18  // here to avoid depending on io/ioutil or os.
    19  // It returns an error if name exceeds maxFileSize bytes.
    20  func readFile(name string) ([]byte, error) {
    21  	f, err := syscall.Open(name, syscall.O_RDONLY, 0)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	defer syscall.Close(f)
    26  	var (
    27  		buf [4096]byte
    28  		ret []byte
    29  		n   int
    30  	)
    31  	for {
    32  		n, err = syscall.Read(f, buf[:])
    33  		if n > 0 {
    34  			ret = append(ret, buf[:n]...)
    35  		}
    36  		if n == 0 || err != nil {
    37  			break
    38  		}
    39  		if len(ret) > maxFileSize {
    40  			return nil, fileSizeError(name)
    41  		}
    42  	}
    43  	return ret, err
    44  }
    45  
    46  func open(name string) (uintptr, error) {
    47  	fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
    48  	if err != nil {
    49  		return 0, err
    50  	}
    51  	return uintptr(fd), nil
    52  }
    53  
    54  func closefd(fd uintptr) {
    55  	syscall.Close(syscall.Handle(fd))
    56  }
    57  
    58  func preadn(fd uintptr, buf []byte, off int) error {
    59  	whence := seekStart
    60  	if off < 0 {
    61  		whence = seekEnd
    62  	}
    63  	if _, err := syscall.Seek(syscall.Handle(fd), int64(off), whence); err != nil {
    64  		return err
    65  	}
    66  	for len(buf) > 0 {
    67  		m, err := syscall.Read(syscall.Handle(fd), buf)
    68  		if m <= 0 {
    69  			if err == nil {
    70  				return errors.New("short read")
    71  			}
    72  			return err
    73  		}
    74  		buf = buf[m:]
    75  	}
    76  	return nil
    77  }