ducky/devices

Paddy 2015-12-19 Parent:vendor/code.google.com/p/go-uuid/uuid/time.go@b6494e1a499e

16:a700ede02f91 Go to Latest

ducky/devices/vendor/github.com/pborman/uuid/time.go

Validate device creation. Update our uuid package to the latest, which is now based on the GitHub fork instead of the Google Code. Also, update our api package to its latest version, which now needs the pqarrays package as a dependency. We fleshed out the validateDeviceCreation. We now pass in the scopes we have (for broad access control) and the user ID (for fine-grained access control). This helper returns the first error it encounters, though it should probably return a slice so we can return multiple errors all at once. Before we even decode the request to create a Device, let's check if the user is even logged in. If we can't ascertain that or they're not, there's no point in even consuming the memory necessary to read the request, because we know we're not going to use it anyways. Finally actually validate the devices we're creating, and return an appropriate error for each error we can get. Also, the api.CheckScopes helper function now takes the scopes passed in as a string slice, and we have an api.GetScopes helper function to retrieve the scopes associated with the request. Let's not keep parsing that. We need two new scopes to control access for device creation; ScopeImport lets users import devices in and is pretty much admin access. ScopeCreateOtherUserDevices allows a user to create Devices that are owned by another user.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vendor/github.com/pborman/uuid/time.go	Sat Dec 19 00:18:25 2015 -0800
     1.3 @@ -0,0 +1,132 @@
     1.4 +// Copyright 2014 Google Inc.  All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style
     1.6 +// license that can be found in the LICENSE file.
     1.7 +
     1.8 +package uuid
     1.9 +
    1.10 +import (
    1.11 +	"encoding/binary"
    1.12 +	"sync"
    1.13 +	"time"
    1.14 +)
    1.15 +
    1.16 +// A Time represents a time as the number of 100's of nanoseconds since 15 Oct
    1.17 +// 1582.
    1.18 +type Time int64
    1.19 +
    1.20 +const (
    1.21 +	lillian    = 2299160          // Julian day of 15 Oct 1582
    1.22 +	unix       = 2440587          // Julian day of 1 Jan 1970
    1.23 +	epoch      = unix - lillian   // Days between epochs
    1.24 +	g1582      = epoch * 86400    // seconds between epochs
    1.25 +	g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs
    1.26 +)
    1.27 +
    1.28 +var (
    1.29 +	mu        sync.Mutex
    1.30 +	lasttime  uint64 // last time we returned
    1.31 +	clock_seq uint16 // clock sequence for this run
    1.32 +
    1.33 +	timeNow = time.Now // for testing
    1.34 +)
    1.35 +
    1.36 +// UnixTime converts t the number of seconds and nanoseconds using the Unix
    1.37 +// epoch of 1 Jan 1970.
    1.38 +func (t Time) UnixTime() (sec, nsec int64) {
    1.39 +	sec = int64(t - g1582ns100)
    1.40 +	nsec = (sec % 10000000) * 100
    1.41 +	sec /= 10000000
    1.42 +	return sec, nsec
    1.43 +}
    1.44 +
    1.45 +// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and
    1.46 +// clock sequence as well as adjusting the clock sequence as needed.  An error
    1.47 +// is returned if the current time cannot be determined.
    1.48 +func GetTime() (Time, uint16, error) {
    1.49 +	defer mu.Unlock()
    1.50 +	mu.Lock()
    1.51 +	return getTime()
    1.52 +}
    1.53 +
    1.54 +func getTime() (Time, uint16, error) {
    1.55 +	t := timeNow()
    1.56 +
    1.57 +	// If we don't have a clock sequence already, set one.
    1.58 +	if clock_seq == 0 {
    1.59 +		setClockSequence(-1)
    1.60 +	}
    1.61 +	now := uint64(t.UnixNano()/100) + g1582ns100
    1.62 +
    1.63 +	// If time has gone backwards with this clock sequence then we
    1.64 +	// increment the clock sequence
    1.65 +	if now <= lasttime {
    1.66 +		clock_seq = ((clock_seq + 1) & 0x3fff) | 0x8000
    1.67 +	}
    1.68 +	lasttime = now
    1.69 +	return Time(now), clock_seq, nil
    1.70 +}
    1.71 +
    1.72 +// ClockSequence returns the current clock sequence, generating one if not
    1.73 +// already set.  The clock sequence is only used for Version 1 UUIDs.
    1.74 +//
    1.75 +// The uuid package does not use global static storage for the clock sequence or
    1.76 +// the last time a UUID was generated.  Unless SetClockSequence a new random
    1.77 +// clock sequence is generated the first time a clock sequence is requested by
    1.78 +// ClockSequence, GetTime, or NewUUID.  (section 4.2.1.1) sequence is generated
    1.79 +// for
    1.80 +func ClockSequence() int {
    1.81 +	defer mu.Unlock()
    1.82 +	mu.Lock()
    1.83 +	return clockSequence()
    1.84 +}
    1.85 +
    1.86 +func clockSequence() int {
    1.87 +	if clock_seq == 0 {
    1.88 +		setClockSequence(-1)
    1.89 +	}
    1.90 +	return int(clock_seq & 0x3fff)
    1.91 +}
    1.92 +
    1.93 +// SetClockSeq sets the clock sequence to the lower 14 bits of seq.  Setting to
    1.94 +// -1 causes a new sequence to be generated.
    1.95 +func SetClockSequence(seq int) {
    1.96 +	defer mu.Unlock()
    1.97 +	mu.Lock()
    1.98 +	setClockSequence(seq)
    1.99 +}
   1.100 +
   1.101 +func setClockSequence(seq int) {
   1.102 +	if seq == -1 {
   1.103 +		var b [2]byte
   1.104 +		randomBits(b[:]) // clock sequence
   1.105 +		seq = int(b[0])<<8 | int(b[1])
   1.106 +	}
   1.107 +	old_seq := clock_seq
   1.108 +	clock_seq = uint16(seq&0x3fff) | 0x8000 // Set our variant
   1.109 +	if old_seq != clock_seq {
   1.110 +		lasttime = 0
   1.111 +	}
   1.112 +}
   1.113 +
   1.114 +// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in
   1.115 +// uuid.  It returns false if uuid is not valid.  The time is only well defined
   1.116 +// for version 1 and 2 UUIDs.
   1.117 +func (uuid UUID) Time() (Time, bool) {
   1.118 +	if len(uuid) != 16 {
   1.119 +		return 0, false
   1.120 +	}
   1.121 +	time := int64(binary.BigEndian.Uint32(uuid[0:4]))
   1.122 +	time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32
   1.123 +	time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48
   1.124 +	return Time(time), true
   1.125 +}
   1.126 +
   1.127 +// ClockSequence returns the clock sequence encoded in uuid.  It returns false
   1.128 +// if uuid is not valid.  The clock sequence is only well defined for version 1
   1.129 +// and 2 UUIDs.
   1.130 +func (uuid UUID) ClockSequence() (int, bool) {
   1.131 +	if len(uuid) != 16 {
   1.132 +		return 0, false
   1.133 +	}
   1.134 +	return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff, true
   1.135 +}