Last active
August 29, 2015 14:08
-
-
Save bfb/a192bc539fa1c4bf52e7 to your computer and use it in GitHub Desktop.
Revisions
-
bfb revised this gist
Nov 7, 2014 . 1 changed file with 53 additions and 49 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,67 +1,71 @@ module GzipCat class File def initialize(file) # puts "FILE => #{file}" @filename = file begin @content = open(::File.expand_path(filename)).read cat! rescue Exception => e # File not found and other errors are reported puts e.message end end attr_accessor :filename, :content def cat! # checks if the file is compressed if ::File.extname(filename) == ".gz" unzip! else puts content end end def unzip! # creates pipe descriptors rd, wr = IO.pipe if fork # closes unused pipe side rd.close # sends file content into the pipe wr.write content wr.close # waits for child process to respect files order Process.wait else # closes unused pipe side wr.close # redirects $stdin to pipe descriptor - i.e. dup2 in clang $stdin.reopen(rd) # invokes gzip exec("gzip -d") end end end class Server def self.start(fifo_path) puts "[#{Time.now}] GzipCat server connected into #{fifo_path} fifo" # opens fifo on read mode input = open(fifo_path, 'r') # keeps the server alive loop do # puts "Wait" # gets fifo content blocking if no content has been got File.new(input.gets.chomp) end end end end # run server GzipCat::Server.start(ARGV.first) # rd, wr = IO.pipe -
bfb created this gist
Nov 7, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ class GzipCat def initialize(file) # puts "FILE => #{file}" @filename = file begin @content = open(File.expand_path(filename)).read cat! rescue Exception => e # File not found and other errors are reported puts e.message end end attr_accessor :filename, :content def cat! # checks if the file is compressed if File.extname(filename) == ".gz" unzip! else puts content end end def unzip! # creates pipe descriptors rd, wr = IO.pipe if fork # closes unused pipe side rd.close # sends file content into the pipe wr.write content wr.close # waits for child process to respect files order Process.wait else # closes unused pipe side wr.close # redirects $stdin to pipe descriptor - i.e. dup2 in clang $stdin.reopen(rd) # invokes gzip exec("gzip -d") end end end class GzipCatServer def self.start(fifo_path) puts "[#{Time.now}] GzipCat server connected into #{fifo_path} fifo" # opens fifo on read mode input = open(fifo_path, 'r') # keeps the server alive loop do # puts "Wait" # gets fifo content blocking if no content has been got GzipCat.new(input.gets.chomp) end end end # run server GzipCatServer.start(ARGV.first) # rd, wr = IO.pipe # if fork # wr.close # $stdin.reopen(rd) # exec("gzip -d") # rd.close # # wait # else # rd.close # wr.write open("./test.txt.gz").read # wr.close # end