~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to whoopsie/identifier/identifier.go

  • Committer: Guillermo Gonzalez
  • Date: 2014-08-04 20:40:50 UTC
  • mto: (91.148.26 automatic)
  • mto: This revision was merged to the branch mainline in revision 119.
  • Revision ID: guillermo.gonzalez@canonical.com-20140804204050-0tbj2xmmy6cs9a6u
replace whoopsie with /var/lib/dbus/machine-id

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 Copyright 2013-2014 Canonical Ltd.
3
 
 
4
 
 This program is free software: you can redistribute it and/or modify it
5
 
 under the terms of the GNU General Public License version 3, as published
6
 
 by the Free Software Foundation.
7
 
 
8
 
 This program is distributed in the hope that it will be useful, but
9
 
 WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
 
 PURPOSE.  See the GNU General Public License for more details.
12
 
 
13
 
 You should have received a copy of the GNU General Public License along
14
 
 with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
*/
16
 
 
17
 
// Package identifier wraps libwhoopsie, and is thus the
18
 
// source of an anonymous and stable system id used by the Ubuntu
19
 
// error tracker and the Ubuntu push notifications service.
20
 
package identifier
21
 
 
22
 
/*
23
 
#cgo pkg-config: libwhoopsie
24
 
#include <glib.h>
25
 
#include <libwhoopsie/identifier.h>
26
 
*/
27
 
import "C"
28
 
import "unsafe"
29
 
import "errors"
30
 
import "time"
31
 
 
32
 
// an Id knows how to generate itself, and how to stringify itself.
33
 
type Id interface {
34
 
        Generate() error
35
 
        String() string
36
 
}
37
 
 
38
 
// Identifier is the default Id implementation.
39
 
type Identifier struct {
40
 
        value     string
41
 
        generator func(**C.char, **C.GError)
42
 
}
43
 
 
44
 
func generator(csp **C.char, errp **C.GError) {
45
 
        C.whoopsie_identifier_generate(csp, errp)
46
 
}
47
 
 
48
 
// New creates an Identifier, but does not call Generate() on it.
49
 
func New() Id {
50
 
        return &Identifier{generator: generator}
51
 
}
52
 
 
53
 
// Generate makes the Identifier create the identifier itself.
54
 
func (id *Identifier) Generate() error {
55
 
        var gerr *C.GError
56
 
        var cs *C.char
57
 
        defer C.g_free((C.gpointer)(unsafe.Pointer(cs)))
58
 
 
59
 
        for i := 0; i < 200; i++ {
60
 
                id.generator(&cs, &gerr)
61
 
 
62
 
                if gerr == nil && cs != nil {
63
 
                        goto Success
64
 
                }
65
 
                C.g_clear_error(&gerr)
66
 
                time.Sleep(600 * time.Millisecond)
67
 
        }
68
 
        return errors.New("whoopsie_identifier_generate still bad after 2m; giving up")
69
 
 
70
 
Success:
71
 
        id.value = C.GoString(cs)
72
 
        return nil
73
 
}
74
 
 
75
 
// String returns the system identifier as a string.
76
 
func (id *Identifier) String() string {
77
 
        return id.value
78
 
}