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".freeze
CREATE =

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

"create".freeze
STDOUT_FLAG =

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

"-".freeze

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) and (mode == ALTER) and (not ::File.exist?(filename)) then
    raise FileAccessError, "Could not open passwd file #{filename} for reading." 
  end

  begin
    @entries  = {}
    @lines    = []
    load_entries if (@mode == ALTER) and (filename != STDOUT_FLAG)
  rescue => 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.



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

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 = self.new(filename, mode)
  if block_given?
    begin
      yield f
    ensure
      f.save! if f and f.dirty?
    end
  end
  return f
end

Instance Method Details

#contentsObject

Internal: Return the String of the entire file contents

Returns String



148
149
150
151
152
153
154
# File 'lib/htauth/file.rb', line 148

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.



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/htauth/file.rb', line 162

def load_entries
  @lines   = IO.readlines(@filename)
  @lines.each_with_index do |line,idx|
    if entry_klass.is_entry?(line) then
      entry = entry_klass.from_line(line)
      v     = { 'entry' => entry, 'line_index' => idx }
      @entries[entry.key] = v
    end
  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
140
141
142
143
# File 'lib/htauth/file.rb', line 129

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