2008-02-11 23:38:31 +00:00
|
|
|
<?php
|
2008-08-05 22:43:14 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2008 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2008-02-11 23:38:31 +00:00
|
|
|
|
2016-07-22 08:41:43 +00:00
|
|
|
// PHP versions of functions in sched/sched_util.cpp
|
2008-02-11 23:38:31 +00:00
|
|
|
|
|
|
|
function filename_hash($filename, $fanout) {
|
|
|
|
$m = md5($filename);
|
|
|
|
$s = substr($m, 1, 7);
|
|
|
|
sscanf($s, "%x", $n);
|
|
|
|
return sprintf("%x", $n%$fanout);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dir_hier_path($filename, $root, $fanout) {
|
|
|
|
$dir = filename_hash($filename, $fanout);
|
|
|
|
$dirpath = "$root/$dir";
|
|
|
|
if (!is_dir($dirpath)) {
|
2017-01-09 19:46:51 +00:00
|
|
|
if (!mkdir($dirpath)) {
|
|
|
|
echo "failed to mkdir: $dirpath\n";
|
|
|
|
}
|
2008-02-11 23:38:31 +00:00
|
|
|
}
|
|
|
|
return "$dirpath/$filename";
|
|
|
|
}
|
|
|
|
|
|
|
|
function dir_hier_url($filename, $base, $fanout) {
|
|
|
|
$dir = filename_hash($filename, $fanout);
|
|
|
|
return "$base/$dir/$filename";
|
|
|
|
}
|
|
|
|
|
2017-01-29 19:54:18 +00:00
|
|
|
// we're about to put a file with the given path into the download hier;
|
|
|
|
// return
|
|
|
|
// 0 if same file is already there and we don't need to copy
|
|
|
|
// 1 if file isn't there, need to copy
|
|
|
|
// -1 if a different file is there
|
2017-02-17 20:58:35 +00:00
|
|
|
// -2 if a file operation failed
|
2017-01-29 19:54:18 +00:00
|
|
|
//
|
|
|
|
// in cases 0 and 1 we make sure a .md5 file is there; create if needed
|
|
|
|
//
|
|
|
|
// Assume that if there's a .md5 file, it's correct.
|
|
|
|
//
|
|
|
|
function check_download_file($path, $dl_path) {
|
|
|
|
$dl_md5_path = "$dl_path.md5";
|
|
|
|
$have_md5_file = false;
|
|
|
|
$md5 = md5_file($path);
|
2017-02-17 20:58:35 +00:00
|
|
|
if ($md5 === FALSE) {
|
|
|
|
return -2;
|
|
|
|
}
|
2017-01-29 19:54:18 +00:00
|
|
|
$size = filesize($path);
|
2017-02-17 20:58:35 +00:00
|
|
|
if ($size === FALSE) {
|
|
|
|
return -2;
|
|
|
|
}
|
2017-01-29 19:54:18 +00:00
|
|
|
|
|
|
|
if (file_exists($dl_md5_path)) {
|
|
|
|
$x = file_get_contents($dl_md5_path);
|
2017-02-17 20:58:35 +00:00
|
|
|
if ($x === FALSE) {
|
|
|
|
return -2;
|
|
|
|
}
|
2017-01-29 19:54:18 +00:00
|
|
|
$x = explode(" ", $x);
|
|
|
|
$dl_md5 = $x[0];
|
|
|
|
$dl_size = $x[1];
|
|
|
|
$have_md5_file = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($dl_path)) {
|
|
|
|
if ($have_md5_file) {
|
2017-02-17 20:58:35 +00:00
|
|
|
$s = filesize($dl_path);
|
|
|
|
if ($s === FALSE) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if ($s == $dl_size && $dl_md5 == $md5) {
|
2017-01-29 19:54:18 +00:00
|
|
|
// looks like the right file is there
|
|
|
|
//
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
// missing the .md5 file; need to look at the file
|
|
|
|
//
|
2017-02-17 20:58:35 +00:00
|
|
|
$m = md5_file($dl_path);
|
|
|
|
if ($m === FALSE) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if ($m == $md5) {
|
|
|
|
if (file_put_contents($dl_md5_path, "$md5 $size\n") === FALSE) {
|
|
|
|
return -2;
|
|
|
|
}
|
2017-01-29 19:54:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-17 20:58:35 +00:00
|
|
|
if (file_put_contents($dl_md5_path, "$md5 $size\n") === FALSE) {
|
|
|
|
return -2;
|
|
|
|
}
|
2017-01-29 19:54:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-11 23:38:31 +00:00
|
|
|
?>
|