2016-08-25 00:14:36 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
2016-08-30 00:23:55 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2016-08-25 00:14:36 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-08-30 00:23:55 +02:00
|
|
|
"syscall"
|
2016-08-25 00:14:36 +02:00
|
|
|
)
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
var zeroDirIV []byte
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
zeroDirIV = make([]byte, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rfs *reverseFS) abs(relPath string, err error) (string, error) {
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
return filepath.Join(rfs.args.Cipherdir, relPath), nil
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ENCRYPT = iota
|
|
|
|
DECRYPT
|
|
|
|
)
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) encryptPath(relPath string) (string, error) {
|
|
|
|
return rfs.transformPath(relPath, ENCRYPT)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) decryptPath(relPath string) (string, error) {
|
|
|
|
return rfs.transformPath(relPath, DECRYPT)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) transformPath(relPath string, direction int) (string, error) {
|
|
|
|
if rfs.args.PlaintextNames || relPath == "" {
|
2016-08-25 00:14:36 +02:00
|
|
|
return relPath, nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var transformedParts []string
|
|
|
|
parts := strings.Split(relPath, "/")
|
|
|
|
for _, part := range parts {
|
|
|
|
var transformedPart string
|
|
|
|
switch direction {
|
|
|
|
case ENCRYPT:
|
2016-08-30 00:23:55 +02:00
|
|
|
transformedPart = rfs.nameTransform.EncryptName(part, zeroDirIV)
|
2016-08-25 00:14:36 +02:00
|
|
|
case DECRYPT:
|
2016-08-30 00:23:55 +02:00
|
|
|
transformedPart, err = rfs.nameTransform.DecryptName(part, zeroDirIV)
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
2016-08-30 00:23:55 +02:00
|
|
|
// We get lots of decrypt requests for names like ".Trash" that
|
|
|
|
// are invalid base64. Convert them to ENOENT so the correct
|
|
|
|
// error gets returned to the user.
|
|
|
|
if _, ok := err.(base64.CorruptInputError); ok {
|
|
|
|
fmt.Printf("converting to ENOENT\n")
|
|
|
|
return "", syscall.ENOENT
|
|
|
|
}
|
2016-08-25 00:14:36 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("bug: invalid direction value")
|
|
|
|
}
|
|
|
|
transformedParts = append(transformedParts, transformedPart)
|
|
|
|
}
|
|
|
|
return filepath.Join(transformedParts...), nil
|
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) isFiltered(relPath string) bool {
|
2016-08-25 00:14:36 +02:00
|
|
|
return false
|
|
|
|
}
|