podman

Форк
0
110 строк · 2.8 Кб
1
// Copyright © 2014 Steve Francia <spf@spf13.com>.
2
// Copyright 2009 The Go Authors. All rights reserved.
3

4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package afero
16

17
import (
18
	"path/filepath"
19
	"sort"
20
	"strings"
21
)
22

23
// Glob returns the names of all files matching pattern or nil
24
// if there is no matching file. The syntax of patterns is the same
25
// as in Match. The pattern may describe hierarchical names such as
26
// /usr/*/bin/ed (assuming the Separator is '/').
27
//
28
// Glob ignores file system errors such as I/O errors reading directories.
29
// The only possible returned error is ErrBadPattern, when pattern
30
// is malformed.
31
//
32
// This was adapted from (http://golang.org/pkg/path/filepath) and uses several
33
// built-ins from that package.
34
func Glob(fs Fs, pattern string) (matches []string, err error) {
35
	if !hasMeta(pattern) {
36
		// Lstat not supported by a ll filesystems.
37
		if _, err = lstatIfPossible(fs, pattern); err != nil {
38
			return nil, nil
39
		}
40
		return []string{pattern}, nil
41
	}
42

43
	dir, file := filepath.Split(pattern)
44
	switch dir {
45
	case "":
46
		dir = "."
47
	case string(filepath.Separator):
48
	// nothing
49
	default:
50
		dir = dir[0 : len(dir)-1] // chop off trailing separator
51
	}
52

53
	if !hasMeta(dir) {
54
		return glob(fs, dir, file, nil)
55
	}
56

57
	var m []string
58
	m, err = Glob(fs, dir)
59
	if err != nil {
60
		return
61
	}
62
	for _, d := range m {
63
		matches, err = glob(fs, d, file, matches)
64
		if err != nil {
65
			return
66
		}
67
	}
68
	return
69
}
70

71
// glob searches for files matching pattern in the directory dir
72
// and appends them to matches. If the directory cannot be
73
// opened, it returns the existing matches. New matches are
74
// added in lexicographical order.
75
func glob(fs Fs, dir, pattern string, matches []string) (m []string, e error) {
76
	m = matches
77
	fi, err := fs.Stat(dir)
78
	if err != nil {
79
		return
80
	}
81
	if !fi.IsDir() {
82
		return
83
	}
84
	d, err := fs.Open(dir)
85
	if err != nil {
86
		return
87
	}
88
	defer d.Close()
89

90
	names, _ := d.Readdirnames(-1)
91
	sort.Strings(names)
92

93
	for _, n := range names {
94
		matched, err := filepath.Match(pattern, n)
95
		if err != nil {
96
			return m, err
97
		}
98
		if matched {
99
			m = append(m, filepath.Join(dir, n))
100
		}
101
	}
102
	return
103
}
104

105
// hasMeta reports whether path contains any of the magic characters
106
// recognized by Match.
107
func hasMeta(path string) bool {
108
	// TODO(niemeyer): Should other magic characters be added here?
109
	return strings.ContainsAny(path, "*?[")
110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.