forked from iamacarpet/go-win64api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winupdate.go
executable file
·126 lines (105 loc) · 3.07 KB
/
winupdate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//go:build windows && amd64
// +build windows,amd64
package winapi
import (
"fmt"
"time"
"github.com/google/cabbie/search"
"github.com/google/cabbie/session"
"github.com/google/cabbie/updatehistory"
"github.com/scjalliance/comshim"
so "github.com/iamacarpet/go-win64api/shared"
)
var updateResultStatus []string = []string{
"Completed", // Was "Pending", swap to "Completed" to match Update UI in OS
"Completed", // Was "In Progress", swap to "Completed" to match Update UI in OS
"Completed",
"Completed With Errors",
"Failed",
"Aborted",
}
func UpdatesPending() (*so.WindowsUpdate, error) {
retData := &so.WindowsUpdate{}
comshim.Add(1)
defer comshim.Done()
reqUpdates, _, err := listUpdates(false)
if err != nil {
return nil, fmt.Errorf("Error getting Windows Update info: %s", err.Error())
}
retData.NumUpdates = len(reqUpdates)
for _, u := range reqUpdates {
retData.UpdateHistory = append(retData.UpdateHistory, &so.WindowsUpdateHistory{
EventDate: time.Now(),
Status: "In Progress",
UpdateName: u,
})
}
history, err := updateHistory()
if err != nil {
return nil, fmt.Errorf("Error getting update history: %s", err.Error())
}
for _, e := range history.Entries {
retData.UpdateHistory = append(retData.UpdateHistory, &so.WindowsUpdateHistory{
EventDate: e.Date,
Status: updateResultStatus[int(e.ResultCode)],
UpdateName: e.Title,
})
}
if retData.NumUpdates > 0 {
retData.UpdatesReq = true
}
return retData, nil
}
func listUpdates(hidden bool) ([]string, []string, error) {
// Set search criteria
c := search.BasicSearch + " OR Type='Driver' OR " + search.BasicSearch + " AND Type='Software'"
if hidden {
c += " and IsHidden=1"
} else {
c += " and IsHidden=0"
}
// Start Windows update session
s, err := session.New()
if err != nil {
return nil, nil, fmt.Errorf("failed to create new Windows Update session: %v", err)
}
defer s.Close()
q, err := search.NewSearcher(s, c, []string{}, 1)
if err != nil {
return nil, nil, fmt.Errorf("failed to create a new searcher object: %v", err)
}
defer q.Close()
uc, err := q.QueryUpdates()
if err != nil {
return nil, nil, fmt.Errorf("error encountered when attempting to query for updates: %v", err)
}
defer uc.Close()
var reqUpdates, optUpdates []string
for _, u := range uc.Updates {
// Add to optional updates list if the update does not match the required categories.
if !u.InCategories([]string{"Critical Updates", "Definition Updates", "Security Updates"}) {
optUpdates = append(optUpdates, u.Title)
continue
}
// Skip virus updates as they always exist.
if !u.InCategories([]string{"Definition Updates"}) {
reqUpdates = append(reqUpdates, u.Title)
}
}
return reqUpdates, optUpdates, nil
}
func updateHistory() (*updatehistory.History, error) {
// Start Windows update session
s, err := session.New()
if err != nil {
return nil, err
}
defer s.Close()
// Create Update searcher interface
searcher, err := search.NewSearcher(s, "", []string{}, 1)
if err != nil {
return nil, err
}
defer searcher.Close()
return updatehistory.Get(searcher)
}