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

refactor(error): improve error messages and file handling #334

Merged
merged 9 commits into from
Nov 18, 2024

Conversation

simonsan
Copy link
Contributor

@simonsan simonsan commented Oct 14, 2024

  • Create parent directory if it does not exist before opening the file for writing.

Fixes rustic-rs/rustic#1315
Fixes #310

@simonsan simonsan added A-errors Area: error handling needs improvement C-refactor Category: Refactoring of already existing code labels Oct 14, 2024
@simonsan simonsan requested a review from aawsome October 14, 2024 16:03
@simonsan simonsan added the S-waiting-for-review Status: PRs waiting for review label Oct 14, 2024
@simonsan
Copy link
Contributor Author

I kept:

        for i in 0u8..=255 {
            let filename = self.path.join("data").join(hex::encode([i]));
            fs::create_dir_all(&filename).map_err(|err| {
                LocalBackendErrorKind::DirectoryCreationFailed {
                    path: filename.clone(),
                    source: err,
                }
            })?;
        }

as that impacts compatibility with restic, and a repository without that, wouldn't be able to be read by it. Hence, I thought, we keep that, but we make sure, that also without these directories (in case someone deletes them), we are able to create and write to these directories. Not sure, how else we could handle that.

Copy link
Member

@aawsome aawsome 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!
I have just found a small thing I would change in this PR...

