-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unify apk installed db for base layer
Signed-off-by: Avi Deitcher <avi@deitcher.net>
- Loading branch information
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package moby | ||
|
||
import ( | ||
"archive/tar" | ||
"bytes" | ||
) | ||
|
||
// apkTarWriter apk-aware tar writer that consolidates installed database, so that | ||
// it can be called multiple times and will do the union of all such databases, | ||
// rather than overwriting the previous one. | ||
const apkInstalledPath = "lib/apk/db/installed" | ||
|
||
type apkTarWriter struct { | ||
*tar.Writer | ||
dbs [][]byte | ||
current *bytes.Buffer | ||
} | ||
|
||
func newAPKTarWriter(w *tar.Writer) *apkTarWriter { | ||
return &apkTarWriter{ | ||
Writer: w, | ||
} | ||
} | ||
|
||
func (a *apkTarWriter) WriteHeader(hdr *tar.Header) error { | ||
if a.current != nil { | ||
a.dbs = append(a.dbs, a.current.Bytes()) | ||
a.current = nil | ||
} | ||
if hdr.Name == apkInstalledPath { | ||
a.current = new(bytes.Buffer) | ||
} | ||
return a.Writer.WriteHeader(hdr) | ||
} | ||
func (a *apkTarWriter) Write(b []byte) (int, error) { | ||
if a.current != nil { | ||
a.current.Write(b) | ||
} | ||
return a.Writer.Write(b) | ||
} | ||
|
||
func (a *apkTarWriter) Close() error { | ||
// before closing, write out the union of all the databases | ||
if a.current != nil { | ||
a.dbs = append(a.dbs, a.current.Bytes()) | ||
a.current = nil | ||
} | ||
if err := a.WriteAPKDB(); err != nil { | ||
return err | ||
} | ||
return a.Writer.Close() | ||
} | ||
|
||
func (a *apkTarWriter) WriteAPKDB() error { | ||
if len(a.dbs) > 1 { | ||
// consolidate the databases | ||
// calculate the size of the new database | ||
var size int | ||
for _, db := range a.dbs { | ||
size += len(db) | ||
size += 2 // 2 trailing newlines for each db | ||
} | ||
hdr := &tar.Header{ | ||
Name: apkInstalledPath, | ||
Mode: 0o644, | ||
Uid: 0, | ||
Gid: 0, | ||
Typeflag: tar.TypeReg, | ||
Size: int64(size), | ||
} | ||
a.Writer.WriteHeader(hdr) | ||
for _, db := range a.dbs { | ||
a.Writer.Write(db) | ||
a.Writer.Write([]byte{'\n', '\n'}) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters