| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build !windows |
| 15 |
// +build !windows |
| 16 |
|
| 17 |
package plugin |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"os" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/notaryproject/notation-plugin-framework-go/plugin" |
| 25 |
) |
| 26 |
|
| 27 |
func binName(name string) string { |
| 28 |
return plugin.BinaryPrefix + name |
| 29 |
} |
| 30 |
|
| 31 |
// isExecutableFile checks if a file at filePath is user executable |
| 32 |
func isExecutableFile(filePath string) (bool, error) { |
| 33 |
fi, err := os.Stat(filePath) |
| 34 |
if err != nil { |
| 35 |
return false, err |
| 36 |
} |
| 37 |
mode := fi.Mode() |
| 38 |
if !mode.IsRegular() { |
| 39 |
return false, ErrNotRegularFile |
| 40 |
} |
| 41 |
return mode.Perm()&0100 != 0, nil |
| 42 |
} |
| 43 |
|
| 44 |
// parsePluginName checks if fileName is a valid plugin file name |
| 45 |
// and gets plugin name from it based on spec: https://github.com/notaryproject/specifications/blob/main/specs/plugin-extensibility.md#installation |
| 46 |
func parsePluginName(fileName string) (string, error) { |
| 47 |
pluginName, found := strings.CutPrefix(fileName, plugin.BinaryPrefix) |
| 48 |
if !found || pluginName == "" { |
| 49 |
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got %s", fileName) |
| 50 |
} |
| 51 |
return pluginName, nil |
| 52 |
} |
| 53 |
|
| 54 |
// setExecutable sets file to be user executable |
| 55 |
func setExecutable(filePath string) error { |
| 56 |
fileInfo, err := os.Stat(filePath) |
| 57 |
if err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
return os.Chmod(filePath, fileInfo.Mode()|os.FileMode(0100)) |
| 61 |
} |