Class: NSWTopo::Archive

Inherits:
Object
  • Object
show all
Extended by:
Safely
Includes:
Enumerable
Defined in:
lib/nswtopo/archive.rb

Constant Summary collapse

Invalid =
Class.new RuntimeError

Constants included from Log

Log::FAILURE, Log::NEUTRAL, Log::SUCCESS, Log::UPDATE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Safely

safely

Methods included from Log

#log_abort, #log_neutral, #log_success, #log_update, #log_warn

Constructor Details

#initialize(tar) ⇒ Archive

Returns a new instance of Archive.



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

def initialize(tar)
  @tar, @entries = tar, Hash[]
end

Class Method Details

.open(out_path: nil, in_path: nil, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nswtopo/archive.rb', line 62

def self.open(out_path: nil, in_path: nil, &block)
  buffer, reader = StringIO.new, in_path ? Zlib::GzipReader : StringIO

  reader.open(*in_path) do |input|
    begin
      version = Version[input.comment]
      raise "map file too old: created with nswtopo %s, minimum %s required" % [version, MIN_VERSION] unless version >= MIN_VERSION
      raise "nswtopo too old: map file created with nswtopo %s, this version %s" % [version, VERSION] unless version <= VERSION
    rescue Version::Error
      raise "unrecognised map file: %s" % in_path
    end if in_path && false != Config["versioning"]
    Gem::Package::TarReader.new(input) do |tar|
      archive = new(tar).tap(&block)
      archive.each do |entry|
        buffer.write entry.header
        buffer.write entry.read
        buffer.write ?\0 while buffer.pos % 512 > 0
      end if archive.changed?
    end
  end

  Dir.mktmppath do |temp_dir|
    log_update "nswtopo: saving map..."
    temp_path = temp_dir / "temp.tgz"
    Zlib::GzipWriter.open temp_path, Config["zlib-level"] || Zlib::BEST_SPEED do |gzip|
      gzip.comment = VERSION.creator_string
      gzip.write buffer.string
    rescue Interrupt
      log_update "nswtopo: interrupted, please wait..."
      raise
    end
    safely "saving map file, please wait..." do
      FileUtils.cp temp_path, out_path
    rescue SystemCallError
      raise "couldn't save #{out_path}"
    end
    log_success "map saved"
  end if out_path && buffer.size.nonzero?

rescue Zlib::GzipFile::Error
  raise Invalid
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/nswtopo/archive.rb', line 58

def changed?
  @entries.any?
end

#delete(filename) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/nswtopo/archive.rb', line 28

def delete(filename)
  find do |entry|
    entry.full_name == filename
  end&.tap do
    @entries[filename] = nil
  end
end

#each(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/nswtopo/archive.rb', line 18

def each(&block)
  @tar.rewind
  @tar.each do |entry|
    yield entry unless @entries.key? entry.full_name
  end
  @entries.each do |filename, entry|
    yield entry.tap(&:rewind) if entry
  end
end

#mtime(filename) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/nswtopo/archive.rb', line 42

def mtime(filename)
  find do |entry|
    entry.full_name == filename
  end&.then do |entry|
    Time.at entry.header.mtime
  end
end

#read(filename) ⇒ Object



36
37
38
39
40
# File 'lib/nswtopo/archive.rb', line 36

def read(filename)
  find do |entry|
    entry.full_name == filename
  end&.read
end

#uptodate?(depender, *dependees) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/nswtopo/archive.rb', line 50

def uptodate?(depender, *dependees)
  return unless mtime(depender)
  dependees.all? do |dependee|
    mtimes = [depender, dependee].map(&method(:mtime))
    mtimes.all? && mtimes.inject(&:>=)
  end
end

#write(filename, content) ⇒ Object



12
13
14
15
16
# File 'lib/nswtopo/archive.rb', line 12

def write(filename, content)
  io = StringIO.new content
  header = Gem::Package::TarHeader.new name: filename, size: io.size, prefix: "", mode: 0o0644, mtime: Time.now
  @entries[filename] = Gem::Package::TarReader::Entry.new header, io
end