/** * Open an encrypted file store with the given settings. * * @param handler the data handler * @param name the file name * @param mode the access mode (r, rw, rws, rwd) * @param cipher the name of the cipher algorithm * @param key the encryption key * @param keyIterations the number of iterations the key should be hashed * @return the created object */ public static FileStore open(DataHandler handler, String name, String mode, String cipher, byte[] key, int keyIterations) { FileStore store; if (cipher == null) { store = new FileStore(handler, name, mode); } else { store = new SecureFileStore(handler, name, mode, cipher, key, keyIterations); } return store; }
String fileName = database.createTempFile(); file = database.openFile(fileName, "rw", false); file.setCheckedWriting(false); file.setLength(FileStore.HEADER_LENGTH); r.append(buff, this); if (i == records.size() - 1 || buff.length() > Constants.UNDO_BLOCK_SIZE) { storedEntriesPos.add(file.getFilePointer()); file.write(buff.getBytes(), 0, buff.length()); buff.reset(); memoryUndo = 0; records.clear(); file.autoDelete(); String fileName = database.createTempFile(); file = database.openFile(fileName, "rw", false); file.setCheckedWriting(false); file.seek(FileStore.HEADER_LENGTH); rowBuff = Data.create(database, Constants.DEFAULT_PAGE_SIZE); Data buff = rowBuff; saveIfPossible(entry, rowBuff); file.autoDelete();
/** * Write a number of bytes. * * @param b the source buffer * @param off the offset * @param len the number of bytes to write */ public void write(byte[] b, int off, int len) { if (SysProperties.CHECK && (len < 0 || len % Constants.FILE_BLOCK_SIZE != 0)) { DbException.throwInternalError( "unaligned write " + name + " len " + len); } checkWritingAllowed(); checkPowerOff(); try { FileUtils.writeFully(file, ByteBuffer.wrap(b, off, len)); } catch (IOException e) { closeFileSilently(); throw DbException.convertIOException(e, name); } filePos += len; fileLength = Math.max(filePos, fileLength); }
/** * Close the result list and delete the temporary file. */ public void close() { if (file != null) { file.autoDelete(); file.closeAndDeleteSilently(); file = null; rowBuff = null; } }
@Override public FileStore openFile(String name, String mode, boolean mustExist) { if (mustExist && !FileUtils.exists(name)) { throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name); } FileStore store; if (cipher == null) { store = FileStore.open(this, name, mode); } else { store = FileStore.open(this, name, mode, cipher, fileEncryptionKey, 0); } store.setCheckedWriting(false); try { store.init(); } catch (DbException e) { store.closeSilently(); throw e; } return store; }
@Override public FileStore openFile(String name, String openMode, boolean mustExist) { if (mustExist && !FileUtils.exists(name)) { throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name); } FileStore store = FileStore.open(this, name, openMode, cipher, filePasswordHash); try { store.init(); } catch (DbException e) { store.closeSilently(); throw e; } return store; }
byte[] salt; byte[] magic = HEADER.getBytes(StandardCharsets.UTF_8); if (length() < HEADER_LENGTH) { writeDirect(magic, 0, len); salt = generateSalt(); writeDirect(salt, 0, len); initKey(salt); write(magic, 0, len); checkedWriting = true; } else { seek(0); byte[] buff = new byte[len]; readFullyDirect(buff, 0, len); if (!Arrays.equals(buff, magic)) { throw DbException.get(ErrorCode.FILE_VERSION_ERROR_1, name); readFullyDirect(salt, 0, len); initKey(salt); readFully(buff, 0, Constants.FILE_BLOCK_SIZE); if (!Arrays.equals(buff, magic)) { throw DbException.get(ErrorCode.FILE_ENCRYPTION_ERROR_1, name);
store.openFile(); if (store.length() == store.getFilePointer()) { close(); return; store.readFully(page.getBytes(), 0, Constants.FILE_BLOCK_SIZE); page.reset(); remainingInBuffer = page.readInt(); page.reset(); page.readInt(); store.readFully(page.getBytes(), Constants.FILE_BLOCK_SIZE, len); page.reset(); page.readInt(); store.closeFile();
FileStore fileOut; if (key == null) { fileOut = FileStore.open(null, temp, "rw"); } else { fileOut = FileStore.open(null, temp, "rw", cipherType, key); fileOut.init(); byte[] buffer = new byte[4 * 1024]; long remaining = in.length() - FileStore.HEADER_LENGTH; long total = remaining; in.seek(FileStore.HEADER_LENGTH); fileOut.seek(FileStore.HEADER_LENGTH); long time = System.nanoTime(); while (remaining > 0) { in.readFully(buffer, 0, len); fileOut.write(buffer, 0, len); remaining -= len; in.close(); fileOut.close(); FileUtils.delete(fileName); FileUtils.move(temp, fileName);
textStorage = Database.isTextStorage(fileName, false); byte[] magic = Database.getMagic(textStorage); FileStore store = FileStore.open(null, fileName, "rw", magic); long length = store.length(); int offset = FileStore.HEADER_LENGTH; int blockSize = DiskFile.BLOCK_SIZE; blockCount = 1; for (int block = 0; block < blocks; block += blockCount) { store.seek(offset + (long) block * blockSize); byte[] bytes = new byte[blockSize]; DataPage s = DataPage.create(this, bytes); long start = store.getFilePointer(); store.readFully(bytes, 0, blockSize); blockCount = s.readInt(); storageId = -1; store.readFully(s.getBytes(), blockSize, blockCount * blockSize - blockSize); s.fill(blockCount * blockSize); s.updateChecksum(); store.seek(start); store.write(s.getBytes(), 0, s.length()); if (trace) { out.println("User: " + userName);
private void writeAllRows() { if (file == null) { Database db = session.getDatabase(); String fileName = db.createTempFile(); file = db.openFile(fileName, "rw", false); file.setCheckedWriting(false); file.seek(FileStore.HEADER_LENGTH); rowBuff = Data.create(db, Constants.DEFAULT_PAGE_SIZE); file.seek(FileStore.HEADER_LENGTH); } Data buff = rowBuff; initBuffer(buff); for (int i = 0, size = list.size(); i < size; i++) { if (i > 0 && buff.length() > Constants.IO_BUFFER_SIZE) { flushBuffer(buff); initBuffer(buff); } Row r = list.get(i); writeRow(buff, r); } flushBuffer(buff); file.autoDelete(); list.clear(); memory = 0; }
/** * Save the row in the file using a buffer. * * @param buff the buffer * @param file the file * @param log the undo log */ void save(Data buff, FileStore file, UndoLog log) { buff.reset(); append(buff, log); filePos = (int) (file.getFilePointer() / Constants.FILE_BLOCK_SIZE); file.write(buff.getBytes(), 0, buff.length()); row = null; state = STORED; }
@Override public void close() { if (store != null) { try { store.close(); } finally { store = null; } } }
private static void closeSilently(FileStore fileStore) { if (fileStore != null) { fileStore.closeSilently(); } }
/** * Close the file without further writing. */ public synchronized void close() { trace.debug("close"); if (log != null) { log.close(); log = null; } if (file != null) { try { file.releaseLock(); file.close(); } finally { file = null; } } }
public void unlink() throws SQLException { if (linked && fileName != null) { String temp; // synchronize on the database, to avoid concurrent temp file // creation / deletion / backup synchronized (handler) { if (handler.getLobFilesInDirectories()) { temp = getFileName(handler, -1, objectId); } else { // just to get a filename - an empty file will be created temp = handler.createTempFile(); } deleteFile(handler, temp); renameFile(handler, fileName, temp); tempFile = FileStore.open(handler, temp, "rw", null); tempFile.autoDelete(); tempFile.closeSilently(); fileName = temp; linked = false; } } }
readStaticHeader(); freeListPagesPerList = PageFreeList.getPagesAddressed(pageSize); fileLength = file.length(); pageCount = (int) (fileLength / pageSize); if (pageCount < MIN_PAGE_COUNT) { fileName + " pageCount: " + pageCount); file.releaseLock(); file.close(); FileUtils.delete(fileName); openNew();
/** * Clear the undo log. This method is called after the transaction is * committed. */ void clear() { records.clear(); storedEntries = 0; storedEntriesPos.clear(); memoryUndo = 0; if (file != null) { file.closeAndDeleteSilently(); file = null; rowBuff = null; } }
private FileStoreOutputStream initLarge(DataHandler h) { this.handler = h; this.tableId = 0; this.linked = false; this.precision = 0; this.small = null; this.hash = 0; String compressionAlgorithm = h.getLobCompressionAlgorithm(type); this.compressed = compressionAlgorithm != null; synchronized (h) { String path = h.getDatabasePath(); if ((path != null) && (path.length() == 0)) { path = new File(Utils.getProperty("java.io.tmpdir", "."), SysProperties.PREFIX_TEMP_FILE).getAbsolutePath(); } objectId = getNewObjectId(h); fileName = getFileNamePrefix(path, objectId) + Constants.SUFFIX_TEMP_FILE; tempFile = h.openFile(fileName, "rw", false); tempFile.autoDelete(); } return new FileStoreOutputStream(tempFile, h, compressionAlgorithm); }
private void writeAllRows() throws SQLException { if (file == null) { Database db = session.getDatabase(); this.cache = db.getDataFile().getCache(); String fileName = db.createTempFile(); file = db.openFile(fileName, "rw", false); file.autoDelete(); file.seek(FileStore.HEADER_LENGTH); rowBuff = DataPage.create(db, Constants.DEFAULT_DATA_PAGE_SIZE); file.seek(FileStore.HEADER_LENGTH); } DataPage buff = rowBuff; initBuffer(buff); for (int i = 0; i < list.size(); i++) { if (i > 0 && buff.length() > Constants.IO_BUFFER_SIZE) { flushBuffer(buff); initBuffer(buff); } Row r = (Row) list.get(i); writeRow(buff, r); } flushBuffer(buff); list.clear(); memory = 0; }