2023-09-28 15:09:45 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-01-06 02:39:05 +00:00
|
|
|
"compress/flate"
|
|
|
|
"compress/gzip"
|
|
|
|
"compress/lzw"
|
|
|
|
"compress/zlib"
|
2023-09-28 15:09:45 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2024-01-06 02:39:05 +00:00
|
|
|
"io"
|
2023-09-28 15:09:45 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-01-06 03:30:08 +00:00
|
|
|
"github.com/golang/snappy"
|
2023-09-28 15:09:45 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
2024-01-06 03:48:40 +00:00
|
|
|
"github.com/pierrec/lz4/v4"
|
2023-09-28 15:09:45 +00:00
|
|
|
"seedno.de/seednode/roulette/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fileIndex struct {
|
|
|
|
mutex *sync.RWMutex
|
|
|
|
list []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) List() []string {
|
|
|
|
index.mutex.RLock()
|
|
|
|
val := make([]string, len(index.list))
|
|
|
|
copy(val, index.list)
|
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) remove(path string) {
|
|
|
|
index.mutex.RLock()
|
|
|
|
tempIndex := make([]string, len(index.list))
|
|
|
|
copy(tempIndex, index.list)
|
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
|
|
|
var position int
|
|
|
|
|
|
|
|
for k, v := range tempIndex {
|
|
|
|
if path == v {
|
|
|
|
position = k
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tempIndex[position] = tempIndex[len(tempIndex)-1]
|
|
|
|
|
|
|
|
index.mutex.Lock()
|
|
|
|
index.list = make([]string, len(tempIndex)-1)
|
|
|
|
copy(index.list, tempIndex[:len(tempIndex)-1])
|
|
|
|
index.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) set(val []string) {
|
|
|
|
length := len(val)
|
|
|
|
|
|
|
|
if length < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index.mutex.Lock()
|
|
|
|
index.list = make([]string, length)
|
|
|
|
copy(index.list, val)
|
|
|
|
index.mutex.Unlock()
|
|
|
|
|
|
|
|
if Index && IndexFile != "" {
|
|
|
|
index.Export(IndexFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) clear() {
|
|
|
|
index.mutex.Lock()
|
|
|
|
index.list = nil
|
|
|
|
index.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) isEmpty() bool {
|
|
|
|
index.mutex.RLock()
|
|
|
|
length := len(index.list)
|
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
|
|
|
return length == 0
|
|
|
|
}
|
|
|
|
|
2024-01-06 03:30:08 +00:00
|
|
|
func getReader(format string, file io.Reader) (io.Reader, error) {
|
2024-01-06 02:39:05 +00:00
|
|
|
switch format {
|
|
|
|
case "flate":
|
|
|
|
return flate.NewReader(file), nil
|
|
|
|
case "gzip":
|
|
|
|
return gzip.NewReader(file)
|
2024-01-06 03:30:08 +00:00
|
|
|
case "lz4":
|
|
|
|
return lz4.NewReader(file), nil
|
2024-01-06 02:39:05 +00:00
|
|
|
case "lzw":
|
|
|
|
return lzw.NewReader(file, lzw.LSB, 8), nil
|
|
|
|
case "none":
|
|
|
|
return io.NopCloser(file), nil
|
2024-01-06 03:30:08 +00:00
|
|
|
case "snappy":
|
|
|
|
return snappy.NewReader(file), nil
|
2024-01-06 02:39:05 +00:00
|
|
|
case "zlib":
|
|
|
|
return zlib.NewReader(file)
|
|
|
|
case "zstd":
|
|
|
|
decoder, err := zstd.NewReader(file)
|
|
|
|
|
|
|
|
return decoder.IOReadCloser(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return io.NopCloser(file), ErrInvalidCompression
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWriter(format string, file io.WriteCloser) (io.WriteCloser, error) {
|
2024-01-06 03:42:22 +00:00
|
|
|
switch {
|
|
|
|
case format == "flate" && CompressionFast:
|
|
|
|
return flate.NewWriter(file, flate.BestCompression)
|
|
|
|
case format == "flate":
|
|
|
|
return flate.NewWriter(file, flate.BestSpeed)
|
|
|
|
case format == "gzip" && CompressionFast:
|
|
|
|
return gzip.NewWriterLevel(file, gzip.BestSpeed)
|
|
|
|
case format == "gzip":
|
2024-01-06 03:30:08 +00:00
|
|
|
return gzip.NewWriterLevel(file, gzip.BestCompression)
|
2024-01-06 03:42:22 +00:00
|
|
|
case format == "lz4" && CompressionFast:
|
|
|
|
encoder := lz4.NewWriter(file)
|
|
|
|
|
|
|
|
err := encoder.Apply(lz4.CompressionLevelOption(lz4.Fast))
|
|
|
|
if err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoder, nil
|
|
|
|
case format == "lz4":
|
2024-01-06 03:30:08 +00:00
|
|
|
encoder := lz4.NewWriter(file)
|
|
|
|
|
|
|
|
err := encoder.Apply(lz4.CompressionLevelOption(lz4.Level9))
|
|
|
|
if err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoder, nil
|
2024-01-06 03:42:22 +00:00
|
|
|
case format == "lzw":
|
2024-01-06 02:39:05 +00:00
|
|
|
return lzw.NewWriter(file, lzw.LSB, 8), nil
|
2024-01-06 03:42:22 +00:00
|
|
|
case format == "none":
|
2024-01-06 02:39:05 +00:00
|
|
|
return file, nil
|
2024-01-06 03:42:22 +00:00
|
|
|
case format == "snappy":
|
2024-01-06 03:30:08 +00:00
|
|
|
return snappy.NewBufferedWriter(file), nil
|
2024-01-06 03:42:22 +00:00
|
|
|
case format == "zlib" && CompressionFast:
|
|
|
|
return zlib.NewWriterLevel(file, zlib.BestSpeed)
|
|
|
|
case format == "zlib":
|
|
|
|
return zlib.NewWriterLevel(file, zlib.BestCompression)
|
|
|
|
case format == "zstd" && CompressionFast:
|
|
|
|
return zstd.NewWriter(file, zstd.WithEncoderLevel(zstd.SpeedFastest))
|
|
|
|
case format == "zstd":
|
|
|
|
return zstd.NewWriter(file, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
|
2024-01-06 02:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return file, ErrInvalidCompression
|
|
|
|
}
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
func (index *fileIndex) Export(path string) error {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-01-06 02:39:05 +00:00
|
|
|
encoder, err := getWriter(Compression, file)
|
2023-09-28 15:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 02:39:05 +00:00
|
|
|
defer encoder.Close()
|
2023-09-28 15:09:45 +00:00
|
|
|
|
2024-01-06 02:39:05 +00:00
|
|
|
enc := gob.NewEncoder(encoder)
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
index.mutex.RLock()
|
2023-10-04 20:47:43 +00:00
|
|
|
err = enc.Encode(&index.list)
|
|
|
|
if err != nil {
|
2024-01-04 18:43:26 +00:00
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
2023-10-04 20:47:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-28 15:09:45 +00:00
|
|
|
length := len(index.list)
|
|
|
|
index.mutex.RUnlock()
|
|
|
|
|
2024-01-04 18:43:26 +00:00
|
|
|
// Close encoder prior to checking file size,
|
|
|
|
// to ensure the correct value is returned.
|
2024-01-06 02:39:05 +00:00
|
|
|
// If no compression is used, skip this step,
|
|
|
|
// as the encoder is just the file itself.
|
|
|
|
if Compression != "none" {
|
|
|
|
encoder.Close()
|
|
|
|
}
|
2024-01-04 18:43:26 +00:00
|
|
|
|
2024-01-04 14:43:52 +00:00
|
|
|
stats, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
if Verbose {
|
2024-01-04 14:43:52 +00:00
|
|
|
fmt.Printf("%s | INDEX: Exported %d entries to %s (%s) in %s\n",
|
2023-09-28 15:09:45 +00:00
|
|
|
time.Now().Format(logDate),
|
|
|
|
length,
|
|
|
|
path,
|
2024-01-04 14:43:52 +00:00
|
|
|
humanReadableSize(int(stats.Size())),
|
2023-09-28 15:09:45 +00:00
|
|
|
time.Since(startTime),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *fileIndex) Import(path string) error {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
file, err := os.OpenFile(path, os.O_RDONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-01-04 14:43:52 +00:00
|
|
|
stats, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:39:05 +00:00
|
|
|
reader, err := getReader(Compression, file)
|
2023-09-28 15:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-06 02:39:05 +00:00
|
|
|
dec := gob.NewDecoder(reader)
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
index.mutex.Lock()
|
|
|
|
err = dec.Decode(&index.list)
|
|
|
|
if err != nil {
|
2024-01-04 18:43:26 +00:00
|
|
|
index.mutex.Unlock()
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-04 20:47:43 +00:00
|
|
|
length := len(index.list)
|
|
|
|
index.mutex.Unlock()
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
if Verbose {
|
2024-01-04 14:43:52 +00:00
|
|
|
fmt.Printf("%s | INDEX: Imported %d entries from %s (%s) in %s\n",
|
2023-09-28 15:09:45 +00:00
|
|
|
time.Now().Format(logDate),
|
|
|
|
length,
|
|
|
|
path,
|
2024-01-04 14:43:52 +00:00
|
|
|
humanReadableSize(int(stats.Size())),
|
2023-09-28 15:09:45 +00:00
|
|
|
time.Since(startTime),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
2023-09-28 15:09:45 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
index.clear()
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
fileList(args, &filters{}, "", index, formats, errorChannel)
|
2023-09-28 15:09:45 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
|
2023-12-17 11:28:24 +00:00
|
|
|
w.Write([]byte("Ok\n"))
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
func importIndex(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) {
|
2023-09-28 15:09:45 +00:00
|
|
|
if IndexFile != "" {
|
|
|
|
err := index.Import(IndexFile)
|
2023-10-20 22:18:35 +00:00
|
|
|
if err == nil {
|
2024-01-06 15:41:30 +00:00
|
|
|
return
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
fileList(args, &filters{}, "", index, formats, errorChannel)
|
2023-09-28 15:09:45 +00:00
|
|
|
}
|