Class: Ole::Storage::FileClass

Inherits:
Object
  • Object
show all
Defined in:
lib/ole/file_system.rb

Defined Under Namespace

Classes: Stat

Instance Method Summary collapse

Constructor Details

#initialize(ole) ⇒ FileClass

Returns a new instance of FileClass.



122
123
124
# File 'lib/ole/file_system.rb', line 122

def initialize ole
  @ole = ole
end

Instance Method Details

#directory?(path) ⇒ Boolean

Returns:



162
163
164
165
# File 'lib/ole/file_system.rb', line 162

def directory? path
  dirent = @ole.dirent_from_path path
  dirent and dirent.dir?
end

#exists?(path) ⇒ Boolean Also known as: exist?

Returns:



152
153
154
# File 'lib/ole/file_system.rb', line 152

def exists? path
  !!@ole.dirent_from_path(path)
end

#expand_path(path) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ole/file_system.rb', line 126

def expand_path path
  # get the raw stored pwd value (its blank for root)
  pwd = @ole.dir.instance_variable_get :@pwd
  # its only absolute if it starts with a '/'
  path = "#{pwd}/#{path}" unless path =~ /^\//
  # at this point its already absolute. we use File.expand_path
  # just for the .. and . handling
  # No longer use RUBY_PLATFORM =~ /win/ as it matches darwin. better way?
  if File::ALT_SEPARATOR == "\\"
    File.expand_path(path)[2..-1]
  else
    File.expand_path path
  end
end

#file?(path) ⇒ Boolean

Returns:



157
158
159
160
# File 'lib/ole/file_system.rb', line 157

def file? path
  dirent = @ole.dirent_from_path path
  dirent and dirent.file?
end

#new(path, mode = 'r') ⇒ Object

explicit wrapper instead of alias to inhibit block



185
186
187
# File 'lib/ole/file_system.rb', line 185

def new path, mode='r'
  open path, mode
end

#open(path, mode = 'r', &block) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ole/file_system.rb', line 167

def open path, mode='r', &block
  if IO::Mode.new(mode).create?
    begin
      dirent = dirent_from_path path
    rescue Errno::ENOENT
      # maybe instead of repeating this everywhere, i should have
      # a get_parent_dirent function.
      parent_path, basename = File.split expand_path(path)
      parent = @ole.dir.send :dirent_from_path, parent_path, path
      parent.children << dirent = Dirent.new(@ole, :type => :file, :name => basename)
    end
  else
    dirent = dirent_from_path path
  end
  dirent.open mode, &block
end

#read(path) ⇒ Object



204
205
206
# File 'lib/ole/file_system.rb', line 204

def read path
  open path, &:read
end

#rename(from_path, to_path) ⇒ Object

most of the work this function does is moving the dirent between 2 parents. the actual name changing is quite simple. File.rename can move a file into another folder, which is why i’ve done it too, though i think its not always possible…

FIXME File.rename can be used for directories too.…

Raises:



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ole/file_system.rb', line 214

def rename from_path, to_path
  # check what we want to rename from exists. do it this
  # way to allow directories.
  dirent = @ole.dirent_from_path from_path
  raise Errno::ENOENT, from_path unless dirent
  # delete what we want to rename to if necessary
  begin
    unlink to_path
  rescue Errno::ENOENT
    # we actually get here, but rcov doesn't think so
  end
  # reparent the dirent
  from_parent_path, from_basename = File.split expand_path(from_path)
  to_parent_path, to_basename = File.split expand_path(to_path)
  from_parent = @ole.dir.send :dirent_from_path, from_parent_path, from_path
  to_parent = @ole.dir.send :dirent_from_path, to_parent_path, to_path
  from_parent.children.delete dirent
  # and also change its name
  dirent.name = to_basename
  to_parent.children << dirent
  0
end

#size(path) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/ole/file_system.rb', line 189

def size path
  dirent_from_path(path).size
rescue Errno::EISDIR
  # kind of arbitrary. I'm getting 4096 from ::File, but
  # the zip tests want 0.
  0
end

#stat(path) ⇒ Object

Raises:



197
198
199
200
201
202
# File 'lib/ole/file_system.rb', line 197

def stat path
  # we do this to allow dirs.
  dirent = @ole.dirent_from_path path
  raise Errno::ENOENT, path unless dirent
  Stat.new dirent
end

crappy copy from Dir.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/ole/file_system.rb', line 238

def unlink(*paths)
  paths.each do |path|
    dirent = @ole.dirent_from_path path
    # i think we should free all of our blocks from the
    # allocation table.
    # i think if you run repack, all free blocks should get zeroed,
    # but currently the original data is there unmodified.
    open(path) { |f| f.truncate 0 }
    # remove ourself from our parent, so we won't be part of the dir
    # tree at save time.
    parent_path, basename = File.split expand_path(path)
    parent = @ole.dir.send :dirent_from_path, parent_path, path
    parent.children.delete dirent
  end
  paths.length # hmmm. as per ::File ?
end