Class: Ole::Storage::FileClass

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

Defined Under Namespace

Classes: Stat

Instance Method Summary collapse

Constructor Details

#initialize(ole) ⇒ FileClass

Returns a new instance of FileClass.



106
107
108
# File 'lib/ole/storage/file_system.rb', line 106

def initialize ole
	@ole = ole
end

Instance Method Details

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/ole/storage/file_system.rb', line 148

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

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

Returns:

  • (Boolean)


138
139
140
# File 'lib/ole/storage/file_system.rb', line 138

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

#expand_path(path) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ole/storage/file_system.rb', line 110

def expand_path path
	# its already absolute if it starts with a '/'
	unless path =~ /^\//
		# get the raw stored pwd value (its blank for root)
		pwd = @ole.dir.instance_variable_get :@pwd
		path = "#{pwd}/#{path}"
	end
	# 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)
	else
		File.expand_path(path)[2..-1]
	end
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
# File 'lib/ole/storage/file_system.rb', line 143

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



171
172
173
# File 'lib/ole/storage/file_system.rb', line 171

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

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ole/storage/file_system.rb', line 153

def open path, mode='r', &block
	if IOMode.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 << dirent = Dirent.new(@ole, :type => :file, :name => basename)
		end
	else
		dirent = dirent_from_path path
	end
	dirent.open mode, &block
end

#read(path) ⇒ Object



197
198
199
# File 'lib/ole/storage/file_system.rb', line 197

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:

  • (Errno::ENOENT)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/ole/storage/file_system.rb', line 207

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. add 1 + 1 to
		# keep rcov happy for now... :)
		1 + 1
	end
	# reparent the dirent
	to_parent_path, to_basename = File.split expand_path(to_path)
	from_parent = dirent.parent
	to_parent = @ole.dir.send :dirent_from_path, to_parent_path, to_path
	from_parent.delete dirent, false
	# and also change its name
	dirent.name = to_basename
	to_parent << dirent
	0
end

#size(path) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/ole/storage/file_system.rb', line 175

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

#size?(path) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
# File 'lib/ole/storage/file_system.rb', line 183

def size? path
	dirent_from_path(path).size
	# any other exceptions i need to rescue?
rescue Errno::ENOENT, Errno::EISDIR
	nil
end

#stat(path) ⇒ Object

Raises:

  • (Errno::ENOENT)


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

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


231
232
233
234
235
236
237
# File 'lib/ole/storage/file_system.rb', line 231

def unlink(*paths)
	paths.each do |path|
		dirent = dirent_from_path path
		dirent.parent.delete dirent
	end
	paths.length # hmmm. as per ::File ?
end