class Pione::Location::LocalLocation

LocalLocation represents local disk locations.

Public Class Methods

new(uri) click to toggle source
Calls superclass method Pione::Location::DataLocation::new
# File lib/pione/location/local-location.rb, line 11
def initialize(uri)
  super(uri.absolute)
end

Public Instance Methods

append(data) click to toggle source
# File lib/pione/location/local-location.rb, line 31
def append(data)
  if exist?
    @path.open("a"){|f| f.write(data)}
  else
    create(data)
  end
  return self
end
copy(dest, option={}) click to toggle source
# File lib/pione/location/local-location.rb, line 163
def copy(dest, option={})
  # setup options
  option[:keep_mtime] ||= true

  if dest.kind_of?(LocalLocation)
    # make parent directories
    dest.path.dirname.mkpath unless dest.path.dirname.exist?

    # copy
    IO.copy_stream(@path.open, dest.path)
  else
    dest.write(read)
  end

  # modify mtime
  begin
    dest.mtime = self.mtime if option[:keep_mtime]
  rescue NotImplementedError
    msg = "the location operation faild to keep mtime: copy from %s to %s"
    Log::SystemLog.debug(msg % [address, dest.address])
  end
end
create(data) click to toggle source
# File lib/pione/location/local-location.rb, line 21
def create(data)
  if @path.exist?
    raise ExistAlready.new(self)
  else
    @path.dirname.mkpath unless @path.dirname.exist?
    @path.open("w"){|f| f.write(data)}
  end
  return self
end
ctime() click to toggle source
# File lib/pione/location/local-location.rb, line 66
def ctime
  @path.exist? ? @path.ctime : (raise NotFound.new(self))
end
delete() click to toggle source
# File lib/pione/location/local-location.rb, line 52
def delete
  if @path.exist?
    if @path.file?
      @path.delete
    else
      FileUtils.remove_entry_secure(@path)
    end
  end
end
directory?() click to toggle source
# File lib/pione/location/local-location.rb, line 143
def directory?
  @path.directory?
end
each_entry(option={rec: false}) { |Location| ... } click to toggle source
# File lib/pione/location/local-location.rb, line 107
def each_entry(option={rec: false}, &b)
  each_rel_entry(option) do |entry|
    yield Location["local:%s" % (@path + entry).expand_path]
  end
rescue Errno::ENOENT
  raise NotFound.new(self)
end
each_rel_entry(option={rec: false}) { |entry| ... } click to toggle source
# File lib/pione/location/local-location.rb, line 115
def each_rel_entry(option={rec: false}, &b)
  if block_given?
    @path.each_entry do |entry|
      # ignore current or parent directory
      next if entry.to_s == "." or entry.to_s == ".."

      # call the block
      yield entry

      # recursion mode
      entry_location = self + entry
      if option[:rec] and entry_location.directory?
        entry_location.rel_entries(option) do |subentry|
          yield File.join(entry, subentry)
        end
      end
    end
  else
    return Enumerator.new(self, :foreach)
  end
rescue Errno::ENOENT
  raise NotFound.new(self)
end
entries(option={rec: false}) click to toggle source
# File lib/pione/location/local-location.rb, line 82
def entries(option={rec: false})
  rel_entries(option).map do |entry|
    Location["local:%s" % (@path + entry).expand_path]
  end
rescue Errno::ENOENT
  raise NotFound.new(self)
end
exist?() click to toggle source
# File lib/pione/location/local-location.rb, line 147
def exist?
  @path.exist?
end
file?() click to toggle source
# File lib/pione/location/local-location.rb, line 139
def file?
  @path.file?
end
mkdir() click to toggle source
# File lib/pione/location/local-location.rb, line 62
def mkdir
  @path.mkpath unless exist?
end
move(dest) click to toggle source
# File lib/pione/location/local-location.rb, line 151
def move(dest)
  raise NotFound.new(self) unless exist?

  if dest.kind_of?(LocalLocation)
    dest.path.dirname.mkpath unless dest.path.dirname.exist?
    FileUtils.mv(@path, dest.path, force: true)
  else
    copy(dest)
    delete
  end
end
mtime() click to toggle source
# File lib/pione/location/local-location.rb, line 70
def mtime
  @path.exist? ? @path.mtime : (raise NotFound.new(self))
end
mtime=(time) click to toggle source
# File lib/pione/location/local-location.rb, line 74
def mtime=(time)
  @path.utime(@path.atime, time)
end
read() click to toggle source
# File lib/pione/location/local-location.rb, line 40
def read
  @path.exist? ? @path.read : (raise NotFound.new(self))
end
rebuild(path) click to toggle source
# File lib/pione/location/local-location.rb, line 15
def rebuild(path)
  scheme = @uri.scheme
  path = path.expand_path.to_s
  Location["%s:%s" % [scheme, path]]
end
rel_entries(option={rec: false}) click to toggle source
# File lib/pione/location/local-location.rb, line 90
def rel_entries(option={rec: false})
  list = []
  @path.entries.each do |entry|
    if not(entry.to_s == "." or entry.to_s == "..")
      list << entry
      entry_location = self + entry
      if option[:rec] and entry_location.directory?
        _list = entry_location.rel_entries(option).map {|subentry| entry + subentry}
        list = list + _list
      end
    end
  end
  return list
rescue Errno::ENOENT
  raise NotFound.new(self)
end
size() click to toggle source
# File lib/pione/location/local-location.rb, line 78
def size
  @path.exist? ? @path.size : (raise NotFound.new(self))
end
turn(dest) click to toggle source
# File lib/pione/location/local-location.rb, line 194
def turn(dest)
  if Global.file_sliding and dest.kind_of?(LocalLocation)
    move(dest)
    link(dest)
  else
    copy(dest)
  end
end
update(data) click to toggle source
# File lib/pione/location/local-location.rb, line 44
def update(data)
  if @path.exist?
    @path.open("w"){|file| file.write(data)}
  else
    raise NotFound.new(@uri)
  end
end