summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNils Schwabe2014-07-14 14:55:09 +0200
committerNils Schwabe2014-07-14 14:55:09 +0200
commit6a21ad5c5674f64c646860648f2a3343458b7006 (patch)
treea91172440aa69deb2060b1c7bc50302f8162c162
parentAdd security checks for image upload (diff)
downloadmasterserver-6a21ad5c5674f64c646860648f2a3343458b7006.tar.gz
masterserver-6a21ad5c5674f64c646860648f2a3343458b7006.tar.xz
masterserver-6a21ad5c5674f64c646860648f2a3343458b7006.zip
Add feature to hold crc sums in RAM when writing to disk fails
-rw-r--r--src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java2
-rw-r--r--src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java68
-rw-r--r--src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java39
3 files changed, 71 insertions, 38 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java b/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java
index 065ca14..f088af4 100644
--- a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java
+++ b/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java
@@ -21,7 +21,7 @@ public class CRCScheduler extends TimerTask
Iterator<UploadingImage> iter = list.iterator();
while ( iter.hasNext() ) {
UploadingImage image = iter.next();
- CRCChecker crcChecker = new CRCChecker( image.getFilename(), image.getCrcFilename() );
+ CRCChecker crcChecker = new CRCChecker( image.getImageFile(), image.getCrcFile() );
for ( int block = 0; block < image.getNumberOfBlocks(); block++ ) {
if ( image.needsCheck( block ) ) {
try {
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java
index f40aece..9758a2c 100644
--- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java
+++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java
@@ -70,23 +70,22 @@ public class ImageProcessor
{
// check image data
if ( DbImage.exists( imageData.uuid ) ) {
- throw new ImageDataException( ImageDataError.INVALID_DATA, "UUID already existing.");
+ throw new ImageDataException( ImageDataError.INVALID_DATA, "UUID already existing." );
} else if ( imageData.imageName == null || imageData.imageName.isEmpty() ) {
- throw new ImageDataException( ImageDataError.INVALID_DATA, "Image name not set.");
+ throw new ImageDataException( ImageDataError.INVALID_DATA, "Image name not set." );
} else if ( imageData.imageName == null || imageData.imageOwner.isEmpty() ) {
- throw new ImageDataException( ImageDataError.INVALID_DATA, "Image owner not set.");
+ throw new ImageDataException( ImageDataError.INVALID_DATA, "Image owner not set." );
} else if ( imageData.contentOperatingSystem == null || imageData.contentOperatingSystem.isEmpty() ) {
- throw new ImageDataException( ImageDataError.INVALID_DATA, "Content operating system not set.");
+ throw new ImageDataException( ImageDataError.INVALID_DATA, "Content operating system not set." );
} else if ( imageData.fileSize <= 0 ) {
- throw new ImageDataException( ImageDataError.INVALID_DATA, "File size is too small.");
+ throw new ImageDataException( ImageDataError.INVALID_DATA, "File size is too small." );
} else if ( !DbUser.exists( imageData.imageOwner ) ) {
- throw new ImageDataException( ImageDataError.INVALID_DATA, "User is not known." );
+ throw new ImageDataException( ImageDataError.INVALID_DATA, "User is not known." );
}
String uuid = imageData.uuid;
String token;
String filepath;
- String crcPath;
int nBlocks;
int[] allBlocks;
UploadingImage image;
@@ -104,22 +103,25 @@ public class ImageProcessor
}
// insert new image
- if ( !CRCFile.sumsAreValid( crcSums ) ) throw new UploadException(UploadError.INVALID_CRC, "CRC sums were invalid.");
+ if ( !CRCFile.sumsAreValid( crcSums ) )
+ throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." );
filepath = Globals.getImageDir() + "/" + uuid + ".vmdk";
token = RandomString.generate( 100, false );
- crcPath = Globals.getImageDir() + "/" + uuid + ".crc";
nBlocks = (int)Math.ceil( imageData.fileSize / Globals.blockSize );
- allBlocks = new int[nBlocks]; // initalize array with all zeros which mean that this block is missing
- image = new UploadingImage( token, allBlocks, new Timestamp( System.currentTimeMillis() ), uuid, filepath, crcPath );
+ allBlocks = new int[ nBlocks ]; // initalize array with all zeros which mean that this block is missing
+ image = new UploadingImage( token, allBlocks, new Timestamp( System.currentTimeMillis() ), uuid, filepath );
uploadingImages.put( uuid, image );
}
-
- try {
- CRCFile.writeCrcFile( crcSums, crcPath );
- } catch (IOException e) {
- log.error( "Could not create crc file", e );
- return null; // TODO: what to do if we can not write the crc file to disk? Give object to crcscheduler?
- }
+
+ CRCFile crcFile;
+ try {
+ // try to write crc file ...
+ crcFile = CRCFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" );
+ } catch ( IOException e ) {
+ // ... and keep it in ram if it fails
+ crcFile = new CRCFile( crcSums );
+ }
+ image.setCrcFile( crcFile );
ConnectionHandler.addConnection( token, filepath, Connection.UPLOADING ).image = image;
DbImage.insert( imageData, System.currentTimeMillis(), token, nBlocks, serverSessionId, filepath );
@@ -168,7 +170,7 @@ public class ImageProcessor
DownloadingClient client = new DownloadingClient();
client.addDownload( uuid, requestedBlocks, token );
downloadingClients.put( serverSessionId, client );
-
+
ConnectionHandler.addConnection( token, filepath, Connection.DOWNLOADING ).client = client;
return new DownloadInfos( token, Globals.getSslSocketPort() );
}
@@ -203,13 +205,14 @@ public class ImageProcessor
int got = 0;
for ( int i = 0; i < image.getNumberOfBlocks(); i++ ) {
- if (image.needsRequest( i )) {
- result.add( i ) ;
+ if ( image.needsRequest( i ) ) {
+ result.add( i );
got++;
}
- if (got == amount) break;
+ if ( got == amount )
+ break;
}
-
+
return result;
}
@@ -224,7 +227,7 @@ public class ImageProcessor
synchronized ( imagesToCheck ) {
imagesToCheck.remove( uuid );
}
-
+
UploadingImage image;
synchronized ( uploadingImages ) {
image = uploadingImages.remove( uuid );
@@ -244,12 +247,13 @@ public class ImageProcessor
}
return result;
}
-
- public static List<Integer> getRequestedBlocks( String token) {
+
+ public static List<Integer> getRequestedBlocks( String token )
+ {
// for the downloader
return null;
}
-
+
/**
* Checks pending uploads in database and adds them to process list again.
*/
@@ -260,7 +264,15 @@ public class ImageProcessor
String token = image.token;
ConnectionHandler.addConnection( token, image.imagePath, Connection.UPLOADING );
UploadingImage infos = new UploadingImage( token, image.blockStatus, image.timestamp,
- image.uuid, image.imagePath, Globals.getImageDir() + "/" + image.uuid + ".crc" );
+ image.uuid, image.imagePath );
+ CRCFile crcFile = new CRCFile( Globals.getImageDir() + "/" + image.uuid + ".crc" );
+ try {
+ if ( !crcFile.isValid() )
+ continue;
+ } catch ( IOException e ) {
+ continue;
+ }
+ infos.setCrcFile( crcFile );
uploadingImages.put( image.uuid, infos );
}
log.info( "Added " + list.size() + " pending upload(s) to process list again." );
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
index 27e18fd..06ad5b1 100644
--- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
+++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
@@ -3,6 +3,9 @@ package org.openslx.imagemaster.serverconnection;
import java.sql.Timestamp;
import org.apache.log4j.Logger;
+import org.openslx.imagemaster.Globals;
+import org.openslx.imagemaster.crcchecker.CRCFile;
+import org.openslx.imagemaster.crcchecker.ImageFile;
import org.openslx.imagemaster.db.DbImage;
/**
@@ -30,14 +33,14 @@ public class UploadingImage
private DbImage dbImage = null; // the DB representation of this image
private String uuid;
private String filename;
- private String crcFilename;
+ private ImageFile imageFile;
+ private CRCFile crcFile = null;
- protected UploadingImage(String token, int[] initialBlockStatus, Timestamp ts, String uuid, String filename, String crcFilename)
+ protected UploadingImage(String token, int[] initialBlockStatus, Timestamp ts, String uuid, String filename)
{
this.token = token;
this.ts = ts;
this.uuid = uuid;
- this.crcFilename = crcFilename;
this.blockStatus = initialBlockStatus;
}
@@ -54,14 +57,14 @@ public class UploadingImage
return;
blockStatus[index] = missing;
}
-
+
protected void setNeedsRequest( int index )
{
if ( blockStatus == null )
return;
blockStatus[index] *= ( blockStatus[index] < missing ) ? -1 : 1; // switch to positive value if needed
}
-
+
protected void setNeedsCheck( int index )
{
if ( blockStatus == null )
@@ -113,14 +116,32 @@ public class UploadingImage
return this.dbImage;
}
- protected String getFilename()
+ // protected String getFilename()
+ // {
+ // return this.filename;
+ // }
+ //
+ // protected String getCrcFilename()
+ // {
+ // return this.crcFilename;
+ // }
+
+ protected ImageFile getImageFile()
+ {
+ if ( imageFile == null ) {
+ imageFile = new ImageFile( filename, Globals.blockSize );
+ }
+ return imageFile;
+ }
+
+ protected CRCFile getCrcFile()
{
- return this.filename;
+ return crcFile;
}
- protected String getCrcFilename()
+ protected void setCrcFile( CRCFile crcFile )
{
- return this.crcFilename;
+ this.crcFile = crcFile;
}
public int getAmountOfMissingBlocks()