aboutsummaryrefslogtreecommitdiff
blob: 8ad9187e96969d89805c38fffea6e9e75103b653 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Update the portage data in the database

package portage

import (
	"log/slog"
	"os"
	"soko/pkg/config"
	"soko/pkg/database"
	"soko/pkg/models"
	"soko/pkg/portage/repository"
	"soko/pkg/portage/utils"
	"strings"
	"time"
)

// Update incrementally updates the whole data in the database. All commits
// since the last update are parsed and the changed data is updated. In case
// this is the first update that is there is no last update a full import
// starting with the first commit in the tree is done.
func Update() {
	database.Connect()
	defer database.DBCon.Close()

	slog.Info("Start update...")

	// update the local useflags
	repository.UpdateUse("profiles/use.local.desc")

	latestCommit := utils.GetLatestCommit()
	changed := utils.ChangedFiles(latestCommit, "HEAD")

	updateMetadata(changed)
	updatePackageData(changed)
	updateHistory()

	repository.CalculateMaskedVersions()
	repository.CalculateDeprecatedToVersion()
}

// updateMetadata updates all USE flags, package masks and arches in the database
// by parsing:
//   - profiles/use.desc
//   - profiles/use.local.desc
//   - profiles/use.local.desc
//   - profiles/desc/*
//   - profiles/package.mask
//   - profiles/package.deprecated
//   - profiles/arch.list
//
// It works incrementally so that files are only parsed and updated whenever the
// file has been modified within the new commits. New commits are determined by
// retrieving the last commit in the database (if present) and parsing all
// following commits. In case no last commit is present a full import
// starting with the first commit in the tree is done.
func updateMetadata(changed []string) {
	slog.Info("Start updating changed metadata")
	slog.Info("Iterating changed files", slog.Int("count", len(changed)))
	for _, path := range changed {
		repository.UpdateUse(path)
		repository.UpdateMask(path)
		repository.UpdatePackagesDeprecated(path)
		repository.UpdateArch(path)
	}
}

// updatePackageData incrementally updates all package data in the database, that has
// been changed since the last update. That is:
//   - categories
//   - packages
//   - versions
//
// changed data is determined by parsing all commits since the last update.
func updatePackageData(changed []string) {
	slog.Info("Start updating changed package data")
	slog.Info("Iterating changed files", slog.Int("count", len(changed)))

	repository.UpdateVersions(changed)
	repository.UpdatePackages(changed)
	repository.UpdateCategories(changed)
}

// updateHistory incrementally imports all new commits. New commits are
// determined by retrieving the last commit in the database (if present)
// and parsing all following commits. In case no last commit is present
// a full import starting with the first commit in the tree is done.
func updateHistory() {
	slog.Info("Start updating the history")

	latestCommit := repository.UpdateCommits()

	if strings.TrimSpace(latestCommit) == "" {
		currentApplicationData := getApplicationData()
		latestCommit = currentApplicationData.LastCommit
	}

	application := &models.Application{
		Id:         "latest",
		LastUpdate: time.Now(),
		Version:    config.Version(),
		LastCommit: latestCommit,
	}

	_, err := database.DBCon.Model(application).OnConflict("(id) DO UPDATE").Insert()
	if err != nil {
		slog.Error("Failed updating application data", slog.Any("err", err))
	}
}

// FullUpdate does - as the name applies - a full update. That is, it
// iterates through *all* files in the tree and updates their records
// in the database. Afterwards it is checked whether all records that
// that are in the database, are present in the main tree. This way
// files which already got deleted from the main tree, get deleted
// from the database. All deleted files will be logged.
// This method is mainly intended for cleaning up the data and finding
// outdated data, which indicates bugs in the incremental update.
// Once there is no outdated data found anymore this method may become
// obsolete.
func FullUpdate() {
	database.Connect()
	defer database.DBCon.Close()

	slog.Info("Full update up...")

	// Add new entries & update existing
	slog.Info("Update all present files")

	// update useflags
	database.TruncateTable((*models.Useflag)(nil))
	repository.UpdateUse("profiles/use.desc")
	repository.UpdateUse("profiles/use.local.desc")
	if entries, err := os.ReadDir(config.PortDir() + "/profiles/desc"); err != nil {
		slog.Error("Error reading profiles/desc", slog.Any("err", err))
	} else {
		for _, entry := range entries {
			repository.UpdateUse("profiles/desc/" + entry.Name())
		}
	}

	allFiles := utils.AllFiles()
	updateMetadata(allFiles)
	repository.UpdateVersions(allFiles)
	repository.UpdatePackages(allFiles)
	repository.UpdateCategories(allFiles)

	// Delete removed entries
	slog.Info("Delete removed files from the database")
	deleteRemovedVersions()
	deleteRemovedPackages()
	deleteRemovedCategories()

	fixPrecedingCommitsOfPackages()

	repository.CalculateMaskedVersions()
	repository.CalculateDeprecatedToVersion()

	slog.Info("Finished update up...")
}

