Class: Pione::Location::LocalLocation

Inherits:
DataLocation show all
Defined in:
lib/pione/location/local-location.rb

Overview

LocalLocation represents local disk locations.

Constant Summary

Constants inherited from DataLocation

DataLocation::KNOWN_ATTRS

Instance Attribute Summary

Attributes inherited from DataLocation

#path, #uri

Attributes inherited from BasicLocation

#address

Instance Method Summary collapse

Methods inherited from DataLocation

#+, #==, #as_directory, #basename, #cached?, define, #directory_entries, #dirname, #extname, #file_entries, #hash, #inspect, #local, #local?, need_caching?, real_appendable?, set_scheme, #sha1, writable?, #write

Methods inherited from BasicLocation

#==, #hash, #inspect, location_type

Constructor Details

#initialize(uri) ⇒ LocalLocation

Returns a new instance of LocalLocation.



11
12
13
# File 'lib/pione/location/local-location.rb', line 11

def initialize(uri)
  super(uri.absolute)
end

Instance Method Details

#append(data) ⇒ Object



31
32
33
34
35
36
37
38
# 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 = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
# 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

#ctimeObject



66
67
68
# File 'lib/pione/location/local-location.rb', line 66

def ctime
  @path.exist? ? @path.ctime : (raise NotFound.new(self))
end

#deleteObject



52
53
54
55
56
57
58
59
60
# 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?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/pione/location/local-location.rb', line 143

def directory?
  @path.directory?
end

#each_entry(option = {rec: false}, &b) ⇒ Object



107
108
109
110
111
112
113
# 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}, &b) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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}) ⇒ Object



82
83
84
85
86
87
88
# 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?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/pione/location/local-location.rb', line 147

def exist?
  @path.exist?
end

#file?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/pione/location/local-location.rb', line 139

def file?
  @path.file?
end


186
187
188
189
190
191
192
# File 'lib/pione/location/local-location.rb', line 186

def link(orig)
  if orig.kind_of?(LocalLocation)
    @path.make_symlink(orig.path)
  else
    orig.copy(self)
  end
end

#mkdirObject



62
63
64
# File 'lib/pione/location/local-location.rb', line 62

def mkdir
  @path.mkpath unless exist?
end

#move(dest) ⇒ Object

Raises:



151
152
153
154
155
156
157
158
159
160
161
# 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

#mtimeObject



70
71
72
# File 'lib/pione/location/local-location.rb', line 70

def mtime
  @path.exist? ? @path.mtime : (raise NotFound.new(self))
end

#mtime=(time) ⇒ Object



74
75
76
# File 'lib/pione/location/local-location.rb', line 74

def mtime=(time)
  @path.utime(@path.atime, time)
end

#readObject



40
41
42
# File 'lib/pione/location/local-location.rb', line 40

def read
  @path.exist? ? @path.read : (raise NotFound.new(self))
end

#rebuild(path) ⇒ Object



15
16
17
18
19
# 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}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

#sizeObject



78
79
80
# File 'lib/pione/location/local-location.rb', line 78

def size
  @path.exist? ? @path.size : (raise NotFound.new(self))
end

#turn(dest) ⇒ Object



194
195
196
197
198
199
200
201
# 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) ⇒ Object



44
45
46
47
48
49
50
# 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