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
|
|
|
function sandbox_dir($user) { |
36
|
|
|
$dir = parse_config(get_config(), "<sandbox_dir>"); |
37
|
|
|
if (!$dir) { |
38
|
|
|
$dir = "../../sandbox/"; |
39
|
|
|
} |
40
|
|
|
if (!is_dir($dir)) { |
41
|
|
|
mkdir($dir); |
42
|
|
|
} |
43
|
|
|
$d = "$dir/$user->id"; |
44
|
|
|
if (!is_dir($d)) { |
45
|
|
|
mkdir($d); |
46
|
|
|
} |
47
|
|
|
return $d; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
function sandbox_write_link_file($path, $size, $md5) { |
51
|
|
|
file_put_contents($path, "sb $size $md5"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// check if a link with the given md5 exists |
55
|
|
|
// |
56
|
|
|
function sandbox_lf_exists($user, $md5) { |
57
|
|
|
$exist = false; |
58
|
|
|
$elf = ""; |
59
|
|
|
$dir = sandbox_dir($user); |
60
|
|
|
$files = sandbox_file_names($user); |
61
|
|
|
foreach ($files as $file) { |
62
|
|
|
$path = "$dir/$file"; |
63
|
|
|
[$err, $file_size, $file_md5] = sandbox_parse_link_file($path); |
64
|
|
|
if (!$err){ |
65
|
|
|
if (strcmp($md5, $file_md5) == 0) { |
66
|
|
|
$exist = true; |
67
|
|
|
$elf = $file; |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
return array($exist, $elf); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// parse a link file and return (error, size, md5) |
76
|
|
|
// |
77
|
|
|
function sandbox_parse_link_file($path) { |
78
|
|
|
if (!file_exists($path)) { return array(true, null, null); } |
79
|
|
|
$x = file_get_contents($path); |
80
|
|
|
$n = sscanf($x, "%s %d %s", $s, $size, $md5); |
|
|
|
|
81
|
|
|
if ($n != 3) return array(true, null, null); |
82
|
|
|
if ($s != 'sb') return array(true, null, null); |
83
|
|
|
return array(false, $size, $md5); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>"); |
87
|
|
|
|
88
|
|
|
// return the physical name of the file |
89
|
|
|
// |
90
|
|
|
function sandbox_file_name($user, $md5) { |
91
|
|
|
return "sb_".$user->id."_".$md5; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// return the path of the file in the download directory |
95
|
|
|
// |
96
|
|
|
function sandbox_physical_path($user, $md5) { |
97
|
|
|
global $fanout; |
98
|
|
|
$f = sandbox_file_name($user, $md5); |
99
|
|
|
return dir_hier_path($f, parse_config(get_config(), "<download_dir>"), $fanout); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// return list of files in sandbox |
103
|
|
|
// |
104
|
|
|
function sandbox_file_names($user) { |
105
|
|
|
$d = opendir(sandbox_dir($user)); |
106
|
|
|
$names = array(); |
107
|
|
|
while (($f = readdir($d)) !== false) { |
108
|
|
|
if ($f == '.') continue; |
109
|
|
|
if ($f == '..') continue; |
110
|
|
|
$names[] = $f; |
111
|
|
|
} |
112
|
|
|
natsort($names); |
113
|
|
|
return $names; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// return a <select> for files in sandbox |
117
|
|
|
// |
118
|
|
|
function sandbox_file_select( |
119
|
|
|
$user, $select_name, $regexp = null, $allow_none = false |
|
|
|
|
120
|
|
|
) { |
121
|
|
|
$x = "<select class=\"form-control\" name=$select_name>\n"; |
122
|
|
|
if ($allow_none) { |
123
|
|
|
$x .= "<option value=\"\">--- None</option>\n"; |
124
|
|
|
} |
125
|
|
|
$files = sandbox_file_names($user); |
126
|
|
|
foreach ($files as $f) { |
127
|
|
|
if ($regexp && !preg_match("/$regexp/",$f)) continue; |
128
|
|
|
$x .= "<option value=\"$f\">$f</option>\n"; |
129
|
|
|
} |
130
|
|
|
$x .= "</select>\n"; |
131
|
|
|
return $x; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// convert sandbox (link) name to physical path |
135
|
|
|
// |
136
|
|
|
function sandbox_log_to_phys($user, $log_name) { |
137
|
|
|
$dir = sandbox_dir($user); |
138
|
|
|
[$err, $file_size, $file_md5] = sandbox_parse_link_file("$dir/$log_name"); |
139
|
|
|
if ($err) return null; |
140
|
|
|
return sandbox_physical_path($user, $file_md5); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// convert sandbox name to physical name |
144
|
|
|
// |
145
|
|
|
function sandbox_name_to_phys_name($user, $log_name) { |
146
|
|
|
$dir = sandbox_dir($user); |
147
|
|
|
[$err, $file_size, $file_md5] = sandbox_parse_link_file("$dir/$log_name"); |
148
|
|
|
if ($err) return null; |
149
|
|
|
return sandbox_file_name($user, $file_md5); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// check if a file is still being used by a unfinished batch |
153
|
|
|
// |
154
|
|
|
// TODO: this is a kludge. |
|
|
|
|
155
|
|
|
// Should we use the job_file and batch_file_assoc tables instead? |
156
|
|
|
// |
157
|
|
|
function sandbox_file_in_use($user, $file){ |
158
|
|
|
$ufiles = array(); |
159
|
|
|
|
160
|
|
|
$pbatches = BoincBatch::enum( |
161
|
|
|
sprintf('user_id=%d and state!=%d and state!=%d', |
162
|
|
|
$user->id, BATCH_STATE_COMPLETE, BATCH_STATE_ABORTED |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
if (!$pbatches) return false; |
166
|
|
|
|
167
|
|
|
foreach ($pbatches as $batch){ |
168
|
|
|
$wus = BoincWorkUnit::enum("batch = $batch->id limit 1" ); |
169
|
|
|
if ($wus == null){ |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
foreach($wus as $wu){ |
173
|
|
|
$x = "<in>".$wu->xml_doc."</in>"; |
174
|
|
|
$x = simplexml_load_string($x); |
175
|
|
|
global $fanout; |
176
|
|
|
foreach($x->workunit->file_ref as $fr){ |
177
|
|
|
$pname = (string)$fr->file_name; |
178
|
|
|
$ufiles[] = $pname; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
$dir = sandbox_dir($user); |
183
|
|
|
$path = $dir."/".$file; |
184
|
|
|
list($err, $size, $md5) = sandbox_parse_link_file($path); |
185
|
|
|
if (!$err){ |
186
|
|
|
$f = sandbox_file_name($user, $md5); |
187
|
|
|
foreach($ufiles as $uf) { |
188
|
|
|
if (strcmp($f,$uf) == 0){ |
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
?> |
197
|
|
|
|