private void check(long id) { if ( id > Integer.MAX_VALUE ) throw new FileException("BlockAccessMem: Bounds exception (id large than an int): "+id) ; if ( !Checking ) return ; if ( id < 0 || id >= blocks.size() ) throw new FileException("BlockAccessMem: Bounds exception: "+id) ; }
@Test public void location_lock_dir_error_03() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a TDB2 format lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // TDB2 format lock file, this writes a new line to the end of the lock file writer.write(Integer.toString(-1234)); writer.write('\n'); } // Trying to get the owner should error accordingly try { lock.canObtain(); Assert.fail("Expected the lock file to be considered invalid"); } catch (FileException e) { String errMsg = e.getMessage(); Assert.assertNotNull(errMsg); Assert.assertTrue(errMsg.contains("appear to be for a TDB2 database")); } } }
private void check(String filename, String ext) { if ( filename == null ) throw new FileException("Location: null filename") ; if ( filename.contains("/") || filename.contains("\\") ) throw new FileException("Illegal file component name: " + filename) ; if ( filename.contains(".") && ext != null ) throw new FileException("Filename has an extension: " + filename) ; if ( ext != null ) { if ( ext.contains(".") ) throw new FileException("Extension has an extension: " + filename) ; } }
private void check(ByteBuffer bb) { if ( !Checking ) return ; if ( bb.capacity() != blockSize ) throw new FileException(format("FileAccessMem: Wrong size block. Expected=%d : actual=%d", blockSize, bb.capacity())) ; if ( bb.order() != SystemTDB.NetworkOrder ) throw new FileException("BlockMgrMem: Wrong byte order") ; }
@Override public void sync() { if ( DebugThis ) log.debug("sync: ["+id+"]: "+filename) ; try { channel.force(false) ; } catch (IOException ex) { throw new FileException("FileBase.sync", ex) ; } }
private static FileChannel open$(String filename, String mode) { try { // "rwd" - Syncs only the file contents // "rws" - Syncs the file contents and metadata // "rw" - OS write behind possible // "r" - read only @SuppressWarnings("resource") RandomAccessFile out = new RandomAccessFile(filename, mode) ; FileChannel channel = out.getChannel() ; return channel ; } catch (IOException ex) { throw new FileException("Failed to open: "+filename+" (mode="+mode+")", ex) ; } }
/** * Checks if a lock file is valid throwing an exception if it is not * * @param lockFile * Lock file * @exception FileException * Thrown if the lock file is invalid */ private void checkLockFileForRead(File lockFile) { if (!lockFile.exists()) return; if (!lockFile.isFile() || !lockFile.canRead()) { // Unable to read lock owner because it isn't a file or we don't // have read permission throw new FileException( "Unable to check TDB lock owner for this location since the expected lock file is not a file/not readable"); } }
private void ensure(String dirname) { if ( isMem() ) return ; File file = new File(dirname) ; if ( file.exists() && !file.isDirectory() ) throw new FileException("Existing file: " + file.getAbsolutePath()) ; if ( !file.exists() ) file.mkdir() ; }
/** * Checks if a lock file is valid throwing an exception if it is not * * @param lockFile * Lock file * @exception FileException * Thrown if the lock file is invalid */ private void checkLockFileForWrite(File lockFile) { if (!lockFile.exists()) return; if (!lockFile.isFile() || !lockFile.canWrite()) { // Unable to read lock owner because it isn't a file or we don't // have read permission throw new FileException( "Unable to check TDB lock owner for this location since the expected lock file is not a file/not writable"); } } }
@Override public Block allocate(int blkSize) { if ( blkSize > 0 && blkSize != this.blockSize ) throw new FileException("Fixed blocksize only: request= "+blkSize+" / fixed size="+this.blockSize) ; int x = blocks.size() ; ByteBuffer bb = ByteBuffer.allocate(blkSize) ; Block block = new Block(x, bb) ; blocks.add(block) ; return block; }
private void readByteBuffer(long id, ByteBuffer dst) { try { int len = file.channel().read(dst, filePosition(id)) ; if ( len != blockSize ) throw new FileException(format("get: short read (%d, not %d)", len, blockSize)) ; } catch (IOException ex) { throw new FileException("FileAccessDirect", ex) ; } }
@Override public Block read(long id) { // Variable length blocks. if ( id < 0 || id >= length || id >= bytes.capacity() ) throw new FileException("Bad id (read): "+id) ; bytes.position((int)id) ; int len = bytes.getInt() ; ByteBuffer bb = ByteBuffer.allocate(len) ; // Copy out the bytes - copy for safety. bytes.get(bb.array(), 0, len) ; return new Block(id, bb) ; }
private String fixupName(String fsName) { if ( isMem() ) return fsName ; File file = new File(fsName) ; try { fsName = file.getCanonicalPath() ; } catch (IOException ex) { throw new FileException("Failed to get canoncial path: " + file.getAbsolutePath(), ex) ; } if ( !fsName.endsWith(File.separator) && !fsName.endsWith(pathSeparator) ) fsName = fsName + pathSeparator ; return fsName ; }
@Override public Block allocate(int blkSize) { if ( blkSize > 0 && blkSize != this.blockSize ) throw new FileException("Fixed blocksize only: request= "+blkSize+"fixed size="+this.blockSize) ; int x = allocateId() ; ByteBuffer bb = ByteBuffer.allocate(blkSize) ; Block block = new Block(x, bb) ; return block; }
private static FileChannel openref$(String filename, String mode) { // Temp - for now, only journal files are tracked. if ( ! filename.endsWith(".jrnl") ) { return open$(filename, mode) ; } FileChannel chan = name2channel.get(filename) ; if ( chan != null ) { // Scream - it's currently open. throw new FileException("Already open: "+filename) ; } chan = open$(filename, mode) ; name2channel.put(filename, chan) ; channel2name.put(chan, filename) ; return chan ; }
@Override public void write(Block block) { // Variable length blocks. long loc = block.getId() ; if ( loc < 0 || loc > length ) // Can be equal => append. throw new FileException("Bad id (write): "+loc+" ("+alloc+","+length+")") ; ByteBuffer bb = block.getByteBuffer() ; int len = bb.capacity() ; if ( loc == length ) { if ( bytes.capacity()-length < len ) { int cap2 = bytes.capacity()+1024 ; while(bytes.capacity()-length < len) cap2 += 1024 ; ByteBuffer bytes2 = ByteBuffer.allocate(cap2) ; bytes2.position(0) ; bytes2.put(bytes) ; } length += len +SizeOfInt ; } bytes.position((int)loc) ; bytes.putInt(len) ; bytes.put(bb.array(), 0, bb.capacity()) ; }
if (rawLockInfo.endsWith("\n")) { throw new FileException("Unable to check TDB lock owner, the lock file contents appear to be for a TDB2 database. Please try loading this location as a TDB2 database."); return owner; } catch (IOException e) { throw new FileException("Unable to check TDB lock owner due to an IO error reading the lock file", e); } catch (NumberFormatException e) { throw new FileException("Unable to check TDB lock owner as the lock file contains invalid data", e);
@Override public Block allocate(int blkSize) { if ( blkSize > 0 && blkSize != this.blockSize ) throw new FileException("Fixed blocksize only: request= "+blkSize+"fixed size="+this.blockSize) ; int id = allocateId() ; ByteBuffer bb = getByteBuffer(id) ; bb.position(0) ; Block block = new Block(id, bb) ; return block ; }
throw new FileException("Negative segment: "+seg) ; throw new FileException("Negative segment offset: "+seg) ; throw new FileException("BlockMgrMapped.segmentAllocate: Segment = "+seg+" : Offset = "+offset) ; throw new FileException("BlockMgrMapped.segmentAllocate: Segment = "+seg, ex) ;
@Override public void write(Block block) { check(block) ; checkIfClosed() ; ByteBuffer bb = block.getByteBuffer() ; // This .clear() except the javadoc suggests this is not the correct use of .clear() // and the name does bb.limit(bb.capacity()) ; // It shouldn't have been changed. bb.rewind() ; try { int len = file.channel().write(bb, filePosition(block.getId())) ; if ( len != blockSize ) throw new FileException(format("write: short write (%d, not %d)", len, blockSize)) ; } catch (IOException ex) { throw new FileException("FileAccessDirect", ex) ; } writeNotification(block) ; }