Skip to content
Merged
Prev Previous commit
Next Next commit
Re-download empty structure or chemical component structures
Files less than 40 bytes are deleted to allow for gzip headers.

This addresses #703
  • Loading branch information
sbliven committed Jun 8, 2018
commit 6ea9ae762946efe87998e4ccca16f70b738087ce
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public static enum FetchBehavior {

protected static final String lineSplit = System.getProperty("file.separator");

/** Minimum size for a valid file, in bytes */
public static final long MIN_PDB_FILE_SIZE = 40;

private File path;
private List<String> extensions;

Expand Down Expand Up @@ -687,6 +690,14 @@ public File getLocalFile(String pdbId) {
for(String ex : getExtensions() ){
File f = new File(searchdir,prefix + pdbId.toLowerCase() + ex) ;
if ( f.exists()) {
// delete files that are too short to have contents
if( f.length() < MIN_PDB_FILE_SIZE ) {
boolean success = f.delete();
if( ! success) {
return null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this rather throw an exception (perhaps IOException)? I don't see how can this null be handled if file can't be deleted.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switched to Files.delete which throws the exception

}
assert(!f.exists());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather log a warning here. Assert is going to be ignored in normal situations

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's intended. If the delete was successful then the file will not exist. This is just a sanity check during development to make sure delete() wasn't asynchronous or something.

}
return f;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.biojava.nbio.core.util.InputStreamProvider;
import org.biojava.nbio.structure.align.util.HTTPConnectionTools;
import org.biojava.nbio.structure.align.util.UserConfiguration;
import org.biojava.nbio.structure.io.LocalPDBDirectory;
import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -344,6 +345,12 @@ private static boolean fileExists(String recordName){

File f = new File(fileName);

// delete files that are too short to have contents
if( f.length() < LocalPDBDirectory.MIN_PDB_FILE_SIZE ) {
f.delete();
return false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the file fails to be deleted, this won't work as expected. I think the fail to delete case should throw an IOException

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't check the status because the delete isn't really necessary here. If this method returns false then we re-download the file and move it on top of the old one. I just added the delete defensively.

}

return f.exists();

}
Expand Down