// deleteRemovedVersions removes all versions from the database
// that are present in the database but not in the main tree.
func deleteRemovedVersions() {
	var versions, toDelete []*models.Version
	err := database.DBCon.Model(&versions).Column("id", "atom", "package", "version").Select()
	if err != nil {
		slog.Error("Failed fetching versions", slog.Any("err", err))
		return
	}

	for _, version := range versions {
		path := config.PortDir() + "/" + version.Atom + "/" + version.Package + "-" + version.Version + ".ebuild"
		if !utils.FileExists(path) {
			slog.Error("Found ebuild version in the database that does not exist", slog.String("version", version.Id))
			toDelete = append(toDelete, version)
		}
	}

	if len(toDelete) > 0 {
		res, err := database.DBCon.Model(&toDelete).Delete()
		if err != nil {
			slog.Error("Failed deleting versions", slog.Any("err", err))
		} else {
			slog.Info("Deleted versions", slog.Int("rows", res.RowsAffected()))
		}
	}
}

// deleteRemovedPackages removes all packages from the database
// that are present in the database but not in the main tree.
func deleteRemovedPackages() {
	var packages, toDelete []*models.Package
	err := database.DBCon.Model(&packages).Column("atom").Select()
	if err != nil {
		slog.Error("Failed fetching packages", slog.Any("err", err))
		return
	}

	for _, pkg := range packages {
		if !utils.FileExists(config.PortDir() + "/" + pkg.Atom) {
			slog.Error("Found package in the database that does not exist", slog.String("atom", pkg.Atom))
			toDelete = append(toDelete, pkg)
		}
	}

	if len(toDelete) > 0 {
		res, err := database.DBCon.Model(&toDelete).Delete()
		if err != nil {
			slog.Error("Failed deleting packages", slog.Any("err", err))
		} else {
			slog.Info("Deleted packages", slog.Int("rows", res.RowsAffected()))
		}
	}
}

// deleteRemovedCategories removes all categories from the database
// that are present in the database but not in the main tree.
func deleteRemovedCategories() {
	var categories, toDelete []*models.Category
	err := database.DBCon.Model(&categories).Column("name").Select()
	if err != nil {
		slog.Error("Failed fetching categories", slog.Any("err", err))
		return
	}

	for _, category := range categories {
		if !utils.FileExists(config.PortDir() + "/" + category.Name) {
			slog.Error("Found category in the database that does not exist", slog.String("name", category.Name))
			toDelete = append(toDelete, category)
		}
	}

	if len(toDelete) > 0 {
		res, err := database.DBCon.Model(&toDelete).Delete()
		if err != nil {
			slog.Error("Failed deleting categories", slog.Any("err", err))
		} else {
			slog.Info("Deleted categories", slog.Int("rows", res.RowsAffected()))
		}
	}
}

// fixPreviousCommitsOfPackages updates packages that have
// preceding commits == null, that is preceding commits == 0
// This should not happen and will thus be logged. Furthermore
// preceding commits will be set to 1 in this case so that
// package does not mistakenly appears in the 'lately added
// packages' section.
func fixPrecedingCommitsOfPackages() {
	var packages []*models.Package
	database.DBCon.Model(&packages).Where("preceding_commits = 0").Select()
	if len(packages) == 0 {
		return
	}

	slog.Error("Found packages with preceding commits == 0. This should not happen. Fixing...", slog.Int("count", len(packages)))
	for _, pkg := range packages {
		pkg.PrecedingCommits = 1
	}
	database.DBCon.Model(&packages).Update()
}

// GetApplicationData is used to retrieve the
// application data from the database
func getApplicationData() models.Application {
	// Select user by primary key.
	applicationData := &models.Application{Id: "latest"}
	err := database.DBCon.Model(applicationData).WherePK().Select()
	if err != nil {
		slog.Error("Failed fetching application data", slog.Any("err", err))
		return models.Application{
			Id:         "latest",
			LastUpdate: time.Now(),
			LastCommit: "unknown",
			Version:    "unknown",
		}
	}
	return *applicationData
}