class RbRotate::File

Represents one log file.

Attributes

path[R]

Public Class Methods

new(path, directory = nil) click to toggle source

Constructor.

# File lib/rb.rotate/file.rb, line 45
def initialize(path, directory = nil)
    @directory = directory
    @path = path
    @stat = ::File.stat(@path.to_s)
end

Public Instance Methods

archivable?() click to toggle source

Indicates, file is suitable for immediate archiving.

# File lib/rb.rotate/file.rb, line 125
def archivable?
    self.too_big? or self.too_old?
end
archive!() click to toggle source

Archives file.

# File lib/rb.rotate/file.rb, line 117
def archive!
    Storage::put(self)
end
create!() click to toggle source

 Creates new file.

# 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
directory() click to toggle source

Returns the file parent directory object.

# 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?() click to toggle source

Indicates file exists.

# File lib/rb.rotate/file.rb, line 168
def exists?
    ::File.exists? @path.to_s
end
remove!() click to toggle source

Removes the file from medium.

# File lib/rb.rotate/file.rb, line 75
def remove!
    FileUtils.remove_file(@path)
    return @path
end
rotate!() click to toggle source

Rotates the file.

# File lib/rb.rotate/file.rb, line 107
def rotate!
    if self.archivable?
        self.archive!
    end
end
state() click to toggle source

Returns state.

# File lib/rb.rotate/file.rb, line 156
def state
    if @state.nil?
        @state = State::get.file(@path)
    end
    
    return @state
end
too_big?() click to toggle source

Indicates, file is bigger than is allowed.

# File lib/rb.rotate/file.rb, line 133
def too_big?
    ::File.size(@path) > @directory.configuration[:"max size"].to_bytes 
end
too_old?() click to toggle source

Indicates, file is too old.

# 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