| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 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 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package zipslicer |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "time" |
| 22 | |
| 23 | "github.com/sassoftware/relic/v7/lib/binpatch" |
| 24 | ) |
| 25 | |
| 26 | type Mangler struct { |
| 27 | outz *Directory |
| 28 | patch *binpatch.PatchSet |
| 29 | newcontents bytes.Buffer |
| 30 | indir, insize int64 |
| 31 | } |
| 32 | |
| 33 | type MangleFunc func(*MangleFile) error |
| 34 | |
| 35 | // Walk all the files in the directory in-order, invoking a callback that can |
| 36 | // decide whether to keep or discard each one. Returns a Mangler that can be |
| 37 | // used to add more files and eventually produce a binary patch against the |
| 38 | // original zip. |
| 39 | func (d *Directory) Mangle(callback MangleFunc) (*Mangler, error) { |
| 40 | m := &Mangler{ |
| 41 | outz: new(Directory), |
| 42 | patch: binpatch.New(), |
| 43 | indir: d.DirLoc, |
| 44 | insize: d.Size, |
| 45 | } |
| 46 | for _, f := range d.File { |
| 47 | mf := &MangleFile{File: *f, m: m} |
| 48 | if err := callback(mf); err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | if mf.deleted { |
| 52 | size, err := mf.GetTotalSize() |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | m.patch.Add(int64(mf.Offset), size, nil) |
| 57 | } else { |
| 58 | if _, err := m.outz.AddFile(&mf.File); err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return m, nil |
| 64 | } |
| 65 | |
| 66 | // Add a new file to a zip mangler |
| 67 | func (m *Mangler) NewFile(name string, contents []byte) error { |
| 68 | deflate := len(contents) != 0 |
| 69 | _, err := m.outz.NewFile(name, nil, contents, &m.newcontents, time.Now(), deflate, true) |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | // Create a binary patchset out of the operations performed in this mangler |
| 74 | func (m *Mangler) MakePatch(forceZip64 bool) (*binpatch.PatchSet, error) { |
| 75 | w := &m.newcontents |
| 76 | if err := m.outz.WriteDirectory(w, w, forceZip64); err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | m.patch.Add(m.indir, m.insize-m.indir, m.newcontents.Bytes()) |
| 80 | return m.patch, nil |
| 81 | } |
| 82 | |
| 83 | type MangleFile struct { |
| 84 | File |
| 85 | m *Mangler |
| 86 | deleted bool |
| 87 | } |
| 88 | |
| 89 | // Mark this file for deletion |
| 90 | func (f *MangleFile) Delete() { |
| 91 | f.deleted = true |
| 92 | } |