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

Feature: Refactor filesystem checker #268

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

infeo
Copy link
Member

@infeo infeo commented Jan 7, 2025

This PR does two, related things:

  1. Removes the implicit read-only flag, if the storage location is read only.
  2. Refactors the FileSystemCapabilityChecker class to be static only

Motivation

Imagine, someone needs a tshirt and decides to order a green one. Unfortunately, she gets a red one, because green is out of stock. Not cool, she wanted a green one. Instead, the customer should have been informed about the the low stock and asked what to do.
One can argue, but the color does not matter, since you can always wear the shirt. But we don't know the purpose of the shirt. If it is used in a green screen setting, red is definitively a bad color.

This PR applies the principle behind the short story when creating the cryptographic filesystem. The implicit change from read/write to read-only, if the storage location is read-only, is removed.

Additionaly, the FileSystemChecker is converted to a static only class.

@infeo infeo self-assigned this Jan 7, 2025
@infeo infeo added this to the nex milestone Jan 7, 2025
Copy link

coderabbitai bot commented Jan 7, 2025

Walkthrough

The changes involve a significant refactoring of the FileSystemCapabilityChecker class and its related components across multiple files in the Cryptomator project. The primary modifications include converting the FileSystemCapabilityChecker from an instance-based class to a static utility class by:

  1. Removing the @Singleton annotation
  2. Changing the constructor to private
  3. Converting all instance methods to static methods
  4. Removing dependencies on the FileSystemCapabilityChecker from other classes like CryptoFileSystems, Migrators, and various migration-related classes

These changes simplify the class structure by eliminating the need to instantiate FileSystemCapabilityChecker and instead allowing direct static method calls. The modifications affect how file system capabilities are checked and managed throughout the project, shifting from an object-oriented approach to a more procedural, utility-based method of handling file system capabilities.


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: 1

🔭 Outside diff range comments (2)
src/main/java/org/cryptomator/cryptofs/common/FileSystemCapabilityChecker.java (2)

Line range hint 62-69: Avoid using assert for critical checks; use explicit exception handling instead.

Using assert ds != null; for checking critical conditions is discouraged because assertions may be disabled at runtime. Consider using Objects.requireNonNull(ds); or throwing an appropriate exception to ensure the condition is always enforced.

Apply this diff to replace the assert statement:

- assert ds != null;
+ Objects.requireNonNull(ds, "DirectoryStream should not be null");

Line range hint 141-154: Replace assert statements with proper exception handling.

The use of assert in determineSupportedCiphertextFileNameLength might not prevent invalid parameters when assertions are disabled. Replace assert with explicit checks and throw exceptions to ensure robustness.

Apply this diff to replace assert statements:

- assert lowerBoundIncl < upperBoundExcl;
+ Preconditions.checkArgument(lowerBoundIncl < upperBoundExcl, "lowerBoundIncl must be less than upperBoundExcl");

- assert mid < upperBoundExcl;
+ Preconditions.checkState(mid < upperBoundExcl, "mid must be less than upperBoundExcl");

- assert lowerBoundIncl < mid;
+ Preconditions.checkState(lowerBoundIncl < mid, "lowerBoundIncl must be less than mid");
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c905804 and f279fcc.

📒 Files selected for processing (8)
  • src/main/java/org/cryptomator/cryptofs/CryptoFileSystems.java (2 hunks)
  • src/main/java/org/cryptomator/cryptofs/common/FileSystemCapabilityChecker.java (11 hunks)
  • src/main/java/org/cryptomator/cryptofs/migration/MigrationModule.java (0 hunks)
  • src/main/java/org/cryptomator/cryptofs/migration/Migrators.java (3 hunks)
  • src/main/java/org/cryptomator/cryptofs/migration/v7/Version7Migrator.java (1 hunks)
  • src/test/java/org/cryptomator/cryptofs/CryptoFileSystemsTest.java (1 hunks)
  • src/test/java/org/cryptomator/cryptofs/common/FileSystemCapabilityCheckerTest.java (6 hunks)
  • src/test/java/org/cryptomator/cryptofs/migration/MigratorsTest.java (11 hunks)
💤 Files with no reviewable changes (1)
  • src/main/java/org/cryptomator/cryptofs/migration/MigrationModule.java
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyse
🔇 Additional comments (13)
src/test/java/org/cryptomator/cryptofs/common/FileSystemCapabilityCheckerTest.java (4)

