Skip to content

Commit 636698b

Browse files
authored
cmd/fscrypt: fix up path formatting in ErrDirNotEmpty suggestion (#229)
Use %q, in case the paths contain whitespace. Also clean the directory path to remove trailing slashes before appending the ".new" suffix.
1 parent bc9f5e5 commit 636698b

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

cli-tests/t_encrypt.out

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,34 @@ ext4 filesystem "MNT" has 0 protectors and 0 policies
1313
Files cannot be encrypted in-place. Instead, encrypt a new directory, copy the
1414
files into it, and securely delete the original directory. For example:
1515

16-
mkdir MNT/dir.new
17-
fscrypt encrypt MNT/dir.new
18-
cp -a -T MNT/dir MNT/dir.new
19-
find MNT/dir -type f -print0 | xargs -0 shred -n1 --remove=unlink
20-
rm -rf MNT/dir
21-
mv MNT/dir.new MNT/dir
16+
mkdir "MNT/dir.new"
17+
fscrypt encrypt "MNT/dir.new"
18+
cp -a -T "MNT/dir" "MNT/dir.new"
19+
find "MNT/dir" -type f -print0 | xargs -0 shred -n1 --remove=unlink
20+
rm -rf "MNT/dir"
21+
mv "MNT/dir.new" "MNT/dir"
22+
23+
Caution: due to the nature of modern storage devices and filesystems, the
24+
original data may still be recoverable from disk. It's much better to encrypt
25+
your files from the start.
26+
ext4 filesystem "MNT" has 0 protectors and 0 policies
27+
28+
[ERROR] fscrypt status: file or directory "MNT/dir" is not
29+
encrypted
30+
31+
# => with trailing slash
32+
[ERROR] fscrypt encrypt: Directory "MNT/dir/" cannot be
33+
encrypted because it is non-empty.
34+
35+
Files cannot be encrypted in-place. Instead, encrypt a new directory, copy the
36+
files into it, and securely delete the original directory. For example:
37+
38+
mkdir "MNT/dir.new"
39+
fscrypt encrypt "MNT/dir.new"
40+
cp -a -T "MNT/dir" "MNT/dir.new"
41+
find "MNT/dir" -type f -print0 | xargs -0 shred -n1 --remove=unlink
42+
rm -rf "MNT/dir"
43+
mv "MNT/dir.new" "MNT/dir"
2244

2345
Caution: due to the nature of modern storage devices and filesystems, the
2446
original data may still be recoverable from disk. It's much better to encrypt

cli-tests/t_encrypt.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ begin "Try to encrypt a nonempty directory"
3535
touch "$dir/file"
3636
_expect_failure "echo hunter2 | fscrypt encrypt --quiet '$dir'"
3737
show_status false
38+
_print_header "=> with trailing slash"
39+
_expect_failure "echo hunter2 | fscrypt encrypt --quiet '$dir/'"
40+
show_status false
3841

3942
begin "Encrypt a directory as non-root user"
4043
chown "$TEST_USER" "$dir"

cmd/fscrypt/errors.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,18 @@ func getErrorSuggestions(err error) string {
179179
180180
> fscrypt lock %q`, e.DirPath, e.DirPath)
181181
case *ErrDirNotEmpty:
182-
dir := e.DirPath
182+
dir := filepath.Clean(e.DirPath)
183183
newDir := dir + ".new"
184184
return fmt.Sprintf(`Files cannot be encrypted in-place. Instead,
185185
encrypt a new directory, copy the files into it, and securely
186186
delete the original directory. For example:
187187
188-
> mkdir %s
189-
> fscrypt encrypt %s
190-
> cp -a -T %s %s
191-
> find %s -type f -print0 | xargs -0 shred -n1 --remove=unlink
192-
> rm -rf %s
193-
> mv %s %s
188+
> mkdir %q
189+
> fscrypt encrypt %q
190+
> cp -a -T %q %q
191+
> find %q -type f -print0 | xargs -0 shred -n1 --remove=unlink
192+
> rm -rf %q
193+
> mv %q %q
194194
195195
Caution: due to the nature of modern storage devices and filesystems,
196196
the original data may still be recoverable from disk. It's much better

0 commit comments

Comments
 (0)