Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package jar |
| 16 | |
| 17 | import ( |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 18 | "bytes" |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 20 | "os" |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 21 | "strings" |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 22 | "time" |
| 23 | |
| 24 | "android/soong/third_party/zip" |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 27 | const ( |
| 28 | MetaDir = "META-INF/" |
| 29 | ManifestFile = MetaDir + "MANIFEST.MF" |
| 30 | ModuleInfoClass = "module-info.class" |
| 31 | ) |
| 32 | |
Colin Cross | bddcf13 | 2017-10-04 17:02:23 -0700 | [diff] [blame] | 33 | var DefaultTime = time.Date(2008, 1, 1, 0, 0, 0, 0, time.UTC) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 34 | |
| 35 | var MetaDirExtra = [2]byte{0xca, 0xfe} |
| 36 | |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 37 | // EntryNamesLess tells whether <filepathA> should precede <filepathB> in |
| 38 | // the order of files with a .jar |
| 39 | func EntryNamesLess(filepathA string, filepathB string) (less bool) { |
| 40 | diff := index(filepathA) - index(filepathB) |
| 41 | if diff == 0 { |
| 42 | return filepathA < filepathB |
| 43 | } |
| 44 | return diff < 0 |
| 45 | } |
| 46 | |
| 47 | // Treats trailing * as a prefix match |
| 48 | func patternMatch(pattern, name string) bool { |
| 49 | if strings.HasSuffix(pattern, "*") { |
| 50 | return strings.HasPrefix(name, strings.TrimSuffix(pattern, "*")) |
| 51 | } else { |
| 52 | return name == pattern |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | var jarOrder = []string{ |
Colin Cross | 3454031 | 2017-09-06 12:52:37 -0700 | [diff] [blame] | 57 | MetaDir, |
| 58 | ManifestFile, |
| 59 | MetaDir + "*", |
Jeff Gaston | 01547b2 | 2017-08-21 20:13:28 -0700 | [diff] [blame] | 60 | "*", |
| 61 | } |
| 62 | |
| 63 | func index(name string) int { |
| 64 | for i, pattern := range jarOrder { |
| 65 | if patternMatch(pattern, name) { |
| 66 | return i |
| 67 | } |
| 68 | } |
| 69 | panic(fmt.Errorf("file %q did not match any pattern", name)) |
| 70 | } |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 71 | |
| 72 | func MetaDirFileHeader() *zip.FileHeader { |
| 73 | dirHeader := &zip.FileHeader{ |
| 74 | Name: MetaDir, |
| 75 | Extra: []byte{MetaDirExtra[1], MetaDirExtra[0], 0, 0}, |
| 76 | } |
| 77 | dirHeader.SetMode(0700 | os.ModeDir) |
| 78 | dirHeader.SetModTime(DefaultTime) |
| 79 | |
| 80 | return dirHeader |
| 81 | } |
| 82 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame^] | 83 | // Create a manifest zip header and contents using the provided contents if any. |
| 84 | func ManifestFileContents(contents []byte) (*zip.FileHeader, []byte, error) { |
| 85 | b, err := manifestContents(contents) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 86 | if err != nil { |
| 87 | return nil, nil, err |
| 88 | } |
| 89 | |
| 90 | fh := &zip.FileHeader{ |
| 91 | Name: ManifestFile, |
| 92 | Method: zip.Store, |
| 93 | UncompressedSize64: uint64(len(b)), |
| 94 | } |
Colin Cross | 2825cb3 | 2017-09-29 13:57:10 -0700 | [diff] [blame] | 95 | fh.SetMode(0700) |
| 96 | fh.SetModTime(DefaultTime) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 97 | |
| 98 | return fh, b, nil |
| 99 | } |
| 100 | |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame^] | 101 | // Create manifest contents, using the provided contents if any. |
| 102 | func manifestContents(contents []byte) ([]byte, error) { |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 103 | manifestMarker := []byte("Manifest-Version:") |
| 104 | header := append(manifestMarker, []byte(" 1.0\nCreated-By: soong_zip\n")...) |
| 105 | |
| 106 | var finalBytes []byte |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame^] | 107 | if !bytes.Contains(contents, manifestMarker) { |
| 108 | finalBytes = append(append(header, contents...), byte('\n')) |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 109 | } else { |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame^] | 110 | finalBytes = contents |
Colin Cross | 635acc9 | 2017-09-12 22:50:46 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return finalBytes, nil |
| 114 | } |