Class: FileSystemImage

Inherits:
Object
  • Object
show all
Extended by:
SubclassTracking
Defined in:
lib/FileSystemImage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SubclassTracking

extended

Constructor Details

#initialize(file_bytes, file_system, image_format, filename = nil) ⇒ FileSystemImage

Returns a new instance of FileSystemImage.



79
80
81
82
83
84
85
# File 'lib/FileSystemImage.rb', line 79

def initialize(file_bytes,file_system,image_format,filename=nil)
  @file_bytes=file_bytes
  @file_system=file_system
  @image_format=image_format.new(file_bytes)
  @filename=filename
  @files=file_system.files(self) if compatability_score>0
end

Instance Attribute Details

#file_systemObject (readonly)

Returns the value of attribute file_system.



8
9
10
# File 'lib/FileSystemImage.rb', line 8

def file_system
  @file_system
end

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/FileSystemImage.rb', line 8

def filename
  @filename
end

#image_formatObject (readonly)

Returns the value of attribute image_format.



8
9
10
# File 'lib/FileSystemImage.rb', line 8

def image_format
  @image_format
end

#volume_nameObject

Returns the value of attribute volume_name.



9
10
11
# File 'lib/FileSystemImage.rb', line 9

def volume_name
  @volume_name
end

Class Method Details

.best_fit(file_bytes, filename = nil) ⇒ Object

given a bytestream (and, optionally, filename the bytes are from), return the class that is the best match for this class



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/FileSystemImage.rb', line 20

def FileSystemImage.best_fit(file_bytes,filename=nil)
  candidates=[]
  file_ext=File.extname(filename.sub(/\.gz$/,"")).downcase
  RipXploreLog.debug("looking for best fit for image format for #{filename} : extension #{file_ext}")
  ImageFormat.all_image_formats.each do |image_format|
    if !file_ext.nil? && !image_format.possible_extensions.include?(file_ext) then
      RipXploreLog.debug("skipping #{image_format} - unmatched extension #{file_ext}")
    else
      image_format.possible_file_systems.each do |file_system|
        RipXploreLog.debug("checking image format #{image_format} and file system #{file_system}")
        candidate=FileSystemImage.new(file_bytes,file_system,image_format,filename)
        RipXploreLog.debug("score - #{candidate.compatability_score}")
        candidates<<candidate
      end
    end    
  end
  
  
  if candidates.length==0 then
    RipXploreLog.debug( 'no valid combination of file system and image format found')
    return nil
  end
  best_candidate=candidates.sort{|a,b| b.compatability_score<=>a.compatability_score}[0]
  RipXploreLog.debug("best candidate = #{best_candidate.file_system}/#{best_candidate.image_format} score: #{best_candidate.compatability_score}")
  best_candidate
end

.is_file_system_image_filename?(filename) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/FileSystemImage.rb', line 11

def FileSystemImage.is_file_system_image_filename?(filename)
  
  extension=File.extname(File.basename(filename,".gz")).downcase
  RipXploreLog.debug("checking if #{filename} is a FileSystemImage (extension #{extension}) - #{ImageFormat.all_possible_extensions.include?(extension)}")
  RipXploreLog.debug(ImageFormat.all_possible_extensions.join(";"))
  ImageFormat.all_possible_extensions.include?(extension)
end

Instance Method Details

#add_file(native_filetype_class_symbol, filename, file_contents, file_type = nil, aux_code = nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/FileSystemImage.rb', line 189

def add_file(native_filetype_class_symbol,filename,file_contents,file_type=nil,aux_code=nil)
  raise "#{file_system} image updates not supported (yet)" unless file_system.respond_to?(:add_file)
  begin
    native_filetype_class=eval("#{native_filetype_class_symbol}")
  rescue 
    raise "#{native_filetype_class_symbol} is not a valid file type. Valid file types for #{file_system} are:\n\t"+NativeFileType.native_file_types_possible_on_file_system(file_system).join("\n\t")
  end

  file=file_system.add_file(self,native_filetype_class,filename,file_contents,file_type,aux_code)
  @files=nil #force a reread of the catalog 
  file
end

#catalogObject

produce a list of all files contained in this file system images



141
142
143
144
145
146
147
148
149
# File 'lib/FileSystemImage.rb', line 141

def catalog
  s="\n%-32s %-16s %04s   %s\n\n" % ["FILENAME","TYPE","LENGTH","LOAD ADDRESS"]
  self.files.sort.each do |file|      
    raise "bogus file '#{file.filename}'" if file.contents.nil?
    contents_length= (file.contents.respond_to?(:length) ? file.contents.length : 0)
    s<<"%-32s %-16s $%04x    $%04x\n" % [file.filename,file.type_description,contents_length,file.load_address]
  end
  s
end

#compatability_scoreObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/FileSystemImage.rb', line 48

