gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/polkit/spec.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package polkit
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  
    26  	"gitee.com/mysnapcore/mysnapd/interfaces"
    27  	"gitee.com/mysnapcore/mysnapd/snap"
    28  )
    29  
    30  type Policy []byte
    31  
    32  // Specification keeps all the polkit policies.
    33  type Specification struct {
    34  	policyFiles map[string]Policy
    35  }
    36  
    37  // AddPolicy adds a polkit policy file to install.
    38  func (spec *Specification) AddPolicy(nameSuffix string, content Policy) error {
    39  	if old, ok := spec.policyFiles[nameSuffix]; ok && !bytes.Equal(old, content) {
    40  		return fmt.Errorf("internal error: polkit policy content for %q re-defined with different content", nameSuffix)
    41  	}
    42  	if spec.policyFiles == nil {
    43  		spec.policyFiles = make(map[string]Policy)
    44  	}
    45  	spec.policyFiles[nameSuffix] = content
    46  	return nil
    47  }
    48  
    49  // Policies returns a map of polkit policies added to the Specification.
    50  func (spec *Specification) Policies() map[string]Policy {
    51  	if spec.policyFiles == nil {
    52  		return nil
    53  	}
    54  	result := make(map[string]Policy, len(spec.policyFiles))
    55  	for k, v := range spec.policyFiles {
    56  		result[k] = make(Policy, len(v))
    57  		copy(result[k], v)
    58  	}
    59  	return result
    60  }
    61  
    62  // Implementation of methods required by interfaces.Specification
    63  
    64  // AddConnectedPlug records polkit-specific side-effects of having a connected plug.
    65  func (spec *Specification) AddConnectedPlug(iface interfaces.Interface, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    66  	type definer interface {
    67  		PolkitConnectedPlug(spec *Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
    68  	}
    69  	if iface, ok := iface.(definer); ok {
    70  		return iface.PolkitConnectedPlug(spec, plug, slot)
    71  	}
    72  	return nil
    73  }
    74  
    75  // AddConnectedSlot records polkit-specific side-effects of having a connected slot.
    76  func (spec *Specification) AddConnectedSlot(iface interfaces.Interface, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    77  	type definer interface {
    78  		PolkitConnectedSlot(spec *Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error
    79  	}
    80  	if iface, ok := iface.(definer); ok {
    81  		return iface.PolkitConnectedSlot(spec, plug, slot)
    82  	}
    83  	return nil
    84  }
    85  
    86  // AddPermanentPlug records polkit-specific side-effects of having a plug.
    87  func (spec *Specification) AddPermanentPlug(iface interfaces.Interface, plug *snap.PlugInfo) error {
    88  	type definer interface {
    89  		PolkitPermanentPlug(spec *Specification, plug *snap.PlugInfo) error
    90  	}
    91  	if iface, ok := iface.(definer); ok {
    92  		return iface.PolkitPermanentPlug(spec, plug)
    93  	}
    94  	return nil
    95  }
    96  
    97  // AddPermanentSlot records polkit-specific side-effects of having a slot.
    98  func (spec *Specification) AddPermanentSlot(iface interfaces.Interface, slot *snap.SlotInfo) error {
    99  	type definer interface {
   100  		PolkitPermanentSlot(spec *Specification, slot *snap.SlotInfo) error
   101  	}
   102  	if iface, ok := iface.(definer); ok {
   103  		return iface.PolkitPermanentSlot(spec, slot)
   104  	}
   105  	return nil
   106  }