Class: FileUtils::Entry_

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



1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
# File 'lib/rubysl/fileutils/fileutils.rb', line 1192

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)


1253
1254
1255
1256
# File 'lib/rubysl/fileutils/fileutils.rb', line 1253

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

#chardev?Boolean

Returns:

  • (Boolean)


1248
1249
1250
1251
# File 'lib/rubysl/fileutils/fileutils.rb', line 1248

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

#chmod(mode) ⇒ Object



1319
1320
1321
1322
1323
1324
1325
# File 'lib/rubysl/fileutils/fileutils.rb', line 1319

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

#chown(uid, gid) ⇒ Object



1327
1328
1329
1330
1331
1332
1333
# File 'lib/rubysl/fileutils/fileutils.rb', line 1327

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



1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
# File 'lib/rubysl/fileutils/fileutils.rb', line 1335

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



1369
1370
1371
1372
1373
1374
1375
# File 'lib/rubysl/fileutils/fileutils.rb', line 1369

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



1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
# File 'lib/rubysl/fileutils/fileutils.rb', line 1377

def (path)
  st = lstat()
  File.utime st.atime, st.mtime, path
  begin
    File.chown st.uid, st.gid, path
  rescue Errno::EPERM
    # clear setuid/setgid
    File.chmod st.mode & 01777, path
  else
    File.chmod st.mode, path
  end
end

#dereference?Boolean

Returns:

  • (Boolean)


1225
1226
1227
# File 'lib/rubysl/fileutils/fileutils.rb', line 1225

def dereference?
  @deref
end

#directory?Boolean

Returns:

  • (Boolean)


1238
1239
1240
1241
# File 'lib/rubysl/fileutils/fileutils.rb', line 1238

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

#door?Boolean

Returns:

  • (Boolean)


1270
1271
1272
1273
# File 'lib/rubysl/fileutils/fileutils.rb', line 1270

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

#entriesObject



1275
1276
1277
1278
1279
1280
1281
# File 'lib/rubysl/fileutils/fileutils.rb', line 1275

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)


1229
1230
1231
# File 'lib/rubysl/fileutils/fileutils.rb', line 1229

def exist?
  lstat! ? true : false
end

#file?Boolean

Returns:

  • (Boolean)


1233
1234
1235
1236
# File 'lib/rubysl/fileutils/fileutils.rb', line 1233

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

#inspectObject



1205
1206
1207
# File 'lib/rubysl/fileutils/fileutils.rb', line 1205

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

#lstatObject



1305
1306
1307
1308
1309
1310
1311
# File 'lib/rubysl/fileutils/fileutils.rb', line 1305

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

#lstat!Object



1313
1314
1315
1316
1317
# File 'lib/rubysl/fileutils/fileutils.rb', line 1313

def lstat!
  lstat()
rescue SystemCallError
  nil
end

#pathObject



1209
1210
1211
1212
1213
1214
1215
# File 'lib/rubysl/fileutils/fileutils.rb', line 1209

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

#pipe?Boolean

Returns:

  • (Boolean)


1263
1264
1265
1266
# File 'lib/rubysl/fileutils/fileutils.rb', line 1263

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

#platform_supportObject



1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
# File 'lib/rubysl/fileutils/fileutils.rb', line 1410

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:



1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
# File 'lib/rubysl/fileutils/fileutils.rb', line 1440

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

#prefixObject



1217
1218
1219
# File 'lib/rubysl/fileutils/fileutils.rb', line 1217

def prefix
  @prefix || @path
end

#preorder_traverseObject Also known as: traverse



1430
1431
1432
1433
1434
1435
1436
# File 'lib/rubysl/fileutils/fileutils.rb', line 1430

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

#relObject



1221
1222
1223
# File 'lib/rubysl/fileutils/fileutils.rb', line 1221

def rel
  @rel
end

#removeObject



1390
1391
1392
1393
1394
1395
1396
# File 'lib/rubysl/fileutils/fileutils.rb', line 1390

def remove
  if directory?
    remove_dir1
  else
    remove_file
  end
end

#remove_dir1Object



1398
1399
1400
1401
1402
# File 'lib/rubysl/fileutils/fileutils.rb', line 1398

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

#remove_fileObject



1404
1405
1406
1407
1408
# File 'lib/rubysl/fileutils/fileutils.rb', line 1404

def remove_file
  platform_support {
    File.unlink path
  }
end

#socket?Boolean

Returns:

  • (Boolean)


1258
1259
1260
1261
# File 'lib/rubysl/fileutils/fileutils.rb', line 1258

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

#statObject



1283
1284
1285
1286
1287
1288
1289
1290
1291
# File 'lib/rubysl/fileutils/fileutils.rb', line 1283

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

#stat!Object



1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
# File 'lib/rubysl/fileutils/fileutils.rb', line 1293

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)


1243
1244
1245
1246
# File 'lib/rubysl/fileutils/fileutils.rb', line 1243

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