Class: FileUtils2::Entry_

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



1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'lib/fileutils2.rb', line 1275

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)


1336
1337
1338
1339
# File 'lib/fileutils2.rb', line 1336

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

#chardev?Boolean

Returns:

  • (Boolean)


1331
1332
1333
1334
# File 'lib/fileutils2.rb', line 1331

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

#chmod(mode) ⇒ Object



1402
1403
1404
1405
1406
1407
1408
# File 'lib/fileutils2.rb', line 1402

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

#chown(uid, gid) ⇒ Object



1410
1411
1412
1413
1414
1415
1416
# File 'lib/fileutils2.rb', line 1410

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



1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
# File 'lib/fileutils2.rb', line 1418

def copy(dest)
  case
  when file?
    copy_file dest
  when directory?
    if !File.exist?(dest) and descendant_diretory?(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?
    raise "cannot handle device file" unless File.respond_to?(:mknod)
    mknod dest, ?c, 0666, lstat().rdev
  when blockdev?
    raise "cannot handle device file" unless File.respond_to?(:mknod)
    mknod dest, ?b, 0666, lstat().rdev
  when socket?
    raise "cannot handle socket" unless File.respond_to?(:mknod)
    mknod dest, nil, lstat().mode, 0
  when pipe?
    raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
    mkfifo dest, 0666
  when door?
    raise "cannot handle door: #{path()}"
  else
    raise "unknown file type: #{path()}"
  end
end

#copy_file(dest) ⇒ Object



1452
1453
1454
1455
1456
1457
1458
# File 'lib/fileutils2.rb', line 1452

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



1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
# File 'lib/fileutils2.rb', line 1460

def (path)
  st = lstat()
  if !st.symlink?
    File.utime st.atime, st.mtime, path
  end
  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
    # clear setuid/setgid
    if st.symlink?
      begin
        File.lchmod st.mode & 01777, path
      rescue NotImplementedError
      end
    else
      File.chmod st.mode & 01777, path
    end
  else
    if st.symlink?
      begin
        File.lchmod st.mode, path
      rescue NotImplementedError
      end
    else
      File.chmod st.mode, path
    end
  end
end

#dereference?Boolean

Returns:

  • (Boolean)


1308
1309
1310
# File 'lib/fileutils2.rb', line 1308

def dereference?
  @deref
end

#directory?Boolean

Returns:

  • (Boolean)


1321
1322
1323
1324
# File 'lib/fileutils2.rb', line 1321

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

#door?Boolean

Returns:

  • (Boolean)


1353
1354
1355
1356
# File 'lib/fileutils2.rb', line 1353

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

#entriesObject



1358
1359
1360
1361
1362
1363
1364
# File 'lib/fileutils2.rb', line 1358

def entries
  opts = {}
  opts[:encoding] = ::Encoding::UTF_8 if fu_windows?
  Dir.entries(path(), opts)\
      .reject {|n| n == '.' or n == '..' }\
      .map {|n| Entry_.new(prefix(), join(rel(), n.untaint)) }
end

#exist?Boolean

Returns:

  • (Boolean)


1312
1313
1314
# File 'lib/fileutils2.rb', line 1312

def exist?
  lstat! ? true : false
end

#file?Boolean

Returns:

  • (Boolean)


1316
1317
1318
1319
# File 'lib/fileutils2.rb', line 1316

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

#inspectObject



1288
1289
1290
# File 'lib/fileutils2.rb', line 1288

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

#lstatObject



1388
1389
1390
1391
1392
1393
1394
# File 'lib/fileutils2.rb', line 1388

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

#lstat!Object



1396
1397
1398
1399
1400
# File 'lib/fileutils2.rb', line 1396

def lstat!
  lstat()
rescue SystemCallError
  nil
end

#pathObject



1292
1293
1294
1295
1296
1297
1298
# File 'lib/fileutils2.rb', line 1292

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

#pipe?Boolean

Returns:

  • (Boolean)


1346
1347
1348
1349
# File 'lib/fileutils2.rb', line 1346

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

#platform_supportObject



1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
# File 'lib/fileutils2.rb', line 1516

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:



1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
# File 'lib/fileutils2.rb', line 1546

def postorder_traverse
  if directory?
    entries().each do |ent|
      ent.postorder_traverse do |e|
        yield e
      end
    end
  end
  yield self
end

#prefixObject



1300
1301
1302
# File 'lib/fileutils2.rb', line 1300

def prefix
  @prefix || @path
end

#preorder_traverseObject Also known as: traverse



1536
1537
1538
1539
1540
1541
1542
# File 'lib/fileutils2.rb', line 1536

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

#relObject



1304
1305
1306
# File 'lib/fileutils2.rb', line 1304

def rel
  @rel
end

#removeObject



1496
1497
1498
1499
1500
1501
1502
# File 'lib/fileutils2.rb', line 1496

def remove
  if directory?
    remove_dir1
  else
    remove_file
  end
end

#remove_dir1Object



1504
1505
1506
1507
1508
# File 'lib/fileutils2.rb', line 1504

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

#remove_fileObject



1510
1511
1512
1513
1514
# File 'lib/fileutils2.rb', line 1510

def remove_file
  platform_support {
    File.unlink path
  }
end

#socket?Boolean

Returns:

  • (Boolean)


1341
1342
1343
1344
# File 'lib/fileutils2.rb', line 1341

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

#statObject



1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/fileutils2.rb', line 1366

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

#stat!Object



1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/fileutils2.rb', line 1376

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)


1326
1327
1328
1329
# File 'lib/fileutils2.rb', line 1326

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

#wrap_traverse(pre, post) ⇒ Object



1557
1558
1559
1560
1561
1562
1563
1564
1565
# File 'lib/fileutils2.rb', line 1557

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