You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2019/02/09 11:54:42 UTC

[Bug 63029] OPCPackage Potentially clobbers files on close()

https://bz.apache.org/bugzilla/show_bug.cgi?id=63029

--- Comment #6 from Yegor Kozlov <ye...@dinom.ru> ---
a unit test to reproduce corruption :

try(OPCPackage pkg = OPCPackage.open(path.toFile(), PackageAccess.READ_WRITE))
{
    // add a marshaller that will throw an exception on save
    pkg.addMarshaller("poi/junit", (part, out) -> {
        throw new RuntimeException("Bugzilla 63029");
    });
    pkg.createPart(PackagingURIHelper.createPartName("/poi/test.xml"),
"poi/junit");
} catch (RuntimeException e){
  assert("Bugzilla 63029", e.getMessage());
}

// try to read the source file once again
try ( ZipFile zip = new ZipFile(path.toFile())){
 // throws java.util.zip.ZipException: archive is not a ZIP archive
}


an exception while saving *may* result in a clobbered file. The size of the
corrupted data depends on how much was saved and flushed on disk: it can be
anywhere from zero-byte to the length of the original file.

a simple change to avoid corruption would be to replace the original file only
if  save() succeeded. Something like this:

boolean success = false;
try {
  save(tempFile);
  success = true;
} finally {
    // Close the current zip file, so we can overwrite it on all platforms
    IOUtils.closeQuietly(this.zipArchive);
    try {
        // Copy the new file over the old one
        if(success) {
            FileHelper.copyFile(tempFile, targetFile);
        }
    } finally {
        // Either the save operation succeed or not, we delete the temporary
file
        if (!tempFile.delete()) {
            LOG.log(POILogger.WARN, "The temporary file: '"
                    + targetFile.getAbsolutePath()
                    + "' cannot be deleted ! Make sure that no other
application use it.");
        }
    }
}

this would leave the origin intact in case of an exception.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org