|
1
|
|
|
<?php |
|
2
|
|
|
// This file is part of BOINC. |
|
3
|
|
|
// http://boinc.berkeley.edu |
|
4
|
|
|
// Copyright (C) 2008 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
|
|
|
// PHP versions of functions in sched/sched_util.cpp |
|
20
|
|
|
|
|
21
|
|
|
function filename_hash($filename, $fanout) { |
|
22
|
|
|
$m = md5($filename); |
|
23
|
|
|
$s = substr($m, 1, 7); |
|
24
|
|
|
sscanf($s, "%x", $n); |
|
25
|
|
|
return sprintf("%x", $n%$fanout); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
function dir_hier_path($filename, $root, $fanout) { |
|
29
|
|
|
$dir = filename_hash($filename, $fanout); |
|
30
|
|
|
$dirpath = "$root/$dir"; |
|
31
|
|
|
if (!is_dir($dirpath)) { |
|
32
|
|
|
if (!mkdir($dirpath)) { |
|
33
|
|
|
echo "failed to mkdir: $dirpath\n"; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
return "$dirpath/$filename"; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
function upload_path($filename) { |
|
40
|
|
|
static $upload_dir=null; |
|
41
|
|
|
if (!$upload_dir) { |
|
42
|
|
|
$config = get_config(); |
|
43
|
|
|
$upload_dir = parse_config($config, '<upload_dir>'); |
|
44
|
|
|
$uldl_dir_fanout = parse_config($config, '<uldl_dir_fanout>'); |
|
45
|
|
|
} |
|
46
|
|
|
return dir_hier_path($filename, $upload_dir, $uldl_dir_fanout); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function dir_hier_url($filename, $base, $fanout) { |
|
50
|
|
|
$dir = filename_hash($filename, $fanout); |
|
51
|
|
|
return "$base/$dir/$filename"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// we're about to put a file with the given path into the download hier; |
|
55
|
|
|
// return |
|
56
|
|
|
// 0 if same file is already there and we don't need to copy |
|
57
|
|
|
// 1 if file isn't there, need to copy |
|
58
|
|
|
// -1 if a different file is there |
|
59
|
|
|
// -2 if a file operation failed |
|
60
|
|
|
// |
|
61
|
|
|
// in cases 0 and 1 we make sure a .md5 file is there; create if needed |
|
62
|
|
|
// |
|
63
|
|
|
// Assume that if there's a .md5 file, it's correct. |
|
64
|
|
|
// |
|
65
|
|
|
function check_download_file($path, $dl_path) { |
|
66
|
|
|
$dl_md5_path = "$dl_path.md5"; |
|
67
|
|
|
$have_md5_file = false; |
|
68
|
|
|
$md5 = md5_file($path); |
|
69
|
|
|
if ($md5 === FALSE) { |
|
70
|
|
|
return -2; |
|
71
|
|
|
} |
|
72
|
|
|
$size = filesize($path); |
|
73
|
|
|
if ($size === FALSE) { |
|
74
|
|
|
return -2; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (file_exists($dl_md5_path)) { |
|
78
|
|
|
$x = file_get_contents($dl_md5_path); |
|
79
|
|
|
if ($x === FALSE) { |
|
80
|
|
|
return -2; |
|
81
|
|
|
} |
|
82
|
|
|
$x = explode(" ", $x); |
|
83
|
|
|
$dl_md5 = $x[0]; |
|
84
|
|
|
$dl_size = $x[1]; |
|
85
|
|
|
$have_md5_file = true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (file_exists($dl_path)) { |
|
89
|
|
|
if ($have_md5_file) { |
|
90
|
|
|
$s = filesize($dl_path); |
|
91
|
|
|
if ($s === FALSE) { |
|
92
|
|
|
return -2; |
|
93
|
|
|
} |
|
94
|
|
|
if ($s == $dl_size && $dl_md5 == $md5) { |
|
|
|
|
|
|
95
|
|
|
// looks like the right file is there |
|
96
|
|
|
// |
|
97
|
|
|
return 0; |
|
98
|
|
|
} |
|
99
|
|
|
return -1; |
|
100
|
|
|
} else { |
|
101
|
|
|
// missing the .md5 file; need to look at the file |
|
102
|
|
|
// |
|
103
|
|
|
$m = md5_file($dl_path); |
|
104
|
|
|
if ($m === FALSE) { |
|
105
|
|
|
return -2; |
|
106
|
|
|
} |
|
107
|
|
|
if ($m == $md5) { |
|
108
|
|
|
if (file_put_contents($dl_md5_path, "$md5 $size\n") === FALSE) { |
|
109
|
|
|
return -2; |
|
110
|
|
|
} |
|
111
|
|
|
return 0; |
|
112
|
|
|
} |
|
113
|
|
|
return -1; |
|
114
|
|
|
} |
|
115
|
|
|
} else { |
|
116
|
|
|
if (file_put_contents($dl_md5_path, "$md5 $size\n") === FALSE) { |
|
117
|
|
|
return -2; |
|
118
|
|
|
} |
|
119
|
|
|
return 1; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
function get_hier_info() { |
|
124
|
|
|
static $dl_dir = null; |
|
125
|
|
|
static $fanout = null; |
|
126
|
|
|
if (!$dl_dir) { |
|
127
|
|
|
$conf = get_config(); |
|
128
|
|
|
$dl_dir = parse_config($conf, "<download_dir>"); |
|
129
|
|
|
$fanout = parse_config($conf, "<uldl_dir_fanout>"); |
|
130
|
|
|
} |
|
131
|
|
|
return [$dl_dir, $fanout]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
function download_hier_path($fname) { |
|
135
|
|
|
[$dl_dir, $fanout] = get_hier_info(); |
|
136
|
|
|
return dir_hier_path($fname, $dl_dir, $fanout); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// Stage (move, not copy) the given file, |
|
140
|
|
|
// which is assumed to not be in download dir already. |
|
141
|
|
|
// |
|
142
|
|
|
function stage_file_basic($dir, $fname) { |
|
143
|
|
|
[$dl_dir, $fanout] = get_hier_info(); |
|
|
|
|
|
|
144
|
|
|
$old_path = "$dir/$fname"; |
|
145
|
|
|
$new_path = download_hier_path($fname); |
|
146
|
|
|
$md5 = md5_file($old_path); |
|
147
|
|
|
$size = filesize($old_path); |
|
148
|
|
|
file_put_contents("$new_path.md5", "$md5 $size\n"); |
|
149
|
|
|
rename($old_path, $new_path); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// copy the given file (with given md5/size) |
|
153
|
|
|
// to the download dir w/ given phys name; and create .md5 file |
|
154
|
|
|
// If phys name is already there, do nothing. |
|
155
|
|
|
// |
|
156
|
|
|
function stage_file_aux($path, $md5, $size, $phys_name) { |
|
157
|
|
|
[$dl_dir, $fanout] = get_hier_info(); |
|
158
|
|
|
$phys_path = dir_hier_path($phys_name, $dl_dir, $fanout); |
|
159
|
|
|
if (file_exists($phys_path)) return; |
|
160
|
|
|
copy($path, $phys_path); |
|
161
|
|
|
file_put_contents("$phys_path.md5", "$md5 $size\n"); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
?> |
|
165
|
|
|
|