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