Class: Archiverb::File

Inherits:
Object
  • Object
show all
Defined in:
lib/archiverb/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, io, buff = io, stat = nil, &blk) ⇒ File

Returns a new instance of File.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/archiverb/file.rb', line 27

def initialize(name, io, buff=io, stat=nil, &blk)
  buff,stat = stat, buff if buff.is_a?(Stat) || buff.is_a?(::File::Stat)
  stat = Stat.new(io) if stat.nil?

  @name  = ::File.basename(name)
  @dir   = ::File.dirname(name)
  @path  = name
  @mtime = stat.mtime.is_a?(Fixnum) ? Time.at(stat.mtime) : stat.mtime
  @uid   = stat.uid
  @gid   = stat.gid
  @mode  = stat.mode
  @size = stat.size

  @readbuff = buff
  @readback = blk unless blk.nil?
  @io       = io.is_a?(String) ? StringIO.new(io) : io
  @io.binmode
  if @io.respond_to?(:path) && ::File.directory?(@io.path)
    @size     = 0
  end
  @stat     = stat
end

Instance Attribute Details

#dirObject (readonly)

the directory path leading to the file



8
9
10
# File 'lib/archiverb/file.rb', line 8

def dir
  @dir
end

#gidObject (readonly)

The user id and group id of the file. Set #stat.uname and #stat.gname to force ownership by name rather than id.



15
16
17
# File 'lib/archiverb/file.rb', line 15

def gid
  @gid
end

#ioObject (readonly)

the raw io object, you can add to it prior to calling read



23
24
25
# File 'lib/archiverb/file.rb', line 23

def io
  @io
end

#modeObject

octal mode



12
13
14
# File 'lib/archiverb/file.rb', line 12

def mode
  @mode
end

#mtimeTime (readonly)

Returns modification time.

Returns:

  • (Time)

    modification time



17
18
19
# File 'lib/archiverb/file.rb', line 17

def mtime
  @mtime
end

#nameObject (readonly)

the basename of the file



6
7
8
# File 'lib/archiverb/file.rb', line 6

def name
  @name
end

#pathObject (readonly)

the path and name of the file



10
11
12
# File 'lib/archiverb/file.rb', line 10

def path
  @path
end

#sizeObject (readonly) Also known as: bytes

Returns the value of attribute size.



19
20
21
# File 'lib/archiverb/file.rb', line 19

def size
  @size
end

#statObject (readonly)

Archiverb::Stat


25
26
27
# File 'lib/archiverb/file.rb', line 25

def stat
  @stat
end

#uidObject (readonly)

The user id and group id of the file. Set #stat.uname and #stat.gname to force ownership by name rather than id.



15
16
17
# File 'lib/archiverb/file.rb', line 15

def uid
  @uid
end

Instance Method Details

#closeObject

Prevents future access to the contents of the file and hopefully frees up memory.



71
72
73
# File 'lib/archiverb/file.rb', line 71

def close
  @raw = nil
end

#readObject

initialize(io, stat)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/archiverb/file.rb', line 50

def read
  return @raw if @raw

  if @readback && @readbuff
    @readback.call(@readbuff)
    @readbuff.close_write
  end

  @io.rewind unless @stat.pipe?

  if @io.respond_to?(:path) && ::File.directory?(@io.path)
    @raw  = ""
  else
    @raw    = @io.read
    @size  = @raw.length
  end
  @io.close
  @raw
end