]> Sergey Matveev's repositories - glocate.git/blob - reader.go
Unify copyright comment format
[glocate.git] / reader.go
1 // glocate -- ZFS-diff-friendly locate-like utility
2 // Copyright (C) 2022-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "bufio"
20         "encoding/binary"
21         "io"
22         "log"
23
24         "github.com/klauspost/compress/zstd"
25 )
26
27 func mustReadFull(r io.Reader, buf []byte) {
28         if _, err := io.ReadFull(r, buf); err != nil {
29                 log.Fatalln(err)
30         }
31 }
32
33 func reader(src io.Reader, sink chan Ent) {
34         comp, err := zstd.NewReader(src)
35         if err != nil {
36                 log.Fatalln(err)
37         }
38         br := bufio.NewReaderSize(comp, 1<<17)
39
40         num := make([]byte, 8)
41         var cols []string
42         var namePrev string
43         var nameLen uint16
44         var depth, depthPrev uint8
45         for {
46                 _, err = io.ReadFull(br, num[:2])
47                 if err != nil {
48                         if err == io.EOF {
49                                 break
50                         }
51                         log.Fatalln(err)
52                 }
53                 nameLen = binary.BigEndian.Uint16(num[:2])
54                 nameRaw := make([]byte, nameLen)
55                 mustReadFull(br, nameRaw)
56                 name := string(nameRaw)
57                 mustReadFull(br, num[:1])
58                 depth = uint8(num[0])
59                 mustReadFull(br, num)
60                 ent := Ent{mtime: int64(binary.BigEndian.Uint64(num))}
61                 mustReadFull(br, num)
62                 ent.size = int64(binary.BigEndian.Uint64(num))
63                 if depth > depthPrev {
64                         cols = append(cols, namePrev[:len(namePrev)-1])
65                 } else if depth < depthPrev {
66                         cols = cols[:len(cols)-int(depthPrev-depth)]
67                 }
68                 ent.name = append([]string{}, append(cols, name)...)
69                 sink <- ent
70                 namePrev = name
71                 depthPrev = depth
72         }
73         close(sink)
74         comp.Close()
75 }