~welt/murse

ref: v0.2.1 murse/files.go -rw-r--r-- 833 bytes
f321b7aawelt Bump version. 4 months ago
                                                                                
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
package main

import (
	"errors"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

var ErrEscapesPath = errors.New("the path supplied would escape the install directory")

func getInstalledRevision(path string) (int, error) {
	rev := -1
	bytes, err := os.ReadFile(filepath.Join(filepath.FromSlash(path), ".revision"))
	if os.IsNotExist(err) {
		return rev, nil
	}
	if err != nil {
		return rev, err
	}

	return strconv.Atoi(strings.TrimSpace(string(bytes)))
}

func resolvePath(installpath, changepath string) (string, error) {
	absinst, err := filepath.Abs(installpath)
	if err != nil {
		return "", err
	}

	absfinal, err := filepath.Abs(filepath.Join(absinst, filepath.FromSlash(changepath)))
	if err != nil {
		return "", err
	}

	if !strings.HasPrefix(absfinal, absinst) {
		return "", ErrEscapesPath
	}

	return absfinal, nil

}