for (int i = 0; i < pieces.length; i++){
check_position = i;
DiskManagerPiece dm_piece = pieces[i];
disk_manager.setPercentDone(((i + 1) * 1000) / disk_manager.getNbPieces() );
boolean pieceCannotExist = false;
byte piece_state = resume_pieces[i];
// valid resume data means that the resume array correctly represents
// the state of pieces on disk, be they done or not
if ( piece_state == PIECE_DONE || !resumeValid || recheck_all ){
// at least check that file sizes are OK for this piece to be valid
DMPieceList list = disk_manager.getPieceList(i);
for (int j=0;j<list.size();j++){
DMPieceMapEntry entry = list.get(j);
Long file_size = (Long)file_sizes.get(entry.getFile());
if ( file_size == null ){
piece_state = PIECE_NOT_DONE;
pieceCannotExist = true;
if (Logger.isEnabled())
Logger.log(new LogEvent(disk_manager, LOGID,
LogEvent.LT_WARNING, "Piece #" + i
+ ": file is missing, " + "fails re-check."));
break;
}
long expected_size = entry.getOffset() + entry.getLength();
if ( file_size.longValue() < expected_size ){
piece_state = PIECE_NOT_DONE;
pieceCannotExist = true;
if (Logger.isEnabled())
Logger.log(new LogEvent(disk_manager, LOGID,
LogEvent.LT_WARNING, "Piece #" + i
+ ": file is too small, fails re-check. File size = "
+ file_size + ", piece needs " + expected_size));
break;
}
}
}
if ( piece_state == PIECE_DONE ){
dm_piece.setDone( true );
}else if ( piece_state == PIECE_NOT_DONE && !recheck_all ){
// if the piece isn't done and we haven't been asked to recheck all pieces
// on restart (only started pieces) then just set as not done
}else{
// We only need to recheck pieces that are marked as not-ok
// if the resume data is invalid or explicit recheck needed
if(pieceCannotExist)
{
dm_piece.setDone( false );
} else if ( piece_state == PIECE_RECHECK_REQUIRED || !resumeValid ){
run_sem.reserve();
while( !stopped ){
if ( recheck_inst.getPermission()){
break;
}
}
if ( stopped ){
break;
}else{
try{
DiskManagerCheckRequest request = disk_manager.createCheckRequest( i, null );
request.setLowPriority( true );
checker.enqueueCheckRequest(
request,
new DiskManagerCheckRequestListener()
{
public void
checkCompleted(
DiskManagerCheckRequest request,
boolean passed )
{
if ( TEST_RECHECK_FAILURE_HANDLING && (int)(Math.random()*10) == 0 ){
disk_manager.getPiece(request.getPieceNumber()).setDone(false);
passed = false;
}
if ( !passed ){
synchronized( failed_pieces ){
failed_pieces.add( request );
}
}
complete();
}
public void
checkCancelled(
DiskManagerCheckRequest request )
{
complete();
}
public void
checkFailed(
DiskManagerCheckRequest request,
Throwable cause )
{
complete();
}
protected void
complete()
{
run_sem.release();
pending_checks_sem.release();
}
});
pending_check_num++;
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}
}
}
while( pending_check_num > 0 ){
pending_checks_sem.reserve();
pending_check_num--;
}
if ( partialPieces != null ){
Iterator iter = partialPieces.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry key = (Map.Entry)iter.next();
int pieceNumber = Integer.parseInt((String)key.getKey());
DiskManagerPiece dm_piece = pieces[ pieceNumber ];
if ( !dm_piece.isDone()){
List blocks = (List)partialPieces.get(key.getKey());
Iterator iterBlock = blocks.iterator();
while (iterBlock.hasNext()) {
dm_piece.setWritten(((Long)iterBlock.next()).intValue());
}
}
}
}
}else{