Package org.apache.derby.io

Examples of org.apache.derby.io.StorageFile


        if (fileLockOnDB != null)
            fileLockOnDB.close();

        if (storageFactory != null)
        {
            StorageFile fileLock =
                storageFactory.newStorageFile(DB_LOCKFILE_NAME);

            fileLock.delete();
        }

    //release the lock that is acquired using tryLock() to prevent
    //multiple jvm booting the same database on Unix environments.
    if(exFileLock != null)
View Full Code Here


        }
    }

    private void privRestoreDataDirectory() throws StandardException
    {
        StorageFile csegdir;  //segment directory in the current db home
        StorageFile dataRoot =
            storageFactory.newStorageFile( null); //root dir of db

        //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"))
                {
                    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"))
            {
                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(
View Full Code Here

           
        case GET_TEMP_DIRECTORY_ACTION:
            return storageFactory.getTempDir();

        case REMOVE_TEMP_DIRECTORY_ACTION:
            StorageFile tempDir = storageFactory.getTempDir();
            if( tempDir != null)
                tempDir.deleteAll();
            return null;

        case GET_CONTAINER_PATH_ACTION:
        case GET_ALTERNATE_CONTAINER_PATH_ACTION:
        {
            StringBuffer sb = new StringBuffer("seg");
            sb.append(containerId.getSegmentId());
            sb.append(storageFactory.getSeparator());
            if( actionCode == GET_CONTAINER_PATH_ACTION)
            {
                sb.append(stub ? 'd' : 'c');
                sb.append(Long.toHexString(containerId.getContainerId()));
                sb.append(".dat");
            }
            else
            {
                sb.append(stub ? 'D' : 'C');
                sb.append(Long.toHexString(containerId.getContainerId()));
                sb.append(".DAT");
            }
            return storageFactory.newStorageFile( sb.toString());
        } // end of cases GET_CONTAINER_PATH_ACTION & GET_ALTERNATE_CONTAINER_PATH_ACTION

        case REMOVE_STUBS_ACTION:
        {
            char separator = storageFactory.getSeparator();
            StorageFile root = storageFactory.newStorageFile( null);

            // get all the non-temporary data segment, they start with "seg"
            String[] segs = root.list();
            for (int s = segs.length-1; s >= 0; s--)
            {
                if (segs[s].startsWith("seg"))
                {
                    StorageFile seg =
                        storageFactory.newStorageFile(root, segs[s]);

                    if (seg.exists() && seg.isDirectory())
                    {
                        String[] files = seg.list();
                        for (int f = files.length-1; f >= 0 ; f--)
                        {
                            // stub
                            if (files[f].startsWith("D") ||
                                files[f].startsWith("d"))
                            {
                                StorageFile stub =
                                    storageFactory.newStorageFile(
                                        root, segs[s] + separator + files[f]);

                                boolean delete_status = stub.delete();
                               
                                if (SanityManager.DEBUG)
                                {
                                    // delete should always work, code which
                                    // created the StorageFactory already
                                    // checked for existence.
                                    if (!delete_status)
                                    {
                                        SanityManager.THROWASSERT(
                                            "delete of stub (" +
                                            stub + ") failed.");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            break;
        } // end of case REMOVE_STUBS_ACTION

        case FIND_MAX_CONTAINER_ID_ACTION:
        {
            long maxnum = 1;
            StorageFile seg = storageFactory.newStorageFile( "seg0");

            if (seg.exists() && seg.isDirectory())
            {
                // create an array with names of all files in seg0
                String[] files = seg.list();

                // loop through array looking for maximum containerid.
                for (int f = files.length-1; f >= 0 ; f--)
                {
                    try
                    {
                        long fileNumber =
                          Long.parseLong(
                              files[f].substring(
                                  1, (files[f].length() -4)), 16);

                        if (fileNumber > maxnum)
                            maxnum = fileNumber;
                    }
                    catch (Throwable t)
                    {
                        // ignore errors from parse, it just means that someone
                        // put a file in seg0 that we didn't expect.  Continue
                        // with the next one.
                    }
                }
            }
            return ReuseFactory.getLong( maxnum);
    } // end of case FIND_MAX_CONTAINER_ID_ACTION

        case DELETE_IF_EXISTS_ACTION:
        {
            boolean ret = actionFile.exists() && actionFile.delete();
            actionFile = null;
            return ret ? this : null;
        } // end of case DELETE_IF_EXISTS_ACTION

        case GET_PATH_ACTION:
        {
            String path = actionFile.getPath();
            actionFile = null;
            return path;
        } // end of case GET_PATH_ACTION

        case POST_RECOVERY_REMOVE_ACTION:
        {
      for (Enumeration e = postRecoveryRemovedFiles.elements();
                    e.hasMoreElements(); )
            {
        StorageFile f = (StorageFile) e.nextElement();
        if (f.exists())
                {
          boolean delete_status = f.delete();

                    if (SanityManager.DEBUG)
                    {
                        // delete should always work, code which
                        // created the StorageFactory already
                        // checked for existence.
                        if (!delete_status)
                        {
                            SanityManager.THROWASSERT(
                                "delete of stub (" + stub + ") failed.");
                        }
                    }
                }
      }
            return null;
        }

        case GET_LOCK_ON_DB_ACTION:
            privGetJBMSLockOnDB();
            return null;

        case RELEASE_LOCK_ON_DB_ACTION:
            privReleaseJBMSLockOnDB();
            return null;

        case RESTORE_DATA_DIRECTORY_ACTION:
            privRestoreDataDirectory();
            return null;
    case GET_CONTAINER_NAMES_ACTION:
        {
            StorageFile seg = storageFactory.newStorageFile( "seg0");
            if (seg.exists() && seg.isDirectory())
            {
                // return the  names of all files in seg0
        return seg.list();
            }
            return null;
        // end of case GET_CONTAINER_NAMES_ACTION
   
    }
View Full Code Here

        free();
    }

    private void deleteFile (StorageFile file) throws IOException {
        try {
            final StorageFile sf = file;
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
                public Object run() throws IOException {
                    sf.delete();
                    return null;
                }
            });
        } catch (PrivilegedActionException pae) {
            Exception e = pae.getException();
View Full Code Here

            //save over file handle and
            //create new file with 0 size
           
            byte tmp [] = new byte [0];
            LOBFile oldFile = tmpFile;
            StorageFile oldStoreFile = lobFile;
            init (tmp, 0);
            byte [] tmpByte = new byte [1024];
            long sz = stPos;
            oldFile.seek(0);
            while (sz != 0) {
View Full Code Here

    protected StorageFile privGetFileName(ContainerKey identity, boolean stub,
                                    boolean errorOK, boolean tryAlternatePath)
        throws StandardException
    {
        StorageFile container = dataFactory.getContainerPath( identity, stub);

        // retry with small case 'c' and 'd'
        // bug fix for track 3444
        if (!container.exists() && tryAlternatePath)
        {
            container = dataFactory.getAlternateContainerPath( identity, stub);
        }

        if (!container.exists()) {

            StorageFile directory = container.getParentDir();

            if (!directory.exists())
            {
                // make sure only 1 thread can create a segment at one time
                synchronized(dataFactory)
                {
                    if (!directory.exists())
                    {
                        if (!directory.mkdirs())
                        {
                            if (errorOK)
                            {
                                return null;
                            }
View Full Code Here

                }
     
                // create container at the backup location.
                if (isStub) {
                    // get the stub ( it is a committted drop table container )
                    StorageFile file = privGetFileName((ContainerKey)getIdentity(),
                                                       true, false, true);
                    backupFile = new File(backupLocation, file.getName());

          // directly copy the stub to the backup
          if(!FileUtil.copyFile(dataFactory.getStorageFactory(),
                                          file, backupFile))
                    {
                        throw StandardException.newException(
                                              SQLState.RAWSTORE_ERROR_COPYING_FILE,
                                              file, backupFile);
                    }
                }else {
                    // regular container file
                    long lastPageNumber= getLastPageNumber(handle);
                    if (lastPageNumber == ContainerHandle.INVALID_PAGE_NUMBER) {
                        // last page number is invalid if there are no pages in
                        // the container yet. No need to backup this container,
                        // this container creation is yet to complete.The reason
                        // backup is getting called on such a container is
                        // because container handle appears in the cache after
                        // the file is created on the disk but before it's
                        // first page is allocated.
                        return;
                    }

                    StorageFile file =
                        privGetFileName(
                            (ContainerKey)getIdentity(), false, false, true);

                    backupFile = new File(backupLocation , file.getName());
                    backupRaf  = new RandomAccessFile(backupFile,  "rw");

                    byte[] encryptionBuf = null;
                    if (dataFactory.databaseEncrypted()) {
                        // Backup uses seperate encryption buffer to encrypt the
View Full Code Here

    protected void encryptContainer(BaseContainerHandle handle,
                                    String newFilePath
        throws StandardException
    {
        BasePage page = null;
        StorageFile newFile =
            dataFactory.getStorageFactory().newStorageFile(newFilePath);
        StorageRandomAccessFile newRaf = null;
        try {
            long lastPageNumber= getLastPageNumber(handle);
View Full Code Here

         case GET_FILE_NAME_ACTION:
             return privGetFileName( actionIdentity, actionStub, actionErrorOK, actionTryAlternatePath);

         case CREATE_CONTAINER_ACTION:
         {
             StorageFile file = privGetFileName( actionIdentity, false, false, false);

             try {
                 if (file.exists()) {
                     // note I'm left in the no-identity state as fillInIdentity()
                     // hasn't been called.
                     throw StandardException.newException( SQLState.FILE_EXISTS, file);
                 }
             } catch (SecurityException se) {
                 throw StandardException.newException( SQLState.FILE_CREATE, se, file);
             }

             try {

                 // OK not to force WAL here, in fact, this operation preceeds the
                 // creation of the log record to ensure sufficient space.

                 dataFactory.writeInProgress();
                 try
                 {
                     fileData = file.getRandomAccessFile( "rw");
                 }
                 finally
                 {
                     dataFactory.writeFinished();
                 }

                 // This container format specifies that the first page is an
                 // allocation page and the container information is stored within
                 // it.  The allocation page needs to be somewhat formatted
                 // because if the system crashed after the create container log
                 // operation is written, it needs to be well formed enough to get
                 // the container information back out of it.
                 //
                 // Don't try to go thru the page cache here because the container
                 // object cannot be found in the container cache at this point
                 // yet.  However, if we use the page cache to store the first
                 // allocation page, then in order to write itself out, it needs to
                 // ask the container to do so, which is going to create a
                 // deadlock.  The allocation page cannot write itself out without
                 // going thru the container because it doesn't know where its
                 // offset is.  Here we effectively hardwired page 0 at offset 0 of
                 // the container file to be the first allocation page.

                 // create an embryonic page - if this is not a temporary container,
                 // synchronously write out the file header.
                 writeRAFHeader(fileData, true,
                                (actionIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT));

             } catch (SecurityException se) {

                 // only thrown by the RandomeAccessFile constructor,
                 // so the file won't exist
                 throw StandardException.newException( SQLState.FILE_CREATE, se, file);

             } catch (IOException ioe) {

                 boolean fileDeleted;
                 try {
                     fileDeleted = privRemoveFile(file);
                 } catch (SecurityException se) {
                     throw StandardException.newException( SQLState.FILE_CREATE_NO_CLEANUP, ioe, file, se.toString());
                 }

                 if (!fileDeleted) {
                     throw StandardException.newException( SQLState.FILE_CREATE_NO_CLEANUP, ioe, file, ioe.toString());
                 }

                 throw StandardException.newException( SQLState.FILE_CREATE, ioe, file);
             }

             canUpdate = true;
             return null;
         } // end of case CREATE_CONTAINER_ACTION

         case REMOVE_FILE_ACTION:
             return privRemoveFile( actionFile) ? this : null;

         case OPEN_CONTAINER_ACTION:
         {
             boolean isStub = false// is this a stub?

             StorageFile file = privGetFileName( actionIdentity, false, true, true);
             if (file == null)
                 return null;

             try {
                 if (!file.exists()) {

                     // file does not exist, may be it has been stubbified
                     file = privGetFileName( actionIdentity, true, true, true);
                     if (!file.exists())
                         return null;
                     isStub = true;
                 }
             } catch (SecurityException se) {
                 throw StandardException.newException(
                     SQLState.DATA_UNEXPECTED_EXCEPTION, se);
             }

             canUpdate = false;
             try {
                 if (!dataFactory.isReadOnly() && file.canWrite())
                     canUpdate = true;
             } catch (SecurityException se) {
                 // just means we can't write to it.
             }

             try {

                 fileData = file.getRandomAccessFile(canUpdate ? "rw" : "r");
                 readHeader(getEmbryonicPage(fileData,
                                             FIRST_ALLOC_PAGE_OFFSET));

                 if (SanityManager.DEBUG)
                 {
                     if (isStub)
                         SanityManager.ASSERT(getDroppedState() && getCommittedDropState(),
                                              "a stub failed to set drop state");
                 }

             } catch (IOException ioe) {

                 if (isStub)
                 {
                     throw dataFactory.
                         markCorrupt(StandardException.
                                     newException(SQLState.
                                                  FILE_CONTAINER_EXCEPTION,
                                                  ioe, this));
                 }

                 // maybe it is being stubbified... try that
                 StorageFile stub =
                     privGetFileName(actionIdentity, true, true, true);

                 if (stub.exists())
                 {
                     try
                     {
                         boolean delete_status = privRemoveFile(file);
                         if (SanityManager.DEBUG)
                         {
                             if (!delete_status)
                             {
                                 SanityManager.THROWASSERT(
                                     "delete of file (" + file + ") failed.");
                             }
                         }

                         fileData =
                             stub.getRandomAccessFile(canUpdate ? "rw" : "r");

                         readHeader(getEmbryonicPage(fileData,
                                                     FIRST_ALLOC_PAGE_OFFSET));
                     }
                     catch (IOException ioe2)
                     {
                         throw dataFactory.
                             markCorrupt(StandardException.
                                         newException(SQLState.
                                                      FILE_CONTAINER_EXCEPTION,
                                                      ioe2, this));
                     }

                     // RESOLVE: this is a temporary hack

                 }
                 else
                     throw dataFactory.
                         markCorrupt(StandardException.
                                     newException(SQLState.
                                                  FILE_CONTAINER_EXCEPTION,
                                                  ioe, this));
             }

             return this;
         } // end of case OPEN_CONTAINER_ACTION

         case STUBBIFY_ACTION:
         {
             StorageFile file = privGetFileName( actionIdentity, false, false, true);
             StorageFile stub = privGetFileName( actionIdentity, true, false, false);

             StorageRandomAccessFile stubData = null;

             try
             {
                 // !!!!!
                 // bumpContainerVersion();
                 //
                 // do NOT bump the container version.  We WANT the stubbify
                 // operation to get redone every time.  This is because this
                 // operation first writes out the stub and then remove the
                 // container file.  If we bump the version, then the stub will
                 // contain the new version.  And if the system crashes right then,
                 // then we will skip the whole operation during redo even though
                 // the container file may not have been removed.  Since we don't
                 // want to have the remove happen before the stub is written, we
                 // cannot sync it and therefore cannot be sure the remove
                 // happened before the system crashed.

                 if (!stub.exists())
                 {
                     // write the header to the stub
                     stubData = stub.getRandomAccessFile( "rw");

                     writeRAFHeader(stubData,
                                    true, /* create */
                                    true); /* sync */

                     stubData.close();
                     stubData = null;
                 }


                 // Force WAL and check for database corruption before removing file.
                 // This is one operation where the container is changed on disk
                 // directly without going thru the container cache, which otherwise
                 // would have force WAL.  Take care of it here.
                 dataFactory.flush(actionInstant);

                 // try to remove the container file
                 // fileDate is not null only if we are redoing a removeContainer
                 // (stubbify) operation.  Then fileData acutally is opened against
                 // the stub and the original container file does not exist.
                 // Then we need to close it here because this method is called by
                 // cache.remove and nobody will be able to see fileData after this.
                 privRemoveFile(file);

             }
             catch (SecurityException se)
             {
                 throw StandardException.
                     newException(SQLState.FILE_CANNOT_REMOVE_FILE, se, file,
                                  se.toString());
             }
             catch (IOException ioe)
             {
                 // exception thrown while in creating the stub.  Remove the
                 // (half-baked) stub
                 try
                 {
                     if (stubData != null)
                     {
                         stubData.close();
                         stub.delete();
                         stubData = null;
                     }

                     if (fileData != null)
                     {
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

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.