Class: HTAuth::File

Inherits:
Object
  • Object
show all
Defined in:
lib/htauth/file.rb

Overview

Internal: A base class for DigestFile and PasswordFile to inherit from.

This class should not be instantiated directly. You must use DigestFile or PasswordFile.

Direct Known Subclasses

DigestFile, PasswdFile

Constant Summary collapse

ALTER =

Public: The mode to pass to #open for updating a file

"alter"
CREATE =

Public: The mode to pass to #open for creating a new file

"create"
STDOUT_FLAG =

Public: A special ‘filename’ that may be passed to #open for ‘saving’ to $stdout

"-"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, mode = ALTER) ⇒ File

Public: Create a new DigestFile or PasswordFile. Generally you do not need to use this method. Use #open instead.

Altering a non-existent file is an error. Creating an existing file results in a truncation and overwrite of the existing file.

filename - The name of the file to open mode - The mode to open the file this must be either CREATE or

ALTER. (default: ALTER)

Examples

df = ::HTAuth::DigestFile.open("my.digest")

pf = ::HTAuth::PasswordFile.open("my.passwd")

Returns the DigestFile or PasswordFile as appropriate. Raises FileAccessError if an invalid mode is used. Raises FileAccessError if ALTERing a non-existent file.

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/htauth/file.rb', line 85

def initialize(filename, mode = ALTER)
  @filename   = filename
  @mode       = mode
  @dirty      = false

  raise FileAccessError, "Invalid mode #{mode}" unless [ALTER, CREATE].include?(mode)

  if (filename != STDOUT_FLAG) && (mode == ALTER) && !::File.exist?(filename)
    raise FileAccessError, "Could not open passwd file #{filename} for reading."
  end

  begin
    @entries  = {}
    @lines    = []
    load_entries if (@mode == ALTER) && (filename != STDOUT_FLAG)
  rescue StandardError => e
    raise FileAccessError, e.message
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



21
22
23
# File 'lib/htauth/file.rb', line 21

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



21
22
23
# File 'lib/htauth/file.rb', line 21

def filename
  @filename
end

Class Method Details

.open(filename, mode = ALTER) ⇒ Object

Public: The method to use to open a DigestFile or PasswordFile. Altering a non-existent file is an error. Creating an existing file results in a truncation and overwrite of the existing file.

filename - The name of the file to open mode - The mode to open the file this must be either CREATE or

ALTER. (default: ALTER)

Yields the instance of DigestFile or PasswordFile that was opened. The File will be saved at the end of the block if any entries have been added, updated, or deleted.

Examples

df = ::HTAuth::DigestFile.open("my.digest")

::HTAuth::Digestfile.open("my.digest") do |df|
  # ...
end

pf = ::HTAuth::PasswordFile.open("my.passwd")

::HTAuth::PasswordFile.open("my.passwd") do |pf|
  # ...
end

Returns the DigestFile or PasswordFile as appropriate. Raises FileAccessError if an invalid mode is used. Raises FileAccessError if ALTERing a non-existent file.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/htauth/file.rb', line 53

def open(filename, mode = ALTER)
  f = new(filename, mode)
  if block_given?
    begin
      yield f
    ensure
      f.save! if f&.dirty?
    end
  end
  f
end

Instance Method Details

#contentsObject

Internal: Return the String of the entire file contents

Returns String



144
145
146
147
148
149
150
# File 'lib/htauth/file.rb', line 144

def contents
  c = StringIO.new
  @lines.each do |l|
    c.puts l if l
  end
  c.string
end

#dirty!Object

Public: Explicitly mark the file as having had alterations

Returns true



115
116
117
# File 'lib/htauth/file.rb', line 115

def dirty!
  @dirty = true
end

#dirty?Boolean

Public: Returns if the file has had any alterations.

Returns true or false

Returns:

  • (Boolean)


108
109
110
# File 'lib/htauth/file.rb', line 108

def dirty?
  @dirty
end

#load_entriesObject

Internal: Loads all the entries from the file into an internal array.

This keeps the original lines in one array and all the entries in a separate structure indexed by key. This allows the file to be written back out in the same order it was read with the appropriate entries updated or deleted.



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/htauth/file.rb', line 158

def load_entries
  @lines = IO.readlines(@filename)
  @lines.each_with_index do |line, idx|
    next unless entry_klass.entry?(line)

    entry = entry_klass.from_line(line)
    v     = { "entry" => entry, "line_index" => idx }
    @entries[entry.key] = v
  end
  nil
end

#save!Object

Public: Write out the file to filename from #open. This will write out the whole file at once. If writing to a filesystem file this overwrites the whole file.

Example

df.save!

Returns nothing Raises FileAccessError if there was a problem writing the file



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/htauth/file.rb', line 129

def save!
  case filename
  when STDOUT_FLAG
    $stdout.write(contents)
  else
    ::File.write(@filename, contents)
  end
  @dirty = false
rescue StandardError => e
  raise FileAccessError, "Error saving file #{@filename} : #{e.message}"
end