]> Sergey Matveev's repositories - glocate.git/blobdiff - writer.go
Raise copyright years
[glocate.git] / writer.go
index e12a19c0d7f2103ae9c88e4c4f3c237bb3d03f62819ea98eb33c93210f221256..7c0921bc81d8fad9c0b3659a5bcb07e0ba6c10fbf164d82321708bf8975dcd15 100644 (file)
--- a/writer.go
+++ b/writer.go
@@ -1,9 +1,29 @@
+/*
+glocate -- ZFS-diff-friendly locate-like utility
+Copyright (C) 2022-2023 Sergey Matveev <stargrave@stargrave.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 3 of the License.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 package main
 
 import (
+       "bufio"
        "encoding/binary"
        "io"
        "log"
+
+       "github.com/klauspost/compress/zstd"
 )
 
 func mustWrite(w io.Writer, buf []byte) {
@@ -12,7 +32,12 @@ func mustWrite(w io.Writer, buf []byte) {
        }
 }
 
-func writer(w io.Writer, sink chan Ent) {
+func writer(dst io.Writer, sink chan Ent) {
+       comp, err := zstd.NewWriter(dst)
+       if err != nil {
+               log.Fatalln(err)
+       }
+       bw := bufio.NewWriterSize(comp, 1<<17)
        num := make([]byte, 8)
        var name string
        for ent := range sink {
@@ -21,15 +46,21 @@ func writer(w io.Writer, sink chan Ent) {
                        panic("too long")
                }
                binary.BigEndian.PutUint16(num[:2], uint16(len(name)))
-               mustWrite(w, num[:2])
-               mustWrite(w, []byte(name))
+               mustWrite(bw, num[:2])
+               mustWrite(bw, []byte(name))
                if len(ent.name) >= 1<<8 {
                        panic("too deep")
                }
-               mustWrite(w, []byte{byte(len(ent.name) - 1)})
+               mustWrite(bw, []byte{byte(len(ent.name) - 1)})
                binary.BigEndian.PutUint64(num, uint64(ent.mtime))
-               mustWrite(w, num)
+               mustWrite(bw, num)
                binary.BigEndian.PutUint64(num, uint64(ent.size))
-               mustWrite(w, num)
+               mustWrite(bw, num)
+       }
+       if err = bw.Flush(); err != nil {
+               log.Fatalln(err)
+       }
+       if err = comp.Close(); err != nil {
+               log.Fatalln(err)
        }
 }