diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-26 14:09:55 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-27 13:28:33 -0300 |
commit | 58bc0833b0198306f62078a5d9e67c9f1e11d476 (patch) | |
tree | 33956cd012f8c3714ea367b6070300cedb3ea90c | |
parent | d3a523a74080ac7b778c36908356e5f6c1f31b8a (diff) | |
download | luarocks-58bc0833b0198306f62078a5d9e67c9f1e11d476.tar.gz luarocks-58bc0833b0198306f62078a5d9e67c9f1e11d476.tar.bz2 luarocks-58bc0833b0198306f62078a5d9e67c9f1e11d476.zip |
perf: speed up untar operation
-rw-r--r-- | src/luarocks/tools/tar.lua | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/luarocks/tools/tar.lua b/src/luarocks/tools/tar.lua index 21d5920b..bac7b2a9 100644 --- a/src/luarocks/tools/tar.lua +++ b/src/luarocks/tools/tar.lua | |||
@@ -42,6 +42,11 @@ end | |||
42 | 42 | ||
43 | local function checksum_header(block) | 43 | local function checksum_header(block) |
44 | local sum = 256 | 44 | local sum = 256 |
45 | |||
46 | if block:byte(1) == 0 then | ||
47 | return 0 | ||
48 | end | ||
49 | |||
45 | for i = 1,148 do | 50 | for i = 1,148 do |
46 | local b = block:byte(i) or 0 | 51 | local b = block:byte(i) or 0 |
47 | sum = sum + b | 52 | sum = sum + b |
@@ -50,6 +55,7 @@ local function checksum_header(block) | |||
50 | local b = block:byte(i) or 0 | 55 | local b = block:byte(i) or 0 |
51 | sum = sum + b | 56 | sum = sum + b |
52 | end | 57 | end |
58 | |||
53 | return sum | 59 | return sum |
54 | end | 60 | end |
55 | 61 | ||
@@ -75,14 +81,17 @@ local function read_header_block(block) | |||
75 | header.devmajor = octal_to_number(nullterm(block:sub(330,337))) | 81 | header.devmajor = octal_to_number(nullterm(block:sub(330,337))) |
76 | header.devminor = octal_to_number(nullterm(block:sub(338,345))) | 82 | header.devminor = octal_to_number(nullterm(block:sub(338,345))) |
77 | header.prefix = block:sub(346,500) | 83 | header.prefix = block:sub(346,500) |
84 | |||
78 | -- if header.magic ~= "ustar " and header.magic ~= "ustar\0" then | 85 | -- if header.magic ~= "ustar " and header.magic ~= "ustar\0" then |
79 | -- return false, ("Invalid header magic %6x"):format(bestring_to_number(header.magic)) | 86 | -- return false, ("Invalid header magic %6x"):format(bestring_to_number(header.magic)) |
80 | -- end | 87 | -- end |
81 | -- if header.version ~= "00" and header.version ~= " \0" then | 88 | -- if header.version ~= "00" and header.version ~= " \0" then |
82 | -- return false, "Unknown version "..header.version | 89 | -- return false, "Unknown version "..header.version |
83 | -- end | 90 | -- end |
84 | if checksum_header(block) ~= header.chksum then | 91 | if header.typeflag == "unknown" then |
85 | return false, "Failed header checksum" | 92 | if checksum_header(block) ~= header.chksum then |
93 | return false, "Failed header checksum" | ||
94 | end | ||
86 | end | 95 | end |
87 | return header | 96 | return header |
88 | end | 97 | end |
@@ -101,12 +110,13 @@ function tar.untar(filename, destdir) | |||
101 | local block | 110 | local block |
102 | repeat | 111 | repeat |
103 | block = tar_handle:read(blocksize) | 112 | block = tar_handle:read(blocksize) |
104 | until (not block) or checksum_header(block) > 256 | 113 | until (not block) or block:byte(1) > 0 |
105 | if not block then break end | 114 | if not block then break end |
106 | if #block < blocksize then | 115 | if #block < blocksize then |
107 | ok, err = nil, "Invalid block size -- corrupted file?" | 116 | ok, err = nil, "Invalid block size -- corrupted file?" |
108 | break | 117 | break |
109 | end | 118 | end |
119 | |||
110 | local header | 120 | local header |
111 | header, err = read_header_block(block) | 121 | header, err = read_header_block(block) |
112 | if not header then | 122 | if not header then |
@@ -114,7 +124,14 @@ function tar.untar(filename, destdir) | |||
114 | break | 124 | break |
115 | end | 125 | end |
116 | 126 | ||
117 | local file_data = tar_handle:read(math.ceil(header.size / blocksize) * blocksize):sub(1,header.size) | 127 | local file_data = "" |
128 | if header.size > 0 then | ||
129 | local nread = math.ceil(header.size / blocksize) * blocksize | ||
130 | file_data = tar_handle:read(header.size) | ||
131 | if nread > header.size then | ||
132 | tar_handle:seek("cur", nread - header.size) | ||
133 | end | ||
134 | end | ||
118 | 135 | ||
119 | if header.typeflag == "long name" then | 136 | if header.typeflag == "long name" then |
120 | long_name = nullterm(file_data) | 137 | long_name = nullterm(file_data) |