blob: d8f063c98e6bb1d1de30fc33071eb42183101ce8 [file] [log] [blame]
Jeff Gaston01547b22017-08-21 20:13:28 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
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
15package jar
16
17import (
18 "fmt"
19 "strings"
20)
21
22// EntryNamesLess tells whether <filepathA> should precede <filepathB> in
23// the order of files with a .jar
24func EntryNamesLess(filepathA string, filepathB string) (less bool) {
25 diff := index(filepathA) - index(filepathB)
26 if diff == 0 {
27 return filepathA < filepathB
28 }
29 return diff < 0
30}
31
32// Treats trailing * as a prefix match
33func patternMatch(pattern, name string) bool {
34 if strings.HasSuffix(pattern, "*") {
35 return strings.HasPrefix(name, strings.TrimSuffix(pattern, "*"))
36 } else {
37 return name == pattern
38 }
39}
40
41var jarOrder = []string{
42 "META-INF/",
43 "META-INF/MANIFEST.MF",
44 "META-INF/*",
45 "*",
46}
47
48func index(name string) int {
49 for i, pattern := range jarOrder {
50 if patternMatch(pattern, name) {
51 return i
52 }
53 }
54 panic(fmt.Errorf("file %q did not match any pattern", name))
55}