Welcome to mirror list, hosted at ThFree Co, Russian Federation.

archive.go « zip « vfs « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1d64e148464ca4228a8335b246ab43e7f4cec7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package zip

import (
	"archive/zip"
	"context"
	"errors"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
	"sync"

	log "github.com/sirupsen/logrus"

	"gitlab.com/gitlab-org/gitlab-pages/internal/httprange"
	"gitlab.com/gitlab-org/gitlab-pages/internal/vfs"
)

const (
	dirPrefix      = "public/"
	maxSymlinkSize = 256
)

var (
	errNotSymlink  = errors.New("not a symlink")
	errSymlinkSize = errors.New("symlink too long")
)

// zipArchive implements the vfs.Root interface.
// It represents a zip archive saving all its files int memory.
// It holds an httprange.Resource that can be read with httprange.RangedReader in chunks.
type zipArchive struct {
	path string
	once sync.Once
	done chan struct{}

	resource *httprange.Resource
	reader   *httprange.RangedReader
	archive  *zip.Reader
	err      error

	files map[string]*zip.File
}

func newArchive(path string) *zipArchive {
	return &zipArchive{
		path:  path,
		done:  make(chan struct{}),
		files: make(map[string]*zip.File),
	}
}

func (a *zipArchive) openArchive(ctx context.Context) error {
	a.once.Do(func() {
		a.readArchive(ctx)
	})

	// wait for readArchive to be done or return when the context is canceled
	select {
	case <-a.done:
		return a.err
	case <-ctx.Done():
		err := ctx.Err()
		log.WithError(err).Traceln("open zip archive timed out")

		return err
	}
}

func (a *zipArchive) readArchive(ctx context.Context) {
	defer close(a.done)

	a.resource, a.err = httprange.NewResource(ctx, a.path)
	if a.err != nil {
		return
	}

	// load all archive files into memory using a cached ranged reader
	a.reader = httprange.NewRangedReader(a.resource)
	a.reader.WithCachedReader(func() {
		a.archive, a.err = zip.NewReader(a.reader, a.resource.Size)
	})

	if a.archive != nil {
		for _, file := range a.archive.File {
			if !strings.HasPrefix(file.Name, dirPrefix) {
				continue
			}
			a.files[file.Name] = file
		}

		// recycle memory
		a.archive.File = nil
	}
}

func (a *zipArchive) findFile(name string) *zip.File {
	name = filepath.Join(dirPrefix, name)

	if file := a.files[name]; file != nil {
		return file
	}

	if dir := a.files[name+"/"]; dir != nil {
		return dir
	}

	return nil
}

// Open finds the file by name inside the zipArchive and returns a reader that can be served by the VFS
func (a *zipArchive) Open(ctx context.Context, name string) (vfs.File, error) {
	file := a.findFile(name)
	if file == nil {
		return nil, os.ErrNotExist
	}

	dataOffset, err := file.DataOffset()
	if err != nil {
		return nil, err
	}

	// only read from dataOffset up to the size of the compressed file
	reader := a.reader.SectionReader(dataOffset, int64(file.CompressedSize64))

	switch file.Method {
	case zip.Deflate:
		return newDeflateReader(reader), nil
	case zip.Store:
		return reader, nil
	default:
		return nil, fmt.Errorf("unsupported compression method: %x", file.Method)
	}
}

// Lstat finds the file by name inside the zipArchive and returns its FileInfo
func (a *zipArchive) Lstat(ctx context.Context, name string) (os.FileInfo, error) {
	file := a.findFile(name)
	if file == nil {
		return nil, os.ErrNotExist
	}

	return file.FileInfo(), nil
}

// ReadLink finds the file by name inside the zipArchive and returns the contents of the symlink
func (a *zipArchive) Readlink(ctx context.Context, name string) (string, error) {
	file := a.findFile(name)
	if file == nil {
		return "", os.ErrNotExist
	}

	if file.FileInfo().Mode()&os.ModeSymlink != os.ModeSymlink {
		return "", errNotSymlink
	}

	rc, err := file.Open()
	if err != nil {
		return "", err
	}
	defer rc.Close()

	symlink := make([]byte, maxSymlinkSize+1)

	// read up to len(symlink) bytes from the link file
	n, err := io.ReadFull(rc, symlink)
	if err != nil && err != io.ErrUnexpectedEOF {
		// if err == io.ErrUnexpectedEOF the link is smaller than len(symlink) so it's OK to not return it
		return "", err
	}

	// return errSymlinkSize if the number of bytes read from the link is too big
	if n > maxSymlinkSize {
		return "", errSymlinkSize
	}

	// only return the n bytes read from the link
	return string(symlink[:n]), nil
}

// close no-op: everything can be recycled by the GC
func (a *zipArchive) close() {}