Top Level Namespace

Defined Under Namespace

Classes: DistributedStorage, LocalStorage, Storage, Torrent

Constant Summary collapse

IB_DIR =
'.ib'
TORRENT_PIECE_SIZE =
2 ** 18
IB_DIR_GAP =
"#{IB_DIR}/gap"
IB_DIR_TORRENTS =
"#{IB_DIR}/torrents/#{name}"
IB_DIR_PIECES =
"#{IB_DIR}/pieces"

Instance Method Summary collapse

Instance Method Details

#get_last_torrentObject



24
25
26
27
28
29
# File 'bin/ib', line 24

def get_last_torrent
  return unless File.directory? IB_DIR_TORRENTS
  torrent = Dir.entries(IB_DIR_TORRENTS).sort_by{|dir| dir.split.first.to_i}
  return if torrent.size == 2 # '.', '..'
  File.join(IB_DIR_TORRENTS, torrent.last)
end

#load_torrent(tname) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/immutablebox.rb', line 65

def load_torrent(tname)
  modifiedfiles = []
  torrent = Torrent.new(File.read(tname))
  info = torrent.info
  pieces = info.pieces.clone
  piece_length = info.piece_length

  files = []
  piece = nil
  info.files.each do |file|
    file_size = file['length']
    fd = if file['path'][0] != IB_DIR
      filename = file['path'].join('/')
      files << filename
      if File.exist?(filename)
        File.open(filename, 'rb')
      else
        StringIO.new(' ' * file_size)
      end
    end
    begin
      loop do
        if fd
          if piece.nil?
            piece = fd.read(piece_length)
            unless piece
              files = []
              break
            end
          else
            piece += fd.read(piece_length - piece.size)
          end
        else
          unless piece
            files = []
            break
          end
          piece += "\000" * file_size
        end
        break if piece.size < piece_length
        piece_hash = Digest::SHA1.digest(piece)
        if pieces.shift == piece_hash # good piece
          yield(piece_hash, piece)
        else
          modifiedfiles += files
        end
        piece = nil
      end
    ensure
      fd.close if fd
    end
  end

  if piece
    piece_hash = Digest::SHA1.digest(piece)
    if pieces.shift == piece_hash # good piece
      yield(piece_hash, piece)
    else
      modifiedfiles += files
    end
  end
  modifiedfiles.uniq
end

#make_torrent(name, path, tracker, priv, gap = false) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/immutablebox.rb', line 183

def make_torrent(name, path, tracker, priv, gap = false)
  torrent_pieces = []
  piece = ''
  gapn = 0
  files = []
  walk(path) do |file|
    next if file.index("./#{IB_DIR}/") == 0
    fileinfo = { 'path' => split_path(file) }
    files << fileinfo
    filesize = 0
    File.open(file, 'rb') do |fd|
      loop do
        data = fd.read(TORRENT_PIECE_SIZE - piece.size)
        break if data.nil?
        piece << data
        if piece.size == TORRENT_PIECE_SIZE
          torrent_pieces << Digest::SHA1.digest(piece)
          filesize += piece.size
          piece = ''
        end
      end
    end
    if piece.size > 0
      filesize += piece.size
      fileinfo['length'] = filesize
      gapsize = TORRENT_PIECE_SIZE - piece.size
      gapfile = "#{IB_DIR}/gap/#{gapn}"
      gapimage = "\000" * gapsize
      fileinfo = { 'length' => gapsize, 'path' => gapfile.split('/') }
      files << fileinfo
      gapn += 1
      piece << gapimage
      torrent_pieces << Digest::SHA1.digest(piece)
      piece = ''
      if gap
        File.open(gapfile, 'wb'){|fd| fd.write(gapimage)}
      end
    else
      fileinfo['length'] = filesize
    end
  end

  torrent = {
    'announce' => tracker,
    'created by' => 'statictorrent 0.0.0',
    'creation date' => Time.now.to_i,
    'info' => {
      'length' => TORRENT_PIECE_SIZE * torrent_pieces.size,
      'name' => name,
      'private' => priv ? 1 : 0,
      'pieces' => torrent_pieces.join,
      'piece length' => TORRENT_PIECE_SIZE,
      'files' => files,
    }
  }
  BEncode.dump(torrent)
end

#save_torrent(tname, storage, files) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/immutablebox.rb', line 129

def save_torrent(tname, storage, files)
  torrent = Torrent.new(File.read(tname))
  info = torrent.info
  pieces = info.pieces.clone
  piece_length = info.piece_length
  info.files.each do |file|
    next if file['path'][0] == IB_DIR
    file_size = file['length']
    filename = file['path'].join('/')
    if files.include?(filename)
      File.open(filename, 'wb') do |fd|
        n = file_size / piece_length
        mod = file_size % piece_length
        n.times do |i|
          piece_hash = pieces.shift
          piece = storage.get(piece_hash)
          fd.write(piece)
        end
        if mod > 0
          piece_hash = pieces.shift
          piece = storage.get(piece_hash)
          fd.write(piece[0, mod])
        end
      end
    else
      piece_hash = pieces.shift((file_size + piece_length - 1) / piece_length)
    end
  end
end

#split_path(path) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/immutablebox.rb', line 170

def split_path(path)
  d = path
  rv = []
  loop do
    d, f = File.split(d)
    rv.insert(0, f)
    break if d == '.'
  end
  rv
end

#walk(path, &block) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/immutablebox.rb', line 159

def walk(path, &block)
  Dir.entries(path).select{|d| !(/\A\.+\z/.match d)}.each do |e|
    file = File.join(path, e)
    if File.directory?(file)
      walk(file, &block)
    else
      yield file
    end
  end
end