Skip to content

chore(tools): Refactor saunafs legacy packet functions LS-361#758

Merged
rolysr merged 12 commits intodevfrom
refactor-saunafs-legacy-packet-functions
Feb 24, 2026
Merged

chore(tools): Refactor saunafs legacy packet functions LS-361#758
rolysr merged 12 commits intodevfrom
refactor-saunafs-legacy-packet-functions

Conversation

@rolysr
Copy link
Collaborator

@rolysr rolysr commented Feb 20, 2026

This PR modernizes network communication in saunafs tools by centralizing message handling with ServerConnection, replacing manual tcpwrite/tcpread and buffer management. It standardizes request/response processing, improves error handling via exceptions, and simplifies tool logic.

Key changes:

  • Centralized communication: Tools now use ServerConnection::sendAndReceive(), reducing repetitive code.
  • Better error handling: Consistent validation, try/catch around network ops, cleaner failure paths.
  • Timeout improvements: Timeout now supports infinite durations for operations that shouldn’t time out.

Impact: Cleaner, safer, and more maintainable tool code with robust and unified networking.

@rolysr rolysr requested a review from Copilot February 20, 2026 21:49
@rolysr
Copy link
Collaborator Author

rolysr commented Feb 20, 2026

@gemini-code-assist review

@gemini-code-assist
Copy link

Summary of Changes

Hello @rolysr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly modernizes the network communication layer within various saunafs tools. By introducing a dedicated ServerConnection class, it centralizes message serialization, transmission, and response handling, replacing scattered, repetitive code with a more unified and maintainable approach. This change also brings improved error handling through exceptions and extends the Timeout utility to support infinite durations, enhancing the system's robustness and flexibility.

Highlights

  • Timeout Class Enhancement: The Timeout class has been extended to support 'infinite' timeouts, allowing for operations that should not time out. This includes modifications to its constructor and deadline(), remainingTime(), remaining_ns(), remaining_us(), remaining_ms(), remaining_s(), and expired() methods to correctly handle this new state.
  • Refactoring of Legacy Packet Functions: Multiple saunafs tools have been refactored to use a new ServerConnection class for sending and receiving messages. This centralizes the logic for network communication, replacing repetitive manual tcpwrite and tcpread calls with a more robust and exception-safe mechanism.
  • Improved Error Handling: The refactored tools now incorporate try-catch blocks around network operations, allowing for more graceful error handling and clearer reporting of communication issues or invalid responses from the master server.
  • Simplified Tool Codebase: By abstracting network communication into ServerConnection, the individual tool functions (append_file, check_file, dir_info, file_info, file_repair, get_eattr, get_trashtime, set_eattr, set_trashtime) are significantly cleaner and easier to maintain, focusing primarily on their specific business logic rather than low-level networking details.
Changelog
  • src/common/time_utils.cc
    • Implemented infinite timeout logic in the Timeout constructor and its time-related methods.
  • src/common/time_utils.h
    • Added a infinite_ boolean member to the Timeout class to track infinite timeout state.
  • src/common/time_utils_unittest.cc
    • Added a new test case to verify the functionality of infinite timeouts.
  • src/tools/append_file.cc
    • Replaced manual packet handling with ServerConnection::sendAndReceive and added exception handling.
    • Removed explicit buffer allocation and tcpwrite/tcpread calls.
  • src/tools/check_file.cc
    • Integrated ServerConnection::sendAndReceive for network communication and introduced exception handling.
    • Streamlined packet parsing by removing manual buffer management.
  • src/tools/dir_info.cc
    • Refactored network interactions to use ServerConnection and try-catch blocks.
    • Simplified response parsing logic for directory statistics.
  • src/tools/file_info.cc
    • Converted chunk information retrieval to use ServerConnection and exception handling.
    • Removed manual tcpwrite/tcptoread and buffer resizing.
  • src/tools/file_repair.cc
    • Adopted ServerConnection::sendAndReceive for file repair requests and added exception handling.
    • Eliminated manual buffer management and direct TCP calls.
  • src/tools/get_eattr.cc
    • Migrated extended attribute retrieval to use ServerConnection and exception handling.
    • Simplified response parsing for both normal and aggregated modes.
  • src/tools/get_trashtime.cc
    • Refactored trash time retrieval to utilize ServerConnection and exception handling.
    • Streamlined response parsing for normal and aggregated trash time data.
  • src/tools/master_functions.cc
    • Updated the master_register function to use ServerConnection for registration and added exception handling.
  • src/tools/set_eattr.cc
    • Converted extended attribute setting to use ServerConnection and exception handling.
    • Simplified response processing for attribute changes.
  • src/tools/set_trashtime.cc
    • Refactored trash time setting to use ServerConnection and exception handling.
    • Streamlined response handling for changed, unchanged, and not permitted inodes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a significant and valuable refactoring of the networking code in several tool files. The move from manual C-style buffer management and error checking to a more modern C++ approach using ServerConnection and try-catch blocks greatly improves code readability, maintainability, and safety. The changes are applied consistently for the most part. I've found a few minor inconsistencies in the new error handling and an unused variable, which I've commented on. Once these are addressed, this will be an excellent improvement.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors legacy packet communication functions in SaunaFS tools to use a more modern and consistent pattern. The changes introduce the ServerConnection::sendAndReceive method for handling master server communication, replacing manual packet serialization and deserialization code. Additionally, the PR adds support for infinite timeouts in the Timeout class by treating negative timeout values as infinite.