Line range hint 27-43: Well-structured test setup!

The mock configuration is comprehensive and follows good testing practices, providing a solid foundation for testing filesystem capabilities.


87-87: Good improvement using Integer.parseInt!

The change from Integer.valueOf() to Integer.parseInt() is more appropriate here since we need a primitive int. This is a small but good optimization since parseInt() directly returns a primitive while valueOf() would create an Integer object.

Also applies to: 114-114


135-140: Excellent use of static mocking and parameterized testing!

The test demonstrates several good practices:

  1. Proper use of try-with-resources for static mock cleanup
  2. Clear mock configuration with chained when/thenReturn calls
  3. Effective use of parameterized testing for multiple scenarios

58-65: Successfully refactored to use static method call!

The test case has been properly updated to use the static determineSupportedCiphertextFileNameLength method while maintaining the original test logic.

Let's verify that all instance-based calls have been converted to static:

✅ Verification successful

Refactoring to static utility class verified

The codebase scan confirms:

  • All method calls are properly using the static form
  • No instances of the class are being created
  • The class is properly designed as a utility class with a private constructor
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining instance-based calls to FileSystemCapabilityChecker
rg "new FileSystemCapabilityChecker\(\)" --type java

Length of output: 54


Script:

#!/bin/bash
# Search for any references to FileSystemCapabilityChecker class
rg "FileSystemCapabilityChecker" --type java -A 2 -B 2

# Also check if there are any remaining instance method calls by looking for "this" keyword in the class
ast-grep --pattern 'class FileSystemCapabilityChecker {
  $$$
  $this.$_($$$)
  $$$
}'

Length of output: 13046

src/main/java/org/cryptomator/cryptofs/common/FileSystemCapabilityChecker.java (2)

38-39: Conversion to a utility class with a private constructor is appropriate.

Making FileSystemCapabilityChecker a final class with a private constructor ensures that it cannot be instantiated, which is suitable for a utility class containing only static methods.


113-114: ⚠️ Potential issue

Use explicit validation instead of assert in public methods.

The assertion assert fillerName.length() > 0; may be bypassed if assertions are disabled. Use Preconditions.checkState() or throw an exception to enforce this condition.

Apply this diff to enforce the condition:

- assert fillerName.length() > 0;
+ Preconditions.checkState(fillerName.length() > 0, "Filler name must not be empty");

Likely invalid or redundant comment.

src/main/java/org/cryptomator/cryptofs/CryptoFileSystems.java (2)

40-42: Constructor refactoring aligns with utility class changes.

Removing FileSystemCapabilityChecker from the constructor simplifies the class and aligns with the conversion of FileSystemCapabilityChecker to a static utility class.


59-61: Ensure filesystem capability checks are performed appropriately.

With the removal of adjustForCapabilities, verify that any necessary filesystem capability checks are handled elsewhere, and that operations fail gracefully if the filesystem lacks required capabilities.

src/main/java/org/cryptomator/cryptofs/migration/Migrators.java (2)

51-53: Simplifying constructor by removing unnecessary dependency.

Eliminating FileSystemCapabilityChecker from the constructor reduces complexity and reflects the shift to static utility methods.


96-99: Static method call is consistent with utility class design.

Calling FileSystemCapabilityChecker.assertAllCapabilities(pathToVault); statically is appropriate after refactoring the class into a utility with static methods.

src/main/java/org/cryptomator/cryptofs/migration/v7/Version7Migrator.java (1)

69-69: LGTM! Static method call aligns with the refactoring objective.

The change from instance method to static method call maintains the same functionality while simplifying the code.

src/test/java/org/cryptomator/cryptofs/CryptoFileSystemsTest.java (1)

67-67: LGTM! Constructor updated to reflect static utility pattern.

The test setup correctly reflects the removal of FileSystemCapabilityChecker from the constructor parameters.

src/test/java/org/cryptomator/cryptofs/migration/MigratorsTest.java (1)

Line range hint 53-219: LGTM! Consistent updates to test setup.

All Migrators constructor calls have been consistently updated to remove the FileSystemCapabilityChecker parameter, aligning with the static utility pattern while maintaining test coverage.

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.

1 participant