@@ -484,7 +484,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
484484 boolean metadataExists = false ;
485485 String token = null ;
486486 Object object = null ;
487-
487+ FileChannel channel = null ;
488488 ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProviderImpl (getContext ());
489489 String publicKey = arbitraryDataProvider .getValue (user .getAccountName (), EncryptionUtils .PUBLIC_KEY );
490490
@@ -499,12 +499,6 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
499499
500500 try {
501501 token = getFolderUnlockTokenOrLockFolder (client , parentFile , counter );
502-
503- if (token == null || token .isEmpty ()) {
504- Log_OC .e (TAG , "Failed to obtain folder lock token for encrypted upload" );
505- return new RemoteOperationResult <>(new IllegalStateException ("Cannot proceed: folder lock token is null or empty" ));
506- }
507- Log_OC .d (TAG , "folder successfully locked" );
508502 } catch (Exception e ) {
509503 Log_OC .e (TAG , "Failed to lock folder" , e );
510504 return new RemoteOperationResult <>(e );
@@ -561,7 +555,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
561555 Triple <FileLock , RemoteOperationResult , FileChannel > channelResult = initFileChannel (result , fileLock , e2eFiles );
562556 fileLock = channelResult .getFirst ();
563557 result = channelResult .getSecond ();
564- FileChannel channel = channelResult .getThird ();
558+ channel = channelResult .getThird ();
565559
566560 size = getChannelSize (channel );
567561 updateSize (size );
@@ -582,7 +576,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
582576 Log_OC .e (TAG , "UploadFileOperation exception: " + e .getLocalizedMessage ());
583577 result = new RemoteOperationResult <>(e );
584578 } finally {
585- result = cleanupE2EUpload (fileLock , e2eFiles , result , object , client , token );
579+ result = cleanupE2EUpload (fileLock , channel , e2eFiles , result , object , client , token );
586580 }
587581
588582 completeE2EUpload (result , e2eFiles , client );
@@ -714,7 +708,8 @@ private void setUploadOperationForE2E(String token,
714708 private Triple <FileLock , RemoteOperationResult , FileChannel > initFileChannel (RemoteOperationResult result , FileLock fileLock , E2EFiles e2eFiles ) throws IOException {
715709 FileChannel channel = null ;
716710
717- try (RandomAccessFile randomAccessFile = new RandomAccessFile (mFile .getStoragePath (), "rw" )) {
711+ try {
712+ RandomAccessFile randomAccessFile = new RandomAccessFile (mFile .getStoragePath (), "rw" );
718713 channel = randomAccessFile .getChannel ();
719714 fileLock = channel .tryLock ();
720715 } catch (IOException ioException ) {
@@ -743,7 +738,7 @@ private Triple<FileLock, RemoteOperationResult, FileChannel> initFileChannel(Rem
743738 Log_OC .d (TAG , "Error caught at getChannelFromFile: " + e );
744739 }
745740 } else {
746- result = new RemoteOperationResult (ResultCode .LOCK_FAILED );
741+ result = new RemoteOperationResult <> (ResultCode .LOCK_FAILED );
747742 }
748743 }
749744 }
@@ -897,17 +892,28 @@ private void completeE2EUpload(RemoteOperationResult result, E2EFiles e2eFiles,
897892 e2eFiles .deleteTemporalFile ();
898893 }
899894
900- private RemoteOperationResult cleanupE2EUpload (FileLock fileLock , E2EFiles e2eFiles , RemoteOperationResult result , Object object , OwnCloudClient client , String token ) {
895+ private RemoteOperationResult cleanupE2EUpload (FileLock fileLock , FileChannel channel , E2EFiles e2eFiles , RemoteOperationResult result , Object object , OwnCloudClient client , String token ) {
901896 mUploadStarted .set (false );
902897
903898 if (fileLock != null ) {
904899 try {
905- fileLock .release ();
900+ // Only release if the channel is still open/valid
901+ if (channel != null && channel .isOpen ()) {
902+ fileLock .release ();
903+ }
906904 } catch (IOException e ) {
907905 Log_OC .e (TAG , "Failed to unlock file with path " + mFile .getStoragePath ());
908906 }
909907 }
910908
909+ if (channel != null ) {
910+ try {
911+ channel .close ();
912+ } catch (IOException e ) {
913+ Log_OC .e (TAG , "Failed to close file channel" , e );
914+ }
915+ }
916+
911917 e2eFiles .deleteTemporalFileWithOriginalFileComparison ();
912918
913919 if (result == null ) {
@@ -960,6 +966,8 @@ private RemoteOperationResult cleanupE2EUpload(FileLock fileLock, E2EFiles e2eFi
960966 // Clear the saved token since folder is now unlocked
961967 mUpload .setFolderUnlockToken (null );
962968 uploadsStorageManager .updateUpload (mUpload );
969+
970+ Log_OC .d (TAG , "Folder unlock token removed" );
963971 }
964972
965973 e2eFiles .deleteEncryptedTempFile ();
0 commit comments