fn write_bytes(&self, tpe: FileType, id: &Id, _cacheable: bool, buf: Bytes) -> Result<()> {
trace!("writing tpe: {:?}, id: {}", &tpe, &id);
let filename = self.path(tpe, id);

// create parent directory if it does not exist
if let Some(parent) = filename.parent() {
Copy link
Member

Choose a reason for hiding this comment

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

I would make a method parent_path and then just use self.parent_path(tpe,id) here.

Copy link
Contributor Author

@simonsan simonsan Oct 16, 2024

Choose a reason for hiding this comment

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

Added! Not sure how we would handle a non-existing parent? e.g. in a root path, that should never happen right? Or do you have another idea?

Copy link
Member

Choose a reason for hiding this comment

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

Actually I thought about having something along the fn path, but just without the last join. We know there is a parent because we just create paths which do have a parent. My idea was just about removing the error handling which will never be called.

Copy link

codecov bot commented Oct 16, 2024

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@171e2aa). Learn more about missing BASE report.

Files with missing lines Patch % Lines
crates/core/src/commands/check.rs 40.0% 3 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
crates/backend/src/local.rs 0.0% <ø> (ø)
crates/core/src/commands/check.rs 65.4% <40.0%> (ø)

@simonsan simonsan requested a review from aawsome October 16, 2024 13:30
@simonsan simonsan added S-blocked Status: Blocked from merging/working on due to some issue and removed S-waiting-for-review Status: PRs waiting for review labels Oct 24, 2024
@simonsan simonsan marked this pull request as draft October 24, 2024 01:19
@simonsan simonsan added this to the NEXT milestone Oct 30, 2024
- Update error messages for file operations in the `LocalBackendErrorKind` enum.
- Refactor the `ReadBackend` and `WriteBackend` implementations in the `LocalBackend` module to handle file opening errors more accurately.
- Add error variants `OpeningFileForPartialReadingFailed` and `OpeningFileForWritingFailed` to provide specific information about file opening failures.
- Create parent directory if it does not exist before opening the file for writing.

Fixes #rustic-rs/rustic#1315

Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com>
@simonsan simonsan force-pushed the fix/create-directories branch from c186960 to 0583d0a Compare November 17, 2024 02:09
@simonsan simonsan marked this pull request as ready for review November 17, 2024 02:09
@simonsan simonsan removed the S-blocked Status: Blocked from merging/working on due to some issue label Nov 17, 2024
@simonsan simonsan requested review from aawsome and removed request for aawsome November 17, 2024 02:12
@simonsan simonsan added the S-waiting-for-review Status: PRs waiting for review label Nov 17, 2024
nardoor
nardoor previously approved these changes Nov 17, 2024
Copy link
Contributor

@nardoor nardoor left a comment

Choose a reason for hiding this comment

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

Hello,

Looks good to me!
Just one question about an error log.

crates/core/src/commands/check.rs Show resolved Hide resolved
@simonsan simonsan merged commit 749879f into main Nov 18, 2024
20 checks passed
@simonsan simonsan deleted the fix/create-directories branch November 18, 2024 03:15
@rustic-release-plz rustic-release-plz bot mentioned this pull request Nov 18, 2024
simonsan pushed a commit that referenced this pull request Nov 18, 2024
## 🤖 New release
* `rustic_backend`: 0.4.2 -> 0.5.0 (⚠️ API breaking changes)
* `rustic_core`: 0.5.5 -> 0.6.0 (⚠️ API breaking changes)
* `rustic_testing`: 0.2.3 -> 0.3.0 (✓ API compatible changes)

### ⚠️ `rustic_backend` breaking changes

```
--- failure enum_missing: pub enum removed or renamed ---

Description:
A publicly-visible enum cannot be imported by its prior path. A `pub use` may have been removed, or the enum itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.36.0/src/lints/enum_missing.ron

Failed in:
  enum rustic_backend::error::LocalBackendErrorKind, previously in file /tmp/.tmp0sSY8G/rustic_backend/src/error.rs:90
  enum rustic_backend::error::RestErrorKind, previously in file /tmp/.tmp0sSY8G/rustic_backend/src/error.rs:67
  enum rustic_backend::error::BackendAccessErrorKind, previously in file /tmp/.tmp0sSY8G/rustic_backend/src/error.rs:10
  enum rustic_backend::error::RcloneErrorKind, previously in file /tmp/.tmp0sSY8G/rustic_backend/src/error.rs:43

--- failure module_missing: pub module removed or renamed ---

Description:
A publicly-visible module cannot be imported by its prior path. A `pub use` may have been removed, or the module may have been renamed, removed, or made non-public.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.36.0/src/lints/module_missing.ron

Failed in:
  mod rustic_backend::error, previously in file /tmp/.tmp0sSY8G/rustic_backend/src/error.rs:1
```

### ⚠️ `rustic_core` breaking changes

```
--- failure inherent_method_missing: pub method removed or renamed ---

Description:
A publicly-visible method or associated fn is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.36.0/src/lints/inherent_method_missing.ron

Failed in:
  LocalDestination::remove_dir, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:129
  LocalDestination::remove_file, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:152
  LocalDestination::create_dir, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:171
  LocalDestination::set_times, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:189
  LocalDestination::set_user_group, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:237
  LocalDestination::set_uid_gid, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:283
  LocalDestination::set_permission, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:324
  LocalDestination::set_extended_attributes, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:385
  LocalDestination::set_length, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:466
  LocalDestination::create_special, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:521
  LocalDestination::read_at, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:599
  LocalDestination::write_at, previously in file /tmp/.tmp0sSY8G/rustic_core/src/backend/local_destination.rs:663
  RusticError::into_inner, previously in file /tmp/.tmp0sSY8G/rustic_core/src/error.rs:46
  RusticError::backend_error, previously in file /tmp/.tmp0sSY8G/rustic_core/src/error.rs:61
```

<details><summary><i><b>Changelog</b></i></summary><p>

## `rustic_backend`
<blockquote>

##
[0.5.0](rustic_backend-v0.4.2...rustic_backend-v0.5.0)
- 2024-11-18

### Added

- *(async)* add `async_compatible` methods to identify backend
compatibility
([#355](#355))
- add 'yandex-disk' to enabled opendal services and update opendal to
0.50.2 ([#360](#360))

### Other

- *(error)* enhance error logging and output formatting
([#361](#361))
- *(backend)* simplify code in local backend
([#362](#362))
- *(backend)* migrate from `backoff` to `backon`
([#356](#356))
- *(error)* improve error messages and file handling
([#334](#334))
- *(deps)* lock file maintenance rust dependencies
([#345](#345))
- *(deps)* [**breaking**] upgrade to new conflate version
([#300](#300))
- *(errors)* [**breaking**] Improve error handling, display and clean up
codebase ([#321](#321))
</blockquote>

## `rustic_core`
<blockquote>

##
[0.6.0](rustic_core-v0.5.5...rustic_core-v0.6.0)
- 2024-11-18

### Added

- *(async)* add `async_compatible` methods to identify backend
compatibility
([#355](#355))

### Fixed

- prevent overwriting hot repository on init
([#353](#353))

### Other

- *(error)* enhance error logging and output formatting
([#361](#361))
- *(deps)* remove Derivative and replace with Default impl due to
RUSTSEC-2024-0388
([#359](#359))
- *(error)* improve error messages and file handling
([#334](#334))
- *(deps)* lock file maintenance rust dependencies
([#345](#345))
- *(deps)* remove cdc and switch to rustic_cdc
([#348](#348))
- *(deps)* [**breaking**] upgrade to new conflate version
([#300](#300))
- *(errors)* [**breaking**] Improve error handling, display and clean up
codebase ([#321](#321))
</blockquote>

## `rustic_testing`
<blockquote>

##
[0.3.0](rustic_testing-v0.2.3...rustic_testing-v0.3.0)
- 2024-11-18

### Added

- *(async)* add `async_compatible` methods to identify backend
compatibility
([#355](#355))

### Other

- *(errors)* [**breaking**] Improve error handling, display and clean up
codebase ([#321](#321))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).

Co-authored-by: rustic-release-plz[bot] <182542030+rustic-release-plz[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-errors Area: error handling needs improvement C-refactor Category: Refactoring of already existing code S-waiting-for-review Status: PRs waiting for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can we avoid creating empty folders when initializing a repo? Uncaught rclone errors causing rustic panic
3 participants