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

fix: update error returned when oci.NewFromTar is used with a non-tar file #793

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ADorigi
Copy link

@ADorigi ADorigi commented Jul 21, 2024

fixes #640

This pull request includes the following changes:

  • Declaring a new error ErrNotTarFile
  • Condition to check if the given path points to a .tar file
  • Corresponding tests

Signed-off-by: Adnan Gulegulzar <gulegulzaradnan@gmail.com>

extension := filepath.Ext(pathAbs)

if extension != ".tar" {
Copy link
Member

Choose a reason for hiding this comment

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

Are compressed formats supported here like tgz?

Copy link
Author

Choose a reason for hiding this comment

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

No, I believe they will not be.
Is there a list of extensions we need to support?

Copy link
Author

Choose a reason for hiding this comment

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

I can add them to the check and add corresponding tests for them too.

Copy link
Member

Choose a reason for hiding this comment

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

I imagine only tar, but I'm not sure.

Copy link
Author

Choose a reason for hiding this comment

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

The current tests work with the files hello-world.tar and test.tar. From my understanding, .tars are the only ones supported as of now.

Copy link
Member

Choose a reason for hiding this comment

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

What if the target file/directory name ends with .tar, but it is actually not a tar type? Or what if the file is a tar file but it does not have a .tar suffix in its name?

Copy link
Member

Choose a reason for hiding this comment

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

In tfs.indexEntries(), maybe we can return a friendly error if tr.Next() fails?

Copy link
Author

Choose a reason for hiding this comment

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

I think tfs.indexEntries would indeed be a better place for this check. We can check if EOF is reached before any entries are added to tfs. This would infer it is an empty file. While a non-tar file will give us an io.ErrUnexpectedEOF while reading it.

We can then give the user our custom exported error.

content/oci/readonlyoci_test.go Outdated Show resolved Hide resolved

extension := filepath.Ext(pathAbs)

if extension != ".tar" {
Copy link
Member

Choose a reason for hiding this comment

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

What if the target file/directory name ends with .tar, but it is actually not a tar type? Or what if the file is a tar file but it does not have a .tar suffix in its name?


extension := filepath.Ext(pathAbs)

if extension != ".tar" {
Copy link
Member

Choose a reason for hiding this comment

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

In tfs.indexEntries(), maybe we can return a friendly error if tr.Next() fails?

@Wwwsylvia
Copy link
Member

Hello @ADorigi , thank you for the PR! I just reviewed it and left some comments.
By the way, can you follow Conventional Commits and rename the PR title to something like "feat: ..."?

Signed-off-by: Adnan Gulegulzar <gulegulzaradnan@gmail.com>
@ADorigi
Copy link
Author

ADorigi commented Jul 23, 2024

Hi @Wwwsylvia
Please check the updates.
Now the check for non-tar file takes place in tfs.indexEntries()

@ADorigi ADorigi changed the title Updating error returned when oci.NewFromTar is used with a non-tar file fix: Updating error returned when oci.NewFromTar is used with a non-tar file Jul 23, 2024
Copy link

codecov bot commented Jul 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.03%. Comparing base (c8c2048) to head (51ec1d4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #793      +/-   ##
==========================================
+ Coverage   77.92%   78.03%   +0.10%     
==========================================
  Files          63       63              
  Lines        4694     4698       +4     
==========================================
+ Hits         3658     3666       +8     
+ Misses        656      654       -2     
+ Partials      380      378       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

internal/fs/tarfs/testdata/nonempty.tar Outdated Show resolved Hide resolved
internal/fs/tarfs/tarfs_test.go Outdated Show resolved Hide resolved
errdef/errors.go Outdated Show resolved Hide resolved
internal/fs/tarfs/tarfs.go Outdated Show resolved Hide resolved
@Wwwsylvia
Copy link
Member

Hi @ADorigi , the updated PR looks good overall. I left a few nit comments.

fix: updated error text

Signed-off-by: Adnan Gulegulzar <gulegulzaradnan@gmail.com>
@ADorigi
Copy link
Author

ADorigi commented Jul 23, 2024

Hi @Wwwsylvia
I made the minor changes and updated the error message.

internal/fs/tarfs/tarfs.go Outdated Show resolved Hide resolved
internal/fs/tarfs/tarfs.go Outdated Show resolved Hide resolved
internal/fs/tarfs/tarfs_test.go Outdated Show resolved Hide resolved
Signed-off-by: Adnan Gulegulzar <gulegulzaradnan@gmail.com>
Signed-off-by: Adnan Gulegulzar <gulegulzaradnan@gmail.com>
Copy link
Member

@Wwwsylvia Wwwsylvia left a comment

Choose a reason for hiding this comment

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

LGTM with some thoughts. @shizhMSFT Please also take a look at this PR when you get a chance.

Comment on lines +139 to +141
if len(tfs.entries) == 0 {
// indicates the given file is empty
return fmt.Errorf("%s: file is empty: %w", tfs.path, errdef.ErrInvalidTarFile)
Copy link
Member

Choose a reason for hiding this comment

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

Just thinking out loud: From the tarfs point of view, the tar file can be empty. But since tarfs is an internal package and is only used by ReadOnlyStorage and ReadOnlyOCI, which requires oci-layout file in it, I think it makes sense to return an error for an empty tar file. 🤔

@shizhMSFT shizhMSFT changed the title fix: Updating error returned when oci.NewFromTar is used with a non-tar file fix: update error returned when oci.NewFromTar is used with a non-tar file Jul 25, 2024
@Wwwsylvia
Copy link
Member

cc @qweeah who opened the original issue

Copy link
Contributor

@shizhMSFT shizhMSFT left a comment

Choose a reason for hiding this comment

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

Set to Request changes to block merge as the original issue might be invalid.

Comment on lines +139 to +142
if len(tfs.entries) == 0 {
// indicates the given file is empty
return fmt.Errorf("%s: file is empty: %w", tfs.path, errdef.ErrInvalidTarFile)
}
Copy link
Contributor

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 should return error on empty valid tar files since it breaks the encapsulation principal.

If we really want to check for empty tar file, it should be checked in the caller.

Comment on lines +145 to +148
if errors.Is(err, io.ErrUnexpectedEOF) {
// indicates that its either not a tarfile or it is a corrupted one
return fmt.Errorf("%s: %w", tfs.path, errdef.ErrInvalidTarFile)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The io.ErrUnexpectedEOF error itself indicates the tar file is corrupted. There is no need to wrap it to errdef.ErrInvalidTarFile.

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.

improve the returned error of oci.NewFromTar when the passed-in path is not a tarfile
4 participants