Class: RbRotate::File

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

Overview

Represents one log file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, directory = nil) ⇒ File

Constructor.



45
46
47
48
49
# File 'lib/rb.rotate/file.rb', line 45

def initialize(path, directory = nil)
    @directory = directory
    @path = path
    @stat = ::File.stat(@path.to_s)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



27
28
29
# File 'lib/rb.rotate/file.rb', line 27

def path
  @path
end

Instance Method Details

#archivable?Boolean

Indicates, file is suitable for immediate archiving.

Returns:

  • (Boolean)


125
126
127
# File 'lib/rb.rotate/file.rb', line 125

def archivable?
    self.too_big? or self.too_old?
end

#archive!Object

Archives file.



117
118
119
# File 'lib/rb.rotate/file.rb', line 117

def archive!
    Storage::put(self)
end

#create!Object Also known as: truncate!

 Creates new file.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rb.rotate/file.rb', line 84

def create!
    ::File.open(@path, "w").close()
    
    # Sets access rights and ownership according to 
    # stat information
    if not @stat.nil?
        ::FileUtils.chmod(@stat.mode, @path)
        ::FileUtils.chown(@stat.uid, @stat.gid, @path)
    end
    
    return @path
end

#directoryObject

Returns the file parent directory object.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rb.rotate/file.rb', line 55

def directory
    if @directory.nil?
        if not self.state.directory.nil?
            @directory = Directory::new(self.state.directory)
        else
            @directory = Configuration::find_path(::File.dirname(@path.to_s))
        end
            
        if @directory.nil?
            raise Exception::new("File from directory which isn't covered by rb.rotate found: " << @path.to_s  << ".")
        end
    end
    
    return @directory
end

#exists?Boolean

Indicates file exists.

Returns:

  • (Boolean)


168
169
170
# File 'lib/rb.rotate/file.rb', line 168

def exists?
    ::File.exists? @path.to_s
end

#remove!Object

Removes the file from medium.



75
76
77
78
# File 'lib/rb.rotate/file.rb', line 75

def remove!
    FileUtils.remove_file(@path)
    return @path
end

#rotate!Object

Rotates the file.



107
108
109
110
111
# File 'lib/rb.rotate/file.rb', line 107

def rotate!
    if self.archivable?
        self.archive!
    end
end

#stateObject

Returns state.



156
157
158
159
160
161
162
# File 'lib/rb.rotate/file.rb', line 156

def state
    if @state.nil?
        @state = State::get.file(@path)
    end
    
    return @state
end

#too_big?Boolean

Indicates, file is bigger than is allowed.

Returns:

  • (Boolean)


133
134
135
# File 'lib/rb.rotate/file.rb', line 133

def too_big?
    ::File.size(@path) > @directory.configuration[:"max size"].to_bytes 
end

#too_old?Boolean

Indicates, file is too old.

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
149
150
# File 'lib/rb.rotate/file.rb', line 141

def too_old?
    if self.state.exists?
        period = @directory.configuration[:period].to_seconds
        result = Time::at(self.state.date + period) < Time::now
    else
        result = false
    end
    
    return result
end