github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/kernel/validate.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 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 kernel 21 22 import ( 23 "fmt" 24 "path/filepath" 25 "strings" 26 27 "github.com/snapcore/snapd/osutil" 28 ) 29 30 func validateAssetsContent(kernelRoot string, info *Info) error { 31 // bare structure content is checked to exist during layout 32 // make sure that filesystem content source paths exist as well 33 for name, as := range info.Assets { 34 for _, c := range as.Content { 35 realSource := filepath.Join(kernelRoot, c) 36 if !osutil.FileExists(realSource) { 37 return fmt.Errorf("asset %q: content %q source path does not exist", name, c) 38 } 39 if strings.HasSuffix(c, "/") { 40 // expecting a directory 41 if !osutil.IsDirectory(realSource + "/") { 42 return fmt.Errorf("asset %q: content %q is not a directory", name, c) 43 } 44 } 45 } 46 } 47 return nil 48 } 49 50 // Validate checks whether the given directory contains valid kernel snap 51 // metadata and a matching content. 52 func Validate(kernelRoot string) error { 53 info, err := ReadInfo(kernelRoot) 54 if err != nil { 55 return fmt.Errorf("invalid kernel metadata: %v", err) 56 } 57 58 if err := validateAssetsContent(kernelRoot, info); err != nil { 59 return err 60 } 61 62 return nil 63 }