| 234 | } |
| 235 | |
| 236 | func (u *Updater) updateAndUpgrade(w *mgr.WorkerCtx, indexURLs []string, ignoreVersion, forceApply bool) (err error) { //nolint:maintidx |
| 237 | // Make sure only one update process is running. |
| 238 | if !u.isUpdateRunning.SetToIf(false, true) { |
| 239 | return fmt.Errorf("an updater task is already running, please try again later") |
| 240 | } |
| 241 | defer u.isUpdateRunning.UnSet() |
| 242 | |
| 243 | // Create a new downloader. |
| 244 | downloader := NewDownloader(u, indexURLs) |
| 245 | |
| 246 | // Update or load the index file. |
| 247 | if len(indexURLs) > 0 { |
| 248 | // Download fresh copy, if indexURLs are given. |
| 249 | err = downloader.updateIndex(w.Ctx()) |
| 250 | if err != nil { |
| 251 | return fmt.Errorf("update index file: %w", err) |
| 252 | } |
| 253 | } else { |
| 254 | // Otherwise, load index from download dir. |
| 255 | downloader.index, err = LoadIndex(filepath.Join(u.cfg.DownloadDirectory, u.cfg.IndexFile), u.cfg.Platform, u.cfg.Verify) |
| 256 | if err != nil { |
| 257 | return fmt.Errorf("load previously downloaded index file: %w", err) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Get index to check version. |
| 262 | u.indexLock.Lock() |
| 263 | index := u.index |
| 264 | u.indexLock.Unlock() |
| 265 | |
| 266 | // Check if there is a new version. |
| 267 | if !ignoreVersion && index != nil { |
| 268 | // Check with local pointer to index. |
| 269 | if err := index.ShouldUpgradeTo(downloader.index); err != nil { |
| 270 | if errors.Is(err, ErrSameIndex) { |
| 271 | log.Infof("updates/%s: no new update", u.cfg.Name) |
| 272 | if u.cfg.Notify && u.instance.Notifications() != nil { |
| 273 | u.instance.Notifications().Notify(¬ifications.Notification{ |
| 274 | EventID: noNewUpdateNotificationID, |
| 275 | Type: notifications.Info, |
| 276 | Title: "Portmaster Is Up-To-Date", |
| 277 | Message: "Portmaster v" + index.Version + " is the newest version.", |
| 278 | Expires: time.Now().Add(1 * time.Minute).Unix(), |
| 279 | AvailableActions: []*notifications.Action{ |
| 280 | { |
| 281 | ID: "ack", |
| 282 | Text: "OK", |
| 283 | }, |
| 284 | }, |
| 285 | }) |
| 286 | } |
| 287 | } else { |
| 288 | log.Warningf("updates/%s: cannot update: %s", u.cfg.Name, err) |
| 289 | if u.cfg.Notify && u.instance.Notifications() != nil { |
| 290 | u.instance.Notifications().Notify(¬ifications.Notification{ |
| 291 | EventID: noNewUpdateNotificationID, |
| 292 | Type: notifications.Info, |
| 293 | Title: "Portmaster Is Up-To-Date*", |