gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/snap/implicit.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2017 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 snap
    21  
    22  import (
    23  	"fmt"
    24  	"io/ioutil"
    25  
    26  	"github.com/snapcore/snapd/osutil"
    27  )
    28  
    29  // addImplicitHooks adds hooks from the installed snap's hookdir to the snap info.
    30  //
    31  // Existing hooks (i.e. ones defined in the YAML) are not changed; only missing
    32  // hooks are added.
    33  func addImplicitHooks(snapInfo *Info) error {
    34  	// First of all, check to ensure the hooks directory exists. If it doesn't,
    35  	// it's not an error-- there's just nothing to do.
    36  	hooksDir := snapInfo.HooksDir()
    37  	if !osutil.IsDirectory(hooksDir) {
    38  		return nil
    39  	}
    40  
    41  	fileInfos, err := ioutil.ReadDir(hooksDir)
    42  	if err != nil {
    43  		return fmt.Errorf("unable to read hooks directory: %s", err)
    44  	}
    45  
    46  	for _, fileInfo := range fileInfos {
    47  		addHookIfValid(snapInfo, fileInfo.Name())
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // addImplicitHooksFromContainer adds hooks from the snap file's hookdir to the snap info.
    54  //
    55  // Existing hooks (i.e. ones defined in the YAML) are not changed; only missing
    56  // hooks are added.
    57  func addImplicitHooksFromContainer(snapInfo *Info, snapf Container) error {
    58  	// Read the hooks directory. If this fails we assume the hooks directory
    59  	// doesn't exist, which means there are no implicit hooks to load (not an
    60  	// error).
    61  	fileNames, err := snapf.ListDir("meta/hooks")
    62  	if err != nil {
    63  		return nil
    64  	}
    65  
    66  	for _, fileName := range fileNames {
    67  		addHookIfValid(snapInfo, fileName)
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func addHookIfValid(snapInfo *Info, hookName string) {
    74  	// Verify that the hook name is actually supported. If not, ignore it.
    75  	if !IsHookSupported(hookName) {
    76  		return
    77  	}
    78  
    79  	// Don't overwrite a hook that has already been loaded from the YAML
    80  	if _, ok := snapInfo.Hooks[hookName]; !ok {
    81  		snapInfo.Hooks[hookName] = &HookInfo{Snap: snapInfo, Name: hookName}
    82  	}
    83  }