Class: FileUtils::Entry_

Inherits:
Object
  • Object
show all
Includes:
StreamUtils_
Defined in:
lib/fileutils.rb

Overview

:nodoc: internal use only

Constant Summary collapse

S_IF_DOOR =
0xD000

Instance Method Summary collapse

Constructor Details

#initialize(a, b = nil, deref = false) ⇒ Entry_

Returns a new instance of Entry_.



2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
# File 'lib/fileutils.rb', line 2074

def initialize(a, b = nil, deref = false)
  @prefix = @rel = @path = nil
  if b
    @prefix = a
    @rel = b
  else
    @path = a
  end
  @deref = deref
  @stat = nil
  @lstat = nil
end

Instance Method Details

#blockdev?Boolean

Returns:

  • (Boolean)


2140
2141
2142
2143
# File 'lib/fileutils.rb', line 2140

def blockdev?
  s = lstat!
  s and s.blockdev?
end

#chardev?Boolean

Returns:

  • (Boolean)


2135
2136
2137
2138
# File 'lib/fileutils.rb', line 2135

def chardev?
  s = lstat!
  s and s.chardev?
end

#chmod(mode) ⇒ Object



2208
2209
2210
2211
2212
2213
2214
2215
# File 'lib/fileutils.rb', line 2208

def chmod(mode)
  if symlink?
    File.lchmod mode, path() if have_lchmod?
  else
    File.chmod mode, path()
  end
rescue Errno::EOPNOTSUPP
end

#chown(uid, gid) ⇒ Object



2217
2218
2219
2220
2221
2222
2223
# File 'lib/fileutils.rb', line 2217

def chown(uid, gid)
  if symlink?
    File.lchown uid, gid, path() if have_lchown?
  else
    File.chown uid, gid, path()
  end
end

#copy(dest) ⇒ Object



2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
# File 'lib/fileutils.rb', line 2241

def copy(dest)
  lstat
  case
  when file?
    copy_file dest
  when directory?
    if !File.exist?(dest) and descendant_directory?(dest, path)
      raise ArgumentError, "cannot copy directory %s to itself %s" % [path, dest]
    end
    begin
      Dir.mkdir dest
    rescue
      raise unless File.directory?(dest)
    end
  when symlink?
    File.symlink File.readlink(path()), dest
  when chardev?, blockdev?
    raise "cannot handle device file"
  when socket?
    begin
      require 'socket'
    rescue LoadError
      raise "cannot handle socket"
    else
      raise "cannot handle socket" unless defined?(UNIXServer)
    end
    UNIXServer.new(dest).close
    File.chmod lstat().mode, dest
  when pipe?
    raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
    File.mkfifo dest, lstat().mode
  when door?
    raise "cannot handle door: #{path()}"
  else
    raise "unknown file type: #{path()}"
  end
end

#copy_file(dest) ⇒ Object



2279
2280
2281
2282
2283
2284
2285
# File 'lib/fileutils.rb', line 2279

def copy_file(dest)
  File.open(path()) do |s|
    File.open(dest, 'wb', s.stat.mode) do |f|
      IO.copy_stream(s, f)
    end
  end
end

#copy_metadata(path) ⇒ Object



2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
# File 'lib/fileutils.rb', line 2287

def (path)
  st = lstat()
  if !st.symlink?
    File.utime st.atime, st.mtime, path
  end
  mode = st.mode
  begin
    if st.symlink?
      begin
        File.lchown st.uid, st.gid, path
      rescue NotImplementedError
      end
    else
      File.chown st.uid, st.gid, path
    end
  rescue Errno::EPERM, Errno::EACCES
    # clear setuid/setgid
    mode &= 01777
  end
  if st.symlink?
    begin
      File.lchmod mode, path
    rescue NotImplementedError, Errno::EOPNOTSUPP
    end
  else
    File.chmod mode, path
  end
end

#dereference?Boolean

Returns:

  • (Boolean)


2107
2108
2109
# File 'lib/fileutils.rb', line 2107

def dereference?
  @deref
end

#directory?Boolean

Returns:

  • (Boolean)


2125
2126
2127
2128
# File 'lib/fileutils.rb', line 2125

def directory?
  s = lstat!
  s and s.directory?
end

#door?Boolean

Returns:

  • (Boolean)


2157
2158
2159
2160
# File 'lib/fileutils.rb', line 2157

def door?
  s = lstat!
  s and (s.mode & 0xF000 == S_IF_DOOR)
end

#entriesObject



2162
2163
2164
2165
2166
2167
2168
2169
2170
# File 'lib/fileutils.rb', line 2162

def entries
  opts = {}
  opts[:encoding] = fu_windows? ? ::Encoding::UTF_8 : path.encoding

  files = Dir.children(path, **opts)

  untaint = RUBY_VERSION < '2.7'
  files.map {|n| Entry_.new(prefix(), join(rel(), untaint ? n.untaint : n)) }
end

#exist?Boolean

Returns:

  • (Boolean)


2111
2112
2113
2114
2115
2116
2117
2118
# File 'lib/fileutils.rb', line 2111

