Search in sources :

Example 6 with StorageFile

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

the class BaseDataFileFactory method privReleaseJBMSLockOnDB.

private void privReleaseJBMSLockOnDB() throws IOException {
    if (fileLockOnDB != null)
        fileLockOnDB.close();
    if (storageFactory != null) {
        StorageFile fileLock = storageFactory.newStorageFile(DB_LOCKFILE_NAME);
        fileLock.delete();
    }
    // multiple jvm booting the same database on Unix environments.
    if (exFileLock != null)
        exFileLock.releaseExclusiveFileLock();
}
Also used : StorageFile(org.apache.derby.io.StorageFile)

Example 7 with StorageFile

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

the class BaseDataFileFactory method privRestoreDataDirectory.

private void privRestoreDataDirectory() throws StandardException {
    // segment directory in the current db home
    StorageFile csegdir;
    StorageFile dataRoot = // root dir of db
    storageFactory.newStorageFile(null);
    // Remove the seg* directories in the current database home directory
    String[] cfilelist = dataRoot.list();
    if (cfilelist != null) {
        for (int i = 0; i < cfilelist.length; i++) {
            // delete only the seg* directories in the database home
            if (cfilelist[i].startsWith("seg") || Database.LUCENE_DIR.equals(cfilelist[i])) {
                csegdir = storageFactory.newStorageFile(cfilelist[i]);
                if (!csegdir.deleteAll()) {
                    throw StandardException.newException(SQLState.UNABLE_TO_REMOVE_DATA_DIRECTORY, csegdir);
                }
            }
        }
    }
    // copy the seg* directories from backup to current database home
    for (int i = 0; i < bfilelist.length; i++) {
        // copy only the seg* directories and copy them from backup
        if (bfilelist[i].startsWith("seg") || Database.LUCENE_DIR.equals(bfilelist[i])) {
            csegdir = storageFactory.newStorageFile(bfilelist[i]);
            File bsegdir1 = new java.io.File(backupRoot, bfilelist[i]);
            if (!FileUtil.copyDirectory(writableStorageFactory, bsegdir1, csegdir)) {
                throw StandardException.newException(SQLState.UNABLE_TO_COPY_DATA_DIRECTORY, bsegdir1, csegdir);
            }
        } else if (databaseEncrypted && bfilelist[i].startsWith(Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE)) {
            // Case of encrypted database and usage of an external
            // encryption key, there is an extra file with name given by
            // Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE that needs to be
            // copied over during createFrom/restore operations.
            // copy the file
            File fromFile = new File(backupRoot, bfilelist[i]);
            StorageFile toFile = storageFactory.newStorageFile(bfilelist[i]);
            if (!FileUtil.copyFile(writableStorageFactory, fromFile, toFile)) {
                throw StandardException.newException(SQLState.UNABLE_TO_COPY_DATA_DIRECTORY, bfilelist[i], toFile);
            }
        }
    }
}
Also used : StorageFile(org.apache.derby.io.StorageFile) StorageRandomAccessFile(org.apache.derby.io.StorageRandomAccessFile) StorageFile(org.apache.derby.io.StorageFile) File(java.io.File)

Example 8 with StorageFile

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

the class BaseDataFileFactory method privGetJBMSLockOnDB.

