-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunkCombiner.sh
More file actions
executable file
·87 lines (76 loc) · 2.29 KB
/
chunkCombiner.sh
File metadata and controls
executable file
·87 lines (76 loc) · 2.29 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
function usage {
echo "Usage: $(basename $0) [-d -o]"
echo " Mandatory Flags."
echo " -d Path to Sharry filesystem database directory."
echo " -o Path to directory to output rebuilt files."
echo " Other Flags."
echo " -h Show usage."
# echo " -e File type extensions you want to rebuild."
exit 1
}
if (($# == 0)); then
echo "Error: Mandatory Flags missing.";
usage;
fi
optstring=":d:o:h";
while getopts ${optstring} arg; do
case "${arg}" in
d) databaseDir=${OPTARG} ;;
o) outputDir=${OPTARG} ;;
# e) echo "Not yet implemented." ;;
h) usage ;;
:) echo "Error: Missing option argument for -${OPTARG}."
usage ;;
?) echo "Error: Invalid option: - ${OPTARG}."
usage ;;
esac
done
if [ ! -d $databaseDir ]; then
echo "Error: Invalid database directory.";
usage;
fi
if [ ! -d $outputDir ]; then
echo "Output directory $outputDir not found.";
echo "Create directory $outputDir and continue? [Y/N]";
read input;
if [[ $input == "Y" || $input == "y" ]]; then
mkdir $outputDir;
echo "Created directory $outputDir.";
elif [[ $input == "N" || $input == "n" ]]; then
echo "Exiting.";
exit 0;
else
echo "Invalid [Y/N] input.";
echo "Exiting.";
exit 1;
fi
fi
echo "Generating list of data directories.";
dirListPath=$outputDir/dirList
find $databaseDir -type f -exec dirname {} >> $dirListPath.tmp +;
awk '!a[$0]++' $dirListPath.tmp >> $dirListPath.txt;
rm $dirListPath.tmp;
echo "Reading in MIME type dictionary.";
declare -A extByType;
while read -r line; do
mimeType=$(echo $line | cut -d "|" -f 1);
ext=$(echo $line | cut -d "|" -f 2);
extByType[$mimeType]="$ext";
done < extByMimeType.txt
#Get padded zero count for filename
fileCount=$(wc -l < $dirListPath.txt);
digitCount=${#fileCount};
i=0;
#Combine chunks and assign extension
while read -r line; do
((i=i+1));
cat $line/* >> $line/output.tmp;
mimeType=$(file -b --mime-type $line/output.tmp);
extension=${extByType[$mimeType]};
paddedInt=$(printf "%0${digitCount}d" ${i});
mv $line/output.tmp $outputDir/file_$paddedInt$extension;
echo -ne "File $i/$fileCount rebuilt.\r";
done <$dirListPath.txt
rm $dirListPath.txt
echo "File $i/$fileCount rebuilt.";