mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
This PR adds structured logging for the aggregator service. Currently, only the text handler is used, but I can extend this to use the JSON handler as well. In this case, probably some code that is shared between the aggregator and the downloader would need to be moved to a common package. I was also wondering, whether this repo is moving to Go 1.21 at the future, since `slog` was introduced in to the standard lib in 1.21. So currently, this still relies on the `x/exp` package. Fixes #462
96 lines
1.8 KiB
Go
96 lines
1.8 KiB
Go
// This file is Free Software under the MIT License
|
|
// without warranty, see README.md and LICENSES/MIT.txt for details.
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
//
|
|
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
|
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/csaf-poc/csaf_distribution/v3/util"
|
|
"golang.org/x/exp/slog"
|
|
)
|
|
|
|
type lazyTransaction struct {
|
|
src string
|
|
dstDir string
|
|
dst string
|
|
}
|
|
|
|
func newLazyTransaction(src, dstDir string) *lazyTransaction {
|
|
return &lazyTransaction{
|
|
src: src,
|
|
dstDir: dstDir,
|
|
}
|
|
}
|
|
|
|
func (lt *lazyTransaction) Src() string {
|
|
return lt.src
|
|
}
|
|
|
|
func (lt *lazyTransaction) Dst() (string, error) {
|
|
if lt.dst != "" {
|
|
return lt.dst, nil
|
|
}
|
|
|
|
srcBase := filepath.Base(lt.src)
|
|
|
|
folder := filepath.Join(lt.dstDir, srcBase)
|
|
|
|
dst, err := util.MakeUniqDir(folder)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Copy old content into new.
|
|
if err := util.DeepCopy(dst, lt.src); err != nil {
|
|
os.RemoveAll(dst)
|
|
return "", err
|
|
}
|
|
lt.dst = dst
|
|
|
|
return dst, nil
|
|
}
|
|
|
|
func (lt *lazyTransaction) rollback() error {
|
|
if lt.dst == "" {
|
|
return nil
|
|
}
|
|
err := os.RemoveAll(lt.dst)
|
|
lt.dst = ""
|
|
return err
|
|
}
|
|
|
|
func (lt *lazyTransaction) commit() error {
|
|
if lt.dst == "" {
|
|
return nil
|
|
}
|
|
defer func() { lt.dst = "" }()
|
|
|
|
// The expanded path of the original link.
|
|
orig, err := filepath.EvalSymlinks(lt.src)
|
|
if err != nil {
|
|
os.RemoveAll(lt.dst)
|
|
return err
|
|
}
|
|
|
|
// Switch directories.
|
|
symlink := filepath.Join(lt.dst, filepath.Base(lt.src))
|
|
if err := os.Symlink(lt.dst, symlink); err != nil {
|
|
os.RemoveAll(lt.dst)
|
|
return err
|
|
}
|
|
|
|
slog.Debug("Moving directory", "from", symlink, "to", lt.src)
|
|
if err := os.Rename(symlink, lt.src); err != nil {
|
|
os.RemoveAll(lt.dst)
|
|
return err
|
|
}
|
|
|
|
return os.RemoveAll(orig)
|
|
}
|