Skip to content

fix(gui): expand tildes for subdir check (fixes #9400) - #9788

Merged
imsodin merged 8 commits into
syncthing:mainfrom
tobifroe:main
Nov 12, 2024
Merged

imsodin merged 8 commits into
syncthing:mainfrom
tobifroe:main

Conversation

@tobifroe

Copy link
Copy Markdown
Contributor

Purpose

This closes #9400 by always expanding tildes when parent/subdir checks are done.

Testing

I tested this by creating folders with paths to parent or subdirectories of the default folder that include a tilde in their path as shown in the attached screenshots.
With this change, overlap will be detected regardless of wether or not tildes are used in other folder paths.

Screenshots

Default Folder:
2024-10-26-At-08h40m33s
Newly created folder (parent directory in this case)
2024-10-26-At-08h40m13s

// Tildes in both xdir and ydir are expanded for comparison
// so that e.g. xdir = "home/a/", ydir = "~/b" will return true.
function isSubDir(xdir, ydir) {
xdir = xdir.replace("~", scope.system.tilde);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we want /home/foo/my~dir/ to get expanded though, so this should probably be limited to replacing tilde followed by slash (and maybe backslash) at the beginning of the string?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right. I adapted the replace call. Thank you for the fast review btw! :)

Comment on lines +12 to +14
var tildeExpansionRegex = new RegExp("^~\/");
xdir = xdir.replace(tildeExpansionRegex, `${scope.system.tilde}/`);
ydir = ydir.replace(tildeExpansionRegex, `${scope.system.tilde}/`);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you want to use scope.system.pathSeparator instead of a hardcoded slash. And perhaps, since we split on that on the line below, perhaps then just check if the first component is ~?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I replaced the hardcoded slash with scope.system.pathSeperator. I do think we need to check for ~/ at the beginning of the string since we wouldn't want to expand ~my-dir/foo, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not asking for any change, just trying to clarify if it's of any interest to you:

How I understand @calmh's suggestion that still works, but no regex required: After splitting, you check that xdirArr[0] === "~" and then replace that. For ~my-dir/foo the array will be [~my-dir, foo], so no match/replace. The small downside: You need to split the expanded tilde again. The upside: No regex or handling of separator types needed, you just get the same behaviour as splitting (rightly or wrongly, but if it's wrong fixing both tilde and splitting consistently would be nice).

@acolomb

acolomb commented Oct 27, 2024 •

Copy link
Copy Markdown
Member

Note that on Windows, both \ and / are considered as path separators. We should treat both ~/ and ~\ as referring to the home directory.

The same incorrect check happens in editFolder() by the way, where we assume that a trailing path separator should be stripped, but only check for the single system path separator, while Windows has two variants.

@acolomb

acolomb commented Oct 27, 2024

Copy link
Copy Markdown
Member

The backslash is not special on other OSes, so would it make more sense to check for ( \ or system.pathSeparator )? In case they are identical, it would not hurt.

@tobifroe

Copy link
Copy Markdown
Contributor Author

@acolomb Shouldn't we check for / or system.pathSeperator to not match backslashes on Linux? I don't have a Windows machine available to check the value of system.pathSeperator there.

@acolomb

acolomb commented Oct 28, 2024

Copy link
Copy Markdown
Member

Sorry, that was a typo of mine. Of course I meant: / OR system.pathSeparator

On Windows, the browser's debug console tells me that the latter is "\\", which should mean a single backslash if I'm reading JSON correctly here.

@tobifroe

Copy link
Copy Markdown
Contributor Author

Thank you for clarifying @acolomb, changed the regex to reflect that.

@imsodin imsodin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved, the below is just a meta-comment on reviewing.

by @acolomb:

Note that on Windows, both \ and / are considered as path separators. We should treat both ~/ and ~\ as referring to the home directory.

This is a case of requiring a PR to do more than what it's main intent is. I believe we should generally not do that, resp. at most suggest that as an optional extension of the main intent. Sometimes it might not be clear if the proposal is integral to the change at hand or not - one possible litmus test is to ask yourself if the change is a net improval without it already (obviously neither final nor complete criteria). Feature creep can introduce all kinds of issues (delaying PRs, introducing unexpected side effects). Don't get me wrong, it's great if we find additional, concrete improvements during a PR, I just would like to see those more treated as optional suggestions than integral parts of the review.

@imsodin

imsodin commented Nov 12, 2024

Copy link
Copy Markdown
Member

@calmh Policybot is listing a missing contributor approval. I noticed that before on PRs you approved. I don't see a way to "kick it" to make it re-evaluate the situation (some CI solution at work is very prone to missing hooks, that's why "kick it" is an ingrained action for me :) ) - do you know of one? Or any known issues/ideas/... for dealing with/troubleshooting this?

@imsodin
imsodin merged commit 1a529e9 into syncthing:main Nov 12, 2024
@calmh

calmh commented Nov 12, 2024

Copy link
Copy Markdown
Member

@imsodin It's silliness... your review is disqualified since you've pushed a commit and become a contributor. Merge commits shouldn't count towards that though, I'll see if I can fix what the problem is.

@calmh

calmh commented Nov 12, 2024

Copy link
Copy Markdown
Member

@imsodin #9818

@acolomb

acolomb commented Nov 12, 2024

Copy link
Copy Markdown
Member

This is a case of requiring a PR to do more than what it's main intent is. I believe we should generally not do that, resp. at most suggest that as an optional extension of the main intent.

@imsodin Thanks for the heads up, will try to keep it in mind next time. However, to defend the circumstances here, I was reacting to a previous review comment which suggested using the pathSeparator. So I meant to point out that this proposed fix to the PR content was incomplete, by not catching all otherwise valid combinations. That's not feature creep IMO, but preventing fixes for improved correctness which are themselves not always correct.

@imsodin

imsodin commented Nov 13, 2024

Copy link
Copy Markdown
Member

@acolomb Fair point. I also came to this PR a lot later - with hindsight it's very easy to to find things to (potentially) improve (if it even applies here).

calmh added a commit to calmh/syncthing that referenced this pull request Nov 13, 2024
* upstream/main:
  chore: update policy to allow approvals by contributors (syncthing#9818)
  fix(gui): expand tildes for subdir check (fixes syncthing#9400) (syncthing#9788)
@calmh calmh added this to the v1.28.1 milestone Nov 14, 2024
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Jul 1, 2026
@syncthing syncthing locked and limited conversation to collaborators Jul 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Folder overlap not detected when using tilde

5 participants