Changes:

  • Refactored 9 tool files to use ServerConnection::sendAndReceive with consistent exception handling
  • Added infinite timeout support to the Timeout class with corresponding unit tests
  • Updated master registration to use the new packet serialization pattern

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
src/tools/set_trashtime.cc Refactored to use ServerConnection::sendAndReceive; replaced manual packet handling
src/tools/set_eattr.cc Refactored to use ServerConnection::sendAndReceive; simplified error handling
src/tools/get_trashtime.cc Refactored to use ServerConnection::sendAndReceive; improved code structure
src/tools/get_eattr.cc Refactored to use ServerConnection::sendAndReceive; cleaner aggregated mode handling
src/tools/file_repair.cc Refactored to use ServerConnection::sendAndReceive; simplified response parsing
src/tools/file_info.cc Refactored to use ServerConnection::sendAndReceive; wrapped in try-catch
src/tools/dir_info.cc Refactored to use ServerConnection::sendAndReceive; improved legacy format handling
src/tools/check_file.cc Refactored to use ServerConnection::sendAndReceive; cleaner format detection
src/tools/append_file.cc Refactored to use deserializeAllLegacyPacketDataNoHeader; simplified parsing
src/tools/master_functions.cc Refactored master_register to use ServerConnection::sendAndReceive
src/common/time_utils.h Added infinite_ flag to Timeout class
src/common/time_utils.cc Implemented infinite timeout support
src/common/time_utils_unittest.cc Added test coverage for infinite timeout functionality

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request successfully refactors several legacy packet functions in the SaunaFS tools to use the more modern ServerConnection and MessageBuffer abstractions. This significantly improves code readability and maintainability by removing manual buffer management and socket I/O. Additionally, the Timeout class has been enhanced to support infinite timeouts. I have noted a couple of instances of variable shadowing in the refactored code that should be addressed to avoid confusion.

@rolysr rolysr self-assigned this Feb 20, 2026
@rolysr rolysr changed the title chore(tools): Refactor saunafs legacy packet functions chore(tools): Refactor saunafs legacy packet functions LS-362 Feb 20, 2026
@rolysr rolysr force-pushed the refactor-saunafs-legacy-packet-functions branch 2 times, most recently from fbed236 to dac5473 Compare February 22, 2026 14:44
@rolysr rolysr changed the title chore(tools): Refactor saunafs legacy packet functions LS-362 chore(tools): Refactor saunafs legacy packet functions LS-361 Feb 23, 2026
@rolysr rolysr force-pushed the refactor-saunafs-legacy-packet-functions branch from dac5473 to 98aca38 Compare February 23, 2026 11:05
@rolysr
Copy link
Collaborator Author

rolysr commented Feb 23, 2026

@gemini-code-assist review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors several SaunaFS tools to use the centralized ServerConnection for network communication, which improves maintainability and error handling. The modernization of the Timeout class to support infinite durations is also a welcome change. I have identified a potential buffer over-read in the master registration logic and an opportunity to better utilize the new infinite timeout functionality in the fileinfo tool.

@rolysr rolysr force-pushed the refactor-saunafs-legacy-packet-functions branch from 98aca38 to 4149085 Compare February 23, 2026 12:41
@rolysr rolysr marked this pull request as ready for review February 23, 2026 12:43
@rolysr rolysr force-pushed the refactor-saunafs-legacy-packet-functions branch 2 times, most recently from ee2ad9a to f1f964b Compare February 23, 2026 13:48
Copy link
Contributor

@lgsilva3087 lgsilva3087 left a comment

Choose a reason for hiding this comment

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

Great change Roly.
Consider my comment and in general:
Standardize exception catching to const references.

Copy link
Collaborator

@dmga44 dmga44 left a comment

Choose a reason for hiding this comment

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

