Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/lnbackup/util.rb

Constant Summary collapse

TOO_BIG =

2MB

1024 * 1024 * 2

Class Method Summary collapse

Class Method Details

.syscopy2(from, to) ⇒ Object

a variant of syscopy, that propagates exception Errno::ENOSPC and handles some extra error states



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lnbackup/util.rb', line 8

def File.syscopy2 from, to
  fsize = size(from)
  fsize = 1024 if fsize < 512
  fsize = TOO_BIG if fsize > TOO_BIG
  fmode = stat(from).mode
  tpath = to
  not_exist = !exist?(tpath)
  begin
    from = open(from, "r")
    from.binmode
    to = open(to, "w")
    to.binmode

    while true
      r = from.sysread(fsize)
      rsize = r.size
      w = 0
      while w < rsize
        t = to.syswrite(r[w, rsize - w])
        w += t
      end
    end
  rescue EOFError
    ret = true
  rescue Errno::ENOSPC
    raise
  rescue
    ret = false
  ensure
    begin; to.close; rescue; end
    begin; from.close; rescue; end
  end
  chmod(fmode, tpath) if not_exist
  ret
end