1 21 22 package org.apache.derby.impl.store.raw; 23 24 import org.apache.derby.iapi.services.daemon.DaemonFactory; 25 import org.apache.derby.iapi.services.daemon.DaemonService; 26 import org.apache.derby.iapi.services.context.ContextManager; 27 import org.apache.derby.iapi.services.context.ContextService; 28 import org.apache.derby.iapi.services.crypto.CipherFactoryBuilder; 29 import org.apache.derby.iapi.services.crypto.CipherFactory; 30 import org.apache.derby.iapi.services.crypto.CipherProvider; 31 import org.apache.derby.iapi.services.locks.LockFactory; 32 import org.apache.derby.iapi.services.monitor.Monitor; 33 import org.apache.derby.iapi.services.monitor.ModuleControl; 34 import org.apache.derby.iapi.services.monitor.ModuleSupportable; 35 import org.apache.derby.iapi.services.monitor.PersistentService; 36 import org.apache.derby.iapi.services.sanity.SanityManager; 37 38 import org.apache.derby.iapi.error.StandardException; 39 import org.apache.derby.iapi.services.i18n.MessageService; 40 41 import org.apache.derby.iapi.services.property.PersistentSet; 42 import org.apache.derby.iapi.store.access.TransactionInfo; 43 import org.apache.derby.iapi.store.access.AccessFactoryGlobals; 44 import org.apache.derby.iapi.store.access.FileResource; 45 import org.apache.derby.iapi.store.raw.ScanHandle; 46 import org.apache.derby.iapi.store.raw.RawStoreFactory; 47 import org.apache.derby.iapi.store.raw.Transaction; 48 import org.apache.derby.iapi.store.raw.xact.RawTransaction; 49 import org.apache.derby.iapi.store.raw.xact.TransactionFactory; 50 import org.apache.derby.iapi.store.raw.data.DataFactory; 51 import org.apache.derby.iapi.store.raw.log.LogFactory; 52 import org.apache.derby.iapi.store.raw.log.LogInstant; 53 import org.apache.derby.impl.services.monitor.UpdateServiceProperties; 54 55 import org.apache.derby.io.StorageFactory; 56 import org.apache.derby.io.WritableStorageFactory; 57 import org.apache.derby.io.StorageFile; 58 import org.apache.derby.iapi.store.access.DatabaseInstant; 59 import org.apache.derby.catalog.UUID; 60 import org.apache.derby.iapi.services.property.PropertyUtil; 61 import org.apache.derby.iapi.services.io.FileUtil; 62 import org.apache.derby.iapi.util.ReuseFactory; 63 import org.apache.derby.iapi.util.StringUtil; 64 import org.apache.derby.iapi.reference.Attribute; 65 import org.apache.derby.iapi.reference.SQLState; 66 import org.apache.derby.iapi.reference.MessageId; 67 import org.apache.derby.iapi.reference.Property; 68 69 import java.security.AccessController ; 70 import java.security.PrivilegedActionException ; 71 import java.security.PrivilegedExceptionAction ; 72 import java.security.SecureRandom ; 73 74 import java.util.Date ; 75 import java.util.Properties ; 76 import java.io.Serializable ; 77 import java.io.File ; 78 import java.io.FileOutputStream ; 79 import java.io.FileInputStream ; 80 import java.io.IOException ; 81 import java.io.FileNotFoundException ; 82 import java.io.OutputStreamWriter ; 83 84 import java.net.MalformedURLException ; 85 import java.net.URL ; 86 87 import java.security.PrivilegedExceptionAction ; 88 import java.lang.SecurityException ; 89 90 91 102 103 public final class RawStore implements RawStoreFactory, ModuleControl, ModuleSupportable, PrivilegedExceptionAction 104 { 105 private static final String BACKUP_HISTORY = "BACKUP.HISTORY"; 106 protected TransactionFactory xactFactory; 107 protected DataFactory dataFactory; 108 protected LogFactory logFactory; 109 private StorageFactory storageFactory; 110 111 private SecureRandom random; 112 private boolean databaseEncrypted; 113 private boolean encryptDatabase; 114 private CipherProvider encryptionEngine; 115 private CipherProvider decryptionEngine; 116 private CipherProvider newEncryptionEngine; 117 private CipherProvider newDecryptionEngine; 118 private CipherFactory currentCipherFactory; 119 private CipherFactory newCipherFactory = null; 120 private int counter_encrypt; 121 private int counter_decrypt; 122 private int encryptionBlockSize = RawStoreFactory.DEFAULT_ENCRYPTION_BLOCKSIZE; 123 124 String dataDirectory; 126 protected DaemonService rawStoreDaemon; 128 129 private int actionCode; 130 private static final int FILE_WRITER_ACTION = 1; 131 private StorageFile actionStorageFile; 132 private StorageFile actionToStorageFile; 133 private boolean actionAppend; 134 private static final int REGULAR_FILE_EXISTS_ACTION = 2; 135 private File actionRegularFile; 136 private static final int STORAGE_FILE_EXISTS_ACTION = 3; 137 private static final int REGULAR_FILE_DELETE_ACTION = 4; 138 private static final int REGULAR_FILE_MKDIRS_ACTION = 5; 139 private static final int REGULAR_FILE_IS_DIRECTORY_ACTION = 6; 140 private static final int REGULAR_FILE_REMOVE_DIRECTORY_ACTION = 7; 141 private static final int REGULAR_FILE_RENAME_TO_ACTION = 8; 142 private File actionRegularFile2; 143 private static final int COPY_STORAGE_DIRECTORY_TO_REGULAR_ACTION = 9; 144 private byte[] actionBuffer; 145 private String [] actionFilter; 146 private boolean actionCopySubDirs; 147 private static final int COPY_REGULAR_DIRECTORY_TO_STORAGE_ACTION = 10; 148 private static final int COPY_REGULAR_FILE_TO_STORAGE_ACTION = 11; 149 private static final int REGULAR_FILE_LIST_DIRECTORY_ACTION = 12; 150 private static final int STORAGE_FILE_LIST_DIRECTORY_ACTION = 13; 151 private static final int COPY_STORAGE_FILE_TO_REGULAR_ACTION = 14; 152 private static final int REGULAR_FILE_GET_CANONICALPATH_ACTION = 15; 153 private static final int STORAGE_FILE_GET_CANONICALPATH_ACTION = 16; 154 private static final int COPY_STORAGE_FILE_TO_STORAGE_ACTION = 17; 155 private static final int STORAGE_FILE_DELETE_ACTION = 18; 156 157 public RawStore() { 158 } 159 160 163 164 167 public boolean canSupport(Properties startParams) { 168 return true; 169 } 170 171 public void boot(boolean create, Properties properties) 172 throws StandardException 173 { 174 dataDirectory = properties.getProperty(PersistentService.ROOT); 175 DaemonFactory daemonFactory = 176 (DaemonFactory)Monitor.startSystemModule(org.apache.derby.iapi.reference.Module.DaemonFactory); 177 rawStoreDaemon = daemonFactory.createNewDaemon("rawStoreDaemon"); 178 xactFactory = (TransactionFactory) 179 Monitor.bootServiceModule( 180 create, this, getTransactionFactoryModule(), properties); 181 182 dataFactory = (DataFactory) 183 Monitor.bootServiceModule( 184 create, this, getDataFactoryModule(), properties); 185 storageFactory = dataFactory.getStorageFactory(); 186 187 String restoreFromBackup = null; 188 189 if (properties != null) 190 { 191 restoreFromBackup = properties.getProperty(Attribute.CREATE_FROM); 193 if(restoreFromBackup == null) 194 restoreFromBackup = properties.getProperty(Attribute.RESTORE_FROM); 195 if(restoreFromBackup == null) 196 restoreFromBackup = 197 properties.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM); 198 199 } 200 201 if (create) 203 setupEncryptionEngines(create, properties); 204 205 206 211 dataFactory.setRawStoreFactory(this, create, properties); 212 xactFactory.setRawStoreFactory(this); 213 214 if( properties instanceof UpdateServiceProperties) 215 { 216 if( storageFactory instanceof WritableStorageFactory) 217 ((UpdateServiceProperties)properties).setStorageFactory( (WritableStorageFactory) storageFactory); 218 } 219 220 logFactory =(LogFactory) Monitor.findServiceModule(this, getLogFactoryModule()); 222 223 224 if(restoreFromBackup !=null) 226 { 227 restoreRemainingFromBackup(restoreFromBackup); 229 ((UpdateServiceProperties)properties).saveServiceProperties(); 230 } 231 232 String logDevice = properties.getProperty(Attribute.LOG_DEVICE); 235 if (logDevice !=null) 236 { 237 if (!isReadOnly() && (create 239 || !logDevice.equals(logFactory.getCanonicalLogPath()) 240 || restoreFromBackup!=null)) 241 { 242 properties.put(Attribute.LOG_DEVICE, logFactory.getCanonicalLogPath()); 244 properties.put(Property.LOG_DEVICE_AT_BACKUP, logFactory.getCanonicalLogPath()); 246 } 247 248 }else{ 249 if(restoreFromBackup!=null && logFactory.getCanonicalLogPath()!=null) 256 { 257 properties.put(Attribute.LOG_DEVICE, logFactory.getCanonicalLogPath()); 259 } 260 else{ 261 properties.remove(Property.LOG_DEVICE_AT_BACKUP); 263 } 264 } 265 266 267 if(restoreFromBackup !=null) 269 { 270 restoreRemainingFromBackup(restoreFromBackup); 272 ((UpdateServiceProperties)properties).saveServiceProperties(); 273 } 274 275 276 277 286 287 288 if (!create) 290 { 291 if(properties.getProperty( 295 RawStoreFactory.DB_ENCRYPTION_STATUS) !=null) 296 { 297 handleIncompleteDatabaseEncryption(properties); 298 } 299 300 setupEncryptionEngines(create, properties); 301 } 302 303 if (databaseEncrypted) { 304 logFactory.setDatabaseEncrypted(false); 306 dataFactory.setDatabaseEncrypted(); 308 } 309 310 logFactory.recover(this, dataFactory, xactFactory); 314 315 if (encryptDatabase) { 318 configureDatabaseForEncryption(properties, 319 newCipherFactory); 320 } 321 } 322 323 public void stop() { 324 325 if (SanityManager.DEBUG) 326 { 327 if (databaseEncrypted) 328 SanityManager.DEBUG_PRINT("encryption statistics", 329 "Encryption called " + counter_encrypt + " times, " + 330 "decryption called " + counter_decrypt + " times"); 331 } 332 333 if (rawStoreDaemon != null) 334 rawStoreDaemon.stop(); 335 336 if (logFactory == null) 337 return; 338 339 try { 340 341 if (logFactory.checkpoint(this, dataFactory, xactFactory, false)) 342 { 343 if (dataFactory != null) 344 dataFactory.removeStubsOK(); 345 } 346 347 } catch (StandardException se) { 348 markCorrupt(se); 350 } 351 } 352 353 356 357 361 public boolean isReadOnly() { 362 return dataFactory.isReadOnly(); 363 } 364 365 public LockFactory getLockFactory() { 366 return xactFactory.getLockFactory(); 367 } 368 369 370 373 public TransactionFactory getXactFactory() { 374 return xactFactory; 375 } 376 377 383 public Object getXAResourceManager() 384 throws StandardException 385 { 386 return(xactFactory.getXAResourceManager()); 387 } 388 389 390 public Transaction startGlobalTransaction( 391 ContextManager contextMgr, 392 int format_id, 393 byte[] global_id, 394 byte[] branch_id) 395 throws StandardException 396 { 397 return xactFactory.startGlobalTransaction( 398 this, contextMgr, format_id, global_id, branch_id); 399 } 400 401 public Transaction startTransaction(ContextManager contextMgr, String transName) 402 throws StandardException 403 { 404 return xactFactory.startTransaction(this, contextMgr, transName); 405 } 406 407 public Transaction startNestedReadOnlyUserTransaction( 408 Object compatibilitySpace, 409 ContextManager contextMgr, 410 String transName) 411 throws StandardException 412 { 413 return( 414 xactFactory.startNestedReadOnlyUserTransaction( 415 this, compatibilitySpace, contextMgr, transName)); 416 } 417 418 public Transaction startNestedUpdateUserTransaction( 419 ContextManager contextMgr, 420 String transName) 421 throws StandardException 422 { 423 return( 424 xactFactory.startNestedUpdateUserTransaction( 425 this, contextMgr, transName)); 426 } 427 428 public Transaction findUserTransaction( 429 ContextManager contextMgr, 430 String transName) 431 throws StandardException 432 { 433 return xactFactory.findUserTransaction(this, contextMgr, transName); 434 } 435 436 437 public Transaction startInternalTransaction(ContextManager contextMgr) throws StandardException { 438 439 return xactFactory.startInternalTransaction(this, contextMgr); 440 } 441 442 public void checkpoint() throws StandardException 443 { 444 logFactory.checkpoint(this, dataFactory, xactFactory, false); 445 } 446 447 public void freeze() throws StandardException 448 { 449 logFactory.checkpoint(this, dataFactory, xactFactory, true); 450 dataFactory.freezePersistentStore(); 451 logFactory.freezePersistentStore(); 452 } 453 454 public void unfreeze() throws StandardException 455 { 456 logFactory.unfreezePersistentStore(); 457 dataFactory.unfreezePersistentStore(); 458 } 459 460 470 public void backup(String backupDir, boolean wait) 471 throws StandardException 472 { 473 if (backupDir == null || backupDir.equals("")) 474 { 475 throw StandardException.newException( 476 SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY, (File)null); 477 } 478 479 String backupDirURL = null; 481 try { 482 URL url = new URL (backupDir); 483 backupDirURL = url.getFile(); 484 } catch (MalformedURLException ex) {} 485 486 if (backupDirURL != null) 487 backupDir = backupDirURL; 488 489 490 RawTransaction t = 493 xactFactory.findUserTransaction(this, 494 ContextService.getFactory().getCurrentContextManager(), 495 AccessFactoryGlobals.USER_TRANS_NAME); 496 497 try { 498 499 504 if (t.isBlockingBackup()) 505 { 506 throw StandardException.newException( 507 SQLState.BACKUP_OPERATIONS_NOT_ALLOWED); 508 } 509 510 511 if (!xactFactory.blockBackupBlockingOperations(wait)) 514 { 515 throw StandardException.newException( 516 SQLState.BACKUP_BLOCKING_OPERATIONS_IN_PROGRESS); 517 } 518 519 backup(t, new File(backupDir)); 521 }finally { 522 xactFactory.unblockBackupBlockingOperations(); 525 } 526 } 527 528 529 541 public synchronized void backup(Transaction t, File backupDir) 542 throws StandardException 543 { 544 if (!privExists(backupDir)) 545 { 546 548 if (!privMkdirs(backupDir)) 549 { 550 throw StandardException.newException( 551 SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY, 552 (File) backupDir); 553 } 554 } 555 else 556 { 557 559 if (!privIsDirectory(backupDir)) 560 { 561 throw StandardException.newException( 562 SQLState.RAWSTORE_CANNOT_BACKUP_TO_NONDIRECTORY, 563 (File) backupDir); 564 } 565 566 572 if (privExists( 573 new File(backupDir, PersistentService.PROPERTIES_NAME))) 574 { 575 throw StandardException.newException( 576 SQLState.RAWSTORE_CANNOT_BACKUP_INTO_DATABASE_DIRECTORY, 577 (File) backupDir); 578 } 579 } 580 581 boolean error = true; 582 boolean renamed = false; 583 boolean renameFailed = false; 584 File oldbackup = null; 585 File backupcopy = null; 586 OutputStreamWriter historyFile = null; 587 StorageFile dbHistoryFile = null; 588 File backupHistoryFile = null; 589 LogInstant backupInstant = logFactory.getFirstUnflushedInstant(); 590 591 try 592 { 593 StorageFile dbase = storageFactory.newStorageFile(null); 595 String canonicalDbName = storageFactory.getCanonicalName(); 596 int lastSep = 597 canonicalDbName.lastIndexOf(storageFactory.getSeparator()); 598 String dbname = 599 canonicalDbName.substring(lastSep + 1); 600 601 historyFile = 603 privFileWriter( 604 storageFactory.newStorageFile(BACKUP_HISTORY), true); 605 606 backupcopy = new File(backupDir, dbname); 607 608 logHistory( 609 historyFile, 610 MessageService.getTextMessage( 611 MessageId.STORE_BACKUP_STARTED, 612 canonicalDbName, 613 getFilePath(backupcopy))); 614 615 616 if (privExists(backupcopy)) 618 { 619 oldbackup = new File(backupDir, dbname+".OLD"); 621 if (privExists(oldbackup)) 622 { 623 if (privIsDirectory(oldbackup)) 624 privRemoveDirectory(oldbackup); 625 else 626 privDelete(oldbackup); 627 } 628 629 if (!privRenameTo(backupcopy,oldbackup)) 630 { 631 renameFailed = true; 632 throw StandardException. 633 newException(SQLState.RAWSTORE_ERROR_RENAMING_FILE, 634 backupcopy, oldbackup); 635 } 636 else 637 { 638 logHistory( 639 historyFile, 640 MessageService.getTextMessage( 641 MessageId.STORE_MOVED_BACKUP, 642 getFilePath(backupcopy), 643 getFilePath(oldbackup))); 644 renamed = true; 645 } 646 } 647 648 if (!privMkdirs(backupcopy)) 650 { 651 throw StandardException.newException( 652 SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY, 653 (File) backupcopy); 654 } 655 656 dbHistoryFile = storageFactory.newStorageFile(BACKUP_HISTORY); 657 backupHistoryFile = new File(backupcopy, BACKUP_HISTORY); 658 659 if(!privCopyFile(dbHistoryFile, backupHistoryFile)) 661 throw StandardException. 662 newException(SQLState.RAWSTORE_ERROR_COPYING_FILE, 663 dbHistoryFile, backupHistoryFile); 664 665 666 StorageFile jarDir = 669 storageFactory.newStorageFile(FileResource.JAR_DIRECTORY_NAME); 670 671 if (privExists(jarDir)) 672 { 673 681 String [] jarSchemaList = privList(jarDir); 682 File backupJarDir = new File(backupcopy, 683 FileResource.JAR_DIRECTORY_NAME); 684 if (!privMkdirs(backupJarDir)) 686 { 687 throw StandardException.newException( 688 SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY, 689 (File) backupJarDir); 690 } 691 692 for (int i = 0; i < jarSchemaList.length; i++) 693 { 694 StorageFile jarSchemaDir = 695 storageFactory.newStorageFile(jarDir, jarSchemaList[i]); 696 File backupJarSchemaDir = 697 new File(backupJarDir, jarSchemaList[i]); 698 699 if (!privCopyDirectory(jarSchemaDir, backupJarSchemaDir, 700 (byte[])null, null, false)) 701 { 702 throw StandardException. 703 newException(SQLState.RAWSTORE_ERROR_COPYING_FILE, 704 jarSchemaDir, backupJarSchemaDir); 705 } 706 } 707 } 708 709 710 714 StorageFile logdir = logFactory.getLogDirectory(); 715 716 try 717 { 718 String name = Monitor.getMonitor().getServiceName(this); 719 PersistentService ps = 720 Monitor.getMonitor().getServiceType(this); 721 String fullName = ps.getCanonicalServiceName(name); 722 Properties prop = 723 ps.getServiceProperties(fullName, (Properties)null); 724 725 StorageFile defaultLogDir = 726 storageFactory.newStorageFile( 727 LogFactory.LOG_DIRECTORY_NAME); 728 729 if (!logdir.equals(defaultLogDir)) 730 { 731 prop.remove(Attribute.LOG_DEVICE); 732 if (SanityManager.DEBUG) 733 { 734 SanityManager.ASSERT( 735 prop.getProperty(Attribute.LOG_DEVICE) == null, 736 "cannot get rid of logDevice property"); 737 } 738 739 logHistory(historyFile, 740 MessageService.getTextMessage( 741 MessageId.STORE_EDITED_SERVICEPROPS)); 742 } 743 744 ps.saveServiceProperties(backupcopy.getPath(), prop, false); 746 747 } 748 catch(StandardException se) 749 { 750 logHistory( 751 historyFile, 752 MessageService.getTextMessage( 753 MessageId.STORE_ERROR_EDIT_SERVICEPROPS) + se); 754 755 return; } 757 758 StorageFile verifyKeyFile = 763 storageFactory.newStorageFile( 764 Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE); 765 if (privExists(verifyKeyFile)) 766 { 767 File backupVerifyKeyFile = 768 new File( 769 backupcopy, Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE); 770 771 if(!privCopyFile(verifyKeyFile, backupVerifyKeyFile)) 772 throw StandardException. 773 newException(SQLState.RAWSTORE_ERROR_COPYING_FILE, 774 verifyKeyFile, backupVerifyKeyFile); 775 } 776 777 File logBackup = 778 new File(backupcopy, LogFactory.LOG_DIRECTORY_NAME); 779 780 if (privExists(logBackup)) 782 { 783 privRemoveDirectory(logBackup); 784 } 785 786 if (!privMkdirs(logBackup)) 788 { 789 throw StandardException.newException( 790 SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY, 791 (File) logBackup); 792 } 793 794 logFactory.checkpoint(this, dataFactory, xactFactory, true); 796 797 logFactory.startLogBackup(logBackup); 799 800 File segBackup = new File(backupcopy, "seg0"); 801 802 if (!privMkdirs(segBackup)) 804 { 805 throw StandardException.newException( 806 SQLState.RAWSTORE_CANNOT_CREATE_BACKUP_DIRECTORY, 807 (File) segBackup); 808 } 809 810 dataFactory.backupDataFiles(t, segBackup); 812 813 logHistory(historyFile, 814 MessageService.getTextMessage( 815 MessageId.STORE_DATA_SEG_BACKUP_COMPLETED, 816 getFilePath(segBackup))); 817 818 819 logFactory.endLogBackup(logBackup); 823 824 logHistory(historyFile, 825 MessageService.getTextMessage( 826 MessageId.STORE_COPIED_LOG, 827 getFilePath(logdir), 828 getFilePath(logBackup))); 829 830 error = false; 831 } 832 catch (IOException ioe) 833 { 834 throw StandardException.newException( 835 SQLState.RAWSTORE_UNEXPECTED_EXCEPTION, ioe); 836 } 837 finally 838 { 839 840 try 841 { 842 if (error) 843 { 844 845 logFactory.abortLogBackup(); 847 848 if(!renameFailed) 853 privRemoveDirectory(backupcopy); 854 855 if (renamed) 856 privRenameTo(oldbackup,backupcopy); 858 859 logHistory(historyFile, 860 MessageService.getTextMessage( 861 MessageId.STORE_BACKUP_ABORTED)); 862 863 } 864 else 865 { 866 if (renamed && privExists(oldbackup)) 868 { 869 privRemoveDirectory(oldbackup); 871 logHistory(historyFile, 872 MessageService.getTextMessage( 873 MessageId.STORE_REMOVED_BACKUP, 874 getFilePath(oldbackup))); 875 } 876 logHistory(historyFile, 877 MessageService.getTextMessage( 878 MessageId.STORE_BACKUP_COMPLETED, 879 backupInstant)); 880 881 if(!privCopyFile(dbHistoryFile, backupHistoryFile)) 884 throw StandardException. 885 newException(SQLState.RAWSTORE_ERROR_COPYING_FILE, 886 dbHistoryFile, backupHistoryFile); 887 } 888 889 historyFile.close(); 890 } 891 catch (IOException ioe) 892 { 893 try 894 { 895 historyFile.close(); 896 } 897 catch (IOException ioe2){}; 898 throw StandardException.newException( 899 SQLState.RAWSTORE_UNEXPECTED_EXCEPTION, ioe); 900 } 901 } 902 903 } 904 905 924 public void backupAndEnableLogArchiveMode( 925 String backupDir, 926 boolean deleteOnlineArchivedLogFiles, 927 boolean wait) 928 throws StandardException 929 { 930 boolean enabledLogArchive = false; 931 try { 932 if(!logFactory.logArchived()) { 934 logFactory.enableLogArchiveMode(); 935 enabledLogArchive = true ; 936 } 937 938 backup(backupDir, wait); 939 940 if (deleteOnlineArchivedLogFiles) 944 { 945 logFactory.deleteOnlineArchivedLogFiles(); 946 } 947 }catch (Throwable error) { 948 if (enabledLogArchive) 951 logFactory.disableLogArchiveMode(); 952 throw StandardException.plainWrapException(error); 953 } 954 } 955 956 957 967 public void disableLogArchiveMode(boolean deleteOnlineArchivedLogFiles) 968 throws StandardException 969 { 970 logFactory.disableLogArchiveMode(); 971 if(deleteOnlineArchivedLogFiles) 972 { 973 logFactory.deleteOnlineArchivedLogFiles(); 974 } 975 } 976 977 978 private void restoreRemainingFromBackup(String backupPath) throws StandardException 981 { 982 988 989 File backuploc = new File(backupPath); 990 String [] fromList = privList(backuploc); 991 for(int i =0 ; i < fromList.length ; i++) 992 { 993 StorageFile toFile = storageFactory.newStorageFile( fromList[i]); 994 if(privExists(toFile) || 995 fromList[i].equals(PersistentService.PROPERTIES_NAME)){ 996 continue; 997 } 998 999 File fromFile = new File(backuploc, fromList[i]); 1000 if(privIsDirectory(fromFile)) 1001 { 1002 if (!privCopyDirectory(fromFile, toFile)){ 1003 throw StandardException.newException( 1004 SQLState.UNABLE_TO_COPY_FILE_FROM_BACKUP, 1005 fromFile, toFile); 1006 } 1007 }else{ 1008 if (!privCopyFile(fromFile, toFile)){ 1009 throw StandardException.newException( 1010 SQLState.UNABLE_TO_COPY_FILE_FROM_BACKUP, 1011 fromFile, toFile); 1012 } 1013 } 1014 } 1015 } 1016 1017 public void idle() throws StandardException { 1018 dataFactory.idle(); 1019 } 1020 1021 1022 public TransactionInfo[] getTransactionInfo() 1023 { 1024 return xactFactory.getTransactionInfo(); 1025 } 1026 1027 1028 public ScanHandle openFlushedScan(DatabaseInstant start, int groupsIWant) 1029 throws StandardException 1030 { 1031 return logFactory.openFlushedScan(start,groupsIWant); 1032 } 1033 1034 public DaemonService getDaemon() 1035 { 1036 return rawStoreDaemon; 1037 } 1038 1039 public void createFinished() throws StandardException 1040 { 1041 xactFactory.createFinished(); 1042 dataFactory.createFinished(); 1043 } 1044 1045 1049 public void getRawStoreProperties(PersistentSet set) 1050 throws StandardException 1051 { 1052 logFactory.getLogFactoryProperties(set); 1053 } 1054 1055 1056 1059 1063 public void freezePersistentStore() throws StandardException 1064 { 1065 logFactory.checkpoint(this, dataFactory, xactFactory,true); 1067 logFactory.freezePersistentStore(); 1068 1069 } 1070 1071 1075 public void unfreezePersistentStore() throws StandardException 1076 { 1077 logFactory.unfreezePersistentStore(); 1078 } 1079 1080 1081 1084 1085 1086 1089 private void setupEncryptionEngines(boolean create, Properties properties) 1090 throws StandardException 1091 { 1092 1093 1096 String dataEncryption = 1097 properties.getProperty(Attribute.DATA_ENCRYPTION); 1098 databaseEncrypted = Boolean.valueOf(dataEncryption).booleanValue(); 1099 1100 boolean reEncrypt = false; 1101 1102 if (!create) { 1103 1113 String name = Monitor.getMonitor().getServiceName(this); 1114 PersistentService ps = Monitor.getMonitor().getServiceType(this); 1115 String canonicalName = ps.getCanonicalServiceName(name); 1116 Properties serviceprops = ps.getServiceProperties(canonicalName, 1117 (Properties)null); 1118 dataEncryption = serviceprops.getProperty(Attribute.DATA_ENCRYPTION); 1119 boolean encryptedDatabase = Boolean.valueOf(dataEncryption).booleanValue(); 1120 1121 if (!encryptedDatabase && databaseEncrypted) { 1122 encryptDatabase = true; 1125 databaseEncrypted = false; 1128 } else { 1129 if (encryptedDatabase) { 1132 if (properties.getProperty( 1133 Attribute.NEW_BOOT_PASSWORD) != null) { 1134 reEncrypt = true; 1135 } 1136 else if (properties.getProperty( 1137 Attribute.NEW_CRYPTO_EXTERNAL_KEY) != null){ 1138 reEncrypt = true; 1139 }; 1140 encryptDatabase = reEncrypt; 1141 } 1142 1143 } 1144 1145 1146 1150 1151 if (encryptDatabase) 1153 { 1154 if (isReadOnly()) 1155 { 1156 if (reEncrypt) 1157 throw StandardException.newException( 1158 SQLState.CANNOT_REENCRYPT_READONLY_DATABASE); 1159 else 1160 throw StandardException.newException( 1161 SQLState.CANNOT_ENCRYPT_READONLY_DATABASE); 1162 } 1163 } 1164 } 1165 1166 if (databaseEncrypted || encryptDatabase) 1168 { 1169 boolean setupEncryption = create || (encryptDatabase && !reEncrypt); 1176 1177 1181 CipherFactoryBuilder cb = (CipherFactoryBuilder) 1182 Monitor.startSystemModule(org.apache.derby.iapi.reference.Module.CipherFactoryBuilder); 1183 1184 currentCipherFactory = cb.createCipherFactory(setupEncryption, 1187 properties, 1188 false); 1189 1190 1197 1201 1202 currentCipherFactory.verifyKey(setupEncryption, storageFactory, properties); 1203 1204 encryptionEngine = currentCipherFactory. 1206 createNewCipher(CipherFactory.ENCRYPT); 1207 1208 1219 if(setupEncryption) 1220 { 1221 encryptionBlockSize = encryptionEngine.getEncryptionBlockSize(); 1222 if (create) 1226 properties.put(RawStoreFactory.ENCRYPTION_BLOCKSIZE, 1227 String.valueOf(encryptionBlockSize)); 1228 } 1229 else 1230 { 1231 if(properties.getProperty(RawStoreFactory.ENCRYPTION_BLOCKSIZE) != null) 1232 encryptionBlockSize = Integer.parseInt(properties.getProperty 1233 (RawStoreFactory.ENCRYPTION_BLOCKSIZE)); 1234 else 1235 encryptionBlockSize = encryptionEngine.getEncryptionBlockSize(); 1236 } 1237 1238 decryptionEngine = currentCipherFactory. 1239 createNewCipher(CipherFactory.DECRYPT); 1240 1241 random = currentCipherFactory.getSecureRandom(); 1242 1243 if (encryptDatabase) { 1244 1245 if (reEncrypt) { 1246 newCipherFactory = 1252 cb.createCipherFactory(setupEncryption, 1253 properties, 1254 true); 1255 newDecryptionEngine = 1256 newCipherFactory.createNewCipher(CipherFactory.DECRYPT); 1257 newEncryptionEngine = 1258 newCipherFactory.createNewCipher(CipherFactory.ENCRYPT); 1259 } else { 1260 newDecryptionEngine = decryptionEngine; 1263 newEncryptionEngine = encryptionEngine; 1264 1265 } 1266 } 1267 1268 if(create) 1271 currentCipherFactory.saveProperties(properties) ; 1272 } 1273 } 1274 1275 1276 1283 public int encrypt(byte[] cleartext, int offset, int length, 1284 byte[] ciphertext, int outputOffset, 1285 boolean newEngine) 1286 throws StandardException 1287 { 1288 if ((databaseEncrypted == false && encryptDatabase == false) || 1289 (encryptionEngine == null && newEncryptionEngine == null)) 1290 { 1291 throw StandardException.newException( 1292 SQLState.STORE_FEATURE_NOT_IMPLEMENTED); 1293 } 1294 1295 counter_encrypt++; 1296 1297 if (newEngine) { 1298 return newEncryptionEngine.encrypt(cleartext, offset, length, 1299 ciphertext, outputOffset); 1300 } else { 1301 return encryptionEngine.encrypt(cleartext, offset, length, 1302 ciphertext, outputOffset); 1303 } 1304 } 1305 1306 1313 public int decrypt(byte[] ciphertext, int offset, int length, 1314 byte[] cleartext, int outputOffset) 1315 throws StandardException 1316 { 1317 if (databaseEncrypted == false || decryptionEngine == null) 1318 { 1319 throw StandardException.newException( 1320 SQLState.STORE_FEATURE_NOT_IMPLEMENTED); 1321 } 1322 1323 counter_decrypt++; 1324 return decryptionEngine.decrypt(ciphertext, offset, length, 1325 cleartext, outputOffset); 1326 } 1327 1328 1332 public int getEncryptionBlockSize() 1333 { 1334 return encryptionBlockSize; 1335 } 1336 1337 public int random() 1338 { 1339 return databaseEncrypted ? random.nextInt() : 0; 1341 } 1342 1343 public Serializable changeBootPassword(Properties properties, Serializable changePassword) 1344 throws StandardException 1345 { 1346 if (isReadOnly()) 1347 throw StandardException.newException(SQLState.DATABASE_READ_ONLY); 1348 1349 if (!databaseEncrypted) 1350 throw StandardException.newException(SQLState.DATABASE_NOT_ENCRYPTED); 1351 1352 if (changePassword == null) 1353 throw StandardException.newException(SQLState.NULL_BOOT_PASSWORD); 1354 1355 if (!(changePassword instanceof String )) 1356 throw StandardException.newException(SQLState.NON_STRING_BP); 1357 1358 String changeString = (String )changePassword; 1361 1362 return currentCipherFactory.changeBootPassword((String )changePassword, properties, encryptionEngine); 1363 1364 } 1365 1366 1367 1373 1374 public static final String TEST_REENCRYPT_CRASH_BEFORE_COMMT = 1375 SanityManager.DEBUG ? "TEST_REENCRYPT_CRASH_BEFORE_COMMT" : null ; 1376 public static final String TEST_REENCRYPT_CRASH_AFTER_COMMT = 1377 SanityManager.DEBUG ? "TEST_REENCRYPT_CRASH_AFTER_COMMT" : null ; 1378 public static final String TEST_REENCRYPT_CRASH_AFTER_SWITCH_TO_NEWKEY = 1379 SanityManager.DEBUG ? "TEST_REENCRYPT_CRASH_AFTER_SWITCH_TO_NEWKEY" : null ; 1380 public static final String TEST_REENCRYPT_CRASH_AFTER_CHECKPOINT = 1381 SanityManager.DEBUG ? "TEST_REENCRYPT_CRASH_AFTER_CHECKPOINT" : null ; 1382 public static final String 1383 TEST_REENCRYPT_CRASH_AFTER_RECOVERY_UNDO_LOGFILE_DELETE = 1384 SanityManager.DEBUG ? 1385 "TEST_REENCRYPT_CRASH_AFTER_RECOVERY_UNDO_LOGFILE_DELETE" : null; 1386 public static final String 1387 TEST_REENCRYPT_CRASH_AFTER_RECOVERY_UNDO_REVERTING_KEY = 1388 SanityManager.DEBUG ? 1389 "TEST_REENCRYPT_CRASH_AFTER_RECOVERY_UNDO_REVERTING_KEY" : null; 1390 public static final String 1391 TEST_REENCRYPT_CRASH_BEFORE_RECOVERY_FINAL_CLEANUP = 1392 SanityManager.DEBUG ? 1393 "TEST_REENCRYPT_CRASH_BEFORE_RECOVERY_FINAL_CLEANUP" : null; 1394 1395 1396 1397 1401 private void crashOnDebugFlag(String debugFlag, 1402 boolean reEncrypt) 1403 throws StandardException 1404 { 1405 if (SanityManager.DEBUG) 1406 { 1407 if (SanityManager.DEBUG_ON(debugFlag)) 1410 { 1411 StandardException se = StandardException.newException( 1412 (reEncrypt ? SQLState.DATABASE_REENCRYPTION_FAILED : 1413 SQLState.DATABASE_ENCRYPTION_FAILED), 1414 debugFlag); 1415 markCorrupt(se); 1416 throw se; 1417 } 1418 } 1419 } 1420 1421 1466 public void configureDatabaseForEncryption(Properties properties, 1467 CipherFactory newCipherFactory) 1468 throws StandardException 1469 { 1470 1471 boolean reEncrypt = (databaseEncrypted && encryptDatabase); 1472 1473 canEncryptDatabase(reEncrypt); 1475 1476 boolean externalKeyEncryption = false; 1477 if (properties.getProperty(Attribute.CRYPTO_EXTERNAL_KEY) != null) 1478 { 1479 externalKeyEncryption = true; 1480 } 1481 1482 1485 logFactory.checkpoint(this, dataFactory, xactFactory, true); 1486 1487 RawTransaction transaction = 1489 xactFactory.startTransaction( 1490 this, 1491 ContextService.getFactory().getCurrentContextManager(), 1492 AccessFactoryGlobals.USER_TRANS_NAME); 1493 1494 try 1495 { 1496 1497 dataFactory.encryptAllContainers(transaction); 1498 1499 1504 1505 if (SanityManager.DEBUG) { 1506 crashOnDebugFlag(TEST_REENCRYPT_CRASH_BEFORE_COMMT, reEncrypt); 1507 } 1508 1509 if (!logFactory.isCheckpointInLastLogFile()) 1513 { 1514 logFactory.checkpoint(this, dataFactory, xactFactory, true); 1517 } 1518 1519 1520 encryptDatabase = false; 1521 1522 logFactory.setDatabaseEncrypted(true); 1527 1528 if (!reEncrypt) { 1531 databaseEncrypted = true; 1534 dataFactory.setDatabaseEncrypted(); 1535 } else { 1536 decryptionEngine = newDecryptionEngine; 1538 encryptionEngine = newEncryptionEngine; 1539 currentCipherFactory = newCipherFactory; 1540 } 1541 1542 1543 1550 logFactory.startNewLogFile(); 1551 1552 1561 1565 properties.put(RawStoreFactory.DB_ENCRYPTION_STATUS, 1566 String.valueOf( 1567 RawStoreFactory.DB_ENCRYPTION_IN_PROGRESS)); 1568 1569 if (reEncrypt) 1570 { 1571 1575 if (externalKeyEncryption) 1576 { 1577 StorageFile verifyKeyFile = 1579 storageFactory.newStorageFile( 1580 Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE); 1581 StorageFile oldVerifyKeyFile = 1582 storageFactory.newStorageFile( 1583 RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE); 1584 1585 if(!privCopyFile(verifyKeyFile, oldVerifyKeyFile)) 1586 throw StandardException. 1587 newException(SQLState.RAWSTORE_ERROR_COPYING_FILE, 1588 verifyKeyFile, oldVerifyKeyFile); 1589 1590 currentCipherFactory.verifyKey(reEncrypt, 1592 storageFactory, 1593 properties); 1594 } else 1595 { 1596 String keyString = 1598 properties.getProperty( 1599 RawStoreFactory.ENCRYPTED_KEY); 1600 if (keyString != null) 1601 properties.put(RawStoreFactory.OLD_ENCRYPTED_KEY, 1602 keyString); 1603 } 1604 } else 1605 { 1606 properties.put(RawStoreFactory.ENCRYPTION_BLOCKSIZE, 1608 String.valueOf(encryptionBlockSize)); 1609 } 1610 1611 currentCipherFactory.saveProperties(properties) ; 1613 1614 if (SanityManager.DEBUG) { 1615 crashOnDebugFlag( 1616 TEST_REENCRYPT_CRASH_AFTER_SWITCH_TO_NEWKEY, 1617 reEncrypt); 1618 } 1619 1620 transaction.commit(); 1626 1627 if (SanityManager.DEBUG) { 1628 crashOnDebugFlag(TEST_REENCRYPT_CRASH_AFTER_COMMT, 1629 reEncrypt); 1630 } 1631 1632 logFactory.checkpoint(this, dataFactory, xactFactory, true); 1634 1635 if (SanityManager.DEBUG) { 1636 crashOnDebugFlag(TEST_REENCRYPT_CRASH_AFTER_CHECKPOINT, 1637 reEncrypt); 1638 } 1639 1640 properties.put(RawStoreFactory.DB_ENCRYPTION_STATUS, 1644 String.valueOf( 1645 RawStoreFactory.DB_ENCRYPTION_IN_CLEANUP)); 1646 1647 dataFactory.removeOldVersionOfContainers(false); 1650 1651 if (reEncrypt) 1652 { 1653 if (externalKeyEncryption) 1654 { 1655 StorageFile oldVerifyKeyFile = 1657 storageFactory.newStorageFile( 1658 RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE); 1659 if (!privDelete(oldVerifyKeyFile)) 1660 throw StandardException.newException( 1661 SQLState.UNABLE_TO_DELETE_FILE, 1662 oldVerifyKeyFile); 1663 } else 1664 { 1665 properties.remove(RawStoreFactory.OLD_ENCRYPTED_KEY); 1667 } 1668 } 1669 1670 1673 properties.remove(RawStoreFactory.DB_ENCRYPTION_STATUS); 1674 1675 transaction.close(); 1677 1678 } catch (StandardException se) { 1679 1680 throw StandardException.newException( 1681 (reEncrypt ? SQLState.DATABASE_REENCRYPTION_FAILED : 1682 SQLState.DATABASE_ENCRYPTION_FAILED), 1683 se, 1684 se.getMessage()); 1685 } finally { 1686 newDecryptionEngine = null; 1688 newEncryptionEngine = null; 1689 } 1690 } 1691 1692 1693 1711 public void handleIncompleteDatabaseEncryption(Properties properties) 1712 throws StandardException 1713 { 1714 int dbEncryptionStatus = 0; 1716 String dbEncryptionStatusStr = 1717 properties.getProperty(RawStoreFactory.DB_ENCRYPTION_STATUS); 1718 if ( dbEncryptionStatusStr != null) 1719 dbEncryptionStatus = Integer.parseInt(dbEncryptionStatusStr); 1720 1721 boolean reEncryption = false; 1722 if (dbEncryptionStatus == RawStoreFactory.DB_ENCRYPTION_IN_PROGRESS) 1724 { 1725 1726 1732 if(logFactory.isCheckpointInLastLogFile()) 1733 { 1734 dbEncryptionStatus = RawStoreFactory.DB_ENCRYPTION_IN_CLEANUP; 1737 }else { 1738 1739 1751 dbEncryptionStatus = RawStoreFactory.DB_ENCRYPTION_IN_UNDO; 1752 properties.put(RawStoreFactory.DB_ENCRYPTION_STATUS, 1753 String.valueOf(dbEncryptionStatus)); 1754 } 1755 } 1756 1757 1758 if (dbEncryptionStatus == RawStoreFactory.DB_ENCRYPTION_IN_UNDO) 1759 { 1760 1766 logFactory.deleteLogFileAfterCheckpointLogFile(); 1767 1768 if (SanityManager.DEBUG) { 1769 crashOnDebugFlag( 1770 TEST_REENCRYPT_CRASH_AFTER_RECOVERY_UNDO_LOGFILE_DELETE, 1771 reEncryption); 1772 } 1773 1774 1779 1783 StorageFile verifyKeyFile = 1784 storageFactory.newStorageFile( 1785 Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE); 1786 1787 if (privExists(verifyKeyFile)) 1788 { 1789 StorageFile oldVerifyKeyFile = 1790 storageFactory.newStorageFile( 1791 RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE); 1792 1793 if (privExists(oldVerifyKeyFile)) 1794 { 1795 if(!privCopyFile(oldVerifyKeyFile, verifyKeyFile)) 1796 throw StandardException. 1797 newException(SQLState.RAWSTORE_ERROR_COPYING_FILE, 1798 oldVerifyKeyFile, verifyKeyFile); 1799 1800 reEncryption = true; 1803 }else 1804 { 1805 if (!privDelete(verifyKeyFile)) 1807 throw StandardException.newException( 1808 SQLState.UNABLE_TO_DELETE_FILE, 1809 verifyKeyFile); 1810 } 1811 1812 } else 1813 { 1814 1816 1820 String OldKeyString = 1821 properties.getProperty(RawStoreFactory.OLD_ENCRYPTED_KEY); 1822 1823 if (OldKeyString != null) { 1824 properties.put(RawStoreFactory.ENCRYPTED_KEY, 1826 OldKeyString); 1827 1828 reEncryption = true; 1831 } 1832 } 1833 1834 if (!reEncryption) { 1835 1839 properties.remove(Attribute.DATA_ENCRYPTION); 1841 properties.remove(RawStoreFactory.LOG_ENCRYPT_ALGORITHM_VERSION); 1842 properties.remove(RawStoreFactory.DATA_ENCRYPT_ALGORITHM_VERSION); 1843 properties.remove(RawStoreFactory.ENCRYPTION_BLOCKSIZE); 1844 1845 properties.remove(Attribute.CRYPTO_KEY_LENGTH); 1847 properties.remove(Attribute.CRYPTO_PROVIDER); 1848 properties.remove(Attribute.CRYPTO_ALGORITHM); 1849 properties.remove(RawStoreFactory.ENCRYPTED_KEY); 1850 1851 } 1852 1853 if (SanityManager.DEBUG) { 1854 crashOnDebugFlag( 1855 TEST_REENCRYPT_CRASH_AFTER_RECOVERY_UNDO_REVERTING_KEY, 1856 reEncryption); 1857 } 1858 1859 } 1861 1862 if (dbEncryptionStatus == RawStoreFactory.DB_ENCRYPTION_IN_CLEANUP) 1863 { 1864 dataFactory.removeOldVersionOfContainers(true); 1866 } 1867 1868 if (SanityManager.DEBUG) { 1869 crashOnDebugFlag( 1870 TEST_REENCRYPT_CRASH_BEFORE_RECOVERY_FINAL_CLEANUP, 1871 reEncryption); 1872 } 1873 1874 1880 StorageFile oldVerifyKeyFile = 1882 storageFactory.newStorageFile( 1883 RawStoreFactory.CRYPTO_OLD_EXTERNAL_KEY_VERIFY_FILE); 1884 if (privExists(oldVerifyKeyFile)) 1885 { 1886 if (!privDelete(oldVerifyKeyFile)) 1887 throw StandardException.newException( 1888 SQLState.UNABLE_TO_DELETE_FILE, 1889 oldVerifyKeyFile); 1890 } else 1891 { 1892 properties.remove(RawStoreFactory.OLD_ENCRYPTED_KEY); 1894 } 1895 1896 properties.remove(RawStoreFactory.DB_ENCRYPTION_STATUS); 1898 } 1899 1900 1901 1902 1903 1914 private void canEncryptDatabase(boolean reEncrypt) 1915 throws StandardException 1916 { 1917 1918 String feature = (reEncrypt ? 1919 "newBootPassword/newEncryptionKey attribute" : 1920 "dataEncryption attribute on an existing database"); 1921 1922 logFactory.checkVersion( 1926 RawStoreFactory.DERBY_STORE_MAJOR_VERSION_10, 1927 RawStoreFactory.DERBY_STORE_MINOR_VERSION_2, 1928 feature); 1929 1930 if (xactFactory.hasPreparedXact()) { 1937 if(reEncrypt) 1938 throw StandardException.newException( 1939 SQLState.REENCRYPTION_PREPARED_XACT_EXIST); 1940 else 1941 throw StandardException.newException( 1942 SQLState.ENCRYPTION_PREPARED_XACT_EXIST); 1943 } 1944 1945 1946 1954 if (logFactory.logArchived()) 1955 { 1956 if(reEncrypt) 1957 throw StandardException.newException( 1958 SQLState.CANNOT_REENCRYPT_LOG_ARCHIVED_DATABASE); 1959 else 1960 throw StandardException.newException( 1961 SQLState.CANNOT_ENCRYPT_LOG_ARCHIVED_DATABASE); 1962 1963 } 1964 } 1965 1966 1967 1970 1971 public StandardException markCorrupt(StandardException originalError) { 1972 1973 logFactory.markCorrupt(originalError); 1974 dataFactory.markCorrupt(originalError); 1975 xactFactory.markCorrupt(originalError); 1976 1977 return originalError; 1978 } 1979 1980 1983 1984 1985 public String getTransactionFactoryModule() 1986 { 1987 return TransactionFactory.MODULE; 1988 } 1989 1990 public String getDataFactoryModule() 1991 { 1992 return DataFactory.MODULE; 1993 } 1994 1995 public String getLogFactoryModule() 1996 { 1997 return LogFactory.MODULE; 1998 } 1999 2000 2001 private void logHistory(OutputStreamWriter historyFile, String msg) throws IOException 2002 { 2003 Date d = new Date (); 2004 historyFile.write(d.toString() + ":" + msg + "\n"); 2005 historyFile.flush(); 2006 } 2007 2008 2018 private String getFilePath(StorageFile file) { 2019 String path = privGetCanonicalPath(file); 2020 if(path != null ) { 2021 return path; 2022 }else { 2023 return file.getPath(); 2026 } 2027 } 2028 2029 2040 private String getFilePath(File file) { 2041 String path = privGetCanonicalPath(file); 2042 if(path != null ) { 2043 return path; 2044 }else { 2045 return file.getPath(); 2048 } 2049 } 2050 2051 protected boolean privCopyDirectory(StorageFile from, File to) 2052 { 2053 return privCopyDirectory(from, to, (byte[])null, 2054 (String [])null, true); 2055 } 2056 2057 protected boolean privCopyDirectory(File from, StorageFile to) 2058 { 2059 return privCopyDirectory(from, to, (byte[])null, (String [])null); 2060 } 2061 2062 2075 public long getMaxContainerId() 2076 throws StandardException 2077 { 2078 return(dataFactory.getMaxContainerId()); 2079 } 2080 2081 2082 2085 2086 private synchronized OutputStreamWriter privFileWriter( StorageFile fileName, boolean append) throws IOException 2087 { 2088 actionCode = FILE_WRITER_ACTION; 2089 actionStorageFile = fileName; 2090 actionAppend = append; 2091 try{ 2092 return (OutputStreamWriter ) java.security.AccessController.doPrivileged( this); 2093 }catch (java.security.PrivilegedActionException pae) 2094 { 2095 throw (IOException ) pae.getException(); 2096 } 2097 finally 2098 { 2099 actionStorageFile = null; 2100 } 2101 } 2102 2103 private synchronized boolean privExists( File file) 2104 { 2105 actionCode = REGULAR_FILE_EXISTS_ACTION; 2106 actionRegularFile = file; 2107 2108 try 2109 { 2110 Object ret = AccessController.doPrivileged( this); 2111 return ((Boolean ) ret).booleanValue(); 2112 } 2113 catch( PrivilegedActionException pae) { return false;} finally 2115 { 2116 actionRegularFile = null; 2117 } 2118 } 2119 2120 private synchronized boolean privExists(final StorageFile file) 2121 { 2122 actionCode = STORAGE_FILE_EXISTS_ACTION; 2123 actionStorageFile = file; 2124 2125 try 2126 { 2127 Object ret = AccessController.doPrivileged( this); 2128 return ((Boolean ) ret).booleanValue(); 2129 } 2130 catch( PrivilegedActionException pae) { return false;} finally 2132 { 2133 actionStorageFile = null; 2134 } 2135 } 2136 2137 2138 private synchronized boolean privDelete( File file) 2139 { 2140 actionCode = REGULAR_FILE_DELETE_ACTION; 2141 actionRegularFile = file; 2142 2143 try 2144 { 2145 Object ret = AccessController.doPrivileged( this); 2146 return ((Boolean ) ret).booleanValue(); 2147 } 2148 catch( PrivilegedActionException pae) { return false;} finally 2150 { 2151 actionRegularFile = null; 2152 } 2153 } 2154 2155 private synchronized boolean privDelete(StorageFile file) 2156 { 2157 actionCode = STORAGE_FILE_DELETE_ACTION; 2158 actionStorageFile = file; 2159 2160 try 2161 { 2162 Object ret = AccessController.doPrivileged( this); 2163 return ((Boolean ) ret).booleanValue(); 2164 } 2165 catch( PrivilegedActionException pae) { return false;} finally 2167 { 2168 actionStorageFile = null; 2169 } 2170 } 2171 2172 2173 2174 private synchronized boolean privMkdirs( File file) 2175 { 2176 actionCode = REGULAR_FILE_MKDIRS_ACTION; 2177 actionRegularFile = file; 2178 2179 try 2180 { 2181 Object ret = AccessController.doPrivileged( this); 2182 return ((Boolean ) ret).booleanValue(); 2183 } 2184 catch( PrivilegedActionException pae) { return false;} finally 2186 { 2187 actionRegularFile = null; 2188 } 2189 } 2190 2191 2192 private synchronized boolean privIsDirectory( File file) 2193 { 2194 actionCode = REGULAR_FILE_IS_DIRECTORY_ACTION; 2195 actionRegularFile = file; 2196 2197 try 2198 { 2199 Object ret = AccessController.doPrivileged( this); 2200 return ((Boolean ) ret).booleanValue(); 2201 } 2202 catch( PrivilegedActionException pae) { return false;} finally 2204 { 2205 actionRegularFile = null; 2206 } 2207 } 2208 2209 private synchronized boolean privRemoveDirectory( File file) 2210 { 2211 actionCode = REGULAR_FILE_REMOVE_DIRECTORY_ACTION; 2212 actionRegularFile = file; 2213 2214 try 2215 { 2216 Object ret = AccessController.doPrivileged( this); 2217 return ((Boolean ) ret).booleanValue(); 2218 } 2219 catch( PrivilegedActionException pae) { return false;} finally 2221 { 2222 actionRegularFile = null; 2223 } 2224 } 2225 2226 private synchronized boolean privRenameTo( File file1, File file2) 2227 { 2228 actionCode = REGULAR_FILE_RENAME_TO_ACTION; 2229 actionRegularFile = file1; 2230 actionRegularFile2 = file2; 2231 2232 try 2233 { 2234 Object ret = AccessController.doPrivileged( this); 2235 return ((Boolean ) ret).booleanValue(); 2236 } 2237 catch( PrivilegedActionException pae) { return false;} finally 2239 { 2240 actionRegularFile = null; 2241 actionRegularFile2 = null; 2242 } 2243 } 2244 2245 private synchronized boolean privCopyDirectory(StorageFile from, 2246 File to, 2247 byte[] buffer, 2248 String [] filter, 2249 boolean copySubdirs) 2250 { 2251 actionCode = COPY_STORAGE_DIRECTORY_TO_REGULAR_ACTION; 2252 actionStorageFile = from; 2253 actionRegularFile = to; 2254 actionBuffer = buffer; 2255 actionFilter = filter; 2256 actionCopySubDirs = copySubdirs; 2257 2258 try 2259 { 2260 Object ret = AccessController.doPrivileged( this); 2261 return ((Boolean ) ret).booleanValue(); 2262 } 2263 catch( PrivilegedActionException pae) { return false;} finally 2265 { 2266 actionStorageFile = null; 2267 actionRegularFile = null; 2268 actionBuffer = null; 2269 actionFilter = null; 2270 } 2271 } 2272 2273 2274 private synchronized boolean privCopyDirectory( File from, StorageFile to, byte[] buffer, String [] filter) 2275 { 2276 actionCode = COPY_REGULAR_DIRECTORY_TO_STORAGE_ACTION; 2277 actionStorageFile = to; 2278 actionRegularFile = from; 2279 actionBuffer = buffer; 2280 actionFilter = filter; 2281 2282 try 2283 { 2284 Object ret = AccessController.doPrivileged( this); 2285 return ((Boolean ) ret).booleanValue(); 2286 } 2287 catch( PrivilegedActionException pae) { return false;} finally 2289 { 2290 actionStorageFile = null; 2291 actionRegularFile = null; 2292 actionBuffer = null; 2293 actionFilter = null; 2294 } 2295 } 2296 2297 2298 private synchronized boolean privCopyFile( File from, StorageFile to) 2299 { 2300 actionCode = COPY_REGULAR_FILE_TO_STORAGE_ACTION; 2301 actionStorageFile = to; 2302 actionRegularFile = from; 2303 2304 try 2305 { 2306 Object ret = AccessController.doPrivileged( this); 2307 return ((Boolean ) ret).booleanValue(); 2308 } 2309 catch( PrivilegedActionException pae) { return false;} finally 2311 { 2312 actionStorageFile = null; 2313 actionRegularFile = null; 2314 } 2315 } 2316 2317 private synchronized boolean privCopyFile( StorageFile from, File to) 2318 { 2319 actionCode = COPY_STORAGE_FILE_TO_REGULAR_ACTION; 2320 actionStorageFile = from; 2321 actionRegularFile = to; 2322 2323 try 2324 { 2325 Object ret = AccessController.doPrivileged( this); 2326 return ((Boolean ) ret).booleanValue(); 2327 } 2328 catch( PrivilegedActionException pae) { return false;} finally 2330 { 2331 actionStorageFile = null; 2332 actionRegularFile = null; 2333 } 2334 } 2335 2336 2337 2338 private synchronized boolean privCopyFile( StorageFile from, StorageFile to) 2339 { 2340 actionCode = COPY_STORAGE_FILE_TO_STORAGE_ACTION; 2341 actionStorageFile = from; 2342 actionToStorageFile = to; 2343 2344 try 2345 { 2346 Object ret = AccessController.doPrivileged( this); 2347 return ((Boolean ) ret).booleanValue(); 2348 } 2349 catch( PrivilegedActionException pae) { return false;} finally 2351 { 2352 actionStorageFile = null; 2353 actionToStorageFile = null; 2354 } 2355 } 2356 2357 2358 private synchronized String [] privList(final File file) 2359 { 2360 actionCode = REGULAR_FILE_LIST_DIRECTORY_ACTION; 2361 actionRegularFile = file; 2362 2363 try 2364 { 2365 return (String []) AccessController.doPrivileged( this); 2366 } 2367 catch( PrivilegedActionException pae) { return null;} finally 2369 { 2370 actionRegularFile = null; 2371 } 2372 } 2373 2374 private synchronized String [] privList(final StorageFile file) 2375 { 2376 actionCode = STORAGE_FILE_LIST_DIRECTORY_ACTION; 2377 actionStorageFile = file; 2378 2379 try 2380 { 2381 return (String []) AccessController.doPrivileged( this); 2382 } 2383 catch( PrivilegedActionException pae) { return null;} finally 2385 { 2386 actionStorageFile = null; 2387 } 2388 } 2389 2390 2391 private synchronized String privGetCanonicalPath(final StorageFile file) 2392 { 2393 actionCode = STORAGE_FILE_GET_CANONICALPATH_ACTION; 2394 actionStorageFile = file; 2395 2396 try 2397 { 2398 return (String ) AccessController.doPrivileged( this); 2399 } 2400 catch( PrivilegedActionException pae) { 2401 return null; 2402 } catch(SecurityException se) { 2404 return null; 2407 } 2408 finally 2409 { 2410 actionStorageFile = null; 2411 } 2412 } 2413 2414 2415 private synchronized String privGetCanonicalPath(final File file) 2416 { 2417 actionCode = REGULAR_FILE_GET_CANONICALPATH_ACTION; 2418 actionRegularFile = file; 2419 2420 try 2421 { 2422 return (String ) AccessController.doPrivileged( this); 2423 } 2424 catch( PrivilegedActionException pae) { 2425 return null; 2426 } catch(SecurityException se) { 2428 return null; 2431 } 2432 finally 2433 { 2434 actionRegularFile = null; 2435 } 2436 } 2437 2438 2439 public final Object run() throws IOException 2441 { 2442 switch(actionCode) 2443 { 2444 case FILE_WRITER_ACTION: 2445 return new OutputStreamWriter ( actionStorageFile.getOutputStream( actionAppend)); 2447 2448 case REGULAR_FILE_EXISTS_ACTION: 2449 return ReuseFactory.getBoolean(actionRegularFile.exists()); 2450 2451 case STORAGE_FILE_EXISTS_ACTION: 2452 return ReuseFactory.getBoolean(actionStorageFile.exists()); 2453 2454 case REGULAR_FILE_DELETE_ACTION: 2455 return ReuseFactory.getBoolean(actionRegularFile.delete()); 2456 2457 case STORAGE_FILE_DELETE_ACTION: 2458 return ReuseFactory.getBoolean(actionStorageFile.delete()); 2459 2460 case REGULAR_FILE_MKDIRS_ACTION: 2461 return ReuseFactory.getBoolean(actionRegularFile.mkdirs()); 2463 2464 case REGULAR_FILE_IS_DIRECTORY_ACTION: 2465 return ReuseFactory.getBoolean(actionRegularFile.isDirectory()); 2467 2468 case REGULAR_FILE_REMOVE_DIRECTORY_ACTION: 2469 return ReuseFactory.getBoolean(FileUtil.removeDirectory(actionRegularFile)); 2471 2472 case REGULAR_FILE_RENAME_TO_ACTION: 2473 return ReuseFactory.getBoolean(actionRegularFile.renameTo(actionRegularFile2)); 2475 2476 case COPY_STORAGE_DIRECTORY_TO_REGULAR_ACTION: 2477 return ReuseFactory.getBoolean(FileUtil.copyDirectory(storageFactory, 2479 actionStorageFile, 2480 actionRegularFile, 2481 actionBuffer, 2482 actionFilter, 2483 actionCopySubDirs)); 2484 2485 case COPY_REGULAR_DIRECTORY_TO_STORAGE_ACTION: 2486 return ReuseFactory.getBoolean(FileUtil.copyDirectory((WritableStorageFactory)storageFactory, 2488 actionRegularFile, 2489 actionStorageFile, 2490 actionBuffer, 2491 actionFilter)); 2492 2493 case COPY_REGULAR_FILE_TO_STORAGE_ACTION: 2494 return ReuseFactory.getBoolean(FileUtil.copyFile((WritableStorageFactory) storageFactory, 2496 actionRegularFile, 2497 actionStorageFile)); 2498 2499 case REGULAR_FILE_LIST_DIRECTORY_ACTION: 2500 return (String [])(actionRegularFile.list()); 2502 2503 case STORAGE_FILE_LIST_DIRECTORY_ACTION: 2504 return (String [])(actionStorageFile.list()); 2506 2507 case COPY_STORAGE_FILE_TO_REGULAR_ACTION: 2508 return ReuseFactory.getBoolean(FileUtil.copyFile( 2510 (WritableStorageFactory) storageFactory, 2511 actionStorageFile, 2512 actionRegularFile)); 2513 2514 2515 case COPY_STORAGE_FILE_TO_STORAGE_ACTION: 2516 return ReuseFactory.getBoolean(FileUtil.copyFile( 2518 (WritableStorageFactory) storageFactory, 2519 actionStorageFile, 2520 actionToStorageFile)); 2521 2522 case REGULAR_FILE_GET_CANONICALPATH_ACTION: 2523 return (String )(actionRegularFile.getCanonicalPath()); 2525 2526 case STORAGE_FILE_GET_CANONICALPATH_ACTION: 2527 return (String )(actionStorageFile.getCanonicalPath()); 2529 } 2530 return null; 2531 } } 2533 | Popular Tags |