Godzilla74
Godzilla74

Reputation: 2512

Extracting tar file with rails : Errno::ENAMETOOLONG: File name too long @ rb_sysopen

I have a tar file that has the following contents:

/results/02-12-2017_13:17:41/
├── events.log
├── network_hosts.gnmap
├── network_hosts.nmap
├── network_hosts.xml
├── report.xml
└── results.xml

In trying to extract and parse the file with Nokogiri I'm getting the following error in rails console:

Errno::ENAMETOOLONG: File name too long @ rb_sysopen

Here is my code so far:

 test = Test.find(test_id)
 gzip = Zlib::GzipReader.open(test.data.path)

 entries = {}
 tar_extract = Gem::Package::TarReader.new(gzip)
 tar_extract.rewind
 tar_extract.each do |entry|
   entries[File.basename(entry.full_name)] = entry.read
 end

 host_file = File.open(entries["network_hosts.xml"]) { |f| Nokogiri::XML(f) }

In the end, my code appears to be opening the host_file since it outputs the contents to the console, but it's not saving anything into host_file since this error is happening:

Errno::ENAMETOOLONG: File name too long @ rb_sysopen - <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nmaprun>
<?xml-stylesheet href="file:///usr/local/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
<nmaprun scanner="nmap" args="nmap -sn -oA /results/02-10-2017_18:17:34/network_hosts 10.10.10.1 10.10.10.2" start="1486768654" startstr="Fri Feb 10 18:17:34 2017" version="7.12" xmloutputversion="1.04">
..................
</nmaprun>
from (pry):102:in `initialize'

What am I doing wrong here?

Upvotes: 1

Views: 1326

Answers (1)

eiko
eiko

Reputation: 5345

File.open(entries["network_hosts.xml"]) gets the content of network_hosts.xml and tries to use it as a filename to open a file. Since you've already read the content of network_hosts.xml and saved it to entries, you can just directly convert the string to XML:

host_file = Nokogiri::XML entries['network_hosts.xml']

Upvotes: 1

Related Questions