Passed
Push — master ( 138def...279b7a )
by Vitalii
10:30
created

outfile_path()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
// This file is part of BOINC.
4
// https://boinc.berkeley.edu
5
// Copyright (C) 2024 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// get output files, individually or zipped groups
21
//
22
// args:
23
// action: get_file or get_batch
24
// get_file:
25
//      path: relative to project root dir
26
//      download: if set, download file; else show it in browser
27
// get_batch:
28
//      batch_id
29
//      downloads zip of batch's output files
30
//      Assumes the layout used by sample_assimilator.cpp
31
//      and sample_assimilate.py:
32
//      <project>/results/
33
//      <batchid>/   (0 if not in a batch)
34
//
35
36
require_once("../inc/util.inc");
37
require_once("../inc/submit_util.inc");
38
39
// the path of an output file in the 'assim move' scheme.
40
// This must agree with the corresponding assimilators:
41
// tools/sample_assimilate.py
42
// sched/sample_assimilator.cpp
43
// and with tools/query_job
44
//
45
function outfile_path($wu, $index, $log_names) {
46
    if (!is_valid_filename($wu->name)) error_page("bad WU name");
47
    if (!is_valid_filename($log_names[$index])) error_page("bad logical name");
48
    return sprintf('results/%d/%s__file_%s',
49
        $wu->batch, $wu->name, $log_names[$index]
50
    );
51
}
52
53
// show or download a single output file,
54
// identified by result ID and file index
55
//
56
function get_file() {
57
    $result_id = get_int('result_id');
58
    $index = get_int('index');
59
    $result = BoincResult::lookup_id($result_id);
60
    if (!$result) error_page('no result');
61
    $wu = BoincWorkunit::lookup_id($result->workunitid);
62
    if (!$wu) error_page('no workunit');
63
    $log_names = get_outfile_log_names($result);
64
    if ($index >= count($log_names)) error_page('bad index');
65
    $path = sprintf('../../%s', outfile_path($wu, $index, $log_names));
66
67
    if (get_str('download', true)) {
68
        do_download($path);
69
    } else {
70
        echo "<pre>\n";
71
        echo htmlspecialchars(file_get_contents($path));
72
        echo "</pre>\n";
73
    }
74
}
75
76
// download a zip of the given directory
77
//
78
function get_batch() {
79
    $batch_id = get_int('batch_id');
80
    $dir = "../../results/$batch_id";
81
    if (!is_dir($dir)) die('no batch dir');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
82
    $name = "batch_$batch_id.zip";
83
    $cmd = "cd $dir; rm -f $name; zip -q $name *";
84
    system($cmd);
85
    do_download("$dir/$name");
86
}
87
88
$action = get_str('action');
89
switch ($action) {
90
case 'get_file': get_file(); break;
0 ignored issues
show
Bug introduced by
The call to get_file() has too few arguments starting with user. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
case 'get_file': /** @scrutinizer ignore-call */ get_file(); break;

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
case 'get_batch': get_batch(); break;
92
}
93
94
?>
95