Class: Distillery::ROM::Path::File

Inherits:
Distillery::ROM::Path show all
Defined in:
lib/distillery/rom/path/file.rb

Overview

Path from a file

Instance Method Summary collapse

Constructor Details

#initialize(entry, basedir = nil) ⇒ File

Returns a new instance of File.

Parameters:

  • entry (String)

    path to file in basedir

  • basedir (String, nil) (defaults to: nil)

    base directory



14
15
16
17
18
19
20
21
# File 'lib/distillery/rom/path/file.rb', line 14

def initialize(entry, basedir=nil)
    if entry.start_with?('/')
        raise ArgumentError, "entry must be relative to basedir"
    end
        
    @entry   = entry
    @basedir = basedir || '.'
end

Instance Method Details

#basenameString

Get path basename

Returns:

  • (String)


47
48
49
# File 'lib/distillery/rom/path/file.rb', line 47

def basename
    ::File.basename(@entry)
end

#copy(to, length = nil, offset = 0, force: false, link: :hard) ⇒ Boolean

Copy ROM content to the filesystem, possibly using link if requested.

Parameters:

  • to (String)

    file destination

  • length (Integer, nil) (defaults to: nil)

    data length to be copied

  • part (:all, :header, :rom)

    which part of the rom file to copy

  • link (:hard, :sym, nil) (defaults to: :hard)

    use link instead of copy if possible

Returns:

  • (Boolean)

    status of the operation



57
58
59
60
61
62
# File 'lib/distillery/rom/path/file.rb', line 57

def copy(to, length = nil, offset = 0, force: false, link: :hard)
    (!force && length.nil? && offset.zero? &&
     ::File.exists?(to) && self.same?(ROM.from_file(to))) ||
        ROM.filecopy(self.file, to, length, offset,
                     force: force, link: link)
end

#delete!Boolean

Delete physical content.

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/distillery/rom/path/file.rb', line 91

def delete!
    ::File.unlink(self.file) == 1
rescue SystemCallError
    false
end

#entryString

Entry

Returns:

  • (String)


42
43
44
# File 'lib/distillery/rom/path/file.rb', line 42

def entry
    @entry
end

#fileString

File directly accessible on the file system

Returns:

  • (String)


29
30
31
32
33
34
# File 'lib/distillery/rom/path/file.rb', line 29

def file
    if @basedir == '.'
    then @entry
    else ::File.join(@basedir, @entry)
    end
end

#reader {|io| ... } ⇒ Object

Note:

Can be costly, prefer existing #copy if possible

ROM reader

Yield Parameters:

  • io (#read)

    stream for reading

Returns:

  • block value



52
53
54
# File 'lib/distillery/rom/path/file.rb', line 52

def reader(&block)
    ::File.open(self.file, ::File::RDONLY, binmode: true, &block)
end

#rename(path, force: false) {|old, new| ... } ⇒ Boolean

Note:

Renaming could lead to silent removing if same ROM is on its way

Rename ROM and physical content.

Parameters:

  • path (String)

    new ROM path

  • force (Boolean) (defaults to: false)

    remove previous file if necessary

Yields:

  • Rename operation (optional)

Yield Parameters:

  • old (String)

    old entry name

  • new (String)

    new entry name

Returns:

  • (Boolean)

    status of the operation



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/distillery/rom/path/file.rb', line 65

def rename(path, force: false)
    case path
    when String
    else raise ArgumentError, "unsupport path type (#{path.class})"
    end


    file = if path.start_with?('/')
           then path
           else ::File.join(@basedir, path)
           end

    if !::File.exists?(file)
        ::File.rename(self.file, file) == 0
    elsif self.same?(ROM.from_file(file))
        ::File.unlink(self.file) == 1
    elsif force
        ::File.rename(self.file, file) == 0
    else
        false
    end
rescue SystemCallError
    false
end

#storageString

File or directory that is considered the storage space for entries

Returns:

  • (String)


37
38
39
# File 'lib/distillery/rom/path/file.rb', line 37

def storage
    @basedir
end

#to_sString

Path value as string.

Returns:

  • (String)


24
25
26
# File 'lib/distillery/rom/path/file.rb', line 24

def to_s
    self.file
end