-
Notifications
You must be signed in to change notification settings - Fork 53
/
ndb.go
202 lines (165 loc) · 5.58 KB
/
ndb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Copyright (c) 2021 SUSE LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package ndb
import (
"encoding/binary"
"fmt"
"io"
"os"
"unsafe"
dbi "github.com/knqyf263/go-rpmdb/pkg/db"
"golang.org/x/xerrors"
)
/* The "New" Database format by RPM is currently not well documented outside
source code, which is located here:
https://github.com/rpm-software-management/rpm/blob/rpm-4.17.0-release/lib/backend/ndb/rpmpkg.c
The format can be summarized this way:
Packages.db File Format:
========================
32 bytes "NDB Header": Format Magic header, with version number etc. Provides the
Slot Pages count "SlotNPages".
Immediately following the NDB Header is an array of "SlotNPages" count Slot Pages.
Each Slot Page is exactly 4k in size and contains NDB_SlotEntriesPerPage individual Slots.
Each Slot Entry can be referring to a Package with an identifier or be a free slot entry (Package
index is zero). If a Slot Entry is non-free, the BlkOffset points to the "Block".
The "Block" has a "Blob Header", directly followed by the "Blob" (the actual package headers) and
a Blob "tail" of up to 16 bytes. The "Blob" is checksummed using Adler32 from RFC1950.
This implementation is currently not validating the blob checksum.
*/
type ndbHeader struct {
HeaderMagic uint32
NDBVersion uint32
NDBGeneration uint32
SlotNPages uint32
_ [4]uint32
}
type ndbSlotEntry struct {
SlotMagic uint32
PkgIndex uint32
BlkOffset uint32
BlkCount uint32
}
type ndbBlobHeader struct {
BlobMagic uint32
PkgIndex uint32
BlobCkSum uint32
BlobLen uint32
}
type RpmNDB struct {
file *os.File
slots []ndbSlotEntry
}
const NDB_SlotEntriesPerPage = 4096 / 16 /* 16 == unsafe.Sizeof(NDBSlotEntry) */
const NDB_HeaderMagic = 'R' | 'p'<<8 | 'm'<<16 | 'P'<<24
const NDB_DBVersion = 0
var ErrorInvalidNDB = xerrors.Errorf("invalid or unsupported NDB format")
func Open(path string) (*RpmNDB, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
err = syscallFlock(int(file.Fd()), syscallLOCK_SH)
if err != nil {
return nil, err
}
hdrBuff := ndbHeader{}
err = binary.Read(file, binary.LittleEndian, &hdrBuff)
if err != nil {
return nil, xerrors.Errorf("failed to read metadata: %w", err)
}
if hdrBuff.HeaderMagic != NDB_HeaderMagic || hdrBuff.SlotNPages == 0 ||
hdrBuff.NDBVersion != NDB_DBVersion {
return nil, ErrorInvalidNDB
}
// Sanity check against excessive memory usage
if hdrBuff.SlotNPages > 2048 {
return nil, xerrors.Errorf("slot page limit exceeded: %x", hdrBuff.SlotNPages)
}
// the first two slots are actually the NDB Header
slots := make([]ndbSlotEntry, hdrBuff.SlotNPages*NDB_SlotEntriesPerPage-2)
err = binary.Read(file, binary.LittleEndian, &slots)
if err != nil {
return nil, xerrors.Errorf("failed to read NDB slot pages: %w", err)
}
return &RpmNDB{
file: file,
slots: slots,
}, nil
}
func (db *RpmNDB) Close() error {
_ = syscallFlock(int(db.file.Fd()), syscallLOCK_UN)
return db.file.Close()
}
func (db *RpmNDB) Read() <-chan dbi.Entry {
entries := make(chan dbi.Entry)
go func() {
defer close(entries)
const NDB_BlobHeaderSize = int64(unsafe.Sizeof(ndbBlobHeader{}))
for _, slot := range db.slots {
const NDB_SlotMagic = 'S' | 'l'<<8 | 'o'<<16 | 't'<<24
if slot.SlotMagic != NDB_SlotMagic {
fmt.Println("bad slot magic", slot.SlotMagic)
entries <- dbi.Entry{
Err: xerrors.Errorf("bad slot Magic: %x", slot.SlotMagic),
}
return
}
// Empty slot?
if slot.PkgIndex == 0 {
continue
}
// Seek to Blob
_, err := db.file.Seek(int64(slot.BlkOffset)*NDB_BlobHeaderSize, io.SeekStart)
if err != nil {
entries <- dbi.Entry{
Err: err,
}
return
}
// Read Blob Header
blobHeaderBuff := ndbBlobHeader{}
err = binary.Read(db.file, binary.LittleEndian, &blobHeaderBuff)
if err != nil {
entries <- dbi.Entry{
Err: err,
}
return
}
const NDB_BlobMagic = 'B' | 'l'<<8 | 'b'<<16 | 'S'<<24
if blobHeaderBuff.BlobMagic != NDB_BlobMagic {
entries <- dbi.Entry{
Err: xerrors.Errorf("unexpected NDB blob Magic for pkg %d: %x", slot.PkgIndex, blobHeaderBuff.BlobMagic),
}
}
if blobHeaderBuff.PkgIndex != slot.PkgIndex {
entries <- dbi.Entry{
Err: xerrors.Errorf("failed to find NDB blob for pkg %d", slot.PkgIndex),
}
}
// ### check that BlkCnt == (BLOBHEAD_SIZE + bloblen + BLOBTAIL_SIZE + PKGDB_BLK_SIZE - 1) / PKGDB_BLK_SIZE)
// Read Blob Content
BlobEntry := make([]byte, blobHeaderBuff.BlobLen)
_, err = db.file.Read(BlobEntry)
entries <- dbi.Entry{
Value: BlobEntry,
Err: err,
}
}
}()
return entries
}