Great job, @rolysr 👍 , but please check my suggestions.

@rolysr rolysr force-pushed the refactor-saunafs-legacy-packet-functions branch 2 times, most recently from 956f09a to fc987e7 Compare February 24, 2026 15:24
@rolysr
Copy link
Collaborator Author

rolysr commented Feb 24, 2026

@gemini-code-assist review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great step towards modernizing the network communication in the saunafs tools. Centralizing message handling with ServerConnection and replacing manual tcpwrite/tcpread significantly cleans up the code and improves maintainability. The introduction of exception-based error handling is also a major improvement for robustness. The changes to support infinite timeouts are a welcome addition.

I've found a recurring issue where the new try-catch blocks are not fully utilized. In several files, error conditions inside the try block are still handled with printf and return -1 instead of throwing an exception. This undermines the new centralized error handling model. I've left a couple of specific comments on this, but the issue is present in check_file.cc, dir_info.cc, file_info.cc, file_repair.cc, get_eattr.cc, get_trashtime.cc, master_functions.cc, set_eattr.cc, and set_trashtime.cc. It would be best to convert all these manual error checks to throw Exception(...) for consistency.

Additionally, I've noted an opportunity to consistently use the new infinite timeout feature in file_info.cc.

Overall, this is a very positive refactoring. Addressing the consistency of error handling will make it even better.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

@rolysr rolysr force-pushed the refactor-saunafs-legacy-packet-functions branch from fc987e7 to 21e9940 Compare February 24, 2026 15:38
The previous appendchunks implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous checkfile implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous dirinfo implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous fileinfo implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous filerepair implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
Previously, passing a negative timeout to a Timeout constructor,
resulted in an immediately expired Timeout instance due to negative
duration handling. This behavior differed from the legacy poll()-based
implementation, where passing -1 meant waiting indefinitely.

Timeout now explicitly supports infinite mode. When constructed with
a negative duration, it behaves as an infinite timeout:

- remaining_*() returns -1 (poll-compatible)
- expired() always returns false
- deadline() returns max time point

This allows seamless substitution of legacy timeout logic with the new
Timeout-based implementation without changing caller semantics.

Unit tests were extended to validate infinite timeout behavior.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous settrashtime implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous gettrashtime implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous register to master implementation in the saunafs command
used an outdated, error-prone manual approach for building, sending,
and receiving packets to and from the master. This made the code
difficult to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous geteattr implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
The previous seteattr implementation in the saunafs command used an
outdated, error-prone manual approach for building, sending, and
receiving packets to and from the master. This made the code difficult
to read and unnecessarily verbose, especially since a cleaner
implementation for handling legacy packets already existed in the
codebase. This commit replaces the old approach with the existing,
cleaner solution.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
@rolysr rolysr force-pushed the refactor-saunafs-legacy-packet-functions branch from 21e9940 to 704f600 Compare February 24, 2026 18:39
@rolysr
Copy link
Collaborator Author

rolysr commented Feb 24, 2026

@gemini-code-assist review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great step forward in modernizing the networking code within the saunafs tools. Centralizing the communication logic in ServerConnection and using exceptions for error handling significantly improves code readability, maintainability, and robustness. The addition of an infinite timeout capability is also a valuable improvement. My review focuses on ensuring consistency and correctness in the application of these new features. I've identified a potential conflict with a design rule regarding infinite timeouts in locking mechanisms, and a potential unintended change in timeout values for some operations. Overall, this is a very positive refactoring.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Previous tcpwrite/tcpread used for master commnunication on tools
functions had a 10 seconds timeout set as default, which is not
the default value if not specified in the corresponding substitutions
of ServerConnection::sendAndReceive functions. This commit sets that
timeout to the expected one as before given new communication bindings
on tools functions.

Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
@rolysr
Copy link
Collaborator Author

rolysr commented Feb 24, 2026

@gemini-code-assist review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a significant improvement, refactoring legacy network communication in several tools to use the modern ServerConnection class. The changes greatly enhance code clarity, maintainability, and error handling by replacing manual buffer management and tcpread/tcpwrite calls with a centralized, exception-based approach. The introduction of support for infinite timeouts in the Timeout class is also a valuable addition. I've found one minor point of inconsistency regarding the use of this new timeout feature, which I've detailed in a specific comment. Overall, this is an excellent and well-executed refactoring.

@rolysr rolysr merged commit 882ea90 into dev Feb 24, 2026
11 checks passed
@rolysr rolysr deleted the refactor-saunafs-legacy-packet-functions branch February 24, 2026 22:48
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.

4 participants