aboutsummaryrefslogtreecommitdiff
path: root/internal/fusefrontend_reverse/rpath.go
blob: 937795899c86145ce2799a870a1549772ac5e071 (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
package fusefrontend_reverse

import (
	"path/filepath"
	"strings"
)

func (fs *FS) abs(relPath string, err error) (string, error) {
	if err != nil {
		return "", err
	}
	return filepath.Join(fs.args.Cipherdir, relPath), nil
}

const (
	ENCRYPT = iota
	DECRYPT
)

func (fs *FS) encryptPath(relPath string) (string, error) {
	return fs.transformPath(relPath, ENCRYPT)
}

func (fs *FS) decryptPath(relPath string) (string, error) {
	return fs.transformPath(relPath, DECRYPT)
}

func (fs *FS) transformPath(relPath string, direction int) (string, error) {
	if fs.args.PlaintextNames {
		return relPath, nil
	}
	var err error
	var transformedParts []string
	iv := make([]byte, 16)
	parts := strings.Split(relPath, "/")
	for _, part := range parts {
		var transformedPart string
		switch direction {
		case ENCRYPT:
			transformedPart = fs.nameTransform.EncryptName(part, iv)
		case DECRYPT:
			transformedPart, err = fs.nameTransform.DecryptName(part, iv)
			if err != nil {
				return "", err
			}
		default:
			panic("bug: invalid direction value")
		}
		transformedParts = append(transformedParts, transformedPart)
	}
	return filepath.Join(transformedParts...), nil
}

func (fs *FS) isFiltered(relPath string) bool {
	return false
}