| 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 | // A means of conveying a series of edits to binary files. Each item in a |
| 18 | // patchset consists of an offset into the old file, the number of bytes to |
| 19 | // remove, and the octet string to replace it with. |
| 20 | package binpatchscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/ |
| 21 | |
| 22 | import ( |
| 23 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 24 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/" |
| 25 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 26 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 27 | "ioscip-go gomod github.com/golang/go/src go1.18 io/" |
| 28 | "osscip-go gomod github.com/golang/go/src go1.18 os/" |
| 29 | "sortscip-go gomod github.com/golang/go/src go1.18 sort/" |
| 30 | |
| 31 | "github.com/sassoftware/relic/v7/lib/atomicfilescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/" |
| 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | MimeTypescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/MimeType. = "application/x-binary-patch" |
| 36 | |
| 37 | uint32Maxscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/uint32Max. = 0xffffffff |
| 38 | ) |
| 39 | |
| 40 | type PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet# struct { |
| 41 | Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. []PatchHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader# |
| 42 | Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs. [][]byte |
| 43 | } |
| 44 | |
| 45 | type PatchSetHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader# struct { |
| 46 | Versionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader#Version., NumPatchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader#NumPatches. uint32 |
| 47 | } |
| 48 | |
| 49 | type PatchHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader# struct { |
| 50 | Offsetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#Offset. int64 |
| 51 | OldSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#OldSize., NewSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#NewSize. uint32 |
| 52 | } |
| 53 | |
| 54 | // Create a new, empty PatchSet |
| 55 | func Newscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/New().() *PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet# { |
| 56 | return new(PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#) |
| 57 | } |
| 58 | |
| 59 | // Add a new patch region to a PatchSet. The bytes beginning at "offset" and |
| 60 | // running for "oldSize" are removed and replaced with "blob". oldSize may be 0. |
| 61 | func (p *PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#) Addscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Add().(offset, oldSize int64, blob []byte) { |
| 62 | if len(p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.) > 0 { |
| 63 | i := len(p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.) - 1 |
| 64 | last := p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.[i] |
| 65 | lastEnd := last.Offsetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#Offset. + int64(last.OldSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#OldSize.) |
| 66 | lastBlob := p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs.[i] |
| 67 | oldCombo := int64(last.OldSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#OldSize.) + oldSize |
| 68 | newCombo := int64(len(lastBlob)) + int64(len(blob)) |
| 69 | if offset == lastEnd && oldCombo <= uint32Maxscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/uint32Max. && newCombo <= uint32Maxscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/uint32Max. { |
| 70 | // coalesce this patch into the previous one |
| 71 | p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.[i].OldSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#OldSize. = uint32(oldCombo) |
| 72 | p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.[i].NewSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#NewSize. = uint32(newCombo) |
| 73 | if len(blob) > 0 { |
| 74 | newBlob := make([]byte, newCombo) |
| 75 | copy(newBlob, lastBlob) |
| 76 | copy(newBlob[len(lastBlob):], blob) |
| 77 | p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs.[i] = newBlob |
| 78 | } |
| 79 | return |
| 80 | } |
| 81 | } |
| 82 | for oldSize > uint32Maxscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/uint32Max. { |
| 83 | p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. = append(p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches., PatchHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#{offset, uint32Maxscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/uint32Max., 0}) |
| 84 | p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs. = append(p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs., nil) |
| 85 | offset += uint32Maxscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/uint32Max. |
| 86 | oldSize -= uint32Maxscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/uint32Max. |
| 87 | } |
| 88 | p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. = append(p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches., PatchHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#{offset, uint32(oldSize), uint32(len(blob))}) |
| 89 | p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs. = append(p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs., blob) |
| 90 | } |
| 91 | |
| 92 | // Unmarshal a PatchSet from bytes |
| 93 | func Loadscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/Load().(blob []byte) (*PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#, error) { |
| 94 | r := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.18 bytes/NewReader().(blob) |
| 95 | var h PatchSetHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader# |
| 96 | if err := binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.Readscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/Read().(r, binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian., &h); err != nil { |
| 97 | return nil, err |
| 98 | } else if h.Versionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader#Version. != 1 { |
| 99 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("unsupported binpatch version %d", h.Versionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader#Version.) |
| 100 | } |
| 101 | num := int(h.NumPatchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader#NumPatches.) |
| 102 | p := &PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#{ |
| 103 | Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.: make([]PatchHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#, num), |
| 104 | Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs.: make([][]byte, num), |
| 105 | } |
| 106 | if err := binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.Readscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/Read().(r, binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian., p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.); err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | for i, hdr := range p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. { |
| 110 | p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs.[i] = make([]byte, int(hdr.NewSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#NewSize.)) |
| 111 | if _, err := ioscip-go gomod github.com/golang/go/src go1.18 io/.ReadFullscip-go gomod github.com/golang/go/src go1.18 io/ReadFull().(r, p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs.[i]); err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | } |
| 115 | return p, nil |
| 116 | } |
| 117 | |
| 118 | // Marshal a PatchSet to bytes |
| 119 | func (p *PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#) Dumpscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Dump().() []byte { |
| 120 | sortscip-go gomod github.com/golang/go/src go1.18 sort/.Sortscip-go gomod github.com/golang/go/src go1.18 sort/Sort().(sorterscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/sorter#{p}) |
| 121 | header := PatchSetHeaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSetHeader#{1, uint32(len(p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.))} |
| 122 | size := 8 + 16*len(p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.) |
| 123 | for _, hdr := range p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. { |
| 124 | size += int(hdr.NewSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#NewSize.) |
| 125 | } |
| 126 | buf := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.18 bytes/NewBuffer().(make([]byte, 0, size)) |
| 127 | _ = binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.Writescip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/Write().(buf, binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian., header) |
| 128 | _ = binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.Writescip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/Write().(buf, binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/BigEndian., p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.) |
| 129 | for _, blob := range p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs. { |
| 130 | _, _ = buf.Writescip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Write().(blob) |
| 131 | } |
| 132 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().() |
| 133 | } |
| 134 | |
| 135 | // Apply a PatchSet by taking the input file, transforming it, and writing the |
| 136 | // result to outpath. If outpath is the same name as infile then the file will |
| 137 | // be updated in-place if a direct overwrite is possible. If they are not the |
| 138 | // same file, or the patch requires moving parts of the old file, then the |
| 139 | // output will be written to a temporary file then renamed over the destination |
| 140 | // path. |
| 141 | func (p *PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#) Applyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Apply().(infile *osscip-go gomod github.com/golang/go/src go1.18 os/.Filescip-go gomod github.com/golang/go/src go1.18 os/File#, outpath string) error { |
| 142 | if outpath == "" { |
| 143 | outpath = infile.Namescip-go gomod github.com/golang/go/src go1.18 os/File#Name().() |
| 144 | } |
| 145 | // Determine if an in-place overwrite is possible. If any test fails then |
| 146 | // fall back to doing a full copy (write-rename). |
| 147 | ininfo, err := infile.Statscip-go gomod github.com/golang/go/src go1.18 os/File#Stat().() |
| 148 | if err != nil { |
| 149 | return p.applyRewritescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#applyRewrite().(infile, outpath) |
| 150 | } |
| 151 | outinfo, err := osscip-go gomod github.com/golang/go/src go1.18 os/.Lstatscip-go gomod github.com/golang/go/src go1.18 os/Lstat().(outpath) |
| 152 | if err != nil || !canOverwritescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/canOverwrite().(ininfo, outinfo) { |
| 153 | return p.applyRewritescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#applyRewrite().(infile, outpath) |
| 154 | } |
| 155 | size := ininfo.Sizescip-go gomod github.com/golang/go/src go1.18 `io/fs`/FileInfo#Size().() |
| 156 | for i, patch := range p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. { |
| 157 | // All patches except the last must have oldsize == newsize |
| 158 | if patch.OldSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#OldSize. == patch.NewSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#NewSize. { |
| 159 | continue |
| 160 | } else if i != len(p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches.)-1 { |
| 161 | return p.applyRewritescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#applyRewrite().(infile, outpath) |
| 162 | } |
| 163 | // For the last patch, either oldsize == newsize or the patch must extend |
| 164 | // or truncate the file, i.e. the end of the old chunk must coincide |
| 165 | // with the end of the file. |
| 166 | oldEnd := patch.Offsetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#Offset. + int64(patch.OldSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#OldSize.) |
| 167 | if oldEnd != ininfo.Sizescip-go gomod github.com/golang/go/src go1.18 `io/fs`/FileInfo#Size().() { |
| 168 | return p.applyRewritescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#applyRewrite().(infile, outpath) |
| 169 | } |
| 170 | size = patch.Offsetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#Offset. + int64(patch.NewSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#NewSize.) |
| 171 | } |
| 172 | // Do in-place rewrite |
| 173 | for i, patch := range p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. { |
| 174 | if _, err := infile.WriteAtscip-go gomod github.com/golang/go/src go1.18 os/File#WriteAt().(p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs.[i], patch.Offsetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#Offset.); err != nil { |
| 175 | return err |
| 176 | } |
| 177 | } |
| 178 | return infile.Truncatescip-go gomod github.com/golang/go/src go1.18 os/File#Truncate().(size) |
| 179 | } |
| 180 | |
| 181 | // Apply a patch by writing the patched result to a new file. This is the |
| 182 | // fallback case whenever an in-place write isn't possible. |
| 183 | func (p *PatchSetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#) applyRewritescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#applyRewrite().(infile *osscip-go gomod github.com/golang/go/src go1.18 os/.Filescip-go gomod github.com/golang/go/src go1.18 os/File#, outpath string) error { |
| 184 | if _, err := infile.Seekscip-go gomod github.com/golang/go/src go1.18 os/File#Seek().(0, 0); err != nil { |
| 185 | return err |
| 186 | } |
| 187 | outfile, err := atomicfilescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/.Newscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/New().(outpath) |
| 188 | if err != nil { |
| 189 | return err |
| 190 | } |
| 191 | defer outfile.Closescip-go gomod github.com/golang/go/src go1.18 io/Closer#Close().() |
| 192 | var pos int64 |
| 193 | for i, patch := range p.Patchesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Patches. { |
| 194 | blob := p.Blobsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Blobs.[i] |
| 195 | delta := patch.Offsetscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#Offset. - pos |
| 196 | if delta < 0 { |
| 197 | return errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("patches out of order") |
| 198 | } |
| 199 | // Copy data before the patch |
| 200 | if delta > 0 { |
| 201 | if _, err := ioscip-go gomod github.com/golang/go/src go1.18 io/.CopyNscip-go gomod github.com/golang/go/src go1.18 io/CopyN().(outfile, infile, delta); err != nil { |
| 202 | return err |
| 203 | } |
| 204 | pos += delta |
| 205 | } |
| 206 | // Skip the old data on the input file |
| 207 | delta = int64(patch.OldSizescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchHeader#OldSize.) |
| 208 | if _, err := infile.Seekscip-go gomod github.com/golang/go/src go1.18 os/File#Seek().(delta, ioscip-go gomod github.com/golang/go/src go1.18 io/.SeekCurrentscip-go gomod github.com/golang/go/src go1.18 io/SeekCurrent.); err != nil { |
| 209 | return err |
| 210 | } |
| 211 | pos += delta |
| 212 | // Write the new data to the output file |
| 213 | if _, err := outfile.Writescip-go gomod github.com/golang/go/src go1.18 io/Writer#Write().(blob); err != nil { |
| 214 | return err |
| 215 | } |
| 216 | } |
| 217 | // Copy everything after the last patch |
| 218 | if _, err := ioscip-go gomod github.com/golang/go/src go1.18 io/.Copyscip-go gomod github.com/golang/go/src go1.18 io/Copy().(outfile, infile); err != nil { |
| 219 | return err |
| 220 | } |
| 221 | infile.Closescip-go gomod github.com/golang/go/src go1.18 os/File#Close().() |
| 222 | return outfile.Commitscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/AtomicFile#Commit.() |
| 223 | } |
| 224 | |
| 225 | func canOverwritescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/canOverwrite().(ininfo, outinfo osscip-go gomod github.com/golang/go/src go1.18 os/.FileInfoscip-go gomod github.com/golang/go/src go1.18 os/FileInfo#) bool { |
| 226 | if !outinfo.Modescip-go gomod github.com/golang/go/src go1.18 `io/fs`/FileInfo#Mode().().IsRegularscip-go gomod github.com/golang/go/src go1.18 `io/fs`/FileMode#IsRegular().() { |
| 227 | return false |
| 228 | } |
| 229 | if !osscip-go gomod github.com/golang/go/src go1.18 os/.SameFilescip-go gomod github.com/golang/go/src go1.18 os/SameFile().(ininfo, outinfo) { |
| 230 | return false |
| 231 | } |
| 232 | if hasLinksscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/hasLinks().(outinfo) { |
| 233 | return false |
| 234 | } |
| 235 | return true |
| 236 | } |