diff --git a/deb/contents.go b/deb/contents.go new file mode 100644 index 00000000..99cf169d --- /dev/null +++ b/deb/contents.go @@ -0,0 +1,71 @@ +package deb + +import ( + "fmt" + "github.com/smira/aptly/aptly" + "io" + "sort" + "strings" +) + +// ContentsIndex calculates mapping from files to packages, with sorting and aggregation +type ContentsIndex struct { + index map[string][]*Package +} + +// NewContentsIndex creates empty ContentsIndex +func NewContentsIndex() *ContentsIndex { + return &ContentsIndex{ + index: make(map[string][]*Package), + } +} + +// Push adds package to contents index, calculating package contents as required +func (index *ContentsIndex) Push(p *Package, packagePool aptly.PackagePool) { + contents := p.Contents(packagePool) + + for _, path := range contents { + index.index[path] = append(index.index[path], p) + } +} + +// Empty checks whether index contains no packages +func (index *ContentsIndex) Empty() bool { + return len(index.index) == 0 +} + +// WriteTo dumps sorted mapping of files to qualified package names +func (index *ContentsIndex) WriteTo(w io.Writer) (int64, error) { + var n int64 + + paths := make([]string, len(index.index)) + + i := 0 + for path := range index.index { + paths[i] = path + i++ + } + + sort.Strings(paths) + + nn, err := fmt.Fprintf(w, "%s %s\n", "FILE", "LOCATION") + n += int64(nn) + if err != nil { + return n, err + } + + for _, path := range paths { + packages := index.index[path] + parts := make([]string, len(packages)) + for i := range packages { + parts[i] = packages[i].QualifiedName() + } + nn, err = fmt.Fprintf(w, "%s %s\n", path, strings.Join(parts, ",")) + n += int64(nn) + if err != nil { + return n, err + } + } + + return n, nil +}