diff --git a/html/inc/db_conn.inc b/html/inc/db_conn.inc index b39ae8d36f..37605a16e6 100644 --- a/html/inc/db_conn.inc +++ b/html/inc/db_conn.inc @@ -116,8 +116,8 @@ class DbConn { function enum_general($classname, $query) { $result = $this->do_query($query); - if (!$result) return null; - $x = array(); + if (!$result) return []; + $x = []; while ($obj = $result->fetch_object($classname)) { $x[] = $obj; } @@ -133,7 +133,7 @@ class DbConn { $where_clause = "where $where_clause"; } $query = "select $fields from DBNAME.$table $where_clause $order_clause"; - return $this->enum_general($classname,$query); + return $this->enum_general($classname, $query); } function enum($table, $classname, $where_clause=null, $order_clause=null) { diff --git a/html/inc/dir_hier.inc b/html/inc/dir_hier.inc index ea898bab91..9f3b54fd04 100644 --- a/html/inc/dir_hier.inc +++ b/html/inc/dir_hier.inc @@ -120,9 +120,7 @@ function check_download_file($path, $dl_path) { } } -// Stage the given file, which is assumed to not be in download dir already. -// -function stage_file_basic($dir, $fname) { +function get_hier_info() { static $dl_dir = null; static $fanout = null; if (!$dl_dir) { @@ -130,6 +128,13 @@ function stage_file_basic($dir, $fname) { $dl_dir = parse_config($conf, ""); $fanout = parse_config($conf, ""); } + return [$dl_dir, $fanout]; +} + +// Stage the given file, which is assumed to not be in download dir already. +// +function stage_file_basic($dir, $fname) { + [$dl_dir, $fanout] = get_hier_info(); $old_path = "$dir/$fname"; $new_path = dir_hier_path($fname, $dl_dir, $fanout); $md5 = md5_file($old_path); @@ -138,4 +143,14 @@ function stage_file_basic($dir, $fname) { rename($old_path, $new_path); } +// copy the given file (with known size/md5) +// to the download dir w/ given phys name +// +function stage_file_aux($path, $md5, $size, $phys_name) { + [$dl_dir, $fanout] = get_hier_info(); + $phys_path = dir_hier_path($phys_name, $dl_dir, $fanout); + copy($path, $phys_path); + file_put_contents("$phys_path.md5", "$md5 $size\n"); +} + ?> diff --git a/html/inc/sandbox.inc b/html/inc/sandbox.inc index 2746048f43..620c8e4d5a 100644 --- a/html/inc/sandbox.inc +++ b/html/inc/sandbox.inc @@ -19,14 +19,23 @@ // Utility functions for user file sandbox feature // // In this system: -// - sandbox files live in the download hierarchy, -// with names of the form sb_userid_md5 -// - each user has a "sandbox dir" project/sandbox/userid. -// The files in this have user-visible names, and contents of the form -// sb file_size file_md5 +// - each user (job submitter) has a 'sandbox' where they can store files +// on the BOINC server, via a web interface. +// These files are mutable; you can modify a file w/ a given name. +// - files are stored in a dir project/sandbox/ +// - When a file is uploaded, its size and MD5 are computed and stored +// in an 'info file' in a parallel dir, project/sandbox//.md5 +// +// Sandbox files can be used for web-based job submissions systems +// like BUDA and autodock on BOINC Central. +// Typically they are used as job input files or app files, +// in which case they are downloadable. +// When a file is used in this way, +// it must be copied to the download hierarchy, +// and assigned a physical name that includes its MD5. +// The name depends on the role of the file. require_once("../inc/util.inc"); -require_once("../inc/submit_db.inc"); require_once("../inc/dir_hier.inc"); // Return path of sandbox directory for the given user. @@ -44,75 +53,53 @@ function sandbox_dir($user) { if (!is_dir($d)) { mkdir($d); } + if (!is_dir("$d/.md5")) { + mkdir("$d/.md5"); + } return $d; } -function sandbox_write_link_file($path, $size, $md5) { - file_put_contents($path, "sb $size $md5"); -} - -// check if a link with the given md5 exists +// parse a sandbox file's info file. +// If missing, create it. // -function sandbox_lf_exists($user, $md5) { - $exist = false; - $elf = ""; +function sandbox_parse_info_file($user, $name) { $dir = sandbox_dir($user); - $files = sandbox_file_names($user); - foreach ($files as $file) { - $path = "$dir/$file"; - [$err, $file_size, $file_md5] = sandbox_parse_link_file($path); - if (!$err){ - if (strcmp($md5, $file_md5) == 0) { - $exist = true; - $elf = $file; - break; - } - } + $info_path = "$dir/.md5/$name"; + $info = parse_info_file($info_path); + if ($info) { + return $info; } - return array($exist, $elf); -} - -// parse a link file and return (error, size, md5) -// -function sandbox_parse_link_file($path) { - if (!file_exists($path)) { return array(true, null, null); } - $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(), ""); - -// return the physical name of the file -// -function sandbox_file_name($user, $md5) { - return "sb_".$user->id."_".$md5; -} - -// return the path of the file in the download directory -// -function sandbox_physical_path($user, $md5) { - global $fanout; - $f = sandbox_file_name($user, $md5); - return dir_hier_path($f, parse_config(get_config(), ""), $fanout); + [$size, $md5] = get_file_info("$dir/$name"); + write_info_file($info_path, $md5, $size); + return [$md5, $size]; } // return list of files in sandbox // function sandbox_file_names($user) { - $d = opendir(sandbox_dir($user)); + $files = scandir(sandbox_dir($user)); $names = array(); - while (($f = readdir($d)) !== false) { - if ($f == '.') continue; - if ($f == '..') continue; + foreach ($files as $f) { + if ($f[0] == '.') continue; $names[] = $f; } natsort($names); return $names; } +// return list of files matching given pattern, +// in the format used for form_select() and form_select_multiple() +// +function sandbox_select_items($user, $pattern=null) { + $sbfiles = sandbox_file_names($user); + $sbitems = []; + foreach ($sbfiles as $f) { + if ($pattern && !preg_match($pattern, $f)) continue; + $sbitems[] = [$f, $f]; + } + return $sbitems; +} + // return a - Upload files to your sandbox:


+

Sandbox contents

"; $files = array(); - while (($f = readdir($d)) !== false) { - if ($f == '.') continue; - if ($f == '..') continue; + foreach (scandir($dir) as $f) { + if ($f[0] == '.') continue; $files[] = $f; } if (count($files) == 0) { @@ -66,19 +60,10 @@ function list_files($user, $notice) { } else { sort($files); start_table(); - table_header("Name

(click to view)

", "Modified", "Size (bytes)", "MD5", "Delete","Download"); + table_header("Name

(click to view text files)

", "Modified", "Size (bytes)", "MD5", "Delete","Download"); foreach ($files as $f) { + [$md5, $size] = sandbox_parse_info_file($user, $f); $path = "$dir/$f"; - list($error, $size, $md5) = sandbox_parse_link_file($path); - if ($error) { - table_row($f, "Can't parse link file", "", "delete"); - continue; - } - $p = sandbox_physical_path($user, $md5); - if (!is_file($p)) { - table_row($f, "Physical file not found", "", ""); - continue; - } $ct = time_str(filemtime($path)); table_row( "$f", @@ -100,10 +85,13 @@ function list_files($user, $notice) { page_tail(); } +// upload one or more files + function upload_file($user) { $notice = ""; + $dir = sandbox_dir($user); $count = count($_FILES['new_file']['tmp_name']); - for ($i = 0; $i < $count; $i++) { + for ($i=0; $i<$count; $i++) { $tmp_name = $_FILES['new_file']['tmp_name'][$i]; if (!is_uploaded_file($tmp_name)) { error_page("$tmp_name is not uploaded file"); @@ -112,79 +100,48 @@ function upload_file($user) { if (strstr($name, "/")) { error_page("no / allowed"); } - $md5 = md5_file($tmp_name); - $s = stat($tmp_name); - $size = $s['size']; - [$exists, $elf] = sandbox_lf_exists($user, $md5); - if (!$exists){ - // move file to download dir - // - $phys_path = sandbox_physical_path($user, $md5); - move_uploaded_file($tmp_name, $phys_path); + if (file_exists("$dir/$name")) { + $notice .= "can't upload $name; file exists.
"; + continue; } + move_uploaded_file($tmp_name, "$dir/$name"); - // write link file + // write info file // - $dir = sandbox_dir($user); - $link_path = "$dir/$name"; - sandbox_write_link_file($link_path, $size, $md5); + [$md5, $size] = get_file_info("$dir/$name"); + write_info_file("$dir/.md5/$name", $md5, $size); + $notice .= "Uploaded file $name
"; } list_files($user, $notice); } -// delete a link to a file. -// check if currently being used by a batch. -// If the last link w/ that contents, delete the file itself +// delete a sandbox file. // function delete_file($user) { $name = get_str('name'); $dir = sandbox_dir($user); - list($error, $size, $md5) = sandbox_parse_link_file("$dir/$name"); - if ($error) { - error_page("can't parse link file"); - } - $p = sandbox_physical_path($user, $md5); - if (!is_file($p)) { - error_page("physical file is missing"); - } - $bused = sandbox_file_in_use($user, $name); - if ($bused){ - $notice = "$name is being used by batch(es), you can not delete it now!
"; - } else{ - unlink("$dir/$name"); - $notice = "$name was deleted from your sandbox
"; - [$exists, $elf] = sandbox_lf_exists($user, $md5); - if (!$exists) { - unlink($p); - } - - } + unlink("$dir/$name"); + unlink("$dir/.md5/$name"); + $notice = "$name was deleted from your sandbox
"; list_files($user, $notice); - //Header("Location: sandbox.php"); } + function download_file($user) { $name = get_str('name'); $dir = sandbox_dir($user); - list($err, $size, $md5) = sandbox_parse_link_file("$dir/$name"); - if ($err) { - error_page("can't parse link file"); - } - $p = sandbox_physical_path($user, $md5); - if (!is_file($p)) { - error_page("$p does not exist!"); - } - do_download($p, $name); + do_download("$dir/$name"); } + function view_file($user) { $name = get_str('name'); $dir = sandbox_dir($user); - list($error, $size, $md5) = sandbox_parse_link_file("$dir/$name"); - if ($error) error_page("no such link file"); - $p = sandbox_physical_path($user, $md5); - if (!is_file($p)) error_page("no such physical file"); + $path = "$dir/$name"; + if (!is_file($path)) { + error_path("no such file $name"); + } echo "
\n";
-    readfile($p);
+    readfile($path);
     echo "
\n"; } diff --git a/tools/submit_buda b/tools/submit_buda index 5b5950c3a7..add5f0abfb 100755 --- a/tools/submit_buda +++ b/tools/submit_buda @@ -1,35 +1,40 @@ #! /usr/bin/env php