2011-12-31 04:33:11 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2011 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/>.
|
|
|
|
|
|
|
|
// Utility functions for user file sandbox feature
|
|
|
|
|
|
|
|
require_once("../inc/util.inc");
|
2012-06-05 11:14:46 +00:00
|
|
|
require_once("../inc/submit_db.inc");
|
2011-12-31 04:33:11 +00:00
|
|
|
require_once("../inc/dir_hier.inc");
|
|
|
|
|
|
|
|
// Return path of sandbox directory for the given user.
|
|
|
|
// Create dir if not present.
|
|
|
|
//
|
|
|
|
function sandbox_dir($user) {
|
|
|
|
if (!is_dir("../../sandbox")) {
|
|
|
|
mkdir("../../sandbox");
|
|
|
|
}
|
|
|
|
$d = "../../sandbox/$user->id";
|
|
|
|
if (!is_dir($d)) {
|
|
|
|
mkdir($d);
|
|
|
|
}
|
|
|
|
return $d;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sandbox_write_link_file($path, $size, $md5) {
|
|
|
|
file_put_contents($path, "sb $size $md5");
|
|
|
|
}
|
|
|
|
|
2012-06-05 20:13:19 +00:00
|
|
|
// check if a newly update files already exists in sandbox via its md5 sum
|
|
|
|
//
|
2012-06-06 05:35:39 +00:00
|
|
|
function sandbox_lf_exist($user, $md5) {
|
2012-06-05 20:13:19 +00:00
|
|
|
$exist = 0;
|
|
|
|
$elf = "";
|
|
|
|
$dir = sandbox_dir($user);
|
|
|
|
$files = sandbox_file_names($user);
|
2012-06-06 05:35:39 +00:00
|
|
|
foreach ($files as $file) {
|
2012-06-05 20:13:19 +00:00
|
|
|
$path = $dir."/".$file;
|
|
|
|
list($err, $file_size, $file_md5) = sandbox_parse_link_file($path);
|
|
|
|
if (!$err){
|
2012-06-06 05:35:39 +00:00
|
|
|
if (strcmp($md5, $file_md5) == 0) {
|
2012-06-05 20:13:19 +00:00
|
|
|
//echo "this file with $md5 already exisits with another name $file";
|
|
|
|
$exist = 1;
|
|
|
|
$elf = $file;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($exist, $elf);
|
2012-06-05 08:49:41 +00:00
|
|
|
}
|
2012-06-05 20:13:19 +00:00
|
|
|
|
|
|
|
// parse a link file and return (error, size, md5)
|
2011-12-31 04:33:11 +00:00
|
|
|
//
|
|
|
|
function sandbox_parse_link_file($path) {
|
|
|
|
$x = file_get_contents($path);
|
|
|
|
$n = sscanf($x, "%s %d %s", $s, $size, $md5);
|
|
|
|
if ($n != 3) return array(true, null, null);
|
|
|
|
if ($s != 'sb') return array(true, null, null);
|
|
|
|
return array(false, $size, $md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>");
|
|
|
|
|
2012-06-05 20:13:19 +00:00
|
|
|
// return the physical name of the file
|
|
|
|
//
|
|
|
|
function sandbox_file_name($user, $md5) {
|
2012-06-05 11:14:46 +00:00
|
|
|
$f = "sb_".$user->id."_".$md5;
|
|
|
|
return $f;
|
|
|
|
}
|
2012-06-05 20:13:19 +00:00
|
|
|
|
2011-12-31 04:33:11 +00:00
|
|
|
// return the path of the file in the download directory
|
|
|
|
//
|
|
|
|
function sandbox_physical_path($user, $md5) {
|
|
|
|
global $fanout;
|
2012-06-05 20:13:19 +00:00
|
|
|
$f = sandbox_file_name($user, $md5);
|
2011-12-31 04:33:11 +00:00
|
|
|
return dir_hier_path($f, "../../download", $fanout);
|
|
|
|
}
|
|
|
|
|
2012-01-03 09:30:43 +00:00
|
|
|
// return list of files in sandbox
|
|
|
|
//
|
|
|
|
function sandbox_file_names($user) {
|
|
|
|
$d = opendir(sandbox_dir($user));
|
|
|
|
$names = array();
|
|
|
|
while (($f = readdir($d)) !== false) {
|
|
|
|
if ($f == '.') continue;
|
|
|
|
if ($f == '..') continue;
|
|
|
|
$names[] = $f;
|
|
|
|
}
|
|
|
|
return $names;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a <select> for files in sandbox
|
|
|
|
//
|
|
|
|
function sandbox_file_select($user, $select_name) {
|
|
|
|
$x = "<select name=$select_name>\n";
|
|
|
|
$files = sandbox_file_names($user);
|
|
|
|
foreach ($files as $f) {
|
2012-03-02 06:12:36 +00:00
|
|
|
if(preg_match("/$select_name/",$f)){
|
|
|
|
$x .= "<option value=\"$f\">$f</option>\n";
|
|
|
|
}
|
2012-01-03 09:30:43 +00:00
|
|
|
}
|
|
|
|
$x .= "</select>\n";
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:14:46 +00:00
|
|
|
//check if a file is still being used by a unfinished batch
|
2012-06-05 20:13:19 +00:00
|
|
|
//
|
|
|
|
function sandbox_file_in_use($user, $file){
|
2012-06-05 11:14:46 +00:00
|
|
|
$ufiles = array();
|
2012-06-05 20:13:19 +00:00
|
|
|
|
|
|
|
// batch status: 2(completed), 3(aborted)
|
|
|
|
// $pbatches = BoincBatch::enum("user_id = $user->id and state != BATCH_STATE_COMPLETE and state != BATCH_STATE_ABORTED");
|
2012-06-05 11:14:46 +00:00
|
|
|
$pbatches = BoincBatch::enum("user_id = $user->id and state != 2 and state != 3");
|
2012-06-05 20:13:19 +00:00
|
|
|
if (!$pbatches) return false;
|
2012-06-05 11:14:46 +00:00
|
|
|
|
2012-06-05 20:13:19 +00:00
|
|
|
foreach ($pbatches as $batch){
|
2012-06-05 11:14:46 +00:00
|
|
|
$wus = BoincWorkUnit::enum("batch = $batch->id limit 1" );
|
2012-06-05 20:13:19 +00:00
|
|
|
if ($wus == null){
|
2012-06-05 11:14:46 +00:00
|
|
|
//echo " no workunit for this batch<br/>";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach($wus as $wu){
|
|
|
|
$x = "<in>".$wu->xml_doc."</in>";
|
|
|
|
$x = simplexml_load_string($x);
|
|
|
|
global $fanout;
|
|
|
|
foreach($x->workunit->file_ref as $fr){
|
2012-06-05 20:13:19 +00:00
|
|
|
$pname = (string)$fr->file_name;
|
2012-06-05 11:14:46 +00:00
|
|
|
$ufiles[] = $pname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//echo "these files are being used:<br/>";
|
|
|
|
//print_r($ufiles);
|
2012-06-05 20:13:19 +00:00
|
|
|
$dir = sandbox_dir($user);
|
|
|
|
$path = $dir."/".$file;
|
|
|
|
list($err, $size, $md5) = sandbox_parse_link_file($path);
|
|
|
|
if (!$err){
|
|
|
|
$f = sandbox_file_name($user, $md5);
|
2012-06-05 11:14:46 +00:00
|
|
|
foreach($ufiles as $uf) {
|
2012-06-05 20:13:19 +00:00
|
|
|
if (strcmp($f,$uf) == 0){
|
|
|
|
return true;
|
2012-06-05 11:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 20:13:19 +00:00
|
|
|
return false;
|
2012-06-05 11:14:46 +00:00
|
|
|
}
|
|
|
|
|
2011-12-31 04:33:11 +00:00
|
|
|
?>
|