8383import java .nio .channels .OverlappingFileLockException ;
8484import java .util .HashMap ;
8585import java .util .HashSet ;
86- import java .util .Iterator ;
8786import java .util .Set ;
8887import java .util .UUID ;
8988import java .util .concurrent .atomic .AtomicBoolean ;
@@ -502,7 +501,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
502501 expectedFile = new File (expectedPath );
503502
504503 result = copyFile (originalFile , expectedPath );
505- if (result != null ) {
504+ if (! result . isSuccess () ) {
506505 return result ;
507506 }
508507
@@ -551,7 +550,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
551550 Files .deleteIfExists (Paths .get (temporalPath ));
552551 result = copy (originalFile , temporalFile );
553552
554- if (result == null ) {
553+ if (result . isSuccess () ) {
555554 if (temporalFile .length () == originalFile .length ()) {
556555 channel = new RandomAccessFile (temporalFile .getAbsolutePath (), "rw" ).getChannel ();
557556 fileLock = channel .tryLock ();
@@ -586,9 +585,8 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
586585 mFile .getEtagInConflict (), timeStamp );
587586 }
588587
589- Iterator <OnDatatransferProgressListener > listener = mDataTransferListeners .iterator ();
590- while (listener .hasNext ()) {
591- mUploadOperation .addDatatransferProgressListener (listener .next ());
588+ for (OnDatatransferProgressListener mDataTransferListener : mDataTransferListeners ) {
589+ mUploadOperation .addDatatransferProgressListener (mDataTransferListener );
592590 }
593591
594592 if (mCancellationRequested .get ()) {
@@ -755,7 +753,7 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
755753 File originalFile = new File (mOriginalStoragePath );
756754 File expectedFile = null ;
757755 FileLock fileLock = null ;
758- long size = 0 ;
756+ long size ;
759757
760758 try {
761759 // check conditions
@@ -772,7 +770,7 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
772770 expectedFile = new File (expectedPath );
773771
774772 result = copyFile (originalFile , expectedPath );
775- if (result != null ) {
773+ if (! result . isSuccess () ) {
776774 return result ;
777775 }
778776
@@ -795,7 +793,7 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
795793 Files .deleteIfExists (Paths .get (temporalPath ));
796794 result = copy (originalFile , temporalFile );
797795
798- if (result == null ) {
796+ if (result . isSuccess () ) {
799797 if (temporalFile .length () == originalFile .length ()) {
800798 channel = new RandomAccessFile (temporalFile .getAbsolutePath (), "rw" ).getChannel ();
801799 fileLock = channel .tryLock ();
@@ -807,7 +805,7 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
807805
808806 try {
809807 size = channel .size ();
810- } catch (IOException e1 ) {
808+ } catch (Exception e1 ) {
811809 size = new File (mFile .getStoragePath ()).length ();
812810 }
813811
@@ -828,16 +826,15 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
828826 mFile .getRemotePath (), mFile .getMimeType (), mFile .getEtagInConflict (), timeStamp );
829827 }
830828
831- Iterator <OnDatatransferProgressListener > listener = mDataTransferListeners .iterator ();
832- while (listener .hasNext ()) {
833- mUploadOperation .addDatatransferProgressListener (listener .next ());
829+ for (OnDatatransferProgressListener mDataTransferListener : mDataTransferListeners ) {
830+ mUploadOperation .addDatatransferProgressListener (mDataTransferListener );
834831 }
835832
836833 if (mCancellationRequested .get ()) {
837834 throw new OperationCancelledException ();
838835 }
839836
840- if (result == null || result .isSuccess () && mUploadOperation != null ) {
837+ if (result .isSuccess () && mUploadOperation != null ) {
841838 result = mUploadOperation .execute (client , mFile .isEncrypted ());
842839
843840 /// move local temporal file or original file to its corresponding
@@ -908,22 +905,20 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
908905
909906 private RemoteOperationResult copyFile (File originalFile , String expectedPath ) throws OperationCancelledException ,
910907 IOException {
911- RemoteOperationResult result = null ;
912-
913908 if (mLocalBehaviour == FileUploader .LOCAL_BEHAVIOUR_COPY && !mOriginalStoragePath .equals (expectedPath )) {
914909 String temporalPath = FileStorageUtils .getInternalTemporalPath (mAccount .name , mContext ) +
915910 mFile .getRemotePath ();
916911 mFile .setStoragePath (temporalPath );
917912 File temporalFile = new File (temporalPath );
918913
919- result = copy (originalFile , temporalFile );
914+ return copy (originalFile , temporalFile );
920915 }
921916
922917 if (mCancellationRequested .get ()) {
923918 throw new OperationCancelledException ();
924919 }
925920
926- return result ;
921+ return new RemoteOperationResult ( ResultCode . OK ) ;
927922 }
928923
929924 private void checkNameCollision (OwnCloudClient client , DecryptedFolderMetadata metadata , boolean encrypted )
@@ -1017,7 +1012,7 @@ private RemoteOperationResult grantFolderExistence(String pathToGrant, OwnCloudC
10171012 if (parentDir != null ) {
10181013 result = new RemoteOperationResult (ResultCode .OK );
10191014 } else {
1020- result = new RemoteOperationResult (ResultCode .UNKNOWN_ERROR );
1015+ result = new RemoteOperationResult (ResultCode .CANNOT_CREATE_FILE );
10211016 }
10221017 }
10231018 return result ;
@@ -1168,25 +1163,19 @@ public boolean isUploadInProgress() {
11681163 private RemoteOperationResult copy (File sourceFile , File targetFile ) throws IOException {
11691164 Log_OC .d (TAG , "Copying local file" );
11701165
1171- RemoteOperationResult result = null ;
1172-
11731166 if (FileStorageUtils .getUsableSpace () < sourceFile .length ()) {
1174- result = new RemoteOperationResult (ResultCode .LOCAL_STORAGE_FULL );
1175- return result ; // error condition when the file should be copied
1176-
1167+ return new RemoteOperationResult (ResultCode .LOCAL_STORAGE_FULL ); // error when the file should be copied
11771168 } else {
11781169 Log_OC .d (TAG , "Creating temporal folder" );
11791170 File temporalParent = targetFile .getParentFile ();
1180- temporalParent .mkdirs ();
1181- if (!temporalParent .isDirectory ()) {
1182- throw new IOException (
1183- "Unexpected error: parent directory could not be created" );
1171+
1172+ if (!temporalParent .mkdirs () && !temporalParent .isDirectory ()) {
1173+ return new RemoteOperationResult (ResultCode .CANNOT_CREATE_FILE );
11841174 }
1175+
11851176 Log_OC .d (TAG , "Creating temporal file" );
1186- targetFile .createNewFile ();
1187- if (!targetFile .isFile ()) {
1188- throw new IOException (
1189- "Unexpected error: target file could not be created" );
1177+ if (!targetFile .createNewFile () && !targetFile .isFile ()) {
1178+ return new RemoteOperationResult (ResultCode .CANNOT_CREATE_FILE );
11901179 }
11911180
11921181 Log_OC .d (TAG , "Copying file contents" );
@@ -1214,14 +1203,10 @@ private RemoteOperationResult copy(File sourceFile, File targetFile) throws IOEx
12141203 } // else: weird but possible situation, nothing to copy
12151204
12161205 if (mCancellationRequested .get ()) {
1217- result = new RemoteOperationResult (new OperationCancelledException ());
1218- return result ;
1206+ return new RemoteOperationResult (new OperationCancelledException ());
12191207 }
1220-
12211208 } catch (Exception e ) {
1222- result = new RemoteOperationResult (ResultCode .LOCAL_STORAGE_NOT_COPIED );
1223- return result ;
1224-
1209+ return new RemoteOperationResult (ResultCode .LOCAL_STORAGE_NOT_COPIED );
12251210 } finally {
12261211 try {
12271212 if (in != null ) {
@@ -1241,19 +1226,17 @@ private RemoteOperationResult copy(File sourceFile, File targetFile) throws IOEx
12411226 }
12421227 }
12431228 }
1244- return result ;
1229+ return new RemoteOperationResult ( ResultCode . OK ) ;
12451230 }
12461231
12471232
12481233 /**
12491234 * TODO rewrite with homogeneous fail handling, remove dependency on {@link RemoteOperationResult},
12501235 * TODO use Exceptions instead
1251- * <p>
12521236 * TODO refactor both this and 'copy' in a single method
12531237 *
12541238 * @param sourceFile Source file to move.
12551239 * @param targetFile Target location to move the file.
1256- * @return {@link RemoteOperationResult} result from remote operation
12571240 * @throws IOException exception if file cannot be read/wrote
12581241 */
12591242 private void move (File sourceFile , File targetFile ) throws IOException {
0 commit comments