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

file_system.go « storage « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18a228a693d2c16de41fc643f37a1185a51efc83 (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
package storage

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"golang.org/x/sys/unix"

	"gitlab.com/gitlab-org/gitlab-pages/internal/client"
)

type fileSystem struct {
	*client.LookupPath
}

func (f *fileSystem) rootPath() string {
	fullPath, err := filepath.EvalSymlinks(filepath.Join(f.DiskPath))
	if err != nil {
		return ""
	}

	return fullPath
}

func (f *fileSystem) resolvePath(path string) (string, error) {
	fullPath := filepath.Join(f.rootPath(), path)
	fullPath, err := filepath.EvalSymlinks(fullPath)
	if err != nil {
		return "", err
	}

	// The requested path resolved to somewhere outside of the root directory
	if !strings.HasPrefix(fullPath, f.rootPath()) {
		return "", fmt.Errorf("%q should be in %q", fullPath, f.rootPath())
	}

	return fullPath, nil
}

func (f *fileSystem) Resolve(path string) (string, error) {
	fullPath, err := f.resolvePath(path)
	if err != nil {
		return "", err
	}

	return fullPath[len(f.rootPath()):], nil
}

func (f *fileSystem) Stat(path string) (os.FileInfo, error) {
	fullPath, err := f.resolvePath(path)
	if err != nil {
		return nil, err
	}

	return os.Lstat(fullPath)
}

func (f *fileSystem) Open(path string) (File, os.FileInfo, error) {
	fullPath, err := f.resolvePath(path)
	if err != nil {
		return nil, nil, err
	}

	file, err := os.OpenFile(fullPath, os.O_RDONLY|unix.O_NOFOLLOW, 0)
	if err != nil {
		return nil, nil, err
	}

	fileInfo, err := file.Stat()
	if err != nil {
		file.Close()
		return nil, nil, err
	}

	return file, fileInfo, err
}

func (f *fileSystem) Close() {
}