mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 23:05:47 +02:00
[385630] [dstore] backup files created during upload should be removed when upload successful
This commit is contained in:
parent
a769989ace
commit
57850d9ac3
1 changed files with 66 additions and 8 deletions
|
@ -16,6 +16,7 @@
|
||||||
* David McKnight (IBM) [281712] [dstore] Warning message is needed when disk is full
|
* David McKnight (IBM) [281712] [dstore] Warning message is needed when disk is full
|
||||||
* David McKnight (IBM) [367424] [dstore] upload mechanism should provide backups of files
|
* David McKnight (IBM) [367424] [dstore] upload mechanism should provide backups of files
|
||||||
* David McKnight (IBM) [380023] [dstore] remote file permissions lost after upload
|
* David McKnight (IBM) [380023] [dstore] remote file permissions lost after upload
|
||||||
|
* David McKnight (IBM) [385630] [dstore] backup files created during upload should be removed when upload successful
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.dstore.core.model;
|
package org.eclipse.dstore.core.model;
|
||||||
|
@ -26,6 +27,8 @@ import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.eclipse.dstore.core.server.SecuredThread;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* The ByteStreamHandler class is used to abstract file read and write operations
|
* The ByteStreamHandler class is used to abstract file read and write operations
|
||||||
|
@ -45,15 +48,27 @@ public class ByteStreamHandler implements IByteStreamHandler
|
||||||
protected DataStore _dataStore;
|
protected DataStore _dataStore;
|
||||||
protected DataElement _log;
|
protected DataElement _log;
|
||||||
protected static final String FILEMSG_REMOTE_SAVE_FAILED = "RSEF5006"; //$NON-NLS-1$
|
protected static final String FILEMSG_REMOTE_SAVE_FAILED = "RSEF5006"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
// for file backups
|
||||||
|
private boolean _doBackups = true;
|
||||||
|
private boolean _keepBackups = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contructor
|
* Constructor
|
||||||
* @param dataStore the DataStore instance
|
* @param dataStore the DataStore instance
|
||||||
*/
|
*/
|
||||||
public ByteStreamHandler(DataStore dataStore, DataElement log)
|
public ByteStreamHandler(DataStore dataStore, DataElement log)
|
||||||
{
|
{
|
||||||
_dataStore = dataStore;
|
_dataStore = dataStore;
|
||||||
_log = log;
|
_log = log;
|
||||||
|
|
||||||
|
String doBackups = System.getProperty("backupfiles"); //$NON-NLS-1$
|
||||||
|
_doBackups = (doBackups == null || doBackups.equals("true")); //$NON-NLS-1$
|
||||||
|
|
||||||
|
if (_doBackups){
|
||||||
|
String keepBackups = System.getProperty("keepbackupfiles"); //$NON-NLS-1$
|
||||||
|
_keepBackups = (keepBackups == null || keepBackups.equals("true")); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId()
|
public String getId()
|
||||||
|
@ -61,6 +76,46 @@ public class ByteStreamHandler implements IByteStreamHandler
|
||||||
return getClass().getName();
|
return getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DeleteBackupThread extends SecuredThread {
|
||||||
|
private File _currentFile;
|
||||||
|
private File _backupFile;
|
||||||
|
private long _initialLength;
|
||||||
|
public DeleteBackupThread(DataStore dataStore, File currentFile, File backupFile){
|
||||||
|
super(dataStore);
|
||||||
|
_currentFile = currentFile;
|
||||||
|
_backupFile = backupFile;
|
||||||
|
_initialLength = _currentFile.length(); // get initial length so we can see if upload is still happening
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(){
|
||||||
|
super.run();
|
||||||
|
boolean doneDelete = false;
|
||||||
|
while (!doneDelete){
|
||||||
|
try {
|
||||||
|
Thread.sleep(10000); // wait 10 seconds
|
||||||
|
}
|
||||||
|
catch (InterruptedException e){
|
||||||
|
}
|
||||||
|
|
||||||
|
long curLength = _currentFile.length();
|
||||||
|
if (curLength == _initialLength){ // looks like total upload is complete
|
||||||
|
_backupFile.delete();
|
||||||
|
doneDelete = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_initialLength = curLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteBackupFile(File currentFile, File backupFile){
|
||||||
|
if (backupFile != null && _keepBackups){ // only matters if there is a backup file
|
||||||
|
DeleteBackupThread thread = new DeleteBackupThread(_dataStore, currentFile, backupFile);
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void backupFile(File file, File backupFile){
|
private void backupFile(File file, File backupFile){
|
||||||
|
|
||||||
/* this is nice but orginal file permissions not perserved
|
/* this is nice but orginal file permissions not perserved
|
||||||
|
@ -129,6 +184,7 @@ public class ByteStreamHandler implements IByteStreamHandler
|
||||||
{
|
{
|
||||||
// need to create directories as well
|
// need to create directories as well
|
||||||
File file = new File(fileName);
|
File file = new File(fileName);
|
||||||
|
File backupFile = null;
|
||||||
File parent = new File(file.getParent());
|
File parent = new File(file.getParent());
|
||||||
if (!file.exists())
|
if (!file.exists())
|
||||||
{
|
{
|
||||||
|
@ -138,11 +194,10 @@ public class ByteStreamHandler implements IByteStreamHandler
|
||||||
{
|
{
|
||||||
if (!_dataStore.isVirtual()){ // only applies to server
|
if (!_dataStore.isVirtual()){ // only applies to server
|
||||||
// backup file on upload by default
|
// backup file on upload by default
|
||||||
String doBackups = System.getProperty("backupfiles"); //$NON-NLS-1$
|
if (_doBackups){
|
||||||
if (doBackups == null || doBackups.equals("true")){ //$NON-NLS-1$
|
|
||||||
// backup the file first
|
// backup the file first
|
||||||
String n = file.getName();
|
String n = file.getName();
|
||||||
File backupFile = new File(parent, '.' + n + '~');
|
backupFile = new File(parent, '.' + n + '~');
|
||||||
_dataStore.trace("Backing up as "+backupFile.getAbsolutePath()); //$NON-NLS-1$
|
_dataStore.trace("Backing up as "+backupFile.getAbsolutePath()); //$NON-NLS-1$
|
||||||
backupFile(file, backupFile);
|
backupFile(file, backupFile);
|
||||||
}
|
}
|
||||||
|
@ -165,6 +220,8 @@ public class ByteStreamHandler implements IByteStreamHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
fileStream.close();
|
fileStream.close();
|
||||||
|
|
||||||
|
deleteBackupFile(newFile, backupFile);
|
||||||
if (status == null)
|
if (status == null)
|
||||||
return;
|
return;
|
||||||
status.setAttribute(DE.A_SOURCE, "success"); //$NON-NLS-1$
|
status.setAttribute(DE.A_SOURCE, "success"); //$NON-NLS-1$
|
||||||
|
@ -213,9 +270,9 @@ public class ByteStreamHandler implements IByteStreamHandler
|
||||||
{
|
{
|
||||||
// need to create directories as well
|
// need to create directories as well
|
||||||
File file = new File(fileName);
|
File file = new File(fileName);
|
||||||
|
File parent = new File(file.getParent());
|
||||||
if (!file.exists())
|
if (!file.exists())
|
||||||
{
|
{
|
||||||
File parent = new File(file.getParent());
|
|
||||||
parent.mkdirs();
|
parent.mkdirs();
|
||||||
|
|
||||||
File newFile = new File(fileName);
|
File newFile = new File(fileName);
|
||||||
|
@ -255,6 +312,7 @@ public class ByteStreamHandler implements IByteStreamHandler
|
||||||
outStream.close();
|
outStream.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status == null)
|
if (status == null)
|
||||||
return;
|
return;
|
||||||
status.setAttribute(DE.A_SOURCE, "success"); //$NON-NLS-1$
|
status.setAttribute(DE.A_SOURCE, "success"); //$NON-NLS-1$
|
||||||
|
|
Loading…
Add table
Reference in a new issue