Esempio n. 1
0
func createPkgFiles(t *testing.T, tmpDir string, repoName string, pkgs []Package) {
	dir := tmpDir + "/var/" + repoName
	if err := os.MkdirAll(dir, 0777); err != nil {
		t.Error("Can't ceate info.xml.lzma file:", err)
		t.Fail()
	}

	f, err := os.Create(dir + "/info.xml.lzma")
	if err != nil {
		t.Error("Can't ceate info.xml.lzma file:", err)
		t.Fail()
	}
	defer f.Close()

	lz := lzma.NewWriter(f)
	defer lz.Close()

	lz.Write([]byte(`<?xml version="1.0" encoding="utf-8"?>` + "\n"))
	lz.Write([]byte("<media_info>"))

	for _, p := range pkgs {
		lz.Write([]byte("<info "))
		lz.Write([]byte(fmt.Sprintf("fn='%s' ", p.FileName)))
		lz.Write([]byte(fmt.Sprintf("disttag='%s' ", p.Disttag)))
		lz.Write([]byte(fmt.Sprintf("distepoch='%s' ", p.Distepoch)))
		lz.Write([]byte(fmt.Sprintf("sourcerpm='%s' ", p.Sourcerpm)))
		lz.Write([]byte(fmt.Sprintf("url='%s' ", p.URL)))
		lz.Write([]byte(fmt.Sprintf("license='%s' ", p.License)))
		lz.Write([]byte(">"))
		lz.Write([]byte(p.Description))
		lz.Write([]byte("\n</info>"))
	}

	lz.Write([]byte("</media_info>"))

	f, err = os.Create(dir + "/synthesis.hdlist.cz")
	if err != nil {
		t.Error("Can't ceate info.xml.lzma file:", err)
		t.Fail()
	}
	defer f.Close()

	gz := gzip.NewWriter(f)
	defer gz.Close()
	for _, p := range pkgs {
		gz.Write([]byte(fmt.Sprintf("@summary@%s\n", p.Summary)))
		gz.Write([]byte(fmt.Sprintf("@filesize@%d\n", p.Size)))
		gz.Write([]byte(fmt.Sprintf("@info@%s@0@12345@%s@[email protected]\n", p.FileName, p.Group)))
	}
}
Esempio n. 2
0
func ConvertToBIFL(r io.ReadSeeker, w io.WriteSeeker) error {
	r.Seek(0, os.SEEK_SET)
	w.Seek(0, os.SEEK_SET)
	bif, err := OpenBif(r)

	bif.Header.Signature = [4]byte{'B', 'I', 'F', 'L'}
	bif.Header.Version = [4]byte{'V', '1', '.', '0'}
	err = binary.Write(w, binary.LittleEndian, bif.Header)
	if err != nil {
		return err
	}
	err = binary.Write(w, binary.LittleEndian, bif.VariableEntries)
	if err != nil {
		return err
	}
	err = binary.Write(w, binary.LittleEndian, bif.FixedEntries)
	if err != nil {
		return err
	}
	outOffset := binary.Size(bif.Header)
	dataOffset := uint32(outOffset + binary.Size(bif.VariableEntries) + binary.Size(bif.FixedEntries))
	for _, entry := range bif.VariableEntries {
		dataIn := make([]byte, entry.Size)
		var lzmaOut bytes.Buffer
		out := lzma.NewWriter(&lzmaOut)

		r.Seek(int64(entry.Offset), os.SEEK_SET)
		io.ReadAtLeast(r, dataIn, len(dataIn))
		out.Write(dataIn)
		out.Close()

		w.Seek(int64(outOffset), os.SEEK_SET)
		entry.Offset = dataOffset
		binary.Write(w, binary.LittleEndian, entry)
		w.Seek(int64(dataOffset), os.SEEK_SET)
		compressedSize := uint32(lzmaOut.Len())
		if compressedSize < entry.Size {
			binary.Write(w, binary.LittleEndian, compressedSize)
			lzmaOut.WriteTo(w)
			// Length of compressed data plus 4 bytes for our 32bit int
			dataOffset += (compressedSize + 4)
		} else {
			fmt.Printf("Compressed size is larger: %d > %d\n", compressedSize, entry.Size)
			binary.Write(w, binary.LittleEndian, uint32(0))
			binary.Write(w, binary.LittleEndian, dataIn)
			dataOffset += (entry.Size + 4)
		}
		outOffset += binary.Size(bif.VariableEntries[0])
	}
	for _, entry := range bif.FixedEntries {
		b := make([]byte, entry.Size*entry.Number)
		//out := lzma.NewWriter(&b)

		r.Seek(int64(entry.Offset), os.SEEK_SET)
		io.ReadAtLeast(r, b, len(b))

		w.Seek(int64(outOffset), os.SEEK_SET)
		entry.Offset = dataOffset
		binary.Write(w, binary.LittleEndian, entry)
		w.Seek(int64(dataOffset), os.SEEK_SET)
		w.Write(b)
		outOffset += binary.Size(bif.FixedEntries[0])
		dataOffset += uint32(len(b))
	}
	return nil
}