def compatability_score
  if @compatability_score.nil? then
    image_format_compatability_score=image_format.compatability_score(self)    
    if image_format_compatability_score>0 then #if we don't have a valid image_format, don't bother checking the file_system!
      file_system_compatability_score=file_system.compatability_score(self)
      @compatability_score=file_system_compatability_score*image_format_compatability_score
    else
      @compatability_score=0
    end
  end
  @compatability_score
end

#delete_file(filename) ⇒ Object



202
203
204
205
206
# File 'lib/FileSystemImage.rb', line 202

def delete_file(filename)
  raise "#{file_system} image updates not supported (yet)" unless file_system.respond_to?(:delete_file)
  file_system.delete_file(self,filename)
  @files=nil #force a reread of the catalog 
end

#dump_sector(track, sector) ⇒ Object

return a formatted hex dump of a single 256 byte sector



119
120
121
122
123
124
125
# File 'lib/FileSystemImage.rb', line 119

def dump_sector(track,sector)
 	s=hline
	s<<sprintf("TRACK: $%02X SECTOR $%02X\n",track,sector)
	sector_data=get_sector(track,sector)
	s<< file_system.host_system.hex_dump(sector_data)
	s
end

#dump_track(track) ⇒ Object

return a formatted hex dump of all sectors in a track



128
129
130
131
132
133
134
# File 'lib/FileSystemImage.rb', line 128

def dump_track(track)
   s=""
     sectors_in_track(track).each do |sector|
			s<<dump_sector(track,sector)
		end
   s
end

#end_trackObject



108
109
110
# File 'lib/FileSystemImage.rb', line 108

def end_track
  image_format.end_track
end

#file_bytesObject



61
62
63
# File 'lib/FileSystemImage.rb', line 61

def file_bytes
    return image_format.file_bytes
end

#filesObject



88
89
90
91
# File 'lib/FileSystemImage.rb', line 88

def files
  @files=file_system.files(self) if @files.nil?
  @files.flatten
end

#fontsObject



177
178
179
# File 'lib/FileSystemImage.rb', line 177

def fonts
  files.find_all{|f| f.respond_to?(:to_font)}
end

#get_sector(track, sector) ⇒ Object



65
66
67
# File 'lib/FileSystemImage.rb', line 65

def get_sector(track,sector)
  image_format.get_sector(track,sector)
end

#host_systemObject



136
137
138
# File 'lib/FileSystemImage.rb', line 136

def host_system
  file_system.host_system
end

#listingsObject



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

def listings
  files.find_all{|f| f.respond_to?(:to_listing)}
end

#picturesObject



173
174
175
# File 'lib/FileSystemImage.rb', line 173

def pictures
  files.find_all{|f| f.respond_to?(:to_picture)}
end

#save_as(filename) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/FileSystemImage.rb', line 161

def save_as(filename)
  if !(filename=~/\.gz$/).nil? then
	require 'zlib'
    f=Zlib::GzipWriter.new(open(filename,"wb"))
  else
    f=open(filename,"wb")
  end    
  f<<file_bytes
  f.close
end

#sectors_in_track(track_no) ⇒ Object

return a list of all valid sectors in the specified track on this image



100
101
102
# File 'lib/FileSystemImage.rb', line 100

def sectors_in_track(track_no)
  image_format.sectors_in_track(track_no)
end

#set_sector(track, sector, sector_contents) ⇒ Object



69
70
71
72
# File 'lib/FileSystemImage.rb', line 69

def set_sector(track,sector,sector_contents)
  raise "#{image_format} image updates not supported (yet)" unless image_format.respond_to?(:set_sector)
  image_format.set_sector(track,sector,sector_contents)
end

#start_trackObject



104
105
106
# File 'lib/FileSystemImage.rb', line 104

def start_track
  image_format.host_system.start_track
end

#textsObject



181
182
183
# File 'lib/FileSystemImage.rb', line 181

def texts
  files.find_all{|f| f.respond_to?(:to_text)}
end

#to_hex_dumpObject



112
113
114
115
116
# File 'lib/FileSystemImage.rb', line 112

def to_hex_dump
s=""   
track_list.each {|track| s<<dump_track(track)}
s
end

#to_info_dumpObject

produce a detailed description of everything we know about this disk and the files on it



152
153
154
155
156
157
158
159
# File 'lib/FileSystemImage.rb', line 152

def to_info_dump
  s=""
  self.files.sort.each do |file|
    s<<"--------------------------------------------\n#{file.filename} (#{file.class})\n"
  	s<<file.to_info_dump
  end
  s
end

#track_countObject

how many tracks are in this particular image?



75
76
77
# File 'lib/FileSystemImage.rb', line 75

def track_count
  image_format.track_count
end

#track_listObject

return an array containing a list of numbers of valid tracks on this image



94
95
96
# File 'lib/FileSystemImage.rb', line 94

def track_list
    image_format.track_list
end