Skip to content
Snippets Groups Projects

Fix expand tilde and invalid path chars on windows

Merged mjptree requested to merge mjptree/arti:fix-expand-tilde-on-windows into main
5 unresolved threads

The crate shellexpand contained an error for windows platforms, where tilde characters where only expanded correctly, if preceeded by a forward slash. Although, a patch is currently in the pipeline, the crate owner has not merged it for almost a year and the repo generally appears inactive.

Further, the colon ':' character is disallowed in filepaths on windows, as it is used on the NTFS filesystem to delimit alternative data streams (e.g. %HOMEPATH%\Downloads\readme.txt:Zone.Identifier). The violation occurs in tor_dirmgr::src::storage::sqlite::SqliteStore::save_blob_internal in the filename generation.

This patch therefore breaks backwards compatibility, but the given issues would prohibit windows portability as well.

The following unit tests that failed on windows are addressed by this patch:

Merge request reports

Merged by Ian JacksonIan Jackson 3 years ago (Mar 7, 2022 4:35pm UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • requested review from @Diziet

    • Hi. Thanks for the attention to portability.

      However, I wonder if it might not be possible to do better than c&p the main part of shellexpand into our codebase. Options include:

      1. Offer to the maintainer of shellexpand to take it over
      2. Create a shellexpand-fork-tor crate for our use
      3. Temporarily use a git branch dependency pointing at the MR branch in the upstream git repo for the fix we need

      I have done (1): https://github.com/netvl/shellexpand/issues/17

      If it's OK with you I propose to wait a few days to see if that bears fruit.

      In the meantime, would you please submit the : filename change as a separate MR? I don't want to block that. Thanks!

    • Author Contributor

      Hi, thanks for reverting back!

      No problem at all, I will split the MRs and resubmit the path fix by EOD.

      Regarding shellexpand I reasoned, the crate was used only in this single context. Also, it made a couple of optmizations, that would not apply in the way that tor_config uses it. I thought it might have been prefereble to just replace the crate entirely, refactoring it to cut out the unnecessary bits, reduce external dependencies, and make it generally a bit clearer to read.

    • Please register or sign in to reply
    • No problem at all, I will split the MRs and resubmit the path fix by EOD.

      Cool, thanks. Whenever you're able is great :-).

      Regarding shellexpand I reasoned, the crate was used only in this single context. Also, it made a couple of optmizations, that would not apply in the way that tor_config uses it. I thought it might have been prefereble to just replace the crate entirely, refactoring it to cut out the unnecessary bits, reduce external dependencies, and make it generally a bit clearer to read.

      Mmm. I can see that argument. However, the less code we have in our tree the less we have to maintain as part of arti. And I like to share with others. So overall I think it's better to reuse rather than import the code. Thanks for understanding.

      If we want to move ahead before shellexpand upstream have had a chance to respond, I think a git dependency in our Cargo.toml is probably a reasonable short-term approach. @nickm, is that something we could do?

    • Author Contributor

      Alright, I opened !277 (merged) to address the invalid path character in tor_dirmgr.

      If we want to move ahead before shellexpand upstream have had a chance to respond, I think a git dependency in our Cargo.toml is probably a reasonable short-term approach.

      If you decide to go with this approach, I could amend this MR. Otherwise, it's probably best to close it?

    • Ian writes:

      Mmm. I can see that argument. However, the less code we have in our tree the less we have to maintain as part of arti. And I like to share with others. So overall I think it's better to reuse rather than import the code. Thanks for understanding.

      FWIW I agree with this; it would be better for the rust ecosystem to have a fixed (or forked) shellexpand than for every project that uses shellexpand to have to take a patch of this kind.

      (As yet another alternative, I guess we could look to see if there are other projects that provide similar functionality. I didn't look very hard before I settled on shellexpand in the first place; as far as I know, there may already be better options.)

    • Please register or sign in to reply
  • mjptree mentioned in merge request !277 (merged)

    mentioned in merge request !277 (merged)

  • I had a look. I found envsubst which is much less popular and seems inferior. I think shellexpand is probably the closest available to what we need.

    • @mjptree Thanks for your patience. I think, having reread this, that our conclusion is that to fix the bugginess in shellexpand, we want to either make it a git dependency, or fork it on crates.io. @nickm didn't express an objection to a git dependency in our Cargo.toml. So let's do that.

      For the avoidance of doubt, I haven't checked whether the git branch for https://github.com/netvl/shellexpand/pull/13 has the right base for us to use it. After all we might be undoing some other fixes. I think checking that is part of sorting this out.

      It's only been 3 days since I volunteered to take over the shellexpand crate. I'll be away next week. If I haven't had a response by the time I get back, I will publish a fork.

    • Author Contributor

      @Diziet No worries at all. Let me know, when you are in place. Then I'll MR the bug fix for windows to the appropriate fork of shellexpand and add a regression test to both repos.

    • Please register or sign in to reply
  • I had no response from shellexpand upstream in https://github.com/netvl/shellexpand/issues/17 . So I have forked it as shellexpand-fork, with the git repo currently in my personal gitlab.com namespace: https://gitlab.com/ijackson/rust-shellexpand

    I merged the change to fix Windows tilde expansion from the upstream MR into my package.

    I would love an MR in my gitlab.com repo to add a test case for this. In the meantime I will prepare an MR for arti to use my fork.

  • mentioned in commit Diziet/arti@72bb8e7f

  • Ian Jackson mentioned in merge request !373 (merged)

    mentioned in merge request !373 (merged)

  • mjptree added 489 commits

    added 489 commits

    Compare with previous version

  • mjptree added 1 commit

    added 1 commit

    • 206714f2 - Test shell variable expansion on windows

    Compare with previous version

  • Author Contributor

    @Diziet Yeeeeh, messed up the first attempt at pushing the tests... so, had to try a second time. Should be fine and Windows-compatible now.

    Although, the addition of shellcheck in the pre-push hooks almost threw me off. Might be worthwile to mention that somewhere in the build dependencies. Luckliy, it's available on Windows via choco.

    Edited by mjptree
  • Ian Jackson
209 208 assert_eq!(p.path().unwrap().to_str(), expected.to_str());
210 209 }
211 210
212 // FIXME: Add test for windows
211 #[cfg(target_family = "windows")]
212 #[test]
213 fn expand_home() {
214 let p = CfgPath::new("~\\.arti\\config".to_string());
  • Were you aware of the Rust raw strings feature? I think you could write this like this, for example:

    Suggested change
    214 let p = CfgPath::new("~\\.arti\\config".to_string());
    214 let p = CfgPath::new(r"~\.arti\config".to_string());

    NB, untested. Whether to do that here is a matter of taste I think, and certainly use of the doubled toothpicks is no reason not to merge this.

  • Please register or sign in to reply
  • Yay, LGTM, thanks :-). Will merge this in a moment.

  • Ian Jackson enabled an automatic merge when the pipeline for 206714f2 succeeds

    enabled an automatic merge when the pipeline for 206714f2 succeeds

  • merged

  • Ian Jackson mentioned in commit 56c2153b

    mentioned in commit 56c2153b

  • Please register or sign in to reply
    Loading