From d973e67ea3078887b299f46c9a34f91a04c2a4ce Mon Sep 17 00:00:00 2001 From: royalcat Date: Thu, 20 Jun 2024 11:20:43 +0300 Subject: [PATCH] fix zip reader --- src/vfs/archive.go | 9 +++++++-- src/vfs/archive_test.go | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/vfs/archive.go b/src/vfs/archive.go index 376ad7f..4dfd8f5 100644 --- a/src/vfs/archive.go +++ b/src/vfs/archive.go @@ -239,6 +239,10 @@ func (d *archiveFile) loadMore(ctx context.Context, to int64) error { if err != nil && err != io.EOF { return fmt.Errorf("error copying from archive file reader: %w", err) } + _, err = d.buffer.Seek(d.offset, io.SeekStart) + if err != nil { + return fmt.Errorf("failed to seek to start of the file: %w", err) + } return nil } @@ -252,6 +256,7 @@ func (d *archiveFile) Read(ctx context.Context, p []byte) (n int, err error) { if err != nil && err != io.EOF { return n, fmt.Errorf("failed to read from buffer: %w", err) } + d.offset += int64(n) return n, nil } @@ -292,12 +297,12 @@ func ZipLoader(ctx context.Context, ctxreader ctxio.ReaderAt, size int64) (map[s zr, err := zip.NewReader(reader, size) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create zip reader: %w", err) } rc, err := zr.File[i].Open() if err != nil { - return nil, err + return nil, fmt.Errorf("failed to open file in zip archive: %w", err) } return rc, nil diff --git a/src/vfs/archive_test.go b/src/vfs/archive_test.go index c6c1ee3..117ea49 100644 --- a/src/vfs/archive_test.go +++ b/src/vfs/archive_test.go @@ -4,7 +4,6 @@ import ( "archive/zip" "bytes" "context" - "io" "testing" "git.kmsign.ru/royalcat/tstor/src/vfs" @@ -59,13 +58,24 @@ func TestZipFilesystem(t *testing.T) { require.Equal("1.txt", e.Name()) require.NotNil(e) - out := make([]byte, 11) + out := make([]byte, 5) f, err := zfs.Open(ctx, "/path/to/test/file/1.txt") require.NoError(err) n, err := f.Read(ctx, out) - require.Equal(io.EOF, err) - require.Equal(11, n) - require.Equal(fileContent, out) + require.NoError(err) + require.Equal(5, n) + require.Equal([]byte("Hello"), out) + + outSpace := make([]byte, 1) + n, err = f.Read(ctx, outSpace) + require.NoError(err) + require.Equal(1, n) + require.Equal([]byte(" "), outSpace) + + n, err = f.Read(ctx, out) + require.NoError(err) + require.Equal(5, n) + require.Equal([]byte("World"), out) }