Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // from line 239 of https://github.com/freenet/fred/blob/next/src/freenet/client/async/ClientPutter.java
- /** Start the insert.
- 140 * @param restart If true, restart the insert even though it has completed before.
- 141 * @param context Contains some useful transient fields such as the schedulers.
- 142 * @throws InsertException If the insert cannot be started for some reason.
- 143 */
- 144 public boolean start(boolean restart, ClientContext context) throws InsertException {
- 145 if(logMINOR)
- 146 Logger.minor(this, "Starting "+this+" for "+targetURI);
- 147 byte cryptoAlgorithm;
- 148 CompatibilityMode mode = ctx.getCompatibilityMode();
- 149 if(!(mode == CompatibilityMode.COMPAT_CURRENT || mode.ordinal() >= CompatibilityMode.COMPAT_1416.ordinal()))
- 150 cryptoAlgorithm = Key.ALGO_AES_PCFB_256_SHA256;
- 151 else
- 152 cryptoAlgorithm = Key.ALGO_AES_CTR_256_SHA256;
- 153 try {
- 154 this.targetURI.checkInsertURI();
- 155 // If the top level key is an SSK, all CHK blocks and particularly splitfiles below it should have
- 156 // randomised keys. This substantially improves security by making it impossible to identify blocks
- 157 // even if you know the content. In the user interface, we will offer the option of inserting as a
- 158 // random SSK to take advantage of this.
- 159 boolean randomiseSplitfileKeys = randomiseSplitfileKeys(targetURI, ctx, persistent());
- 160
- 161 if(data == null)
- 162 throw new InsertException(InsertExceptionMode.BUCKET_ERROR, "No data to insert", null);
- 163
- 164 boolean cancel = false;
- 165 synchronized(this) {
- 166 if(restart) {
- 167 clearCountersOnRestart();
- 168 if(currentState != null && !finished) {
- 169 if(logMINOR) Logger.minor(this, "Can't restart, not finished and currentState != null : "+currentState);
- 170 return false;
- 171 }
- 172 if(finished)
- 173 startedStarting = false;
- 174 finished = false;
- 175 }
- 176 if(startedStarting) {
- 177 if(logMINOR) Logger.minor(this, "Can't "+(restart?"restart":"start")+" : startedStarting = true");
- 178 return false;
- 179 }
- 180 startedStarting = true;
- 181 if(currentState != null) {
- 182 if(logMINOR) Logger.minor(this, "Can't "+(restart?"restart":"start")+" : currentState != null : "+currentState);
- 183 return false;
- 184 }
- 185 cancel = this.cancelled;
- 186 cryptoKey = null;
- 187 if(overrideSplitfileCrypto != null) {
- 188 cryptoKey = overrideSplitfileCrypto;
- 189 if (cryptoKey.length != 32)
- 190 throw new InsertException(InsertExceptionMode.INVALID_URI, "overrideSplitfileCryptoKey must be of length 32", null);
- 191 } else if(randomiseSplitfileKeys) {
- 192 cryptoKey = new byte[32];
- 193 context.random.nextBytes(cryptoKey);
- 194 }
- 195 if(!cancel) {
- 196 if(!binaryBlob) {
- 197 ClientMetadata meta = cm;
- 198 if(meta != null) meta = persistent() ? meta.clone() : meta;
- 199 currentState =
- 200 new SingleFileInserter(this, this, new InsertBlock(data, meta, targetURI), isMetadata, ctx, realTimeFlag,
- 201 false, false, null, null, false, targetFilename, false, persistent(), 0, 0, null, cryptoAlgorithm, cryptoKey, metadataThreshold);
- 202 } else
- 203 currentState =
- 204 new BinaryBlobInserter(data, this, getClient(), false, priorityClass, ctx, context);
- 205 }
- 206 }
- 207 if(cancel) {
- 208 onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
- 209 return false;
- 210 }
- 211 synchronized(this) {
- 212 cancel = cancelled;
- 213 }
- 214 if(cancel) {
- 215 onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
- 216 return false;
- 217 }
- 218 if(logMINOR)
- 219 Logger.minor(this, "Starting insert: "+currentState);
- 220 if(currentState instanceof SingleFileInserter)
- 221 ((SingleFileInserter)currentState).start(context);
- 222 else
- 223 currentState.schedule(context);
- 224 synchronized(this) {
- 225 cancel = cancelled;
- 226 }
- 227 if(cancel) {
- 228 onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
- 229 return false;
- 230 }
- 231 } catch (InsertException e) {
- 232 Logger.error(this, "Failed to start insert: "+e, e);
- 233 synchronized(this) {
- 234 finished = true;
- 235 currentState = null;
- 236 }
- 237 // notify the client that the insert could not even be started
- 238 if (this.client!=null) {
- 239 this.client.onFailure(e, this);
- 240 }
- 241 } catch (IOException e) {
- 242 Logger.error(this, "Failed to start insert: "+e, e);
- 243 synchronized(this) {
- 244 finished = true;
- 245 currentState = null;
- 246 }
- 247 // notify the client that the insert could not even be started
- 248 if (this.client!=null) {
- 249 this.client.onFailure(new InsertException(InsertExceptionMode.BUCKET_ERROR, e, null), this);
- 250 }
- 251 } catch (BinaryBlobFormatException e) {
- 252 Logger.error(this, "Failed to start insert: "+e, e);
- 253 synchronized(this) {
- 254 finished = true;
- 255 currentState = null;
- 256 }
- 257 // notify the client that the insert could not even be started
- 258 if (this.client!=null) {
- 259 this.client.onFailure(new InsertException(InsertExceptionMode.BINARY_BLOB_FORMAT_ERROR, e, null), this);
- 260 }
- 261 }
- 262 if(logMINOR)
- 263 Logger.minor(this, "Started "+this);
- 264 return true;
- 265 }
Advertisement
Add Comment
Please, Sign In to add comment