Class: Bundler::FileUtils::Entry_

Inherits:
Object
  • Object
show all
Includes:
StreamUtils_
Defined in:
lib/bundler/vendor/fileutils/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_.



2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2084

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)


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

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

#chardev?Boolean

Returns:

  • (Boolean)


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

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

#chmod(mode) ⇒ Object



2218
2219
2220
2221
2222
2223
2224
2225
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2218

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



2227
2228
2229
2230
2231
2232
2233
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2227

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



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
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2251

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



2289
2290
2291
2292
2293
2294
2295
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2289

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



2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2297

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)


2117
2118
2119
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2117

def dereference?
  @deref
end

#directory?Boolean

Returns:

  • (Boolean)


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

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

#door?Boolean

Returns:

  • (Boolean)


2167
2168
2169
2170
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2167

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

#entriesObject



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

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)


2121
2122
2123
2124
2125
2126
2127
2128
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2121

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

#file?Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



2097
2098
2099
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2097

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


2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2235

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



2204
2205
2206
2207
2208
2209
2210
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2204

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

#lstat!Object



2212
2213
2214
2215
2216
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2212

def lstat!
  lstat()
rescue SystemCallError
  nil
end

#pathObject



2101
2102
2103
2104
2105
2106
2107
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2101

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

#pipe?Boolean

Returns:

  • (Boolean)


2160
2161
2162
2163
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2160

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

#platform_supportObject



2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2346

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:



2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2376

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



2109
2110
2111
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2109

def prefix
  @prefix || @path
end

#preorder_traverseObject Also known as: traverse



2366
2367
2368
2369
2370
2371
2372
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2366

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

#relObject



2113
2114
2115
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2113

def rel
  @rel
end

#removeObject



2326
2327
2328
2329
2330
2331
2332
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2326

def remove
  if directory?
    remove_dir1
  else
    remove_file
  end
end

#remove_dir1Object



2334
2335
2336
2337
2338
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2334

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

#remove_fileObject



2340
2341
2342
2343
2344
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2340

def remove_file
  platform_support {
    File.unlink path
  }
end

#socket?Boolean

Returns:

  • (Boolean)


2155
2156
2157
2158
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2155

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

#statObject



2182
2183
2184
2185
2186
2187
2188
2189
2190
# File 'lib/bundler/vendor/fileutils/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
end

#stat!Object



2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2192

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)


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

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

#wrap_traverse(pre, post) ⇒ Object



2396
2397
2398
2399
2400
2401
2402
2403
2404
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2396

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