def exist?
  begin
    lstat
    true
  rescue Errno::ENOENT
    false
  end
end

#file?Boolean

Returns:

  • (Boolean)


2120
2121
2122
2123
# File 'lib/fileutils.rb', line 2120

def file?
  s = lstat!
  s and s.file?
end

#inspectObject



2087
2088
2089
# File 'lib/fileutils.rb', line 2087

def inspect
  "\#<#{self.class} #{path()}>"
end


2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
# File 'lib/fileutils.rb', line 2225

def link(dest)
  case
  when directory?
    if !File.exist?(dest) and descendant_directory?(dest, path)
      raise ArgumentError, "cannot link directory %s to itself %s" % [path, dest]
    end
    begin
      Dir.mkdir dest
    rescue
      raise unless File.directory?(dest)
    end
  else
    File.link path(), dest
  end
end

#lstatObject



2194
2195
2196
2197
2198
2199
2200
# File 'lib/fileutils.rb', line 2194

def lstat
  if dereference?
    @lstat ||= File.stat(path())
  else
    @lstat ||= File.lstat(path())
  end
end

#lstat!Object



2202
2203
2204
2205
2206
# File 'lib/fileutils.rb', line 2202

def lstat!
  lstat()
rescue SystemCallError
  nil
end

#pathObject



2091
2092
2093
2094
2095
2096
2097
# File 'lib/fileutils.rb', line 2091

def path
  if @path
    File.path(@path)
  else
    join(@prefix, @rel)
  end
end

#pipe?Boolean

Returns:

  • (Boolean)


2150
2151
2152
2153
# File 'lib/fileutils.rb', line 2150

def pipe?
  s = lstat!
  s and s.pipe?
end

#platform_supportObject



2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
# File 'lib/fileutils.rb', line 2336

def platform_support
  return yield unless fu_windows?
  first_time_p = true
  begin
    yield
  rescue Errno::ENOENT
    raise
  rescue => err
    if first_time_p
      first_time_p = false
      begin
        File.chmod 0700, path()   # Windows does not have symlink
        retry
      rescue SystemCallError
      end
    end
    raise err
  end
end

#postorder_traverse {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
# File 'lib/fileutils.rb', line 2366

def postorder_traverse
  if directory?
    begin
      children = entries()
    rescue Errno::EACCES
      # Failed to get the list of children.
      # Assuming there is no children, try to process the parent directory.
      yield self
      return
    end

    children.each do |ent|
      ent.postorder_traverse do |e|
        yield e
      end
    end
  end
  yield self
end

#prefixObject



2099
2100
2101
# File 'lib/fileutils.rb', line 2099

def prefix
  @prefix || @path
end

#preorder_traverseObject Also known as: traverse



2356
2357
2358
2359
2360
2361
2362
# File 'lib/fileutils.rb', line 2356

def preorder_traverse
  stack = [self]
  while ent = stack.pop
    yield ent
    stack.concat ent.entries.reverse if ent.directory?
  end
end

#relObject



2103
2104
2105
# File 'lib/fileutils.rb', line 2103

def rel
  @rel
end

#removeObject



2316
2317
2318
2319
2320
2321
2322
# File 'lib/fileutils.rb', line 2316

def remove
  if directory?
    remove_dir1
  else
    remove_file
  end
end

#remove_dir1Object



2324
2325
2326
2327
2328
# File 'lib/fileutils.rb', line 2324

def remove_dir1
  platform_support {
    Dir.rmdir path().chomp(?/)
  }
end

#remove_fileObject



2330
2331
2332
2333
2334
# File 'lib/fileutils.rb', line 2330

def remove_file
  platform_support {
    File.unlink path
  }
end

#socket?Boolean

Returns:

  • (Boolean)


2145
2146
2147
2148
# File 'lib/fileutils.rb', line 2145

def socket?
  s = lstat!
  s and s.socket?
end

#statObject



2172
2173
2174
2175
2176
2177
2178
2179
2180
# File 'lib/fileutils.rb', line 2172

def stat
  return @stat if @stat
  if lstat() and lstat().symlink?
    @stat = File.stat(path())
  else
    @stat = lstat()
  end
  @stat
end

#stat!Object



2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
# File 'lib/fileutils.rb', line 2182

def stat!
  return @stat if @stat
  if lstat! and lstat!.symlink?
    @stat = File.stat(path())
  else
    @stat = lstat!
  end
  @stat
rescue SystemCallError
  nil
end

#symlink?Boolean

Returns:

  • (Boolean)


2130
2131
2132
2133
# File 'lib/fileutils.rb', line 2130

def symlink?
  s = lstat!
  s and s.symlink?
end

#wrap_traverse(pre, post) ⇒ Object



2386
2387
2388
2389
2390
2391
2392
2393
2394
# File 'lib/fileutils.rb', line 2386

def wrap_traverse(pre, post)
  pre.call self
  if directory?
    entries.each do |ent|
      ent.wrap_traverse pre, post
    end
  end
  post.call self
end