forked from davidpanderson/hera
/
hl_rpc_handler.php
225 lines (204 loc) · 5.59 KB
/
hl_rpc_handler.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
// CGI handler for Librarian RPCs
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
require_once("hl_db.inc");
require_once("hera_util.inc");
// return JSON error reply
//
function error($msg) {
$reply = new StdClass;
$reply->success = false;
$reply->message = $msg;
echo json_encode($reply);
}
// return success reply object
//
function success() {
$reply = new StdClass;
$reply->success = true;
return $reply;
}
// get size and md5 of file or dir
//
function get_file_info($store, $file_name) {
$path = "$store->path_prefix/$file_name";
if ($store->ssh_prefix) {
// if file is remote, issue ssh commands to storage host
//
$remote_cmd = "bash -c \"if [ -d $path ]; then zip $path | md5sum; else md5sum $path; fi\"";
$cmd = "ssh $store->ssh_prefix '$remote_cmd'";
$out = exec($cmd);
$x = preg_split('/\s+/', $out);
$md5 = $x[0];
$remote_cmd = "bash -c \"if [ -d $path ]; then du -b $path; else wc -c $path; fi\"";
$cmd = "ssh $store->ssh_prefix '$remote_cmd'";
$out = exec($cmd);
$x = preg_split('/\s+/', $out);
$size = $x[0];
} else {
// File is local
//
if (is_dir($path)) {
$out = exec("zip $path | md5sum");
$x = preg_split('/\s+/', $out);
$md5 = $x[0];
$out = exec("du -b $path");
$x = preg_split('/\s+/', $out);
$size = $x[0];
} else {
$md5 = md5_file($path);
$size = filesize($path);
}
}
if (!is_numeric($size) || strlen($md5) != 32) {
return array(-1, '');
}
return array($size, $md5);
}
// handler for create observation RPC
//
function create_observation($req) {
$source = source_lookup_auth($req->authenticator);
if (!$source) {
error("auth failure");
return;
}
$req->source_id = $source->id;
if (!observation_insert($req)) {
error(db_error());
return;
}
$reply = success();
echo json_encode($reply);
}
// handler for create file RPC
//
function create_file($req) {
$source = source_lookup_auth($req->authenticator);
if (!$source) {
error("auth failure");
return;
}
$req->create_time = time();
$req->source_id = $source->id;
$store = store_lookup_name($req->store_name);
if (!$store) {
error("bad store name");
return;
}
$req->store_id = $store->id;
if (!$req->md5) {
list($size, $md5) = get_file_info($store, $req->file_name);
if ($size < 0 || !$md5) {
error("couldn't get file info");
return;
}
$req->size = $size;
$req->md5 = $md5;
}
$result = file_insert($req);
if (!$result) {
error(db_error());
return;
}
list($id) = pg_fetch_row($result);
store_update($store->id, "used = used+$req->size");
$reply = success();
echo json_encode($reply);
}
function delete_file($req) {
$source = source_lookup_auth($req->authenticator);
if (!$source) {
error("auth failure");
return;
}
$store = store_lookup_name($req->store_name);
if (!$store) {
error("bad store name");
return;
}
$file = file_lookup_name_store($req->name, $store->id);
if (!$file) {
error("no such file");
return;
}
$now = time();
$ret = file_update($file->id, "deleted=1, extract(epoch from deleted_time)=$now");
if (!$ret) {
error(db_error());
return;
}
store_update($store->id, "used = used-$file->size");
echo json_encode(success());
}
function create_copy_task($req) {
$source = source_lookup_auth($req->authenticator);
if (!$source) {
error("auth failure");
return;
}
// do as much error checking here as we can
//
$store = store_lookup_name($req->local_store_name);
if (!$store) {
error("no such local store $req->local_store_name");
return;
}
$file = file_lookup_name_store($req->file_name, $store->id);
if (!$store) {
error("no such file $req->file_name");
return;
}
$req->local_store_id = $store->id;
if (!task_insert($req)) {
error(db_error());
return;
}
$reply = success();
echo json_encode($reply);
}
function get_store_list($req) {
$stores = store_enum('unavailable=0');
$reply = success();
$reply->stores = $stores;
echo json_encode($reply);
}
function recommended_store($req) {
$source = source_lookup_auth($req->authenticator);
if (!$source) {
error("auth failure");
return;
}
$stores = store_enum('unavailable=0');
foreach ($stores as $store) {
$space = $store->capacity - $store->used;
if ($req->file_size < $space) {
$reply = success();
$reply->store = $store;
echo json_encode($reply);
return;
}
}
error("no store has sufficient free space");
return;
}
$config = get_server_config();
if (!$config) {
error("no config file");
}
if (!init_db($config)) {
error("can't connect to DB");
}
$req = json_decode($_POST['request']);
switch ($req->operation) {
case 'create_observation': create_observation($req); break;
case 'create_file': create_file($req); break;
case 'delete_file': delete_file($req); break;
case 'create_copy_task': create_copy_task($req); break;
case 'get_store_list': get_store_list($req); break;
case 'recommended_store': recommended_store($req); break;
default: error("unknown op $req->operation");
}
?>