blob: 5960bf0a7c7133b4c0f42bc8c5117e0a4801035e [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
Colin Cross34540312017-09-06 12:52:37 -070022const (
23 MetaDir = "META-INF/"
24 ManifestFile = MetaDir + "MANIFEST.MF"
25 ModuleInfoClass = "module-info.class"
26)
27
Jeff Gaston01547b22017-08-21 20:13:28 -070028// EntryNamesLess tells whether <filepathA> should precede <filepathB> in
29// the order of files with a .jar
30func EntryNamesLess(filepathA string, filepathB string) (less bool) {
31 diff := index(filepathA) - index(filepathB)
32 if diff == 0 {
33 return filepathA < filepathB
34 }
35 return diff < 0
36}
37
38// Treats trailing * as a prefix match
39func patternMatch(pattern, name string) bool {
40 if strings.HasSuffix(pattern, "*") {
41 return strings.HasPrefix(name, strings.TrimSuffix(pattern, "*"))
42 } else {
43 return name == pattern
44 }
45}
46
47var jarOrder = []string{
Colin Cross34540312017-09-06 12:52:37 -070048 MetaDir,
49 ManifestFile,
50 MetaDir + "*",
Jeff Gaston01547b22017-08-21 20:13:28 -070051 "*",
52}
53
54func index(name string) int {
55 for i, pattern := range jarOrder {
56 if patternMatch(pattern, name) {
57 return i
58 }
59 }
60 panic(fmt.Errorf("file %q did not match any pattern", name))
61}