|
1
|
|
|
<?php |
|
2
|
|
|
// This file is part of BOINC. |
|
3
|
|
|
// http://boinc.berkeley.edu |
|
4
|
|
|
// Copyright (C) 2011 University of California |
|
5
|
|
|
// |
|
6
|
|
|
// BOINC is free software; you can redistribute it and/or modify it |
|
7
|
|
|
// under the terms of the GNU Lesser General Public License |
|
8
|
|
|
// as published by the Free Software Foundation, |
|
9
|
|
|
// either version 3 of the License, or (at your option) any later version. |
|
10
|
|
|
// |
|
11
|
|
|
// BOINC is distributed in the hope that it will be useful, |
|
12
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
14
|
|
|
// See the GNU Lesser General Public License for more details. |
|
15
|
|
|
// |
|
16
|
|
|
// You should have received a copy of the GNU Lesser General Public License |
|
17
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
|
|
19
|
|
|
// Utility functions for user file sandbox feature |
|
20
|
|
|
// |
|
21
|
|
|
// In this system: |
|
22
|
|
|
// - sandbox files live in the download hierarchy, |
|
23
|
|
|
// with names of the form sb_userid_md5 |
|
24
|
|
|
// - each user has a "sandbox dir" project/sandbox/userid. |
|
25
|
|
|
// The files in this have user-visible names, and contents of the form |
|
26
|
|
|
// sb file_size file_md5 |
|
27
|
|
|
|
|
28
|
|
|
require_once("../inc/util.inc"); |
|
29
|
|
|
require_once("../inc/submit_db.inc"); |
|
30
|
|
|
require_once("../inc/dir_hier.inc"); |
|
31
|
|
|
|
|
32
|
|
|
// Return path of sandbox directory for the given user. |
|
33
|
|
|
// Create dir if not present. |
|
34
|
|
|
// |
|
35
|
|
|
if (!function_exists("sandbox_dir")){ |
|
36
|
|
|
function sandbox_dir($user) { |
|
37
|
|
|
$dir = parse_config(get_config(), "<sandbox_dir>"); |
|
38
|
|
|
if (!$dir) { |
|
39
|
|
|
$dir = "../../sandbox/"; |
|
40
|
|
|
} |
|
41
|
|
|
if (!is_dir($dir)) { |
|
42
|
|
|
mkdir($dir); |
|
43
|
|
|
} |
|
44
|
|
|
$d = "$dir/$user->id"; |
|
45
|
|
|
if (!is_dir($d)) { |
|
46
|
|
|
mkdir($d); |
|
47
|
|
|
} |
|
48
|
|
|
return $d; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
function sandbox_write_link_file($path, $size, $md5) { |
|
53
|
|
|
file_put_contents($path, "sb $size $md5"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// check if a link with the given md5 exists |
|
57
|
|
|
// |
|
58
|
|
|
function sandbox_lf_exists($user, $md5) { |
|
59
|
|
|
$exist = false; |
|
60
|
|
|
$elf = ""; |
|
61
|
|
|
$dir = sandbox_dir($user); |
|
62
|
|
|
$files = sandbox_file_names($user); |
|
63
|
|
|
foreach ($files as $file) { |
|
64
|
|
|
$path = "$dir/$file"; |
|
65
|
|
|
[$err, $file_size, $file_md5] = sandbox_parse_link_file($path); |
|
66
|
|
|
if (!$err){ |
|
67
|
|
|
if (strcmp($md5, $file_md5) == 0) { |
|
68
|
|
|
$exist = true; |
|
69
|
|
|
$elf = $file; |
|
70
|
|
|
break; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
return array($exist, $elf); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// parse a link file and return (error, size, md5) |
|
78
|
|
|
// |
|
79
|
|
|
function sandbox_parse_link_file($path) { |
|
80
|
|
|
if (!file_exists($path)) { return array(true, null, null); } |
|
81
|
|
|
$x = file_get_contents($path); |
|
82
|
|
|
$n = sscanf($x, "%s %d %s", $s, $size, $md5); |
|
|
|
|
|
|
83
|
|
|
if ($n != 3) return array(true, null, null); |
|
84
|
|
|
if ($s != 'sb') return array(true, null, null); |
|
85
|
|
|
return array(false, $size, $md5); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>"); |
|
89
|
|
|
|
|
90
|
|
|
// return the physical name of the file |
|
91
|
|
|
// |
|
92
|
|
|
function sandbox_file_name($user, $md5) { |
|
93
|
|
|
$f = "sb_".$user->id."_".$md5; |
|
94
|
|
|
return $f; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// return the path of the file in the download directory |
|
98
|
|
|
// |
|
99
|
|
|
if (!function_exists('sandbox_physical_path')) { |
|
100
|
|
|
function sandbox_physical_path($user, $md5) { |
|
101
|
|
|
global $fanout; |
|
102
|
|
|
$f = sandbox_file_name($user, $md5); |
|
103
|
|
|
return dir_hier_path($f, parse_config(get_config(), "<download_dir>"), $fanout); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// return list of files in sandbox |
|
108
|
|
|
// |
|
109
|
|
|
if (!function_exists('sandbox_file_names')) { |
|
110
|
|
|
function sandbox_file_names($user) { |
|
111
|
|
|
$d = opendir(sandbox_dir($user)); |
|
112
|
|
|
$names = array(); |
|
113
|
|
|
while (($f = readdir($d)) !== false) { |
|
114
|
|
|
if ($f == '.') continue; |
|
115
|
|
|
if ($f == '..') continue; |
|
116
|
|
|
$names[] = $f; |
|
117
|
|
|
} |
|
118
|
|
|
natsort($names); |
|
119
|
|
|
return $names; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// return a <select> for files in sandbox |
|
124
|
|
|
// |
|
125
|
|
|
if (!function_exists('sandbox_file_select')) { |
|
126
|
|
|
function sandbox_file_select($user, $select_name, $regexp = null, $allow_none = false) { |
|
127
|
|
|
$x = "<select class=\"form-control\" name=$select_name>\n"; |
|
128
|
|
|
if ($allow_none) { |
|
129
|
|
|
$x .= "<option value=\"\">--- None</option>\n"; |
|
130
|
|
|
} |
|
131
|
|
|
$files = sandbox_file_names($user); |
|
132
|
|
|
foreach ($files as $f) { |
|
133
|
|
|
if ($regexp && !preg_match("/$regexp/",$f)) continue; |
|
134
|
|
|
$x .= "<option value=\"$f\">$f</option>\n"; |
|
135
|
|
|
} |
|
136
|
|
|
$x .= "</select>\n"; |
|
137
|
|
|
return $x; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// convert logical (link) name to physical path |
|
142
|
|
|
// |
|
143
|
|
|
function sandbox_log_to_phys($user, $log_name) { |
|
144
|
|
|
$dir = sandbox_dir($user); |
|
145
|
|
|
[$err, $file_size, $file_md5] = sandbox_parse_link_file("$dir/$log_name"); |
|
146
|
|
|
if ($err) return null; |
|
147
|
|
|
return sandbox_physical_path($user, $file_md5); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// check if a file is still being used by a unfinished batch |
|
151
|
|
|
// |
|
152
|
|
|
// TODO: this is a kludge. |
|
|
|
|
|
|
153
|
|
|
// Should we use the job_file and batch_file_assoc tables instead? |
|
154
|
|
|
// |
|
155
|
|
|
function sandbox_file_in_use($user, $file){ |
|
156
|
|
|
$ufiles = array(); |
|
157
|
|
|
|
|
158
|
|
|
$pbatches = BoincBatch::enum( |
|
159
|
|
|
sprintf('user_id=%d and state!=%d and state!=%d', |
|
160
|
|
|
$user->id, BATCH_STATE_COMPLETE, BATCH_STATE_ABORTED |
|
161
|
|
|
) |
|
162
|
|
|
); |
|
163
|
|
|
if (!$pbatches) return false; |
|
164
|
|
|
|
|
165
|
|
|
foreach ($pbatches as $batch){ |
|
166
|
|
|
$wus = BoincWorkUnit::enum("batch = $batch->id limit 1" ); |
|
167
|
|
|
if ($wus == null){ |
|
168
|
|
|
continue; |
|
169
|
|
|
} |
|
170
|
|
|
foreach($wus as $wu){ |
|
171
|
|
|
$x = "<in>".$wu->xml_doc."</in>"; |
|
172
|
|
|
$x = simplexml_load_string($x); |
|
173
|
|
|
global $fanout; |
|
174
|
|
|
foreach($x->workunit->file_ref as $fr){ |
|
175
|
|
|
$pname = (string)$fr->file_name; |
|
176
|
|
|
$ufiles[] = $pname; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
$dir = sandbox_dir($user); |
|
181
|
|
|
$path = $dir."/".$file; |
|
182
|
|
|
list($err, $size, $md5) = sandbox_parse_link_file($path); |
|
183
|
|
|
if (!$err){ |
|
184
|
|
|
$f = sandbox_file_name($user, $md5); |
|
185
|
|
|
foreach($ufiles as $uf) { |
|
186
|
|
|
if (strcmp($f,$uf) == 0){ |
|
187
|
|
|
return true; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
?> |
|
196
|
|
|
|