Search in sources :

Example 51 with StorageFile

use of org.apache.derby.io.StorageFile in project derby by apache.

the class StreamFileContainer method getFileName.

/**
 * Return a file name for the identity.
 * <p>
 * Return a valid file name for the identity, or null if the data
 * directory for this segment cannot be created
 *
 * @exception StandardException Segment directory cannot be created
 */
protected StorageFile getFileName(ContainerKey identity, boolean forCreate, boolean errorOK) throws StandardException {
    if (identity.getSegmentId() == StreamContainerHandle.TEMPORARY_SEGMENT) {
        return (dataFactory.storageFactory.newStorageFile(dataFactory.storageFactory.getTempDir(), "T" + identity.getContainerId() + ".tmp"));
    } else {
        if (SanityManager.DEBUG)
            SanityManager.THROWASSERT("cannot create stream container in non-temp segments yet.");
        StorageFile container = dataFactory.getContainerPath(identity, false);
        if (!privExists(container)) {
            if (!forCreate)
                return null;
            StorageFile directory = container.getParentDir();
            if (!privExists(directory)) {
                // make sure only 1 thread can create a segment at one time
                synchronized (dataFactory) {
                    if (!privExists(directory)) {
                        boolean created = false;
                        IOException ex = null;
                        try {
                            created = privMkdirs(directory);
                        } catch (IOException ioe) {
                            ex = ioe;
                        }
                        if (!created) {
                            if (errorOK)
                                return null;
                            else
                                throw StandardException.newException(SQLState.FILE_CANNOT_CREATE_SEGMENT, ex, directory);
                        }
                    }
                }
            }
        }
        return container;
    }
}
Also used : StorageFile(org.apache.derby.io.StorageFile) IOException(java.io.IOException)

Example 52 with StorageFile

use of org.apache.derby.io.StorageFile in project derby by apache.

the class StorageFactoryService method resolveServicePropertiesFiles.

/**
 * Resolves situations where a failure condition left the service properties
 * file, and/or the service properties file backup, in an inconsistent
 * state.
 * <p>
 * Note that this method doesn't resolve the situation where both the
 * current service properties file and the backup file are missing.
 *
 * @param sf the storage factory for the service
 * @param spf the service properties file
 * @throws StandardException if a file operation on a service properties
 *      file fails
 */
private void resolveServicePropertiesFiles(StorageFactory sf, StorageFile spf) throws StandardException {
    StorageFile spfOld = sf.newStorageFile(PROPERTIES_NAME.concat("old"));
    FileOperationHelper foh = new FileOperationHelper();
    boolean hasCurrent = foh.exists(spf, true);
    boolean hasBackup = foh.exists(spfOld, true);
    // Shortcut the normal case.
    if (hasCurrent && !hasBackup) {
        return;
    }
    // Backup file, but no current file.
    if (hasBackup && !hasCurrent) {
        // Writing the new service properties file must have failed during
        // an update. Rename the backup to be the current file.
        foh.renameTo(spfOld, spf, true);
        Monitor.getStream().printlnWithHeader(MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_RESTORED));
    // Backup file and current file.
    } else if (hasBackup && hasCurrent) {
        // See if the new (current) file is valid. If so delete the backup,
        // if not, rename the backup to be the current.
        BufferedReader bin = null;
        String lastLine = null;
        try {
            // service.properties always in ISO-8859-1 because written with Properties.store()
            bin = new BufferedReader(new InputStreamReader(new FileInputStream(spf.getPath()), "ISO-8859-1"));
            String line;
            while ((line = bin.readLine()) != null) {
                if (line.trim().length() != 0) {
                    lastLine = line;
                }
            }
        } catch (IOException ioe) {
            throw StandardException.newException(SQLState.UNABLE_TO_OPEN_FILE, ioe, spf.getPath(), ioe.getMessage());
        } finally {
            try {
                if (bin != null) {
                    bin.close();
                }
            } catch (IOException ioe) {
            // Ignore exception during close
            }
        }
        if (lastLine != null && lastLine.startsWith(SERVICE_PROPERTIES_EOF_TOKEN)) {
            // Being unable to delete the backup file is fine as long as
            // the current file appears valid.
            String msg;
            if (foh.delete(spfOld, false)) {
                msg = MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_BACKUP_DELETED);
            } else {
                // Include path so the user can delete file manually.
                msg = MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_BACKUP_DEL_FAILED, getMostAccuratePath(spfOld));
            }
            Monitor.getStream().printlnWithHeader(msg);
        } else {
            foh.delete(spf, false);
            foh.renameTo(spfOld, spf, true);
            Monitor.getStream().printlnWithHeader(MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_RESTORED));
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StorageFile(org.apache.derby.io.StorageFile) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 53 with StorageFile

use of org.apache.derby.io.StorageFile in project derby by apache.

the class LOBStreamControl method init.

private void init(byte[] b, long len) throws IOException, StandardException {
    Object monitor = findService(Property.DATABASE_MODULE, conn.getDBName());
    final DataFactory df = (DataFactory) findServiceModule(monitor, DataFactory.MODULE);
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws IOException {
                // create a temporary file
                StorageFile lobFile = df.getStorageFactory().createTemporaryFile("lob", null);
                if (df.databaseEncrypted()) {
                    tmpFile = new EncryptedLOBFile(lobFile, df);
                } else {
                    tmpFile = new LOBFile(lobFile);
                }
                return null;
            }
        });
    } catch (PrivilegedActionException pae) {
        throw (IOException) pae.getCause();
    }
    conn.addLobFile(tmpFile);
    isBytes = false;
    // now this call will write into the file
    if (len != 0)
        write(b, 0, (int) len, 0);
    dataBytes = null;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) StorageFile(org.apache.derby.io.StorageFile) DataFactory(org.apache.derby.iapi.store.raw.data.DataFactory) IOException(java.io.IOException)

Aggregations

StorageFile (org.apache.derby.io.StorageFile)53 IOException (java.io.IOException)21 StorageRandomAccessFile (org.apache.derby.io.StorageRandomAccessFile)13 File (java.io.File)12 PrivilegedActionException (java.security.PrivilegedActionException)9 StandardException (org.apache.derby.shared.common.error.StandardException)9 StorageFactory (org.apache.derby.io.StorageFactory)6 FileNotFoundException (java.io.FileNotFoundException)4 Properties (java.util.Properties)4 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)4 WritableStorageFactory (org.apache.derby.io.WritableStorageFactory)4 OutputStreamWriter (java.io.OutputStreamWriter)3 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)3 FileInputStream (java.io.FileInputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ContextManager (org.apache.derby.iapi.services.context.ContextManager)2 DataStore (org.apache.derby.impl.io.vfmem.DataStore)2 VirtualFile (org.apache.derby.impl.io.vfmem.VirtualFile)2