Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Reuse code to determine stream size by seeking
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Nov 8, 2024
1 parent e310539 commit 76ea0d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
20 changes: 20 additions & 0 deletions archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@ func isSymlink(info fs.FileInfo) bool {
return info.Mode()&os.ModeSymlink != 0
}

// streamSizeBySeeking determines the size of the stream by
// seeking to the end, then back again, so the resulting
// seek position upon returning is the same as when called
// (assuming no errors).
func streamSizeBySeeking(s io.Seeker) (int64, error) {
currentPosition, err := s.Seek(0, io.SeekCurrent)
if err != nil {
return 0, fmt.Errorf("getting current offset: %w", err)
}
maxPosition, err := s.Seek(0, io.SeekEnd)
if err != nil {
return 0, fmt.Errorf("fast-forwarding to end: %w", err)
}
_, err = s.Seek(currentPosition, io.SeekStart)
if err != nil {
return 0, fmt.Errorf("returning to prior offset %d: %w", currentPosition, err)
}
return maxPosition, nil
}

// skipList keeps a list of non-intersecting paths
// as long as its add method is used. Identical
// elements are rejected, more specific paths are
Expand Down
6 changes: 1 addition & 5 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,10 @@ func FileSystem(ctx context.Context, filename string, stream ReaderAtSeeker) (fs

// determine size -- we know that the stream value we get back from
// Identify is the same type as what we input because it is a Seeker
size, err := stream.Seek(0, io.SeekEnd)
size, err := streamSizeBySeeking(stream)
if err != nil {
return nil, fmt.Errorf("seeking for size: %w", err)
}
_, err = stream.Seek(0, io.SeekStart)
if err != nil {
return nil, fmt.Errorf("seeking back to beginning: %w", err)
}

sr := io.NewSectionReader(stream, 0, size)

Expand Down
16 changes: 0 additions & 16 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,6 @@ type seekReaderAt interface {
io.Seeker
}

func streamSizeBySeeking(s io.Seeker) (int64, error) {
currentPosition, err := s.Seek(0, io.SeekCurrent)
if err != nil {
return 0, fmt.Errorf("getting current offset: %w", err)
}
maxPosition, err := s.Seek(0, io.SeekEnd)
if err != nil {
return 0, fmt.Errorf("fast-forwarding to end: %w", err)
}
_, err = s.Seek(currentPosition, io.SeekStart)
if err != nil {
return 0, fmt.Errorf("returning to prior offset %d: %w", currentPosition, err)
}
return maxPosition, nil
}

// Additional compression methods not offered by archive/zip.
// See https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.4.5.
const (
Expand Down

0 comments on commit 76ea0d6

Please sign in to comment.