Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use truncate() of CleartextChannel when opening new Channel with TRUNCATE_EXISTING #270

Merged
merged 6 commits into from
Jan 16, 2025

Conversation

infeo
Copy link
Member

@infeo infeo commented Jan 10, 2025

This PR fixes #269.

Instead of writing an "optimized", but wrong truncate block, we can simply use CleartextChannel.truncate(0) when opening a new file. Benefit of this method: It is already tested! And since it is synchronized and also creating a new file channel is synchronized, we can be sure that side calls cannot interfere.

@infeo infeo requested a review from overheadhunter January 10, 2025 18:06
@infeo infeo self-assigned this Jan 10, 2025
Copy link

coderabbitai bot commented Jan 10, 2025

Walkthrough

The pull request introduces changes to the file channel management in the Cryptomator CryptoFS library. The primary modification occurs in the OpenCryptoFile class, where the truncation logic for file channels has been altered. Instead of truncating the ciphertext file channel, the implementation now focuses on truncating the cleartext file channel when the truncateExisting option is set. This change affects how files are handled during the opening process, specifically in scenarios involving file truncation. The test classes have been updated to reflect and validate these changes, including modifications to the CryptoFileChannelWriteReadIntegrationTest and OpenCryptoFileTest to ensure the new behavior is correctly implemented and tested.

Assessment against linked issues

Objective Addressed Explanation
Prevent invalid ciphertext during concurrent truncation [#269]
Improve file channel truncation handling

Possibly related PRs

Suggested Labels

enhancement

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/test/java/org/cryptomator/cryptofs/CryptoFileChannelWriteReadIntegrationTest.java (1)

Line range hint 619-635: LGTM! Consider documenting the sleep duration.

The test changes improve reliability and execution time:

  • Reduced repetitions from 50 to 15 maintains coverage while improving execution time
  • Added sleep ensures proper testing of concurrent behavior

Consider adding a comment explaining why 1000ms sleep is needed:

+  // Sleep for 1s to ensure writing operation has time to proceed before truncation
   Thread.sleep(1000);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2dedb3b and 1aab041.

📒 Files selected for processing (3)
  • src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (1 hunks)
  • src/test/java/org/cryptomator/cryptofs/CryptoFileChannelWriteReadIntegrationTest.java (2 hunks)
  • src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (1 hunks)
🔇 Additional comments (2)
src/main/java/org/cryptomator/cryptofs/fh/OpenCryptoFile.java (1)

77-79: LGTM! Clean implementation of file truncation.

The implementation correctly uses cleartextFileChannel.truncate(0) when truncateExisting is set. The change is:

  • Thread-safe due to the synchronized context
  • Protected by proper null checks and cleanup in the finally block
  • Aligned with the PR objective to use CleartextChannel's truncate method
src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (1)

96-109: LGTM! Test accurately verifies the new truncation behavior.

The test has been properly updated to:

  • Verify that truncate(0) is called on the cleartextChannel
  • Use clear and descriptive test name
  • Set up proper mocking of dependencies

Copy link
Member

@overheadhunter overheadhunter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, did you spot it in the failed CI run?

@infeo
Copy link
Member Author

infeo commented Jan 10, 2025

did you spot it in the failed CI run?

yes. I worked on a completely different feature (FileNameDecryptor) and was tempted to declare the failing build as a fluke. Luckily i decided to investigate the issue^^ We have tests for a reason!

@infeo infeo requested a review from overheadhunter January 13, 2025 09:32
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (1)

97-103: Consider extracting mock setup to a helper method.

The mock setup for channel components is repeated in multiple test methods. Consider extracting it to a helper method to improve test readability and maintainability.

+ private void setupChannelMocks(CleartextFileChannel cleartextChannel) {
+     Mockito.when(openCryptoFileComponent.newChannelComponent()).thenReturn(channelComponentFactory);
+     Mockito.when(channelComponentFactory.create(any(), any(), any())).thenReturn(channelComponent);
+     Mockito.when(channelComponent.channel()).thenReturn(cleartextChannel);
+ }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f753b72 and 5363d4b.

📒 Files selected for processing (2)
  • src/test/java/org/cryptomator/cryptofs/CryptoFileChannelWriteReadIntegrationTest.java (3 hunks)
  • src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/test/java/org/cryptomator/cryptofs/CryptoFileChannelWriteReadIntegrationTest.java
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Analyse
  • GitHub Check: Build and Test
🔇 Additional comments (2)
src/test/java/org/cryptomator/cryptofs/fh/OpenCryptoFileTest.java (2)

73-73: LGTM! Constructor changes are consistent.

The removal of the chunkCache parameter from OpenCryptoFile constructor calls is applied consistently across all test methods.

Also applies to: 84-84, 104-104, 116-116, 200-200


94-107: LGTM! Test correctly verifies truncate behavior.

The test has been properly updated to verify that truncate(0) is called on the cleartextChannel when opening with TRUNCATE_EXISTING option. The mock setup and verification are comprehensive.

@infeo infeo added this to the next milestone Jan 16, 2025
@infeo infeo merged commit 212d44b into develop Jan 16, 2025
8 checks passed
@infeo infeo deleted the feature/269-truncate-while-write branch January 16, 2025 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Truncating a channel which is currently written can create invalid ciphertext
2 participants