Change to a more object-oriented approach

Now the code can simply initialize a PartitionInfo from a file and then
call LookupUUIDForLabel on it.

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis
2022-11-11 13:04:17 +02:00
parent 8ca95e953b
commit 4a6c79f6a6
2 changed files with 115 additions and 104 deletions

View File

@@ -10,12 +10,56 @@ import (
"gopkg.in/yaml.v3"
)
const DefaultPartitionInfoFile = "/oem/partition_info.yaml"
// PartitionInfo maps a partition label to a partition UUID.
// It's used in order to be able to ask the kcrypt-challenger for the passphrase
// using the partition label, even when the label is not accessible (e.g. before
// decrypting the partition). The UUID can be used to lookup the partition label
// and make the request.
type PartitionInfo map[string]string
type PartitionInfo struct {
file string
mapping map[string]string
}
func NewPartitionInfoFromFile(file string) (*PartitionInfo, error) {
mapping, err := ParsePartitionInfoFile(file)
if err != nil {
return nil, err
}
return &PartitionInfo{
file: file,
mapping: mapping,
}, nil
}
func (pi PartitionInfo) LookupUUIDForLabel(l string) string {
return pi.mapping[l]
}
// UpdatePartitionLabelMapping takes partition information as a string argument
// the the form: `label:name:uuid` (that's what the `kcrypt encrypt` command returns
// on success. This function stores it in the PartitionInfoFile yaml file for
// later use.
func (pi PartitionInfo) UpdateMapping(partitionData string) error {
label, uuid := PartitionDataFromString(partitionData)
pi.mapping[label] = uuid
return pi.save()
}
func (pi PartitionInfo) save() error {
data, err := yaml.Marshal(&pi.mapping)
if err != nil {
return errors.Wrap(err, "marshalling the new partition info to yaml")
}
err = ioutil.WriteFile(pi.file, data, 0)
if err != nil {
return errors.Wrap(err, "writing back the partition info file")
}
return nil
}
func PartitionToString(p *block.Partition) string {
return fmt.Sprintf("%s:%s:%s", p.Label, p.Name, p.UUID)
@@ -29,29 +73,14 @@ func PartitionDataFromString(partitionStr string) (string, string) {
return parts[0], parts[2]
}
// UpdatePartitionLabelMapping takes partition information as a string argument
// the the form: `label:name:uuid` (that's what the `kcrypt encrypt` command returns
// on success. This function stores it in the PartitionInfoFile yaml file for
// later use.
func UpdatePartitionLabelMapping(partitionData, file string) error {
partitionInfo, err := ParsePartitionInfoFile(file)
if err != nil {
return err
}
label, uuid := PartitionDataFromString(partitionData)
partitionInfo[label] = uuid
return UpdatePartitionInfoFile(partitionInfo, file)
}
func ParsePartitionInfoFile(file string) (PartitionInfo, error) {
var result PartitionInfo
func ParsePartitionInfoFile(file string) (map[string]string, error) {
var result map[string]string
yamlFile, err := ioutil.ReadFile(file)
if err != nil {
return result, errors.Wrap(err, "reading the partition info file")
}
err = yaml.Unmarshal(yamlFile, &result)
if err != nil {
return result, errors.Wrap(err, "unmarshalling partition info file")
@@ -59,15 +88,3 @@ func ParsePartitionInfoFile(file string) (PartitionInfo, error) {
return result, nil
}
func UpdatePartitionInfoFile(partitionInfo PartitionInfo, file string) error {
data, err := yaml.Marshal(&partitionInfo)
if err != nil {
return errors.Wrap(err, "marshalling the new partition info to yaml")
}
err = ioutil.WriteFile(file, data, 0)
if err != nil {
return errors.Wrap(err, "writing back the partition info file")
}
return nil
}