apparmor.d/pkg/prebuild/prepare/merge.go
Alexandre Pujol 59ac54e2fc
build: reorganise build: abi4, fallback, prebuild cli
- ABI4 by default, fallback to abi 3.
- aa-prebuild cli that can be used by other project shipping profiles.
- --file option to cli to only build one dev profile.
- add abi version filter to only & exclude directives.
2024-10-02 16:22:46 +01:00

61 lines
1.3 KiB
Go

// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package prepare
import (
"os"
"path/filepath"
"github.com/roddhjav/apparmor.d/pkg/paths"
"github.com/roddhjav/apparmor.d/pkg/prebuild"
)
type Merge struct {
prebuild.Base
}
func init() {
RegisterTask(&Merge{
Base: prebuild.Base{
Keyword: "merge",
Msg: "Merge all profiles into a unified apparmor.d directory",
},
})
}
func (p Merge) Apply() ([]string, error) {
res := []string{}
dirToMerge := []string{
"groups/*/*", "groups",
"profiles-*-*/*", "profiles-*",
}
idx := 0
for idx < len(dirToMerge)-1 {
dirMoved, dirRemoved := dirToMerge[idx], dirToMerge[idx+1]
files, err := filepath.Glob(prebuild.RootApparmord.Join(dirMoved).String())
if err != nil {
return res, err
}
for _, file := range files {
err := os.Rename(file, prebuild.RootApparmord.Join(filepath.Base(file)).String())
if err != nil {
return res, err
}
}
files, err = filepath.Glob(prebuild.RootApparmord.Join(dirRemoved).String())
if err != nil {
return []string{}, err
}
for _, file := range files {
if err := paths.New(file).RemoveAll(); err != nil {
return res, err
}
}
idx = idx + 2
}
return res, nil
}