@@ -524,30 +524,32 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
524524 return result ;
525525 }
526526
527- long counter = getE2ECounter (parentFile );
528-
529- try {
530- token = getFolderUnlockTokenOrLockFolder (client , parentFile , counter );
531- } catch (Exception e ) {
532- Log_OC .e (TAG , "Failed to lock folder" , e );
533- return new RemoteOperationResult <>(e );
534- }
535-
536- // Update metadata
537527 EncryptionUtilsV2 encryptionUtilsV2 = new EncryptionUtilsV2 ();
538528 object = EncryptionUtils .downloadFolderMetadata (parentFile , client , mContext , user );
539529 if (object instanceof DecryptedFolderMetadataFileV1 decrypted && decrypted .getMetadata () != null ) {
540530 metadataExists = true ;
541531 }
542532
543- if (isEndToEndVersionAtLeastV2 ()) {
533+ boolean isEndToEndVersionAtLeastV2 = isEndToEndVersionAtLeastV2 ();
534+
535+ if (isEndToEndVersionAtLeastV2 ) {
544536 if (object == null ) {
545537 return new RemoteOperationResult <>(new IllegalStateException ("Metadata does not exist" ));
546538 }
547539 } else {
548540 object = getDecryptedFolderMetadataV1 (publicKey , object );
549541 }
550542
543+ long counter = getE2ECounter (parentFile , object , isEndToEndVersionAtLeastV2 );
544+ Log_OC .d (TAG , "counter: " + counter );
545+
546+ try {
547+ token = getFolderUnlockTokenOrLockFolder (client , parentFile , counter );
548+ } catch (Exception e ) {
549+ Log_OC .e (TAG , "Failed to lock folder" , e );
550+ return new RemoteOperationResult <>(e );
551+ }
552+
551553 E2EClientData clientData = new E2EClientData (client , token , publicKey );
552554
553555 List <String > fileNames = getCollidedFileNames (object );
@@ -621,14 +623,16 @@ private boolean isEndToEndVersionAtLeastV2() {
621623 return E2EVersionHelper .INSTANCE .isV2Plus (capability );
622624 }
623625
624- private long getE2ECounter (OCFile parentFile ) {
625- long counter = -1 ;
626+ private long getE2ECounter (OCFile parentFile , Object metadata , boolean isEndToEndVersionAtLeastV2 ) {
627+ if (!isEndToEndVersionAtLeastV2 ) {
628+ return -1 ;
629+ }
626630
627- if (isEndToEndVersionAtLeastV2 () ) {
628- counter = parentFile . getE2eCounter () + 1 ;
631+ if (metadata instanceof DecryptedFolderMetadataFile decrypted ) {
632+ return decrypted . getMetadata (). getCounter () + 1 ;
629633 }
630634
631- return counter ;
635+ return parentFile . getE2eCounter () + 1 ;
632636 }
633637
634638 private String getFolderUnlockTokenOrLockFolder (OwnCloudClient client , OCFile parentFile , long counter ) throws UploadException {
@@ -900,6 +904,9 @@ private void updateMetadataForV2(DecryptedFolderMetadataFile metadata, Encryptio
900904 metadata ,
901905 getStorageManager ());
902906
907+ parentFile .setE2eCounter (metadata .getMetadata ().getCounter ());
908+ getStorageManager ().saveFile (parentFile );
909+
903910 // upload metadata
904911 encryptionUtilsV2 .serializeAndUploadMetadata (parentFile ,
905912 metadata ,
0 commit comments