Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #7 from aesiris/master

avoid temp files
  • Loading branch information...
commit 32c9a49d6f8cd9406bd5f1dfbb5b118329ad1f6d 2 parents 7edb977 + f590ad6
graysky authored December 10, 2012

Showing 1 changed file with 30 additions and 45 deletions. Show diff stats Hide diff stats

  1. 75  profile-cleaner
75  profile-cleaner
@@ -7,8 +7,6 @@ if [[ -z $(which parallel) ]]; then
7 7
 	exit 1
8 8
 fi
9 9
 
10  
-find /tmp -name 'work2do.*' -o -name 'accounting.*' -o -name "totalwork2do" 2>/dev/null | SHELL=/bin/bash parallel rm -f
11  
-
12 10
 echo -e "${RED}profile-cleaner v$VERS${NRM}"
13 11
 echo
14 12
 
@@ -21,30 +19,41 @@ XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
21 19
 
22 20
 do_clean() {
23 21
 	echo -en "${GRN} Cleaning${NRM} ${1##*/}"
24  
-	bsize=$(ls -l "$1" | gawk {'print $5'})
  22
+	bsize=$(du -b "$1" | cut -f 1)
  23
+	
25 24
 	sqlite3 "$1" vacuum
26 25
 	sqlite3 "$1" reindex
27  
-	asize=$(ls -l "$1" | gawk {'print $5'})
  26
+	
  27
+	asize=$(du -b "$1" | cut -f 1)
28 28
 	dsize=$(echo "scale=2; ($bsize-$asize)/1048576" | bc)
29 29
 	echo -e "$(tput cr)$(tput cuf 46) ${GRN}done${NRM}  -${YLW}${dsize}${NRM} Mbytes"
30  
-	echo "$name,$dsize" >> /tmp/accounting.$name
  30
+}
  31
+
  32
+do_clean_parallel () {
  33
+	bsize=$(du -b -c "${toclean[@]}" | tail -n 1 | cut -f 1)
  34
+	
  35
+	SHELL=/bin/bash parallel -k do_clean ::: "${toclean[@]}" 2>/dev/null
  36
+	
  37
+	asize=$(du -b -c "${toclean[@]}" | tail -n 1 | cut -f 1)
  38
+	cleanedsize=$(echo "scale=2; ($bsize-$asize)/1048576" | bc)
31 39
 }
32 40
 
33 41
 find_dbs() {
34  
-	find "$profilepath" -maxdepth 2 -type f | while read i; do 
35  
-	[[ -n $(file "$i" | grep SQLite) ]] && echo "${i}" >> /tmp/work2do.$name
36  
-done
  42
+	# fill toclean array by looking for SQLite files in each input directory
  43
+	toclean=()
  44
+	while read i; do
  45
+		toclean+=("${i}")
  46
+	done < <( find -L "$@" -maxdepth 2 -type f -print0 2>/dev/null | xargs -0 file -e ascii | sed -n -e "s/:.*SQLite.*//p" )
37 47
 }
38 48
 
39 49
 do_chromebased() {
40 50
 	[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
41 51
 	[[ ! -d "$profilepath" ]] &&	echo -e ${RED}"Error: no profile directory for $name found."${NRM} &&	exit 1
42 52
 	echo -e " ${YLW}Cleaning profile for $name${NRM}"
43  
-	find_dbs
44  
-	cat /tmp/work2do.$name | SHELL=/bin/bash parallel -k do_clean 2>/dev/null
45  
-	echo -e " ${BLD}Profile for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
46  
-	[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
47  
-	[[ -f /tmp/work2do.$name ]] && rm -f /tmp/work2do.$name
  53
+	find_dbs "$profilepath"
  54
+	do_clean_parallel
  55
+	echo
  56
+	echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}${cleanedsize}${NRM} ${BLD}Mbytes.${NRM}"
48 57
 }
49 58
 
50 59
 do_xulbased() {
@@ -63,13 +72,6 @@ do_xulbased() {
63 72
 	[[ ! -f $profilepath/profiles.ini ]] && echo -e ${RED}"Error: cannot locate $profilepath/profiles.ini to determine names of profiles for $name."${NRM} &&	exit 1
64 73
 
65 74
 	#
66  
-	# read each profile into a temp file for processing
67  
-	#
68  
-
69  
-	profiles_extracted=/tmp/$name.tmplist
70  
-	grep '[P,p]'ath $profilepath/profiles.ini | sed -e 's/[P,p]ath=//' -e 's/\r//' >$profiles_extracted
71  
-
72  
-	#
73 75
 	# build an array correcting for rel and abs paths therein
74 76
 	# while read will read line-by-line and will tolerate spaces whereas a for loop will not
75 77
 	#
@@ -82,40 +84,23 @@ do_xulbased() {
82 84
 			finalArr[index]="$profilepath/$line"
83 85
 		fi
84 86
 		index=$index+1
85  
-	done <$profiles_extracted
86  
-
87  
-	[[ -f $profiles_extracted ]] && rm -f $profiles_extracted
  87
+	done < <(grep '[P,p]'ath "$profilepath/profiles.ini" | sed -e 's/[P,p]ath=//' -e 's/\r//' )
88 88
 
89  
-	#
90  
-	# identify which files are sqlite dbs and add them to the work2do file for parallel processing
91  
-	#
92 89
 	echo -e " ${YLW}Cleaning profile for $name${NRM}"
93  
-	for i in "${finalArr[@]}"; do
94  
-		find "$i" -maxdepth 2 -type f | while read line; do
95  
-		[[ -n $(file "$line" | grep SQLite) ]] && echo "$line" >> /tmp/work2do.$name
96  
-	done
97  
-done
98  
-
99  
-#
100  
-# process the work
101  
-#
102  
-
103  
-cat /tmp/work2do.$name | SHELL=/bin/bash parallel -k do_clean 2>/dev/null
104  
-echo
105  
-echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
106  
-[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
107  
-[[ -f /tmp/work2do.$name ]] && rm -f /tmp/work2do.$name
  90
+	find_dbs "${finalArr[@]}"
  91
+	do_clean_parallel
  92
+	echo
  93
+	echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}${cleanedsize}${NRM} ${BLD}Mbytes.${NRM}"
108 94
 }
109 95
 
110 96
 do_dbbased() {
111 97
 	[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
112 98
 	[[ ! -d "$profilepath" ]] &&	echo -e ${RED}"Error: no profile directory for $name found."${NRM} &&	exit 1
113 99
 	echo -e " ${YLW}Cleaning profile for $name${NRM}"
114  
-	find ${profilepath} -maxdepth 2 -type f -name '*.db' | SHELL=/bin/bash parallel do_clean 2>/dev/null
  100
+	find_dbs "${profilepath}"
  101
+	do_clean_parallel
115 102
 	echo
116  
-	echo -e " ${BLD}Profile for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
117  
-	[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
118  
-	[[ -f /tmp/work2do.$name ]] && rm -f /tmp/work2do.$name
  103
+	echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}${cleanedsize}${NRM} ${BLD}Mbytes.${NRM}"
119 104
 }
120 105
 
121 106
 export -f do_clean

0 notes on commit 32c9a49

Please sign in to comment.
Something went wrong with that request. Please try again.