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_.



2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2072

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)


2138
2139
2140
2141
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2138

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

#chardev?Boolean

Returns:

  • (Boolean)


2133
2134
2135
2136
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2133

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

#chmod(mode) ⇒ Object



2206
2207
2208
2209
2210
2211
2212
2213
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2206

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



2215
2216
2217
2218
2219
2220
2221
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2215

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



2239
2240
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
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2239

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



2277
2278
2279
2280
2281
2282
2283
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2277

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



2285
2286
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
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2285

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)


2105
2106
2107
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2105

def dereference?
  @deref
end

#directory?Boolean

Returns:

  • (Boolean)


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

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

#door?Boolean

Returns:

  • (Boolean)


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

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

#entriesObject



2160
2161
2162
2163
2164
2165
2166
2167
2168
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2160

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)


2109
2110
2111
2112
2113
2114
2115
2116
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2109

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

#file?Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



2085
2086
2087
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2085

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


2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2223

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



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

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

#lstat!Object



2200
2201
2202
2203
2204
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2200

def lstat!
  lstat()
rescue SystemCallError
  nil
end

#pathObject



2089
2090
2091
2092
2093
2094
2095
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2089

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

#pipe?Boolean

Returns:

  • (Boolean)


2148
2149
2150
2151
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2148

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

#platform_supportObject



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

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:



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

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



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

def prefix
  @prefix || @path
end

#preorder_traverseObject Also known as: traverse



2354
2355
2356
2357
2358
2359
2360
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2354

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

#relObject



2101
2102
2103
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2101

def rel
  @rel
end

#removeObject



2314
2315
2316
2317
2318
2319
2320
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2314

def remove
  if directory?
    remove_dir1
  else
    remove_file
  end
end

#remove_dir1Object



2322
2323
2324
2325
2326
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2322

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

#remove_fileObject



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

def remove_file
  platform_support {
    File.unlink path
  }
end

#socket?Boolean

Returns:

  • (Boolean)


2143
2144
2145
2146
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2143

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

#statObject



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

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

#stat!Object



2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2180

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)


2128
2129
2130
2131
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2128

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

#wrap_traverse(pre, post) ⇒ Object



2384
2385
2386
2387
2388
2389
2390
2391
2392
# File 'lib/bundler/vendor/fileutils/lib/fileutils.rb', line 2384

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