~ecryptfs/ecryptfs/ecryptfs-utils

346 by Dustin Kirkland
src/utils/ecryptfs-rewrite-file: new script, to rewrite a file,
1
#!/bin/sh -e
2
#
3
#    ecryptfs-rewrite-file
4
#    Copyright (C) 2008 Canonical Ltd.
5
#
6
#    Authors: Dustin Kirkland <kirkland@canonical.com>
7
#
8
#    This program is free software: you can redistribute it and/or modify
9
#    it under the terms of the GNU General Public License as published by
10
#    the Free Software Foundation, version 2 of the License.
11
#
12
#    This program is distributed in the hope that it will be useful,
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
#    GNU General Public License for more details.
16
#
17
#    You should have received a copy of the GNU General Public License
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
20
TEXTDOMAIN="ecryptfs-utils"
21
346 by Dustin Kirkland
src/utils/ecryptfs-rewrite-file: new script, to rewrite a file,
22
error() {
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
23
	echo `gettext "[FAILED]"`
24
	echo `gettext "ERROR:"` "$1" 1>&2
346 by Dustin Kirkland
src/utils/ecryptfs-rewrite-file: new script, to rewrite a file,
25
}
360 by Dustin Kirkland
add a status indicator, as this could take some time
26
j=0
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
27
OKs=0
346 by Dustin Kirkland
src/utils/ecryptfs-rewrite-file: new script, to rewrite a file,
28
for i in "$@"; do
360 by Dustin Kirkland
add a status indicator, as this could take some time
29
	j=`expr $j + 1`
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
30
	echo -n `gettext "INFO:"` `gettext "Rewriting"` "[$j/$#] [$i] ... "
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
31
	if [ ! -e "$i" ] ; then
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
32
		error `gettext "File does not exist"`
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
33
		continue
34
	fi
35
	if [ "$i" = "." ]; then
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
36
		echo `gettext "[EXCLUDED]"` >&2
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
37
		continue
38
	fi
352 by Dustin Kirkland
support rewriting filenames, dirnames, and symlinks (useful for migrating
39
	opt=
360 by Dustin Kirkland
add a status indicator, as this could take some time
40
	if [ -d "$i" -a ! -h "$i" ]; then
41
		# A directory, re-encrypt the filename
42
		temp1=`mktemp -d "$i".XXXXXXXXXX` || {
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
43
			error `gettext "Could not create tempdir"`
360 by Dustin Kirkland
add a status indicator, as this could take some time
44
			continue
45
		}
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
46
		mv -f -T "$i" "$temp1" 2>/dev/null  || {
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
47
			error `gettext "Could not rename"` "[$i] -> [$temp1]"
366 by Dustin Kirkland
ecryptfs-rewrite-file: rm temp dir
48
			rmdir "$temp1"
360 by Dustin Kirkland
add a status indicator, as this could take some time
49
			continue
50
		}
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
51
		mv -f "$temp1" "$i" 2>/dev/null || {
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
52
			error `gettext "Could not rename"` "[$temp1] -> [$i]"
360 by Dustin Kirkland
add a status indicator, as this could take some time
53
		}
54
	else
55
		# A file or symlink, re-encrypt the contents
56
		temp1=`mktemp "$i".XXXXXXXXXX` || {
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
57
			error `gettext "Could not create tempfile"`
360 by Dustin Kirkland
add a status indicator, as this could take some time
58
			continue
59
		}
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
60
		cp -a "$i" "$temp1" 2>/dev/null || {
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
61
			error `gettext "Could not copy"` "[$i] -> [$temp1]"
360 by Dustin Kirkland
add a status indicator, as this could take some time
62
			rm -f "$temp1"
63
			continue
64
		}
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
65
		mv -f "$temp1" "$i" 2>/dev/null || {
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
66
			error `gettext "Could not rename"` "[$temp1] -> [$i]"
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
67
			continue
360 by Dustin Kirkland
add a status indicator, as this could take some time
68
		}
69
	fi
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
70
	echo `gettext "[OK]"`
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
71
	OKs=$((OKs+1))
346 by Dustin Kirkland
src/utils/ecryptfs-rewrite-file: new script, to rewrite a file,
72
done
437 by Dustin Kirkland
* src/utils/ecryptfs-mount-private, src/utils/ecryptfs-rewrite-file,
73
echo "$OKs/$j" `gettext "rewrites succeeded"`
384.1.18 by Michal Hlavinka
ecryptfs-rewrite-file: polish output
74
[ $OKs -ne $j ] && exit 1
346 by Dustin Kirkland
src/utils/ecryptfs-rewrite-file: new script, to rewrite a file,
75
exit 0