Search in sources :

Example 6 with RobustFileOutputStream

use of net.sourceforge.processdash.util.RobustFileOutputStream in project processdash by dtuma.

the class TamperDeterrent method addThumbprint.

/**
     * Read a file and write a new version containing a tamper-detection
     * thumbprint.
     * 
     * @param src
     *            the file to read. The file might or might not contain a
     *            thumbprint already. If it does, the existing thumbprint will
     *            be removed.
     * @param dest
     *            the file where the 'signed' data should be written. Can be the
     *            same as <tt>src</tt>
     * @param t
     *            the type of data the file contains
     * @throws IOException
     *             if data could not be read or written
     * @throws AccessControlException
     *             if this method is called by untrusted code. (Only core
     *             dashboard logic should be calling this method.)
     */
public final void addThumbprint(File src, File dest, FileType t) throws IOException, AccessControlException {
    // only allow trusted code to add thumbprints to files
    PERMISSION.checkPermission();
    // if this is the no-op implementation, abort without changing the file
    String thisType = getThumbprintType();
    if (thisType == null) {
        if (!src.equals(dest)) {
            RobustFileOutputStream out = new RobustFileOutputStream(dest);
            FileUtils.copyFile(src, out);
            out.close();
        }
        return;
    }
    // read the file and calculate the thumbprint
    FileData fileData = readFile(src, t);
    fileData.thumbprint = null;
    calcThumbprint(fileData);
    String thumbData = thisType + SEP + fileData.thumbprint;
    // write the file as requested, with the new thumbprint at the end
    OutputStream out = new BufferedOutputStream(new RobustFileOutputStream(dest));
    if (fileData.thumbStartPos > 0)
        out.write(fileData.content, 0, fileData.thumbStartPos);
    if (fileData.thumbEndPos < fileData.content.length)
        out.write(fileData.content, fileData.thumbEndPos, fileData.content.length - fileData.thumbEndPos);
    out.write(t.thumbStart);
    out.write(thumbData.getBytes(t.encoding));
    out.write(t.thumbEnd);
    out.close();
}
Also used : OutputStream(java.io.OutputStream) RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 7 with RobustFileOutputStream

use of net.sourceforge.processdash.util.RobustFileOutputStream in project processdash by dtuma.

the class UserGroupManager method saveFile.

private void saveFile(boolean custom) throws IOException {
    // find the file that should be modified
    File targetFile = custom ? customFile : sharedFile;
    targetFile.getParentFile().mkdirs();
    OutputStream out = new BufferedOutputStream(new RobustFileOutputStream(targetFile));
    // start an XML document
    XmlSerializer xml = XMLUtils.getXmlSerializer(true);
    xml.setOutput(out, "UTF-8");
    xml.startDocument("UTF-8", null);
    xml.startTag(null, GROUPS_TAG);
    // write the data timestamp, if we have one
    Date ts = timestamps.get(custom);
    if (ts != null)
        xml.attribute(null, TIMESTAMP_ATTR, XMLUtils.saveDate(ts));
    // write XML for each group
    List<UserGroup> groups = new ArrayList<UserGroup>(this.groups.values());
    Collections.sort(groups);
    for (UserGroup oneGroup : groups) {
        if (oneGroup.isCustom() == custom)
            oneGroup.getAsXml(xml);
    }
    // end the document and close the file
    xml.endTag(null, GROUPS_TAG);
    xml.ignorableWhitespace(System.lineSeparator());
    xml.endDocument();
    out.close();
    // add a tamper-deterrent thumbprint to the shared groups file.
    if (!custom) {
        TamperDeterrent.getInstance().addThumbprint(targetFile, targetFile, TamperDeterrent.FileType.XML);
    }
    // if we saved the file successfully, clear its dirty flag
    needsSave.remove(custom);
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) ArrayList(java.util.ArrayList) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Date(java.util.Date) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 8 with RobustFileOutputStream

use of net.sourceforge.processdash.util.RobustFileOutputStream in project processdash by dtuma.

the class DatasetAutoMigrator method copyDataFiles.

private void copyDataFiles() throws IOException {
    // Copy all important data files from the source directory to the
    // working directory.
    File destDir = workingDir.getDirectory();
    List<String> files = FileUtils.listRecursively(sourceDir, DashboardInstanceStrategy.INSTANCE.getFilenameFilter());
    for (String filename : files) {
        File srcFile = new File(sourceDir, filename);
        File destFile = new File(destDir, filename);
        RobustFileOutputStream out = new RobustFileOutputStream(destFile);
        FileUtils.copyFile(srcFile, out);
        out.close();
    }
}
Also used : RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) File(java.io.File)

Example 9 with RobustFileOutputStream

use of net.sourceforge.processdash.util.RobustFileOutputStream in project processdash by dtuma.

the class CompressedWorkingDirectory method flushData.

public boolean flushData() throws LockFailureException, IOException {
    List<String> filesToBackup = FileUtils.listRecursively(extractDirectory, CWD_FILE_FILTER);
    File destZip = getTargetZipFile();
    RobustFileOutputStream rOut = new RobustFileOutputStream(destZip);
    OutputStream out = rOut;
    if (isPdbk(destZip))
        out = new XorOutputStream(out, PDBK_XOR_BITS);
    try {
        ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(out));
        for (String filename : filesToBackup) {
            File f = new File(extractDirectory, filename);
            ZipEntry e = new ZipEntry(filename);
            e.setTime(f.lastModified());
            zipOut.putNextEntry(e);
            FileUtils.copyFile(f, zipOut);
            zipOut.closeEntry();
        }
        zipOut.finish();
        zipOut.flush();
    } catch (IOException ioe) {
        rOut.abort();
        throw ioe;
    }
    out.close();
    return true;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) XorOutputStream(net.sourceforge.processdash.util.XorOutputStream) ZipEntry(java.util.zip.ZipEntry) RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) XorOutputStream(net.sourceforge.processdash.util.XorOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 10 with RobustFileOutputStream

use of net.sourceforge.processdash.util.RobustFileOutputStream in project processdash by dtuma.

the class TeamProject method saveUserSettings.

private boolean saveUserSettings(File directory) {
    File userSettingsFile = new File(directory, USER_SETTINGS_FILENAME);
    try {
        RobustFileOutputStream out = new RobustFileOutputStream(userSettingsFile);
        userSettings.store(out, null);
        out.close();
        return true;
    } catch (IOException ioe) {
        System.out.println("Encountered problem when saving " + userSettingsFile);
        ioe.printStackTrace();
        return false;
    }
}
Also used : RobustFileOutputStream(net.sourceforge.processdash.util.RobustFileOutputStream) IOException(java.io.IOException) File(java.io.File)

Aggregations

RobustFileOutputStream (net.sourceforge.processdash.util.RobustFileOutputStream)17 File (java.io.File)10 IOException (java.io.IOException)9 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)7 XmlSerializer (org.xmlpull.v1.XmlSerializer)5 OutputStreamWriter (java.io.OutputStreamWriter)3 BufferedWriter (java.io.BufferedWriter)2 Iterator (java.util.Iterator)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1