]> Sergey Matveev's repositories - glocate.git/blob - writer.go
Raise copyright years
[glocate.git] / writer.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 mustWrite(w io.Writer, buf []byte) {
30         if _, err := w.Write(buf); err != nil {
31                 log.Fatalln(err)
32         }
33 }
34
35 func writer(dst io.Writer, sink chan Ent) {
36         comp, err := zstd.NewWriter(dst)
37         if err != nil {
38                 log.Fatalln(err)
39         }
40         bw := bufio.NewWriterSize(comp, 1<<17)
41         num := make([]byte, 8)
42         var name string
43         for ent := range sink {
44                 name = ent.name[len(ent.name)-1]
45                 if len(ent.name) >= 1<<16 {
46                         panic("too long")
47                 }
48                 binary.BigEndian.PutUint16(num[:2], uint16(len(name)))
49                 mustWrite(bw, num[:2])
50                 mustWrite(bw, []byte(name))
51                 if len(ent.name) >= 1<<8 {
52                         panic("too deep")
53                 }
54                 mustWrite(bw, []byte{byte(len(ent.name) - 1)})
55                 binary.BigEndian.PutUint64(num, uint64(ent.mtime))
56                 mustWrite(bw, num)
57                 binary.BigEndian.PutUint64(num, uint64(ent.size))
58                 mustWrite(bw, num)
59         }
60         if err = bw.Flush(); err != nil {
61                 log.Fatalln(err)
62         }
63         if err = comp.Close(); err != nil {
64                 log.Fatalln(err)
65         }
66 }