2 glocate -- ZFS-diff-friendly locate-like utility
3 Copyright (C) 2022-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 func IsDir(s string) bool {
28 return s[len(s)-1] == '/'
31 type ByName [][]string
33 func (a ByName) Len() int {
37 func (a ByName) Swap(i, j int) {
38 a[i], a[j] = a[j], a[i]
41 func (a ByName) Less(i, j int) bool {
42 return namesCmp(a[i], a[j]) < 0
45 func nameSplit(name string) []string {
46 cols := strings.Split(name, "/")
48 cols = cols[:len(cols)-1]
49 cols[len(cols)-1] += "/"
54 func nameJoin(name []string) (s string) {
55 s = path.Join(name...)
56 if IsDir(name[len(name)-1]) {
62 func namesCmp(n1, n2 []string) int {
68 for i := 0; i < min; i++ {
69 t1 = strings.TrimSuffix(n1[i], "/")
70 t2 = strings.TrimSuffix(n2[i], "/")
78 if len(n1) > len(n2) {
81 if len(n1) < len(n2) {
87 func hasPrefix(name, prefix []string) bool {
88 if len(name) < len(prefix) {
91 return namesCmp(name[:len(prefix)], prefix) == 0
94 func deoctalize(s string) string {
95 chars := make([]byte, 0, len(s))
96 for i := 0; i < len(s); i++ {
98 b, err := strconv.ParseUint(s[i+1:i+1+4], 0, 8)
102 chars = append(chars, byte(b))
105 chars = append(chars, s[i])