Changeset 1160


Ignore:
Timestamp:
12/07/11 18:14:11 (6 months ago)
Author:
bogdan2412
Message:

Added size verification to check-attachments script.

This was necessary because the grader checks that the downloaded test
case size matches the size stored in the database. Because we once ran a
script that added newlines to the end of some .ok files, this was
broken.

REVIEW URL http://reviewboard.infoarena.ro/r/178/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scripts/check-attachments

    r1071 r1160  
    99log_warn("This script consumes tons of memory so errors might occur"); 
    1010 
    11 $res = db_query("SELECT `id`, `name`, `page` FROM ia_file"); 
     11$res = db_query("SELECT `id`, `name`, `page`, `size` FROM ia_file"); 
    1212 
    1313$extra_files = array_flip(glob(IA_ROOT_DIR . "attach/*")); 
    14  
    1514$extra_atts = array(); 
    1615$total_files = count($extra_files); 
    1716$total_atts = 0; 
     17$mismatched_sizes = array(); 
    1818 
    1919// Check attachments. 
     
    2626        $extra_atts[] = $att; 
    2727    } 
     28    if (filesize($fname) != $att['size']) { 
     29        $att['file_size'] = filesize($fname); 
     30        $mismatched_sizes[] = $att; 
     31    } 
    2832} 
    2933 
     
    3438    log_print("Database and attach dir are in perfect sync, $total_files total files."); 
    3539} else { 
    36     log_print("There are are ".count($extra_files)." out of ".$total_files." disk files with no db entry."); 
     40    log_print("There are ".count($extra_files)." out of ".$total_files." disk files with no db entry."); 
    3741    if (count($extra_files)) { 
    3842        if (read_bool("Do you want to see a list?", false)) { 
     
    4852    } 
    4953 
    50     log_print("There are are ".count($extra_atts)." out of ".$total_atts." db files with no disk file."); 
     54    log_print("There are ".count($extra_atts)." out of ".$total_atts." db files with no disk file."); 
    5155    if (count($extra_atts)) { 
    5256        if (read_bool("Do you want to see a list?", false)) { 
     
    6569unset($extra_atts); 
    6670unset($extra_files); 
     71 
     72if (count($mismatched_sizes) != 0) { 
     73    log_print('There are ' . count($mismatched_sizes) . ' files with ' . 
     74              'mismatching sizes on disk and in the db'); 
     75    if (read_bool('Do you want to see a list?', false)) { 
     76        foreach ($mismatched_sizes as $att) { 
     77            log_print(sprintf('page %s name %s file_size %s db_size %s', 
     78                              $att['page'], $att['name'], $att['file_size'], 
     79                              $att['size'])); 
     80        } 
     81    } 
     82    if (read_bool('Do you want to try to fix this?', false)) { 
     83        foreach ($mismatched_sizes as $att) { 
     84            if ($att['file_size'] == 0 && $att['size'] != 0) { 
     85                // File was likely uploaded when disk was out of space. 
     86                // Nothing we can do, delete it. 
     87                log_print(sprintf('Deleting attachment page %s name %s', 
     88                                  $att['page'], $att['name'])); 
     89                attachment_delete_by_id($att['id']); 
     90                continue; 
     91            } 
     92 
     93            $safe_update = false; 
     94            if (starts_with($att['name'], 'grader_test') && 
     95                $att['file_size'] == $att['size'] + 1) { 
     96                // A script was run that added missing trailing endlines 
     97                // to grader tests. 
     98                $safe_update = true; 
     99            } 
     100            if ($att['file_size'] != 0 && $att['size'] == 0) { 
     101                $safe_update = true; 
     102            } 
     103 
     104            if ($safe_update) { 
     105                $new_att = attachment_get_by_id($att['id']); 
     106                $new_att['size'] = $att['file_size']; 
     107                attachment_update($new_att['id'], $new_att['name'], 
     108                                  $new_att['size'], $new_att['mime_type'], 
     109                                  $new_att['page'], $new_att['user_id'], 
     110                                  $new_att['remote_ip_info']); 
     111                continue; 
     112            } 
     113            log_print(sprintf('I don\'t know how to handle attachment page ' . 
     114                              $att['page'] . ' name ' . $att['name'])); 
     115        } 
     116    } 
     117} 
    67118 
    68119// Again, in case we changed something. 
Note: See TracChangeset for help on using the changeset viewer.