Class: SlowFat::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/slowfat/dir.rb

Overview

Directory represents one directory on a FAT filesystem.

Defined Under Namespace

Classes: Dentry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backing:, base:, max_entries:) ⇒ Directory

Initialize a new Directory object (normally only called from Filesystem.dir)

Parameters:

  • backing (IO)

    the storage containing the filesystem (e.g. open file)

  • base (Integer)

    the location of the beginning of this directory structure within the backing device

  • max_entries (Integer)

    the maximum number of entries that can be in this directory



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/slowfat/dir.rb', line 13

def initialize(backing:, base:, max_entries:)
  @backing = backing
  @entries = []
  (0..max_entries-1).each do |idx|
    @backing.seek base+idx*32
    dir_data = @backing.read(32)
    entry = Dentry.new(dir_data)
    break if entry.type == :end_of_dentries
    @entries << entry
  end
end

Instance Attribute Details

#entriesArray<Dentry> (readonly)

Returns an array of directory entries within this directory.

Returns:

  • (Array<Dentry>)

    an array of directory entries within this directory



6
7
8
# File 'lib/slowfat/dir.rb', line 6

def entries
  @entries
end

Instance Method Details

#dir_entry(filename) ⇒ Dentry

Return a directory entry for a specific subdirectory within this directory

Parameters:

  • filename (String)

    the name of the directory to access

Returns:

  • (Dentry)

    the directory entry object matching the requested subdirectory



42
43
44
45
46
47
48
49
# File 'lib/slowfat/dir.rb', line 42

def dir_entry(filename)
  @entries.each do |dentry|
    full_dentry_filename = dentry.extension.length > 0 ? "#{dentry.filename}.#{dentry.extension}".downcase : dentry.filename.downcase
    return dentry if dentry.type == :directory and filename.downcase == full_dentry_filename
  end

  nil
end

#file_entry(filename) ⇒ Dentry

Return a directory entry for a specific file within this directory

Parameters:

  • filename (String)

    the name of the file to access

Returns:

  • (Dentry)

    the directory entry object matching the requested file



29
30
31
32
33
34
35
36
# File 'lib/slowfat/dir.rb', line 29

def file_entry(filename)
  @entries.each do |dentry|
    full_dentry_filename = dentry.extension.length > 0 ? "#{dentry.filename}.#{dentry.extension}".downcase : dentry.filename.downcase
    return dentry if dentry.type == :file and filename.downcase == full_dentry_filename
  end

  nil
end

#to_sString

Convert a Directory into a vaguely DOS-style file listing

Returns:

  • (String)

    the contents of this directory, formatted as a human-readable listing



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/slowfat/dir.rb', line 54

def to_s
  buf = ""
  @entries.each do |dentry|
    if(dentry.type == :file) then
      buf += sprintf("%-8s %-3s   %d\n", dentry.filename, dentry.extension, dentry.size)
    elsif(dentry.type == :directory)
      buf += sprintf("%-8s %-3s   <DIR>\n", dentry.filename, dentry.extension)
    end
  end

  buf
end