dont update globals until end of storage check

This commit is contained in:
zzz
2011-01-12 13:54:41 +00:00
parent 1ae6c28592
commit b4e0fe121c

View File

@ -538,7 +538,7 @@ public class Storage
} else { } else {
// the following sets the needed variable // the following sets the needed variable
changed = true; changed = true;
checkCreateFiles(); checkCreateFiles(false);
} }
if (complete()) { if (complete()) {
_util.debug("Torrent is complete", Snark.NOTICE); _util.debug("Torrent is complete", Snark.NOTICE);
@ -649,15 +649,26 @@ public class Storage
/** /**
* This is called at the beginning, and at presumed completion, * This is called at the beginning, and at presumed completion,
* so we have to be careful about locking. * so we have to be careful about locking.
*
* @param recheck if true, this is a check after we downloaded the
* last piece, and we don't modify the global bitfield unless
* the check fails.
*/ */
private void checkCreateFiles() throws IOException private void checkCreateFiles(boolean recheck) throws IOException
{ {
// Whether we are resuming or not, // Whether we are resuming or not,
// if any of the files already exists we assume we are resuming. // if any of the files already exists we assume we are resuming.
boolean resume = false; boolean resume = false;
_probablyComplete = true; _probablyComplete = true;
needed = metainfo.getPieces(); // use local variables during the check
int need = metainfo.getPieces();
BitField bfield;
if (recheck) {
bfield = new BitField(need);
} else {
bfield = bitfield;
}
// Make sure all files are available and of correct length // Make sure all files are available and of correct length
for (int i = 0; i < rafs.length; i++) for (int i = 0; i < rafs.length; i++)
@ -718,8 +729,8 @@ public class Storage
} }
if (correctHash) if (correctHash)
{ {
bitfield.set(i); bfield.set(i);
needed--; need--;
} }
if (listener != null) if (listener != null)
@ -739,6 +750,15 @@ public class Storage
// } // }
//} //}
// do this here so we don't confuse the user during checking
needed = need;
if (recheck && need > 0) {
// whoops, recheck failed
synchronized(bitfield) {
bitfield = bfield;
}
}
if (listener != null) { if (listener != null) {
listener.storageAllChecked(this); listener.storageAllChecked(this);
if (needed <= 0) if (needed <= 0)
@ -903,11 +923,7 @@ public class Storage
// checkCreateFiles() which will set 'needed' and 'bitfield' // checkCreateFiles() which will set 'needed' and 'bitfield'
// and also call listener.storageCompleted() if the double-check // and also call listener.storageCompleted() if the double-check
// was successful. // was successful.
// Todo: set a listener variable so the web shows "checking" and don't checkCreateFiles(true);
// have the user panic when completed amount goes to zero temporarily?
needed = metainfo.getPieces();
bitfield = new BitField(needed);
checkCreateFiles();
if (needed > 0) { if (needed > 0) {
if (listener != null) if (listener != null)
listener.setWantedPieces(this); listener.setWantedPieces(this);