code.vegaprotocol.io/vega@v0.79.0/datanode/networkhistory/fsutil/fsutil.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     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 Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package fsutil
    17  
    18  import (
    19  	"archive/zip"
    20  	"fmt"
    21  	"io"
    22  	"path/filepath"
    23  )
    24  
    25  type readerAtWrapper struct {
    26  	r io.ReadSeeker
    27  }
    28  
    29  func (rw *readerAtWrapper) ReadAt(p []byte, off int64) (n int, err error) {
    30  	// Seek to the requested offset using the underlying io.Reader and io.Seeker
    31  	_, err = rw.r.Seek(off, io.SeekStart)
    32  	if err != nil {
    33  		return 0, err
    34  	}
    35  	return rw.r.Read(p)
    36  }
    37  
    38  // ReadNetworkHistorySegmentData takes a io.Reader reading from a network history segment .tar archive then
    39  //   - looks inside the .tar archive for historysnapshot.tar.gz
    40  //   - looks looks historysnapshot.tar.gz for a file called `historyFileName`
    41  //   - returns an io.Reader for reading that file
    42  func ReadNetworkHistorySegmentData(file io.ReadSeekCloser, size int64, historyFileName string) (io.Reader, error) {
    43  	w := &readerAtWrapper{file}
    44  	zipReader, err := zip.NewReader(w, size)
    45  	if err != nil {
    46  		return nil, fmt.Errorf("error opening zip file reader for history segment:%w", err)
    47  	}
    48  
    49  	for _, zipEntry := range zipReader.File {
    50  		if filepath.Base(zipEntry.Name) == historyFileName {
    51  			reader, err := zipEntry.Open()
    52  			if err != nil {
    53  				return nil, fmt.Errorf("error opening table csv file inside zip segment:%w", err)
    54  			}
    55  			return reader, nil
    56  		}
    57  	}
    58  
    59  	return nil, fmt.Errorf("table file '%s' not found in segment", historyFileName)
    60  }