Skip to content

std.tar: error on insufficient buffers provided to iterator #19155

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

Merged
merged 13 commits into from
Mar 12, 2024

Conversation

ianic
Copy link
Contributor

@ianic ianic commented Mar 2, 2024

This is follow up of #19128 where we changed iterator interface to accept buffers from caller. Internally we were assuming that buffers are big enough. Now when they are provided from outside we are checking that and raise error if there is not enough space.

I also make few changes to make iterator more ergonomic to the end users:

Don't require user to call file.skip() on file returned from iterator.next if it is not read. Iterator will now handle this.
Previously that returned header parsing error, without knowing some tar internals it is hard to understand what is required from user.

Use iterator.File.kind enum which is similar to fs.File.Kind. Internal Header.Kind has many types which are not exposed but the user needed to have else in kind switch to cover those cases. Now there are only three kind of iterator.File: .directory, .file, .sym_link.

Add reader interface to the iterator.File.

@ianic ianic force-pushed the tar_max_file_size branch from 32fea44 to 5e7b816 Compare March 4, 2024 08:51
lib/std/tar.zig Outdated
Comment on lines 897 to 900
// With test enabled this is hanging under windows:
// zig build test docs --zig-lib-dir .\lib\ -Dstatic-llvm -Dskip-non-native -Denable-symlinks-windows
const builtin = @import("builtin");
if (builtin.os.tag == .windows) return error.SkipZigTest;
Copy link
Member

Choose a reason for hiding this comment

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

When disabling a test, please first file an issue, then link to the issue when disabling it.

Copy link
Contributor Author

@ianic ianic Mar 9, 2024

Choose a reason for hiding this comment

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

I traced problem a little bit more and file the issue.
I fixed this test. The problem was that created files were not closed.

@ianic ianic requested a review from andrewrk March 9, 2024 18:28
lib/std/tar.zig Outdated
Comment on lines 253 to 259
/// var file_name_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
/// var link_name_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
///
/// var iter = std.tar.iterator(archive.reader(), .{
/// .file_name_buffer = &file_name_buffer,
/// .link_name_buffer = &link_name_buffer,
/// });
Copy link
Member

Choose a reason for hiding this comment

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

Don't put code in doc comments. Instead, use a doctest like this:

test iterator {
    ...
}

Then it will show up as example usage like this:

image

This will require the test to be valid code that passes the tests, and therefore cannot bitrot.

@ianic ianic force-pushed the tar_max_file_size branch from 35c7ef8 to 44da9be Compare March 10, 2024 15:34
ianic added 13 commits March 11, 2024 12:22
Should do that before I broke package manager!
Tar header stores name in max 256 bytes and link name in max 100 bytes.
Those are minimums for provided buffers. Error is raised during iterator
init if buffers are not long enough.

Pax and gnu extensions can store longer names. If such extension is
reached during unpack and don't fit into provided buffer error is
returned.
Don't assert min buffer size on iterator init. That was changing public
interface. This way we don't break that interface.
for the then end users:

1. Don't require user to call file.skip() on file returned from
iterator.next if file is not read. Iterator will now handle this.
Previously that returned header parsing error, without knowing some tar
internals it is hard to understand what is required from user.

2. Use iterator.File.kind enum which is similar to fs.File.Kind,
something familiar. Internal Header.Kind has many types which are not
exposed but the user needs to have else in kind switch to cover those
cases.

3. Add reader interface to the iterator.File.
Fixing error from ci:

std/tar.zig:423:54: error: expected type 'usize', found 'u64'
std/tar.zig:423:54: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
Fixing ci error: error:

'tar.test.test.pipeToFileSystem' failed: slices differ. first difference occurs at index 2 (0x2)

============ expected this output: =============  len: 9 (0x9)

2E 2E 2F 61 2F 66 69 6C  65                       ../a/file

============= instead found this: ==============  len: 9 (0x9)

2E 2E 5C 61 5C 66 69 6C  65                       ..\a\file

After ziglang#19136 dir.symlink changes path separtors to \ on windows.
When this test is enabled something like:
`zig build test docs --zig-lib-dir .\lib\ -Dstatic-llvm -Dskip-non-native -Denable-symlinks-windows`
never finishes.

Those are failed runs from ci:
https://github.com/ziglang/zig/actions/runs/8137710393
https://github.com/ziglang/zig/actions/runs/8129619923
https://github.com/ziglang/zig/actions/runs/8125845128

Isolating that test and running it is not a problem. Running something
like `zig test .\lib\std\std.zig  --zig-lib-dir .\lib\` is fine.
Problem was manifested only on windows with target `-target
aarch64-windows-gnu`.

I was creating new files but not closing any of them. Tmp dir cleanup
hangs looping in deleteTree forever.
Make std.tar look better in docs. Remove from public interface what is
not necessary. Add comment to the public methods. Add doctest as usage
examples for iterator and pipeToFileSystem.
It is no longer need to call skip if file content is not consumed. It is
handled internally now. File types are now same as in os.File.
@ianic ianic force-pushed the tar_max_file_size branch from ede03b5 to c974e19 Compare March 11, 2024 12:39
@ianic ianic requested a review from andrewrk March 11, 2024 18:34
@andrewrk
Copy link
Member

Thanks for the docs fixes!

@andrewrk andrewrk merged commit f60c24c into ziglang:master Mar 12, 2024
@ianic ianic deleted the tar_max_file_size branch April 3, 2024 20:18
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.

2 participants