// Called from within a privilege block
private void privGetJBMSLockOnDB() throws StandardException {
    boolean fileLockExisted = false;
    String blownUUID = null;
    StorageFile fileLock = storageFactory.newStorageFile(DB_LOCKFILE_NAME);
    try {
        // SECURITY PERMISSION MP1
        if (fileLock.exists()) {
            fileLockExisted = true;
            // see what it says in case we cannot count on delete failing
            // when someone else have an opened file descriptor.
            // I may be blowing this JBMS's lock away
            // SECURITY PERMISSION MP1
            // SECURITY PERMISSION OP4
            fileLockOnDB = fileLock.getRandomAccessFile("rw");
            try {
                blownUUID = fileLockOnDB.readUTF();
            } catch (IOException ioe) {
                // The previous owner of the lock may have died before
                // finish writing its UUID down.
                fileLockExisted = false;
            }
            fileLockOnDB.close();
            fileLockOnDB = null;
            // SECURITY PERMISSION OP5
            if (!fileLock.delete()) {
                throw StandardException.newException(SQLState.DATA_MULTIPLE_JBMS_ON_DB, databaseDirectory);
            }
        }
        // if file does not exists, we grab it immediately - there is a
        // possibility that some other JBMS got to it sooner than we do,
        // check the UUID after we write it to make sure
        // SECURITY PERMISSION MP1
        // SECURITY PERMISSION OP5
        fileLockOnDB = fileLock.getRandomAccessFile("rw");
        fileLock.limitAccessToOwner();
        // write it out for future reference
        fileLockOnDB.writeUTF(myUUID.toString());
        fileLockOnDB.sync();
        fileLockOnDB.seek(0);
        // check the UUID
        UUID checkUUID = uuidFactory.recreateUUID(fileLockOnDB.readUTF());
        if (!checkUUID.equals(myUUID)) {
            throw StandardException.newException(SQLState.DATA_MULTIPLE_JBMS_ON_DB, databaseDirectory);
        }
    } catch (IOException ioe) {
        // probably a read only db, don't do anything more
        readOnly = true;
        try {
            if (fileLockOnDB != null)
                fileLockOnDB.close();
        } catch (IOException ioe2) {
        /* did the best I could */
        }
        fileLockOnDB = null;
        return;
    }
    if (fileLock.delete()) {
        // if I can delete it while I am holding a opened file descriptor,
        // then the file lock is unreliable - send out a warning if I
        // have blown off another JBMS's lock on the DB
        Object[] args = new Object[3];
        args[0] = myUUID;
        args[1] = databaseDirectory;
        args[2] = blownUUID;
        // Try the exlcusive file lock method approach available in jdk1.4 or
        // above jvms where delete machanism  does not reliably prevent
        // double booting of derby databases. If we don't get a reliable
        // exclusive lock still we send out a warning.
        int exLockStatus = StorageFile.NO_FILE_LOCK_SUPPORT;
        // about applying exclusive file lock mechanism
        if (!throwDBlckException) {
            exFileLock = storageFactory.newStorageFile(DB_EX_LOCKFILE_NAME);
            exLockStatus = exFileLock.getExclusiveFileLock();
        }
        if (exLockStatus == StorageFile.NO_FILE_LOCK_SUPPORT) {
            if (fileLockExisted && !throwDBlckException) {
                String warningMsg = MessageService.getTextMessage(SQLState.DATA_MULTIPLE_JBMS_WARNING, args);
                logMsg(warningMsg);
                // RESOLVE - need warning support.  Output to
                // system.err.println rather than just send warning
                // message to derby.log.
                System.err.println(warningMsg);
            }
        }
        // there to warn the next person
        try {
            // again
            if (fileLockOnDB != null)
                fileLockOnDB.close();
            fileLockOnDB = fileLock.getRandomAccessFile("rw");
            fileLock.limitAccessToOwner();
            // write it out for future reference
            fileLockOnDB.writeUTF(myUUID.toString());
            fileLockOnDB.sync();
            fileLockOnDB.close();
        } catch (IOException ioe) {
            try {
                fileLockOnDB.close();
            } catch (IOException ioe2) {
            /* did the best I could */
            }
        } finally {
            fileLockOnDB = null;
        }
        if (fileLockExisted && throwDBlckException) {
            // now that we have reinstated the lock file.
            throw StandardException.newException(SQLState.DATA_MULTIPLE_JBMS_FORCE_LOCK, args);
        }
        if (exLockStatus == StorageFile.EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE) {
            throw StandardException.newException(SQLState.DATA_MULTIPLE_JBMS_ON_DB, databaseDirectory);
        }
    }
}
Also used : StorageFile(org.apache.derby.io.StorageFile) IOException(java.io.IOException) UUID(org.apache.derby.catalog.UUID)

Example 9 with StorageFile

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

the class RawStore method restoreRemainingFromBackup.

/*
	 * Restore any remaining files from backup that are not 
	 * restored by the individual factories.  
	 *  1) copy jar files from backup..
	 *  2) copy backup history file. 
	 */
private void restoreRemainingFromBackup(String backupPath) throws StandardException {
    // if they are any jar files in the backup copy,
    // copy them into the database directory, if they
    // are not already there.
    File backupJarDir = new File(backupPath, FileResource.JAR_DIRECTORY_NAME);
    StorageFile dbJarDir = storageFactory.newStorageFile(FileResource.JAR_DIRECTORY_NAME);
    if (!privExists(dbJarDir) && privExists(backupJarDir)) {
        if (!privCopyDirectory(backupJarDir, dbJarDir)) {
            throw StandardException.newException(SQLState.UNABLE_TO_COPY_FILE_FROM_BACKUP, backupJarDir, dbJarDir);
        }
    }
    // copy the backup history file from the backup.
    StorageFile dbHistoryFile = storageFactory.newStorageFile(BACKUP_HISTORY);
    File backupHistoryFile = new File(backupPath, BACKUP_HISTORY);
    // copy the history file. (DERBY-3035)
    if (privExists(backupHistoryFile) && !privExists(dbHistoryFile))
        if (!privCopyFile(backupHistoryFile, dbHistoryFile))
            throw StandardException.newException(SQLState.RAWSTORE_ERROR_COPYING_FILE, backupHistoryFile, dbHistoryFile);
}
Also used : StorageFile(org.apache.derby.io.StorageFile) StorageFile(org.apache.derby.io.StorageFile) File(java.io.File)

Example 10 with StorageFile

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

the class RawStore method createDataWarningFile.

/**
 * @see RawStoreFactory#createDataWarningFile
 */
public void createDataWarningFile() throws StandardException {
    StorageFile fileReadMe = storageFactory.newStorageFile("seg0", PersistentService.DB_README_FILE_NAME);
    OutputStreamWriter osw = null;
    if (!privExists(fileReadMe)) {
        try {
            osw = privGetOutputStreamWriter(fileReadMe);
            osw.write(MessageService.getTextMessage(MessageId.README_AT_SEG_LEVEL));
        } catch (IOException ioe) {
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException ioe) {
                // Ignore exception on close
                }
            }
        }
    }
}
Also used : StorageFile(org.apache.derby.io.StorageFile) OutputStreamWriter(java.io.OutputStreamWriter) 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