Package org.apache.derby.io

Examples of org.apache.derby.io.StorageFile


    // 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);
 
    // if this is a roll-forward recovery, backup history file
    // will already there in the database and will be the latest
View Full Code Here


                // doing updates with new values.

                if (externalKeyEncryption)
                {
                    // save the current copy of verify key file.
                    StorageFile verifyKeyFile =
                        storageFactory.newStorageFile(
                                 Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE);
                    StorageFile oldVerifyKeyFile =
                        storageFactory.newStorageFile(
                          RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE);

                    if(!privCopyFile(verifyKeyFile, oldVerifyKeyFile))
                        throw StandardException.
                            newException(SQLState.RAWSTORE_ERROR_COPYING_FILE,
                                         verifyKeyFile, oldVerifyKeyFile);

                    // update the verify key file with the new key info.
                    currentCipherFactory.verifyKey(reEncrypt,
                                                   storageFactory,
                                                   properties);
                } else
                {
                    // save the current generated encryption key
                    String keyString =
                        properties.getProperty(
                                               RawStoreFactory.ENCRYPTED_KEY);
                    if (keyString != null)
                        properties.put(RawStoreFactory.OLD_ENCRYPTED_KEY,
                                       keyString);
                }
            } else
            {
                // save the encryption block size;
                properties.put(RawStoreFactory.ENCRYPTION_BLOCKSIZE,
                               String.valueOf(encryptionBlockSize));
            }

            // save the new encryption properties into service.properties
            currentCipherFactory.saveProperties(properties) ;
            if (SanityManager.DEBUG) {
                crashOnDebugFlag(
                                 TEST_REENCRYPT_CRASH_AFTER_SWITCH_TO_NEWKEY,
                                 reEncrypt);
            }

            // commit the transaction that is used to
            // (re) encrypt the database. Note that
            // this will be logged with newly generated
            // encryption key in the new log file created
            // above.
            transaction.commit();

            if (SanityManager.DEBUG) {
                crashOnDebugFlag(TEST_REENCRYPT_CRASH_AFTER_COMMT,
                                 reEncrypt);
            }

            // force the checkpoint with new encryption key.
            logFactory.checkpoint(this, dataFactory, xactFactory, true);

            if (SanityManager.DEBUG) {
                crashOnDebugFlag(TEST_REENCRYPT_CRASH_AFTER_CHECKPOINT,
                                 reEncrypt);
            }

            // once the checkpont makes it to the log, re-encrption
            // is complete. only cleanup is remaining ; update the
            // re-encryption status flag to cleanup.
            properties.put(RawStoreFactory.DB_ENCRYPTION_STATUS,
                           String.valueOf(
                               RawStoreFactory.DB_ENCRYPTION_IN_CLEANUP));

            // database is (re)encrypted successfuly,
            // remove the old version of the container files.
            dataFactory.removeOldVersionOfContainers(false);
               
            if (reEncrypt)
            {
                if (externalKeyEncryption)
                {
                    // remove the saved copy of the verify.key file
                    StorageFile oldVerifyKeyFile =
                        storageFactory.newStorageFile(
                          RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE);
                    if (!privDelete(oldVerifyKeyFile))
                        throw StandardException.newException(
                                    SQLState.UNABLE_TO_DELETE_FILE,
View Full Code Here

            // check if this is a external key encryption and
            // if it replace the current verify key file with
            // the old copy.

            StorageFile verifyKeyFile =
                storageFactory.newStorageFile(
                                 Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE);
           
            if (privExists(verifyKeyFile))
            {
                StorageFile oldVerifyKeyFile =
                    storageFactory.newStorageFile(
                      RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE);
           
                if (privExists(oldVerifyKeyFile))
                {
                    if(!privCopyFile(oldVerifyKeyFile, verifyKeyFile))
                        throw StandardException.
                            newException(SQLState.RAWSTORE_ERROR_COPYING_FILE,
                                         oldVerifyKeyFile, verifyKeyFile)
                   
                    // only incase of re-encryption there should
                    // be old verify key file.
                    reEncryption = true;
                }else
                {
                    // remove the verify key file.
                    if (!privDelete(verifyKeyFile))
                        throw StandardException.newException(
                             SQLState.UNABLE_TO_DELETE_FILE,
                             verifyKeyFile);
                }

            } else
            {
                // database enrypted with boot password.
               
                // replace the current encryption key with the old key
                // in the service.properties file.
                // retreive the old encryption key

                String OldKeyString =
                    properties.getProperty(RawStoreFactory.OLD_ENCRYPTED_KEY);

                if (OldKeyString != null) {
                    // set the current encrypted key to the old one.
                    properties.put(RawStoreFactory.ENCRYPTED_KEY,
                                   OldKeyString);
                   
                    // only incase of re-encryption there should
                    // be old encryted key .
                    reEncryption = true;
                }
            }

            if (!reEncryption) {
                // crash occured when database was getting reconfigured
                // for encryption , all encryption properties should be
                // removed from service.properties
               
                // common props for external key or password.
                properties.remove(Attribute.DATA_ENCRYPTION);
                properties.remove(RawStoreFactory.LOG_ENCRYPT_ALGORITHM_VERSION);
                properties.remove(RawStoreFactory.DATA_ENCRYPT_ALGORITHM_VERSION);
                properties.remove(RawStoreFactory.ENCRYPTION_BLOCKSIZE);

                // properties specific to password based encryption.
                properties.remove(Attribute.CRYPTO_KEY_LENGTH);
                properties.remove(Attribute.CRYPTO_PROVIDER);
                properties.remove(Attribute.CRYPTO_ALGORITHM);
                properties.remove(RawStoreFactory.ENCRYPTED_KEY);

            }

            if (SanityManager.DEBUG) {
                crashOnDebugFlag(
                    TEST_REENCRYPT_CRASH_AFTER_RECOVERY_UNDO_REVERTING_KEY,
                    reEncryption);
            }

        } // end of UNDO


        if (dbEncryptionStatus == RawStoreFactory.DB_ENCRYPTION_IN_CLEANUP)
        {
            // remove all the old versions of the  containers.
            dataFactory.removeOldVersionOfContainers(true);
        }
       
        if (SanityManager.DEBUG) {
                crashOnDebugFlag(
                   TEST_REENCRYPT_CRASH_BEFORE_RECOVERY_FINAL_CLEANUP,
                   reEncryption);
        }

        // either the (re) encryption was complete ,
        // or undone (except for rollback that needs to be
        // done by the recovery). Remove re-encryption specific
        // flags from the service.properties and old copy
        // of the verify key file.
       
        // delete the old verify key file , if it exists.
        StorageFile oldVerifyKeyFile =
            storageFactory.newStorageFile(
                      RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE);
        if (privExists(oldVerifyKeyFile))
        {
            if (!privDelete(oldVerifyKeyFile))
View Full Code Here

                // some way ...
        if (logEnd == LogCounter.INVALID_LOG_INSTANT)
        {
          Monitor.logTextMessage(MessageId.LOG_LOG_NOT_FOUND);

          StorageFile logFile = getLogFileName(logFileNumber);

                    if (privExists(logFile))
          {
            // if we can delete this strange corrupted file, do so,
            // otherwise, skip it
                        if (!privDelete(logFile))
            {
              logFile = getLogFileName(++logFileNumber);
            }
          }

          try
          {
                        theLog =   privRandomAccessFile(logFile, "rw");
          }
          catch (IOException ioe)
          {
            theLog = null;
          }

                    if (theLog == null || !privCanWrite(logFile))
          {
            if (theLog != null)
              theLog.close();

            theLog = null;

            ReadOnlyDB = true;
          }
          else
          {
            try
            {
              // no previous log file or previous log position
              if (!initLogFile(
                                    theLog, logFileNumber,
                                    LogCounter.INVALID_LOG_INSTANT))
                            {
                throw markCorrupt(
                                    StandardException.newException(
                                        SQLState.LOG_SEGMENT_NOT_EXIST,
                                        logFile.getPath()));
                            }
            }
            catch (IOException ioe)
            {
              throw markCorrupt(
                                StandardException.newException(
                                    SQLState.LOG_IO_ERROR, ioe));
            }

                        // successfully init'd the log file - set up markers,
                        // and position at the end of the log.
            endPosition = theLog.getFilePointer();
            lastFlush   = endPosition;
           
            //if write sync is true , prellocate the log file
            //and reopen the file in rwd mode.
            if(isWriteSynced)
            {
              //extend the file by wring zeros to it
              preAllocateNewLogFile(theLog);
              theLog.close();
              theLog = openLogFileInWriteMode(logFile);
              //postion the log at the current end postion
              theLog.seek(endPosition);
            }
           
            if (SanityManager.DEBUG)
            {
              SanityManager.ASSERT(
                                endPosition == LOG_FILE_HEADER_SIZE,
                                "empty log file has wrong size");
            }
           
            //because we already incrementing the log number
            //here, no special log switch required for
            //backup recoveries.
            logSwitchRequired = false;
          }
        }
        else
        {
          // logEnd is the instant of the next log record in the log
          // it is used to determine the last known good position of
          // the log
          logFileNumber = LogCounter.getLogFileNumber(logEnd);

          ReadOnlyDB = df.isReadOnly();

          StorageFile logFile = getLogFileName(logFileNumber);

          if (!ReadOnlyDB)
          {
            // if datafactory doesn't think it is readonly, we can
            // do some futher test of our own
View Full Code Here

        return;
      }

      // log file isn't being flushed right now and logOut is not being
      // used.
      StorageFile newLogFile = getLogFileName(logFileNumber+1);

      if (logFileNumber+1 >= maxLogFileNumber)
            {
        throw StandardException.newException(
                        SQLState.LOG_EXCEED_MAX_LOG_FILE_NUMBER,
                        new Long(maxLogFileNumber));
            }

      StorageRandomAccessFile newLog = null// the new log file
      try
      {
        // if the log file exist and cannot be deleted, cannot
        // switch log right now
                if (privExists(newLogFile) && !privDelete(newLogFile))
        {
          logErrMsg(MessageService.getTextMessage(
                        MessageId.LOG_NEW_LOGFILE_EXIST,
              newLogFile.getPath()));
          return;
        }

        try
        {
                    newLog =   privRandomAccessFile(newLogFile, "rw");
        }
        catch (IOException ioe)
        {
          newLog = null;
        }

                if (newLog == null || !privCanWrite(newLogFile))
        {
          if (newLog != null)
            newLog.close();
          newLog = null;

          return;
        }

        if (initLogFile(newLog, logFileNumber+1,
                LogCounter.makeLogInstantAsLong(logFileNumber, endPosition)))
        {

          // New log file init ok, close the old one and
          // switch over, after this point, need to shutdown the
          // database if any error crops up
          switchedOver = true;

          // write out an extra 0 at the end to mark the end of the log
          // file.
         
          logOut.writeEndMarker(0);

          endPosition += 4;
          //set that we are in log switch to prevent flusher
          //not requesting  to switch log again
          inLogSwitch = true;
          // flush everything including the int we just wrote
          flush(logFileNumber, endPosition);
         
         
          // simulate out of log error after the switch over
          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON(TEST_SWITCH_LOG_FAIL2))
              throw new IOException("TestLogSwitchFail2");
          }


          logOut.close();    // close the old log file
         
          logWrittenFromLastCheckPoint += endPosition;

          endPosition = newLog.getFilePointer();
          lastFlush = endPosition;
         
          if(isWriteSynced)
          {
            //extend the file by wring zeros to it
            preAllocateNewLogFile(newLog);
            newLog.close();
            newLog = openLogFileInWriteMode(newLogFile);
            newLog.seek(endPosition);
          }

          logOut = new LogAccessFile(this, newLog, logBufferSize);
          newLog = null;


          if (SanityManager.DEBUG)
          {
            if (endPosition != LOG_FILE_HEADER_SIZE)
              SanityManager.THROWASSERT(
                      "new log file has unexpected size" +
                       + endPosition);
          }
          logFileNumber++;

          if (SanityManager.DEBUG)
          {
            SanityManager.ASSERT(endPosition == LOG_FILE_HEADER_SIZE,
                       "empty log file has wrong size");
          }

        }
        else  // something went wrong, delete the half baked file
        {
          newLog.close();
          newLog = null;

          if (privExists(newLogFile))
              privDelete(newLogFile);
          newLogFile = null;

          logErrMsg(MessageService.getTextMessage(
                        MessageId.LOG_CANNOT_CREATE_NEW,
                        newLogFile.getPath()));
         }

      }
      catch (IOException ioe)
      {

        inLogSwitch = false;
        // switching log file is an optional operation and there is no direct user
        // control.  Just sends a warning message to whomever, if any,
        // system adminstrator there may be

                logErrMsg(MessageService.getTextMessage(
                    MessageId.LOG_CANNOT_CREATE_NEW_DUETO,
                    newLogFile.getPath(),
                    ioe.toString()));

        try
        {
          if (newLog != null)
View Full Code Here

    oldFirstLog = firstLogFileNumber;
    firstLogFileNumber = firstLogNeeded;

    while(oldFirstLog < firstLogNeeded)
    {
      StorageFile uselessLogFile = null;
      try
      {
        uselessLogFile = getLogFileName(oldFirstLog);
                if (privDelete(uselessLogFile))
        {
          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
              SanityManager.DEBUG(DBG_FLAG, "truncating useless log file " + uselessLogFile.getPath());
          }
        }
        else
        {
          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
              SanityManager.DEBUG(DBG_FLAG, "Fail to truncate useless log file " + uselessLogFile.getPath());
          }
        }
      }
      catch (StandardException se)
      {
        if (SanityManager.DEBUG)
          SanityManager.THROWASSERT("error opening log segment while deleting "
                        + uselessLogFile.getPath(), se);

        // if insane, just leave it be
      }

      oldFirstLog++;
View Full Code Here

     * Create the directory where transaction log should go.
     * @exception StandardException Standard Error Policy
    */
  private void createLogDirectory() throws StandardException
  {
    StorageFile logDir =
            logStorageFactory.newStorageFile(LogFactory.LOG_DIRECTORY_NAME);

        if (privExists(logDir)) {
            // make sure log directory is empty.
            String[] logfiles = privList(logDir);
            if (logfiles != null) {
                if(logfiles.length != 0) {
                    throw StandardException.newException(
                        SQLState.LOG_SEGMENT_EXIST, logDir.getPath());
                }
            }
           
        }else {
            // create the log directory.
            if (!privMkdirs(logDir)) {
                throw StandardException.newException(
                    SQLState.LOG_SEGMENT_NOT_EXIST, logDir.getPath());
            }
        }
  }
View Full Code Here

    <P> MT- read only
    @exception StandardException Derby Standard Error Policy
  */
  public StorageFile getLogDirectory() throws StandardException
  {
    StorageFile logDir = null;

    logDir = logStorageFactory.newStorageFile( LogFactory.LOG_DIRECTORY_NAME);

        if (!privExists(logDir))
    {
      throw StandardException.newException(
                    SQLState.LOG_SEGMENT_NOT_EXIST, logDir.getPath());
    }

    return logDir;
  }
View Full Code Here

    checkCorrupt();

    long filenum = LogCounter.getLogFileNumber(logInstant);
    long filepos = LogCounter.getLogFilePosition(logInstant);

    StorageFile fileName = getLogFileName(filenum);
        if (!privExists(fileName))
    {
      if (SanityManager.DEBUG)
      {
        if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
          SanityManager.DEBUG(LogToFile.DBG_FLAG, fileName.getPath() + " does not exist");
      }

      return null;
    }


    StorageRandomAccessFile log = null;

    try
    {
            log = privRandomAccessFile(fileName, "r");

      // verify that the log file is of the right format
      if (!verifyLogFormat(log, filenum))
      {
        if (SanityManager.DEBUG)
        {
          if (SanityManager.DEBUG_ON(LogToFile.DBG_FLAG))
            SanityManager.DEBUG(LogToFile.DBG_FLAG, fileName.getPath() + " format mismatch");
        }

        log.close();
        log = null;
      }
      else
      {
        log.seek(filepos);
      }
    }
    catch (IOException ioe)
    {
      try
      {
        if (log != null)
        {
          log.close();
          log = null;
        }

        if (SanityManager.DEBUG)
        {
          SanityManager.THROWASSERT("cannot get to position " + filepos +
                        " for log file " + fileName.getPath(), ioe);
        }
      }
      catch (IOException ioe2)
      {}
      throw ioe;
View Full Code Here

                            home = relativeRoot.getPath();
                            canonicalHome = relativeRoot.getCanonicalPath();
                            rootStorageFactory = getStorageFactoryInstance( true, null, null, null);
                            if( home != null)
                            {
                                StorageFile rootDir = rootStorageFactory.newStorageFile( null);
                                rootDir.mkdirs();
                            }
                            return null;
                        }
                    }
                    );
View Full Code Here

TOP

Related Classes of org.apache.derby.io.StorageFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.