Class: Google::Cloud::Env::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/env/file_system.rb

Overview

Access to file system contents.

This is a simple class that reads the contents of objects in the file system, caching data so that subsequent accesses do not need to reread the file system.

You can also "mock" the file system by providing a hash of overrides. If overrides are present, actual file system access is disabled; that is, overrides are "all or nothing".

This class does not provide any controls for data size. If you read a large file, its contents will stay in memory for the lifetime of the Ruby process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileSystem

Create a file system access object with no overrides.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/google/cloud/env/file_system.rb', line 41

def initialize
  @overrides = nil
  @cache = LazyDict.new do |path, binary|
    if binary
      File.binread path
    else
      File.read path
    end
  rescue IOError, SystemCallError
    nil
  end
  # This mutex protects the overrides variable. Its setting (i.e.
  # whether nil or an overrides hash) will not change within a
  # synchronize block.
  @mutex = Thread::Mutex.new
end

Instance Attribute Details

#overridesHash{String => String}?

The overrides hash, or nil if overrides are not present. The hash maps paths to contents of the file at that path.

Returns:

  • (Hash{String => String}, nil)


86
87
88
# File 'lib/google/cloud/env/file_system.rb', line 86

def overrides
  @overrides
end

Instance Method Details

#read(path, binary: false) ⇒ String?

Read the given file from the file system and return its contents.

Parameters:

  • path (String)

    The path to the file.

  • binary (boolean) (defaults to: false)

    Whether to read in binary mode. Defaults to false. This must be consistent across multiple requests for the same path; if it is not, an error will be raised.

Returns:

  • (String)

    if the file exists.

  • (nil)

    if the file does not exist.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/google/cloud/env/file_system.rb', line 68

def read path, binary: false
  result = false
  @mutex.synchronize do
    result = @overrides[path] if @overrides
  end
  result = @cache.get(path, binary) if result == false
  if result && binary != (result.encoding == Encoding::ASCII_8BIT)
    raise IOError, "binary encoding flag mismatch"
  end
  result
end

#with_overrides(temp_overrides) ⇒ Object

Run the given block with the overrides replaced with the given hash (or nil to disable overrides in the block). The original overrides setting is restored at the end of the block. This is used for debugging/testing/mocking.

Parameters:

  • temp_overrides (nil, Hash{String => String})


109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/google/cloud/env/file_system.rb', line 109

def with_overrides temp_overrides
  old_overrides = @overrides
  begin
    @mutex.synchronize do
      @overrides = temp_overrides
    end
    yield
  ensure
    @mutex.synchronize do
      @overrides = old_overrides
    end
  end
end