s243a

ClientPutter.stat (restart,context)

Jun 6th, 2015
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 KB | None | 0 0
  1. // from line 239 of https://github.com/freenet/fred/blob/next/src/freenet/client/async/ClientPutter.java
  2.     /** Start the insert.
  3. 140      * @param restart If true, restart the insert even though it has completed before.
  4. 141      * @param context Contains some useful transient fields such as the schedulers.
  5. 142      * @throws InsertException If the insert cannot be started for some reason.
  6. 143      */
  7. 144     public boolean start(boolean restart, ClientContext context) throws InsertException {
  8. 145         if(logMINOR)
  9. 146             Logger.minor(this, "Starting "+this+" for "+targetURI);
  10. 147         byte cryptoAlgorithm;
  11. 148         CompatibilityMode mode = ctx.getCompatibilityMode();
  12. 149         if(!(mode == CompatibilityMode.COMPAT_CURRENT || mode.ordinal() >= CompatibilityMode.COMPAT_1416.ordinal()))
  13. 150             cryptoAlgorithm = Key.ALGO_AES_PCFB_256_SHA256;
  14. 151         else
  15. 152             cryptoAlgorithm = Key.ALGO_AES_CTR_256_SHA256;
  16. 153         try {
  17. 154             this.targetURI.checkInsertURI();
  18. 155             // If the top level key is an SSK, all CHK blocks and particularly splitfiles below it should have
  19. 156             // randomised keys. This substantially improves security by making it impossible to identify blocks
  20. 157             // even if you know the content. In the user interface, we will offer the option of inserting as a
  21. 158             // random SSK to take advantage of this.
  22. 159             boolean randomiseSplitfileKeys = randomiseSplitfileKeys(targetURI, ctx, persistent());
  23. 160
  24.  
  25. 161             if(data == null)
  26. 162                 throw new InsertException(InsertExceptionMode.BUCKET_ERROR, "No data to insert", null);
  27. 163
  28.  
  29. 164             boolean cancel = false;
  30. 165             synchronized(this) {
  31. 166                 if(restart) {
  32. 167                     clearCountersOnRestart();
  33. 168                     if(currentState != null && !finished) {
  34. 169                         if(logMINOR) Logger.minor(this, "Can't restart, not finished and currentState != null : "+currentState);
  35. 170                         return false;
  36. 171                     }
  37. 172                     if(finished)
  38. 173                         startedStarting = false;
  39. 174                     finished = false;
  40. 175                 }
  41. 176                 if(startedStarting) {
  42. 177                     if(logMINOR) Logger.minor(this, "Can't "+(restart?"restart":"start")+" : startedStarting = true");
  43. 178                     return false;
  44. 179                 }
  45. 180                 startedStarting = true;
  46. 181                 if(currentState != null) {
  47. 182                     if(logMINOR) Logger.minor(this, "Can't "+(restart?"restart":"start")+" : currentState != null : "+currentState);
  48. 183                     return false;
  49. 184                 }
  50. 185                 cancel = this.cancelled;
  51. 186                 cryptoKey = null;
  52. 187                 if(overrideSplitfileCrypto != null) {
  53. 188                     cryptoKey = overrideSplitfileCrypto;
  54. 189                     if (cryptoKey.length != 32)
  55. 190                         throw new InsertException(InsertExceptionMode.INVALID_URI, "overrideSplitfileCryptoKey must be of length 32", null);
  56. 191                 } else if(randomiseSplitfileKeys) {
  57. 192                     cryptoKey = new byte[32];
  58. 193                     context.random.nextBytes(cryptoKey);
  59. 194                 }
  60. 195                 if(!cancel) {
  61. 196                     if(!binaryBlob) {
  62. 197                         ClientMetadata meta = cm;
  63. 198                         if(meta != null) meta = persistent() ? meta.clone() : meta;
  64. 199                         currentState =
  65. 200                             new SingleFileInserter(this, this, new InsertBlock(data, meta, targetURI), isMetadata, ctx, realTimeFlag,  
  66. 201                                     false, false, null, null, false, targetFilename, false, persistent(), 0, 0, null, cryptoAlgorithm, cryptoKey, metadataThreshold);
  67. 202                     } else
  68. 203                         currentState =
  69. 204                             new BinaryBlobInserter(data, this, getClient(), false, priorityClass, ctx, context);
  70. 205                 }
  71. 206             }
  72. 207             if(cancel) {
  73. 208                 onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
  74. 209                 return false;
  75. 210             }
  76. 211             synchronized(this) {
  77. 212                 cancel = cancelled;
  78. 213             }
  79. 214             if(cancel) {
  80. 215                 onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
  81. 216                 return false;
  82. 217             }
  83. 218             if(logMINOR)
  84. 219                 Logger.minor(this, "Starting insert: "+currentState);
  85. 220             if(currentState instanceof SingleFileInserter)
  86. 221                 ((SingleFileInserter)currentState).start(context);
  87. 222             else
  88. 223                 currentState.schedule(context);
  89. 224             synchronized(this) {
  90. 225                 cancel = cancelled;
  91. 226             }
  92. 227             if(cancel) {
  93. 228                 onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
  94. 229                 return false;
  95. 230             }
  96. 231         } catch (InsertException e) {
  97. 232             Logger.error(this, "Failed to start insert: "+e, e);
  98. 233             synchronized(this) {
  99. 234                 finished = true;
  100. 235                 currentState = null;
  101. 236             }
  102. 237             // notify the client that the insert could not even be started
  103. 238             if (this.client!=null) {
  104. 239                 this.client.onFailure(e, this);
  105. 240             }
  106. 241         } catch (IOException e) {
  107. 242             Logger.error(this, "Failed to start insert: "+e, e);
  108. 243             synchronized(this) {
  109. 244                 finished = true;
  110. 245                 currentState = null;
  111. 246             }
  112. 247             // notify the client that the insert could not even be started
  113. 248             if (this.client!=null) {
  114. 249                 this.client.onFailure(new InsertException(InsertExceptionMode.BUCKET_ERROR, e, null), this);
  115. 250             }
  116. 251         } catch (BinaryBlobFormatException e) {
  117. 252             Logger.error(this, "Failed to start insert: "+e, e);
  118. 253             synchronized(this) {
  119. 254                 finished = true;
  120. 255                 currentState = null;
  121. 256             }
  122. 257             // notify the client that the insert could not even be started
  123. 258             if (this.client!=null) {
  124. 259                 this.client.onFailure(new InsertException(InsertExceptionMode.BINARY_BLOB_FORMAT_ERROR, e, null), this);
  125. 260             }
  126. 261         }
  127. 262         if(logMINOR)
  128. 263             Logger.minor(this, "Started "+this);
  129. 264         return true;
  130. 265     }
Advertisement
Add Comment
Please, Sign In to add comment