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

Add symlink support, closes #8 #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/archive/tar/minitar/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ def mkdir(name, opts = {})
nil
end

# Creates a symbolic link entry in the tar.
def symlink(name, link_target, opts = {})
raise ClosedStream if @closed

raise FileNameTooLong if link_target.size > 100

name, prefix = split_name(name)
header = {
:name => name,
:mode => opts[:mode],
:typeflag => "2",
:size => 0,
:linkname => link_target,
:gid => opts[:gid],
:uid => opts[:uid],
:mtime => opts[:mtime],
:prefix => prefix
}
@io.write(PosixHeader.new(header))
nil
end

# Passes the #flush method to the wrapped stream, used for buffered
# streams.
def flush
Expand Down
14 changes: 10 additions & 4 deletions test/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ def tar_dir_header(name, prefix, mode)
header("5", name, prefix, 0, mode, checksum)
end

def header(type, fname, dname, length, mode, checksum = nil)
def tar_symlink_header(name, prefix, mode, target)
h = header("2", name, prefix, 0, mode, nil, target)
checksum = calc_checksum(h)
header("2", name, prefix, 0, mode, checksum, target)
end

def header(type, fname, dname, length, mode, checksum = nil, linkname = "")
checksum ||= " " * 8
arr = [ASCIIZ(fname, 100), Z(to_oct(mode, 7)), Z(to_oct(nil, 7)),
Z(to_oct(nil, 7)), Z(to_oct(length, 11)), Z(to_oct(0, 11)),
checksum, type, "\0" * 100, "ustar\0", "00", ASCIIZ("", 32),
ASCIIZ("", 32), Z(to_oct(nil, 7)), Z(to_oct(nil, 7)),
ASCIIZ(dname, 155) ]
checksum, type, ASCIIZ(linkname, 100), "ustar\0", "00",
ASCIIZ("", 32), ASCIIZ("", 32), Z(to_oct(nil, 7)),
Z(to_oct(nil, 7)), ASCIIZ(dname, 155) ]
arr = arr.join.bytes.to_a
h = arr.pack("C100C8C8C8C12C12C8CC100C6C2C32C32C8C8C155")
ret = h + "\0" * (512 - h.size)
Expand Down
16 changes: 16 additions & 0 deletions test/test_tar_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def test_write_operations_fail_after_closed
assert_raises(Minitar::ClosedStream) { @os.flush }
assert_raises(Minitar::ClosedStream) { @os.add_file("dfdsf", :mode => 0644) {} }
assert_raises(Minitar::ClosedStream) { @os.mkdir "sdfdsf", :mode => 0644 }
assert_raises(Minitar::ClosedStream) { @os.symlink "a", "b", :mode => 0644 }
end

def test_file_name_is_split_correctly
Expand Down Expand Up @@ -169,4 +170,19 @@ def test_file_size_is_checked
end
@os.add_file_simple("lib/foo/bar", :mode => 0644, :size => 10) {|f| }
end

def test_symlink
@dummyos.reset
@os.symlink("lib/foo/bar", "lib/foo/baz", :mode => 0644)
@os.flush
assert_headers_equal(tar_dir_header("lib/foo", "", 0644),
@dummyos.data[0, 512])
end

def test_symlink_target_size_is_checked
@dummyos.reset
assert_raises(Minitar::FileNameTooLong) do
@os.symlink("lib/foo/bar", "x" * 101)
end
end
end