]> Sergey Matveev's repositories - glocate.git/blobdiff - reader.go
Newer ZFS has different octalize rule
[glocate.git] / reader.go
index 269fc3295dac975ee17b8a4007c9995da5813562f989a1ff4ce3f92366daa7eb..03b950705b4969cc6a88caecb42bf5b153811fd118244fd26719af288170347d 100644 (file)
--- a/reader.go
+++ b/reader.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 mustReadFull(r io.Reader, buf []byte) {
@@ -12,15 +32,20 @@ func mustReadFull(r io.Reader, buf []byte) {
        }
 }
 
-func reader(r io.Reader, sink chan Ent) {
-       var err error
+func reader(src io.Reader, sink chan Ent) {
+       comp, err := zstd.NewReader(src)
+       if err != nil {
+               log.Fatalln(err)
+       }
+       br := bufio.NewReaderSize(comp, 1<<17)
+
        num := make([]byte, 8)
        var cols []string
        var namePrev string
        var nameLen uint16
        var depth, depthPrev uint8
        for {
-               _, err = io.ReadFull(r, num[:2])
+               _, err = io.ReadFull(br, num[:2])
                if err != nil {
                        if err == io.EOF {
                                break
@@ -29,13 +54,13 @@ func reader(r io.Reader, sink chan Ent) {
                }
                nameLen = binary.BigEndian.Uint16(num[:2])
                nameRaw := make([]byte, nameLen)
-               mustReadFull(r, nameRaw)
+               mustReadFull(br, nameRaw)
                name := string(nameRaw)
-               mustReadFull(r, num[:1])
+               mustReadFull(br, num[:1])
                depth = uint8(num[0])
-               mustReadFull(r, num)
+               mustReadFull(br, num)
                ent := Ent{mtime: int64(binary.BigEndian.Uint64(num))}
-               mustReadFull(r, num)
+               mustReadFull(br, num)
                ent.size = int64(binary.BigEndian.Uint64(num))
                if depth > depthPrev {
                        cols = append(cols, namePrev[:len(namePrev)-1])
@@ -48,4 +73,5 @@ func reader(r io.Reader, sink chan Ent) {
                depthPrev = depth
        }
        close(sink)
+       comp.Close()
 }