/* glocate -- ZFS-diff-friendly locate-like utility Copyright (C) 2022-2023 Sergey Matveev 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 . */ package main import ( "bufio" "encoding/binary" "io" "log" "github.com/klauspost/compress/zstd" ) func mustWrite(w io.Writer, buf []byte) { if _, err := w.Write(buf); err != nil { log.Fatalln(err) } } 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 { name = ent.name[len(ent.name)-1] if len(ent.name) >= 1<<16 { panic("too long") } binary.BigEndian.PutUint16(num[:2], uint16(len(name))) mustWrite(bw, num[:2]) mustWrite(bw, []byte(name)) if len(ent.name) >= 1<<8 { panic("too deep") } mustWrite(bw, []byte{byte(len(ent.name) - 1)}) binary.BigEndian.PutUint64(num, uint64(ent.mtime)) mustWrite(bw, num) binary.BigEndian.PutUint64(num, uint64(ent.size)) mustWrite(bw, num) } if err = bw.Flush(); err != nil { log.Fatalln(err) } if err = comp.Close(); err != nil { log.Fatalln(err) } }