Class: AppleDos

Inherits:
FileSystem show all
Defined in:
lib/file_systems/AppleDos.rb

Overview

10-FF Up to 120 more track and sector pairs

Class Method Summary collapse

Methods inherited from FileSystem

all_file_systems, code_for_tests, compatability_score, is_valid_file_system_if, matching_score, non_matching_score

Methods included from SubclassTracking

extended

Class Method Details

.add_file(file_system_image, native_filetype_class, filename, file_contents, file_type = nil, aux_code = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/file_systems/AppleDos.rb', line 173

def self.add_file(file_system_image,native_filetype_class,filename,file_contents,file_type=nil,aux_code=nil)
  #make sure the file type is supported on DOS 3.3!
  raise "#{native_filetype_class} not supported on Apple DOS 3.3 file system" if  native_filetype_class.file_system_file_types[self].nil?
  expected_file_type=native_filetype_class.file_system_file_types[self] 
  file_type=expected_file_type if file_type.nil?
  raise "inconsistent file_type (should be #{expected_file_type} but was #{file_type})" unless expected_file_type==file_type
  native_file=native_filetype_class.new(file_system_image,filename,file_contents,file_type,aux_code)
  raise "couldn't make #{native_filetype_class}" if native_file.nil?
  #if this file exists, delete it first
  file_system_image.delete_file(filename) unless file_system_image.files[filename].nil?
  catalog_slot=find_catalog_slot(file_system_image,nil)
  raise "CATALOG IS FULL!" if catalog_slot.nil?
  raise "invalid catalog slot " if catalog_slot[2].nil?
  free_sectors=free_sector_list(file_system_image)
  sectors_needed=1+(255+file_contents.length)/256
  raise "not enough free space - #{sectors_needed} sectors needed, #{free_sectors.length} available " unless sectors_needed<=free_sectors.length
  #TODO - allow files of more than 122 sectors
  raise "only files up to 122 sectors currently supported " if sectors_needed>122
  #TRACK/SECTOR LIST FORMAT (from Beneath Apple DOS p 4-6)
  # 00	Not used
  # 01	Track number of next T/S list of one is needed or zero if no more t/s list
  # 02 	Sector number of next T/S list (if one is present)
  # 03-04	Not used
  # 05-06	Sector offset in file of the first sector described by this list
  # 07-oB	Not used
  # 0C-0D	Track and sector of first data sector or zeros
  # 0E-0F	Track and sector of second data sector or zeros
  # 10-FF	Up to 120 more track and sector pairs
  track_sector_list="\0"*256
  track_sector_list_sector=free_sectors[0]
    (0..sectors_needed-2).each do |sector_in_file|
    sector_to_use=free_sectors[sector_in_file+1]
    track_sector_list[(sector_in_file*2)+0x0C]=sector_to_use[0]
    track_sector_list[(sector_in_file*2)+0X0D]=sector_to_use[1]
    sector_contents=file_contents[(sector_in_file*256),256] || ""
    self.set_sector(file_system_image,sector_to_use[0],sector_to_use[1],sector_contents)
  end
  #write the track/sector list
  self.set_sector(file_system_image,track_sector_list_sector[0],track_sector_list_sector[1],track_sector_list)

  #update the catalog file descriptive entry
  #FILE DESCRIPTIVE ENTRY (from Beneath Apple DOS p 4-6)
  # 00    Track of first track/sector list sector, if this is a deleted file this contains FF
  #	and the original track number is copied to the last byte of the file name (BYTE 20)
  #	If this byte contains a 00, the entry is assumed to never have been used and is
  #	available for use. (This means track 0 can never be used for data even if the DOS image
  #	is 'wiped' from the disk)
  #
  # 01    Sector of first track/sector list sector
  # 02    File type and flags:
  #	80+file type - file is locked
  #	00+file type - file is not locked
  #
  #	00 - TEXT file
  #	01 - INTEGER BASIC file
  #	02 - APPLESOFT BASIC file
  #	04 - BINARY file
  #	08 - S type file
  #	10 - RELOCATABLE object module file
  #	20 - a type file
  #	40 - b type file
  #
  # 03-20 File Name (30 characters)
  # 21-22 Length of file in sectors (LO/HI format)

  catalog_sector=file_system_image.get_sector(catalog_slot[0],catalog_slot[1])
  raise "invalid catalog track #{catalog_slot[0]},sector #{catalog_slot[1]}" if catalog_sector.nil?
  file_descriptive_entry="\0"*0x23
  file_descriptive_entry[0]=track_sector_list_sector[0]
  file_descriptive_entry[1]=track_sector_list_sector[1]
  file_descriptive_entry[2]=file_type
  file_descriptive_entry[3..0x20]=catalog_filename(filename)
  file_descriptive_entry[0x21]=(sectors_needed-1)%256
  file_descriptive_entry[0x22]=(sectors_needed-1)/256

  catalog_sector[catalog_slot[2],0x23]=file_descriptive_entry
  
  self.set_sector(file_system_image,catalog_slot[0],catalog_slot[1],catalog_sector)
  
  raise "catalog not updated correctly!" if find_catalog_slot(file_system_image,filename).nil?
  raise "error: file should now be in catalog" if files(file_system_image)[filename].nil?
  return native_file
end

.catalog_filename(filename) ⇒ Object

render a filename in form suitable for inclusion in a DOS catalog



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/file_systems/AppleDos.rb', line 258

def self.catalog_filename(filename)
    s=""
    for i in 0..29
      c=(filename[i])
      if c.nil? then
        c=0xA0        
        else 
        c=(c|0x80)
      end
      s+=c.chr
    end
    s
end

.delete_file(file_system_image, filename) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/file_systems/AppleDos.rb', line 349

def self.delete_file(file_system_image,filename)
  this_files_catalog_slot=find_catalog_slot(file_system_image,filename)    
  #if file not in catalog, do nothing
  return if this_files_catalog_slot.nil? 
  file_descriptive_entry=file_system_image.get_sector(this_files_catalog_slot[0],this_files_catalog_slot[1])[this_files_catalog_slot[2],0x23]
  
  #mark sector as free in sector usage list
  sector_usage_bitmap_sector=file_system_image.get_sector(vtoc_track_no,vtoc_sector_no)
  sectors_to_mark_available=get_track_sector_list(file_system_image,file_descriptive_entry[0x00],file_descriptive_entry[0x01])
  sectors_to_mark_available<<[file_descriptive_entry[0x01],file_descriptive_entry[0x00]]
  
  sectors_to_mark_available.each do |ts|
    offset_of_byte_containing_this_sector=0x38+(ts[0]*4)
    if ts[1]<8 then 
      offset_of_byte_containing_this_sector+=1
    end
    byte_containing_this_sector=sector_usage_bitmap_sector[offset_of_byte_containing_this_sector]   
    byte_containing_this_sector=byte_containing_this_sector|(2**(ts[1]%8))
    sector_usage_bitmap_sector[offset_of_byte_containing_this_sector]=byte_containing_this_sector
  end
  file_system_image.set_sector(vtoc_track_no,vtoc_sector_no,sector_usage_bitmap_sector)
  
  #mark slot as available in catalog
  catalog_sector=file_system_image.get_sector(this_files_catalog_slot[0],this_files_catalog_slot[1])
  catalog_sector[this_files_catalog_slot[2]+0x20]=catalog_sector[this_files_catalog_slot[2]] #save the current "first track no" in last byte of filename
  catalog_sector[this_files_catalog_slot[2]]=0xFF
  file_system_image.set_sector(this_files_catalog_slot[0],this_files_catalog_slot[1],catalog_sector)
end

.files(file_system_image) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/file_systems/AppleDos.rb', line 114

def self.files(file_system_image)
  files=FileContainer.new
  vtoc_sector=file_system_image.get_sector(vtoc_track_no,vtoc_sector_no)
  catalog_sector=file_system_image.get_sector(vtoc_sector[01],vtoc_sector[02])
  done=false
  visited_sectors={}
  while !done
    break if catalog_sector.nil?
    (0..6).each {|file_number|
      file_descriptive_entry_start=11+file_number*35
      file_descriptive_entry=catalog_sector[file_descriptive_entry_start,36]					
      break if (file_descriptive_entry[0]==0xFF) # skip deleted files
      filename=""
      file_descriptive_entry[3..32].to_s.each_byte{|b| filename+=(b.%128).chr}
      filename.gsub!(/ *$/,"") #strip off trailing spaces
      filename.tr!("\x00-\x1f","\x40-\x5f") #convert non-printable chars to corresponding uppercase letter
      locked=(file_descriptive_entry[2]>=0x80)
      sector_count=file_descriptive_entry[0x21]+file_descriptive_entry[0x22]*256
  
      file_type_code=file_descriptive_entry[2]%0x80
#      puts "filename #{filename} sector count #{sector_count}"
      contents=""
      ts_list_track_no=file_descriptive_entry[0]
      ts_list_sector_no=file_descriptive_entry[1]
      while (ts_list_track_no>0) && (ts_list_track_no<=0X22) && (ts_list_sector_no<=0x0f)
        ts_list_sector=file_system_image.get_sector(ts_list_track_no,ts_list_sector_no)
        ts_list_track_no=ts_list_sector[1]
        ts_list_sector_no=ts_list_sector[2]

        0x0C.step(0xff,2) {|i|						
          data_track_no=ts_list_sector[i]
          data_sector_no=ts_list_sector[i+1]
          if (data_track_no>0) && (data_track_no<=0X22) && (data_sector_no<=0x0f) then
            contents+=file_system_image.get_sector(data_track_no,data_sector_no)
          end
        }
        if contents.length>0 then
          files<<NativeFileType.best_fit(file_system_image,filename,contents,file_type_code)
        end
      end
    }
    next_track=catalog_sector[1]		
    next_sector=catalog_sector[2]%0x10
    if (next_track==0) &&( next_sector==0) then
      done=true
    else 
      #check we haven't got into an endless loop
      s="#{next_track}/#{next_sector}"
  #    puts s
      if (!visited_sectors[s].nil?) then
        done=true
      end
      visited_sectors[s]=true
      catalog_sector=file_system_image.get_sector(next_track,next_sector)
    end
  end
  files
end

.find_catalog_slot(file_system_image, filename) ⇒ Object

iterate through the CATALOG to find either the named file or (if nil is passed in) an empty slot



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/file_systems/AppleDos.rb', line 295

def self.find_catalog_slot(file_system_image,filename)
  vtoc_sector=file_system_image.get_sector(vtoc_track_no,vtoc_sector_no)
  catalog_filename=catalog_filename(filename.upcase) unless filename.nil?
  catalog_track_no=vtoc_sector[01]
  catalog_sector_no=vtoc_sector[02]
  
  while (catalog_track_no+catalog_sector_no>0) do
    catalog=file_system_image.get_sector(catalog_track_no,catalog_sector_no)    
    (0..7).each do |slot_no|
      slot_start=slot_no*0x23+0x0B
      if filename.nil? && (catalog[slot_start]==0x00)|| (catalog[slot_start]==0xFF) then        
        return [catalog_track_no,catalog_sector_no,slot_start]
      end
      if (!filename.nil?) && (catalog[slot_start+0x03..slot_start+0x20]==catalog_filename) then
        return [catalog_track_no,catalog_sector_no,slot_start]
      end
    end
    catalog_track_no=catalog[01]
    catalog_sector_no=catalog[02]    
  end
  nil
end

.free_sector_list(file_system_image) ⇒ Object

iterate through the sector usage bitmap, return a list of [track,sector] for sectors marked available



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/file_systems/AppleDos.rb', line 273

def self.free_sector_list(file_system_image)
  end_of_sector_usage_bitmap=(file_system_image.track_count*4+0x38)-1
  sector_usage_bitmap=file_system_image.get_sector(vtoc_track_no,vtoc_sector_no)[0x38..end_of_sector_usage_bitmap]
  free_sectors=[]
  #skip track 0 - even if sectors there are unused, we can't include them in a catalog or track/sector list
    (1..file_system_image.track_count-1).each do |track|
      track_bitmap_lo=sector_usage_bitmap[track*4+1]
      track_bitmap_hi=sector_usage_bitmap[track*4]
      (0..7).each do |sector|
        if ((track_bitmap_lo & (2**(sector)))!=0) then
          free_sectors<<[track,sector,0]
        end
        if ((track_bitmap_hi & (2**(sector)))!=0) then
          free_sectors<<[track,sector+8,0]
        end        
      end
    end
    free_sectors.sort
end

.get_track_sector_list(file_system_image, ts_list_track_no, ts_list_sector_no) ⇒ Object

given a track and sector, treat it as a track/sector list and return an array containing track/sector pairs



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/file_systems/AppleDos.rb', line 336

def self.get_track_sector_list(file_system_image,ts_list_track_no,ts_list_sector_no)
  ts_list_sector=file_system_image.get_sector(ts_list_track_no,ts_list_sector_no)
  ts_list=[]
  for entry_number in 0..121
    data_track_no=ts_list_sector[entry_number*2+0x0C]
    data_sector_no=ts_list_sector[entry_number*2+0x0D]
    if( (data_track_no!=0 || data_sector_no!=0)  && data_track_no<file_system_image.track_count && data_sector_no<=0x0f) then
      ts_list<<[data_track_no,data_sector_no]
    end
  end
  ts_list
end

.host_systemObject



88
89
90
# File 'lib/file_systems/AppleDos.rb', line 88

def self.host_system
  Apple2
end

.set_sector(file_system_image, track, sector, contents) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/file_systems/AppleDos.rb', line 318

def self.set_sector(file_system_image,track,sector,contents)
  file_system_image.set_sector(track,sector,contents)  
  
  #now mark sector as used in sector usage list  
  if ((track!=vtoc_track_no) || (sector!=vtoc_sector_no)) then #don't bother marking the VTOC sectors used
    sector_usage_bitmap_sector=file_system_image.get_sector(vtoc_track_no,vtoc_sector_no)  
    offset_of_byte_containing_this_sector=0x38+(track*4)
    if sector<8 then 
        offset_of_byte_containing_this_sector+=1
    end
    byte_containing_this_sector=sector_usage_bitmap_sector[offset_of_byte_containing_this_sector]
    byte_containing_this_sector=byte_containing_this_sector&(0xff-(2**(sector%8)))
    sector_usage_bitmap_sector[offset_of_byte_containing_this_sector]=byte_containing_this_sector
    file_system_image.set_sector(vtoc_track_no,vtoc_sector_no,sector_usage_bitmap_sector)
  end
end

.vtoc_sector_noObject



110
111
112
# File 'lib/file_systems/AppleDos.rb', line 110

def self.vtoc_sector_no
  0
end

.vtoc_track_noObject



106
107
108
# File 'lib/file_systems/AppleDos.rb', line 106

def self.vtoc_track_no
  0x11
end