Download 16GB torrent via tablet with 4GB free space







Task:







There is a PC without the Internet but it is possible to transfer the file via USB. There is a tablet with the Internet from which this file can be transferred. On the tablet, you can download the desired torrent but not enough free space. The file in the torrent is single and large.







Way to the solution:







I started the torrent to download. When the free space almost came to an end, I paused the download. I connected the tablet to the PC and moved the file from the tablet to the PC. He paused and, to my surprise, the file was created again and the torrent continued to swing further as if nothing had happened.







Due to the fact that the torrent client sets the sparse flag to a file in which it writes the received data, the system does not immediately try to reserve 16GB and there will be no error when trying to write to a file beyond 4GB.







Repeating the procedure four times, I received four files on the PC in which different parts of the same torrent. Now it remains to assemble them together. The procedure is essentially simple. It is necessary to replace zero bytes with another value if it is in this position in one of the four files.







It seemed to me that such a simple program should be on the Internet. Really nobody faced such a task? But I realized that I don’t even know what keywords to look for her. Therefore, I quickly threw a Lua script for this task and now I have optimized it. I want to share it.







Download torrent in parts



  1. start downloading torrent on the first device
  2. wait until the ROM is filled
  3. pause the download
  4. transfer the file to the second device and add a digit to the file name
  5. go back to the first point until the file is downloaded completely


Merge parts into one file



After the last part is received, it is necessary to collect them into one whole file.







The task is simple:







  1. Read all parts at once
  2. If in some part in the position the non-zero byte is written to the output, otherwise we write zero


The merge_part



function takes an array of streams_in



streams from which it reads a part of size buffer_length



and returns the result of merging parts from different streams.







 function merge_part(streams_in, buffer_length) local out_part for _, stream in ipairs(streams_in) do local in_part = stream:read(buffer_length) if not out_part then out_part = in_part --       elseif in_part and #in_part > 0 then if #out_part < #in_part then out_part, in_part = in_part, out_part end if out_part ~= in_part --   and in_part:find("[^\0]") --    in_part and out_part:find("\0", 1, true) --     out_part then local find_index = 1 --[[
      
      





The string.gsub



function string.gsub



suitable for the task as it will find pieces filled with zeros and put what is passed to it.







 --]] out_part = out_part:gsub("\0+", function(zero_string) if #in_part < find_index then return --     end --[[
      
      





string.gsub



does not pass the position at which a match was found. Therefore, we do a parallel search for the position zero_string



using the string.find



function. It is enough to find the first zero byte.







 --]] local start_index = out_part:find("\0", find_index, true) find_index = start_index + #zero_string --[[
      
      





Now, if in_part



has data for out_part



copy it.







 --]] if #in_part >= start_index then local end_index = start_index + #zero_string - 1 --[[
      
      





We cut out the part corresponding to the sequence of zeros from in_part



.







 --]] local part = in_part:sub(start_index, end_index) if (part:byte(1) ~= 0) or part:find("[^\0]") then --[[
      
      





There is data in part



.







 --]] if #part == #zero_string then return part else --[[
      
      





part



turned out to be less than a sequence of zeros. We supplement it with them.







 --]] return part..zero_string:sub(1, end_index - #in_part) end end end end) end end end return out_part end
      
      





Conclusion



Thus, it was possible to download and assemble this file on a PC. After the merger, I pulled a torrent file from the tablet. I installed the torrent client on the PC and checked the file with it.







The last downloaded part on the tablet can be left on the distribution, but you need to include before this re-check the parts and uncheck the file so that it does not download again.







Used:







  1. Torrent client Flud on the tablet.
  2. QBittorent torrent client on PC.
  3. Lua script



All Articles