Package org.apache.derby.io

Examples of org.apache.derby.io.StorageFile


            if (fileName.equals(filter[j]))
              continue nextFile;
          }
        }

        StorageFile entry = storageFactory.newStorageFile(from, fileName);

        if (entry.isDirectory())
        {
                    if(copySubDirs) {
                        if (!copyDirectory( storageFactory, entry,
                                            new File(to,fileName), buffer,
                                            filter, copySubDirs))
View Full Code Here


     * @return The abstract name of the temp directory;
     */
    static StorageFile getTempDir() throws IOException
    {
        File temp = File.createTempFile("derby", "tmp");
        StorageFile parent = new DirFile( temp.getParent());
        temp.delete();

    return parent;
  } // End of getTempDir
View Full Code Here

    boolean renamed = false;
    boolean renameFailed = false;
    File oldbackup = null;
    File backupcopy = null;
    OutputStreamWriter historyFile = null;
        StorageFile dbHistoryFile = null;
        File backupHistoryFile = null;
    LogInstant backupInstant = logFactory.getFirstUnflushedInstant();
       
    try
    {
      // get name of the current db, ie. database directory of current db.
      StorageFile dbase           = storageFactory.newStorageFile(null);
            String      canonicalDbName = storageFactory.getCanonicalName();
            int         lastSep         =
                canonicalDbName.lastIndexOf(storageFactory.getSeparator());
      String      dbname          =
                canonicalDbName.substring(lastSep + 1);

      // append to end of history file
      historyFile =
                privFileWriter(
                    storageFactory.newStorageFile(BACKUP_HISTORY), true);
           
      backupcopy = new File(backupDir, dbname);

      logHistory(
                historyFile,
                MessageService.getTextMessage(
                    MessageId.STORE_BACKUP_STARTED,
                    canonicalDbName,
                    getFilePath(backupcopy)));

           
            // check if a backup copy of this database already exists,
            if (privExists(backupcopy))
      {
        // first make a backup of the backup
        oldbackup = new File(backupDir, dbname+".OLD");
                if (privExists(oldbackup))
        {
                    if (privIsDirectory(oldbackup))
                        privRemoveDirectory(oldbackup);
          else
                        privDelete(oldbackup);
        }

                if (!privRenameTo(backupcopy,oldbackup))
                {
                    renameFailed = true;
                    throw StandardException.
                        newException(SQLState.RAWSTORE_ERROR_RENAMING_FILE,
                                     backupcopy, oldbackup);
                }
        else
        {
          logHistory(
                        historyFile,
                        MessageService.getTextMessage(
                            MessageId.STORE_MOVED_BACKUP,
                            getFilePath(backupcopy),
                            getFilePath(oldbackup)));
          renamed = true;
        }
      }

            // create the backup database directory
            if (!privMkdirs(backupcopy))
            {
                throw StandardException.newException(
                    SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY,
                    (File) backupcopy);
            }

            dbHistoryFile = storageFactory.newStorageFile(BACKUP_HISTORY);
            backupHistoryFile = new File(backupcopy, BACKUP_HISTORY);

            // copy the history file into the backup.
            if(!privCopyFile(dbHistoryFile, backupHistoryFile))
                throw StandardException.
                    newException(SQLState.RAWSTORE_ERROR_COPYING_FILE,
                                 dbHistoryFile, backupHistoryFile)


            // if they are any jar file stored in the database, copy them into
            // the backup.
            StorageFile jarDir =
                storageFactory.newStorageFile(FileResource.JAR_DIRECTORY_NAME);

            if (privExists(jarDir))
            {
                // find the list of schema directories under the jar dir and
                // then copy only the plain files under those directories. One
                // could just use the recursive copy of directory to copy all
                // the files under the jar dir, but the problem with that is if
                // a user gives jar directory as the backup path by mistake,
                // copy will fail while copying the backup dir onto itself in
                // recursion

                String [] jarSchemaList = privList(jarDir);
                File backupJarDir = new File(backupcopy,
                                             FileResource.JAR_DIRECTORY_NAME);
                // Create the backup jar directory
                if (!privMkdirs(backupJarDir))
                {
                    throw StandardException.newException(
                          SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY,
                          (File) backupJarDir);
                }

                for (int i = 0; i < jarSchemaList.length; i++)
                {
                    StorageFile jarSchemaDir =
                        storageFactory.newStorageFile(jarDir, jarSchemaList[i]);
                    File backupJarSchemaDir =
                        new File(backupJarDir, jarSchemaList[i]);

                    if (!privCopyDirectory(jarSchemaDir, backupJarSchemaDir,
                                           (byte[])null, null, false))
                    {
                        throw StandardException.
                            newException(SQLState.RAWSTORE_ERROR_COPYING_FILE,
                                         jarSchemaDir, backupJarSchemaDir)
                    }
                }
            }


            // save service properties into the backup, Read in property
            // from service.properties file, remove logDevice from it,
            // then write it to the backup.

            StorageFile logdir = logFactory.getLogDirectory();
           
            try
            {
                String name = Monitor.getMonitor().getServiceName(this);
                PersistentService ps =
                    Monitor.getMonitor().getServiceType(this);
                String fullName = ps.getCanonicalServiceName(name);
                Properties prop =
                    ps.getServiceProperties(fullName, (Properties)null);

                StorageFile defaultLogDir =
                    storageFactory.newStorageFile(
                        LogFactory.LOG_DIRECTORY_NAME);

                if (!logdir.equals(defaultLogDir)) 
                {
                    prop.remove(Attribute.LOG_DEVICE);
                    if (SanityManager.DEBUG)
                    {
                        SanityManager.ASSERT(
                            prop.getProperty(Attribute.LOG_DEVICE) == null,
                            "cannot get rid of logDevice property");
                    }

                    logHistory(historyFile,
                               MessageService.getTextMessage(
                               MessageId.STORE_EDITED_SERVICEPROPS));
                }
           
                // save the service properties into the backup.
                ps.saveServiceProperties(backupcopy.getPath(), prop, false);

            }
            catch(StandardException se)
            {
                logHistory(
                   historyFile,
                   MessageService.getTextMessage(
                       MessageId.STORE_ERROR_EDIT_SERVICEPROPS) + se);

                return; // skip the rest and let finally block clean up
            }

            // Incase of encrypted database and the key is an external
            // encryption key, there is an extra file with name 
            // Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE, this file should be
            // copied in to the backup.
            StorageFile verifyKeyFile =
                storageFactory.newStorageFile(
                                 Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE);
            if (privExists(verifyKeyFile))
            {
                File backupVerifyKeyFile =
View Full Code Here

    File backuploc = new File(backupPath);
    String[] fromList = privList(backuploc);
    for(int i =0 ; i < fromList.length ; i++)
    {
      StorageFile toFile = storageFactory.newStorageFile( fromList[i]);
      if(privExists(toFile) ||
         fromList[i].equals(PersistentService.PROPERTIES_NAME)){
        continue;
      }
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 rws 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

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.