-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakefs.sh
178 lines (160 loc) · 3.6 KB
/
fakefs.sh
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
DEVICE_MAGIC=$(echo -ne "\xFA\xCE")
off_header=0;
off_filemeta=32;
len_filemeta_name=28;
len_filemeta_loc=4;
filemeta_count=100; #$(expr 1024 \* 1024 / 32 - 1 );
function usage {
echo "Usage:";
echo "fakefs <mode> [args]"
echo "mode:"
echo "download <fileOnFs> <device> <location>"
echo "upload <fileOnFs> <device> <location>"
echo "copy <fileOnFs> <device> <newFileOnFs>"
echo "ls <device>"
echo "rm <fileOnFs> <device>"
}
function get_length {
echo -ne "$1" | dd 2>&1 >/dev/null | awk ' NR==3 {print $1} '
}
function error_arg {
if [[ $1 < $2 ]]; then
echo "ERROR: Wrong number of arguments";
usage;
exit;
fi
}
function check_fs {
if [[ "$(get_block $off_header $(get_length $DEVICE_MAGIC))" != "$DEVICE_MAGIC" ]]; then
echo "ERROR: Not a fakefs device";
exit 1;
fi
}
function get_block {
local l_skip=$1;
local l_size=${3:-1};
local l_count=${2:-1};
local ret=$(dd if=$device bs=$l_size count=$l_count skip=$l_skip status=none conv=notrunc,sync)
if [[ $? > 0 ]]; then
echo ""
else
echo $ret
fi
}
function set_block {
local l_bytes=$1;
local l_seek=${2:-0};
local l_count=${3:-$(get_length $l_bytes)};
echo -ne $l_bytes | dd of=$device bs=1 seek=$l_seek count=$l_count status=none conv=notrunc,sync
}
function find_free_slot {
for (( off=$off_filemeta; off<$filemeta_count*$off_filemeta; off+=$off_filemeta ))
do
if [[ $(get_block $off 1) == "\x00" ]]; then
echo $(expr $off / 32);
break;
fi
done
echo $off_filemeta;
}
function set_filemeta {
local l_filename=${1:0:$len_filemeta_name};
local l_slot=${2:-$(find_free_slot)};
set_block $l_filename $(expr $l_slot \* 32 + $off_filemeta);
}
function get_filemeta {
local l_slot=${1:-0}
local ret=$(get_block $(expr $l_slot \* 32 + $off_filemeta) $len_filemeta_name)
if [[ $? > 0 ]]; then
echo ""
else
echo $ret
fi
}
function get_file_offset {
local l_slot=${1:-0}
get_block $(expr $l_slot \* 32 + $off_filemeta + $len_filemeta_name) $len_filemeta_loc
}
function create_device {
touch $device;
set_block $DEVICE_MAGIC $off_header;
for (( i=1; i<$filemeta_count; i++ ))
do
echo -n "["
for ((j=0; j<i; j++)) ; do echo -n '='; done
echo -n '=>'
for ((j=i; j<100; j++)) ; do echo -n ' '; done
echo -n "] $i% / 100%" $'\r'
set_filemeta "\x00" $i;
done
echo
}
function list_device {
for (( i=1; i<$filemeta_count; i++ ))
do
local l_filename=$(get_filemeta $i)
if [[ "$l_filename" != "" ]]; then
echo "Slot $i: $(get_filemeta $i) Offset: $(get_file_offset $i)"
fi
done
}
error_arg $# 1;
mode=$1;
case $mode in
download | upload | copy )
error_arg $# 4;
filename=$2;
device=$3;
location=$4;
;;
ls | format )
error_arg $# 2;
device=$2;
;;
rm )
error_arg $# 3;
filename=$2;
device=$3;
;;
* )
echo "Unknown mode";
usage;
exit;
;;
esac
case $mode in
format )
rm -f $device
echo "Creating Device"
create_device;
check_fs;
echo "Device is OK"
;;
ls )
check_fs;
echo "Device is OK"
echo "Device listing:"
list_device;
;;
download )
echo "Download mode active"
check_fs;
echo "Device is OK, working"
;;
upload )
echo "Upload mode active"
check_fs;
echo "Device is OK, working"
;;
copy )
echo "Copy mode active"
check_fs;
echo "Device is OK, working"
;;
rm )
echo "Removing file from device"
check_fs;
echo "Device is OK, working"
;;
esac