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

fsutil: Add SlashedPathDir() and SlashedPathClean() functions #75

Closed
wants to merge 3 commits into from

Conversation

woky
Copy link
Contributor

@woky woky commented Jun 21, 2023

From the code:

These functions exists because we work with slash terminated
directory paths that come from deb package tarballs but standard
library path functions treat slash terminated paths as unclean.

We use ad-hoc code to make filepath.Dir() slash terminated in two places
right now. For example this code

targetDir := filepath.Dir(strings.TrimRight(targetPath, "/")) + "/"

is not strictly correct as "/a/b/.///" will be turned into "/a/b/./"
which is still "/a/b".

Since we deal with slash terminated directory paths everywhere, create
and use dedicated helper functions to process them.

@woky woky added the Priority Look at me first label Jun 21, 2023
@cjdcordeiro cjdcordeiro removed the Priority Look at me first label Jun 22, 2023
@woky woky added the Priority Look at me first label Aug 29, 2023
Copy link
Member

@rebornplusplus rebornplusplus left a comment

Choose a reason for hiding this comment

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

Nice.

Copy link

@Aflynn50 Aflynn50 left a comment

Choose a reason for hiding this comment

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

Looking good, just a few pedantic things.

internal/fsutil/path.go Show resolved Hide resolved
internal/fsutil/path.go Outdated Show resolved Hide resolved
internal/fsutil/path.go Outdated Show resolved Hide resolved
internal/fsutil/path.go Outdated Show resolved Hide resolved
internal/fsutil/path.go Outdated Show resolved Hide resolved
internal/fsutil/path_test.go Outdated Show resolved Hide resolved
internal/slicer/slicer.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@cjdcordeiro cjdcordeiro left a comment

Choose a reason for hiding this comment

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

Thanks! @Aflynn50 comments make sense to me ;)

Copy link

@flotter flotter left a comment

Choose a reason for hiding this comment

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

This is a great initiative for simplifying the code. Some minor tweak proposals.

internal/fsutil/path.go Show resolved Hide resolved
internal/fsutil/path.go Outdated Show resolved Hide resolved
Comment on lines 39 to 41
cleanPath := filepath.Clean(path)
slashPath := cleanPath
if path[len(path)-1] == '/' && cleanPath != "/" {
slashPath += "/"
}
path = fsutil.Clean(path)
Copy link

Choose a reason for hiding this comment

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

The output of these two snippets are not identical I believe?

slashpath will always end with a Separator.
path uses fsutil.Clean() only, which could end without a Separator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate on why you think slashpath will always end with separator? From diff snippet it looks like it'll only end with separator when path ends with /'.

Copy link

Choose a reason for hiding this comment

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

I just did a test because my brain failed to produce an explanation:

old addKnownPath("/"):
output: "/"

new addKnownPath("/"):
output: "/"
output: "//"

Check your new Clean() unit test does not try the "/" root case. It produces "//". Did I miss something ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the find. I didn't notice the bug in Clean() (now SlashedPathClean()). I've fixed the bug there and added test cases for root directory. Please resolve this thread if appropriate.

internal/slicer/slicer.go Show resolved Hide resolved
@woky woky force-pushed the pub/fsutil-dir-clean branch 2 times, most recently from c9cff06 to 7c281e9 Compare September 19, 2023 06:07
@woky woky removed the Priority Look at me first label Sep 19, 2023
@cjdcordeiro cjdcordeiro added Priority Look at me first and removed Priority Look at me first labels Sep 25, 2023
input string
output string
}{
{"/a/b/c", "/a/b/c"},
Copy link

Choose a reason for hiding this comment

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

I think you should add "/" root here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Indeed it didn't handle / correctly. It should be fixed now and there test cases testing various forms of the root directory. Please check it out and resolve if appropriate.

@woky woky force-pushed the pub/fsutil-dir-clean branch 2 times, most recently from a4c15cb to 51ac20a Compare October 9, 2023 17:01
@woky woky added Blocked Waiting for something external and removed Priority Look at me first labels Oct 9, 2023
@woky woky force-pushed the pub/fsutil-dir-clean branch 4 times, most recently from af0c12e to bee9816 Compare October 9, 2023 17:50
@woky woky force-pushed the pub/fsutil-dir-clean branch 3 times, most recently from f0e4b5e to b003238 Compare October 9, 2023 19:15
@woky woky changed the title fsutil: Add Dir() and Clean() functions fsutil: Add SlashedPathDir() and SlashedPathClean() functions Oct 12, 2023
@woky
Copy link
Contributor Author

woky commented Oct 12, 2023

This PR depends on #69.

Currently we only test with the embedded base-files files package. This
quite limits what we can test. But since commit a3315e3
("testutil/pkgdata: Create deb programatically") we have the ability to
define our own custom package content.

Add support for testing with custom packages. These can be defined in
each test case per archive name. The content can be defined directly in
test case definition with the help of testutil.MustMakeDeb().

The package content is wrapped in the testPackage structure. This
introduces one level of nesting in each test case package definition. In
future, we will add package metadata to the package definition, namely
version. So wrapping the package content in the structure now and later
just adding a new field to it the structure later will make the future
diff much smaller.
At some point we will need to get information about (deb) packages from
(Go) packages other than archive, namely from the slicer package.
For instance:
- Package entries in Chisel DB will include version, digest and
  architecture.
- Multi-archive package selection will need to know versions of a
  package in all configured archives.

Add Info() method to the Archive interface that returns a new
PackageInfo interface that contains accessors for the underlying
control.Section of the package. Currently these accessors are Name(),
Version(), Arch() and SHA256() methods.

The reason to introduce PackageInfo interface instead of returing
control.Section directly is keep the archive abstraction and not leak
implementation details.
From the code:

  These functions exists because we work with slash terminated
  directory paths that come from deb package tarballs but standard
  library path functions treat slash terminated paths as unclean.

We use ad-hoc code to make filepath.Dir() slash terminated in two places
right now. For example this code

  targetDir := filepath.Dir(strings.TrimRight(targetPath, "/")) + "/"

is not strictly correct as "/a/b/.///" will be turned into "/a/b/./"
which is still "/a/b".

Since we deal with slash terminated directory paths everywhere, create
and use dedicated helper functions to process them.
Copy link

@flotter flotter left a comment

Choose a reason for hiding this comment

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

Looks great to me!

@cjdcordeiro cjdcordeiro added Priority Look at me first and removed Priority Look at me first labels Oct 23, 2023
@letFunny
Copy link
Collaborator

Per our offline discussion, I think our best way forward is to close this PR and discuss with @rebornplusplus to see if it is a priority right now, and how to amend the code here to make it compatible with the latest changes. We will take that discussion offline and we can always re-open the PRs or create new ones as we see fit.

@cjdcordeiro cjdcordeiro added the Decaying It's been a while, close or act on it label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked Waiting for something external Decaying It's been a while, close or act on it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants