use of org.apache.derby.io.StorageFile in project derby by apache.
the class StreamFileContainer method getFileName.
/**
* Return a file name for the identity.
* <p>
* Return a valid file name for the identity, or null if the data
* directory for this segment cannot be created
*
* @exception StandardException Segment directory cannot be created
*/
protected StorageFile getFileName(ContainerKey identity, boolean forCreate, boolean errorOK) throws StandardException {
if (identity.getSegmentId() == StreamContainerHandle.TEMPORARY_SEGMENT) {
return (dataFactory.storageFactory.newStorageFile(dataFactory.storageFactory.getTempDir(), "T" + identity.getContainerId() + ".tmp"));
} else {
if (SanityManager.DEBUG)
SanityManager.THROWASSERT("cannot create stream container in non-temp segments yet.");
StorageFile container = dataFactory.getContainerPath(identity, false);
if (!privExists(container)) {
if (!forCreate)
return null;
StorageFile directory = container.getParentDir();
if (!privExists(directory)) {
// make sure only 1 thread can create a segment at one time
synchronized (dataFactory) {
if (!privExists(directory)) {
boolean created = false;
IOException ex = null;
try {
created = privMkdirs(directory);
} catch (IOException ioe) {
ex = ioe;
}
if (!created) {
if (errorOK)
return null;
else
throw StandardException.newException(SQLState.FILE_CANNOT_CREATE_SEGMENT, ex, directory);
}
}
}
}
}
return container;
}
}
use of org.apache.derby.io.StorageFile in project derby by apache.
the class StorageFactoryService method resolveServicePropertiesFiles.
/**
* Resolves situations where a failure condition left the service properties
* file, and/or the service properties file backup, in an inconsistent
* state.
* <p>
* Note that this method doesn't resolve the situation where both the
* current service properties file and the backup file are missing.
*
* @param sf the storage factory for the service
* @param spf the service properties file
* @throws StandardException if a file operation on a service properties
* file fails
*/
private void resolveServicePropertiesFiles(StorageFactory sf, StorageFile spf) throws StandardException {
StorageFile spfOld = sf.newStorageFile(PROPERTIES_NAME.concat("old"));
FileOperationHelper foh = new FileOperationHelper();
boolean hasCurrent = foh.exists(spf, true);
boolean hasBackup = foh.exists(spfOld, true);
// Shortcut the normal case.
if (hasCurrent && !hasBackup) {
return;
}
// Backup file, but no current file.
if (hasBackup && !hasCurrent) {
// Writing the new service properties file must have failed during
// an update. Rename the backup to be the current file.
foh.renameTo(spfOld, spf, true);
Monitor.getStream().printlnWithHeader(MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_RESTORED));
// Backup file and current file.
} else if (hasBackup && hasCurrent) {
// See if the new (current) file is valid. If so delete the backup,
// if not, rename the backup to be the current.
BufferedReader bin = null;
String lastLine = null;
try {
// service.properties always in ISO-8859-1 because written with Properties.store()
bin = new BufferedReader(new InputStreamReader(new FileInputStream(spf.getPath()), "ISO-8859-1"));
String line;
while ((line = bin.readLine()) != null) {
if (line.trim().length() != 0) {
lastLine = line;
}
}
} catch (IOException ioe) {
throw StandardException.newException(SQLState.UNABLE_TO_OPEN_FILE, ioe, spf.getPath(), ioe.getMessage());
} finally {
try {
if (bin != null) {
bin.close();
}
} catch (IOException ioe) {
// Ignore exception during close
}
}
if (lastLine != null && lastLine.startsWith(SERVICE_PROPERTIES_EOF_TOKEN)) {
// Being unable to delete the backup file is fine as long as
// the current file appears valid.
String msg;
if (foh.delete(spfOld, false)) {
msg = MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_BACKUP_DELETED);
} else {
// Include path so the user can delete file manually.
msg = MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_BACKUP_DEL_FAILED, getMostAccuratePath(spfOld));
}
Monitor.getStream().printlnWithHeader(msg);
} else {
foh.delete(spf, false);
foh.renameTo(spfOld, spf, true);
Monitor.getStream().printlnWithHeader(MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_RESTORED));
}
}
}
use of org.apache.derby.io.StorageFile in project derby by apache.
the class LOBStreamControl method init.
private void init(byte[] b, long len) throws IOException, StandardException {
Object monitor = findService(Property.DATABASE_MODULE, conn.getDBName());
final DataFactory df = (DataFactory) findServiceModule(monitor, DataFactory.MODULE);
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws IOException {
// create a temporary file
StorageFile lobFile = df.getStorageFactory().createTemporaryFile("lob", null);
if (df.databaseEncrypted()) {
tmpFile = new EncryptedLOBFile(lobFile, df);
} else {
tmpFile = new LOBFile(lobFile);
}
return null;
}
});
} catch (PrivilegedActionException pae) {
throw (IOException) pae.getCause();
}
conn.addLobFile(tmpFile);
isBytes = false;
// now this call will write into the file
if (len != 0)
write(b, 0, (int) len, 0);
dataBytes = null;
}
Aggregations