Class: File

Inherits:
Object show all
Defined in:
lib/rwd/ftools.rb,
lib/zip/stdrubyext.rb

Direct Known Subclasses

BugFix::Tempfile

Class Method Summary collapse

Class Method Details

.read(fileName) ⇒ Object

singleton method read does not exist in 1.6.x



29
30
31
# File 'lib/zip/stdrubyext.rb', line 29

def self.read(fileName)
  open(fileName) { |f| f.read }
end

.rollbackup(file, mode = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rwd/ftools.rb', line 92

def self.rollbackup(file, mode=nil)
  backupfile	= file + ".RB.BACKUP"
  controlfile	= file + ".RB.CONTROL"
  res		= nil

  File.touch(file)    unless File.file?(file)

	# Rollback

  if File.file?(backupfile) and File.file?(controlfile)
    $stderr.puts "Restoring #{file}..."

    File.copy(backupfile, file)				# Rollback from phase 3
  end

	# Reset

  File.delete(backupfile)	if File.file?(backupfile)	# Reset from phase 2 or 3
  File.delete(controlfile)	if File.file?(controlfile)	# Reset from phase 3 or 4

	# Backup

  File.copy(file, backupfile)					# Enter phase 2
  File.touch(controlfile)					# Enter phase 3

	# The real thing

  if block_given?
    if mode.nil?
      res	= yield
    else
      File.open(file, mode) do |f|
        res	= yield(f)
      end
    end
  end

	# Cleanup

  File.delete(backupfile)					# Enter phase 4
  File.delete(controlfile)					# Enter phase 5

	# Return, like File.open

  res	= File.open(file, (mode or "r"))	unless block_given?

  res
end

.touch(file) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/rwd/ftools.rb', line 141

def self.touch(file)
  if File.exists?(file)
    File.utime(Time.now, File.mtime(file), file)
  else
    File.open(file, "a"){|f|}
  end
end

.which(file) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rwd/ftools.rb', line 149

def self.which(file)
  res	= nil

  if windows?
    file	= file.gsub(/\.exe$/i, "") + ".exe"
    sep		= ";"
  else
    sep		= ":"
  end

  catch :stop do
    ENV["PATH"].split(/#{sep}/).reverse.each do |d|
      if File.directory?(d)
        Dir.new(d).each do |e|
           if e.downcase == file.downcase
             res	= File.expand_path(e, d)
             throw :stop
          end
        end
      end
    end
  end

  res
end