Class: FileUtils::Entry_
- Inherits:
-
Object
- Object
- FileUtils::Entry_
- Includes:
- StreamUtils_
- Defined in:
- lib/fileutils.rb
Overview
:nodoc: internal use only
Constant Summary collapse
- S_IF_DOOR =
0xD000
Instance Method Summary collapse
- #blockdev? ⇒ Boolean
- #chardev? ⇒ Boolean
- #chmod(mode) ⇒ Object
- #chown(uid, gid) ⇒ Object
- #copy(dest) ⇒ Object
- #copy_file(dest) ⇒ Object
- #copy_metadata(path) ⇒ Object
- #dereference? ⇒ Boolean
- #directory? ⇒ Boolean
- #door? ⇒ Boolean
- #entries ⇒ Object
- #exist? ⇒ Boolean
- #file? ⇒ Boolean
-
#initialize(a, b = nil, deref = false) ⇒ Entry_
constructor
A new instance of Entry_.
- #inspect ⇒ Object
- #link(dest) ⇒ Object
- #lstat ⇒ Object
- #lstat! ⇒ Object
- #path ⇒ Object
- #pipe? ⇒ Boolean
- #platform_support ⇒ Object
- #postorder_traverse ⇒ Object
- #prefix ⇒ Object
- #preorder_traverse ⇒ Object (also: #traverse)
- #rel ⇒ Object
- #remove ⇒ Object
- #remove_dir1 ⇒ Object
- #remove_file ⇒ Object
- #socket? ⇒ Boolean
- #stat ⇒ Object
- #stat! ⇒ Object
- #symlink? ⇒ Boolean
- #wrap_traverse(pre, post) ⇒ Object
Constructor Details
#initialize(a, b = nil, deref = false) ⇒ Entry_
Returns a new instance of Entry_.
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 |
# File 'lib/fileutils.rb', line 1203 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
1269 1270 1271 1272 |
# File 'lib/fileutils.rb', line 1269 def blockdev? s = lstat! s and s.blockdev? end |
#chardev? ⇒ Boolean
1264 1265 1266 1267 |
# File 'lib/fileutils.rb', line 1264 def chardev? s = lstat! s and s.chardev? end |
#chmod(mode) ⇒ Object
1342 1343 1344 1345 1346 1347 1348 1349 |
# File 'lib/fileutils.rb', line 1342 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
1351 1352 1353 1354 1355 1356 1357 |
# File 'lib/fileutils.rb', line 1351 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
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 |
# File 'lib/fileutils.rb', line 1375 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
1413 1414 1415 1416 1417 1418 1419 |
# File 'lib/fileutils.rb', line 1413 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
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 |
# File 'lib/fileutils.rb', line 1421 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
1236 1237 1238 |
# File 'lib/fileutils.rb', line 1236 def dereference? @deref end |
#directory? ⇒ Boolean
1254 1255 1256 1257 |
# File 'lib/fileutils.rb', line 1254 def directory? s = lstat! s and s.directory? end |
#door? ⇒ Boolean
1286 1287 1288 1289 |
# File 'lib/fileutils.rb', line 1286 def door? s = lstat! s and (s.mode & 0xF000 == S_IF_DOOR) end |
#entries ⇒ Object
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 |
# File 'lib/fileutils.rb', line 1291 def entries opts = {} opts[:encoding] = fu_windows? ? ::Encoding::UTF_8 : path.encoding files = if Dir.respond_to?(:children) Dir.children(path, **opts) else Dir.entries(path(), **opts) .reject {|n| n == '.' or n == '..' } end untaint = RUBY_VERSION < '2.7' files.map {|n| Entry_.new(prefix(), join(rel(), untaint ? n.untaint : n)) } end |
#exist? ⇒ Boolean
1240 1241 1242 1243 1244 1245 1246 1247 |
# File 'lib/fileutils.rb', line 1240 def exist? begin lstat true rescue Errno::ENOENT false end end |
#file? ⇒ Boolean
1249 1250 1251 1252 |
# File 'lib/fileutils.rb', line 1249 def file? s = lstat! s and s.file? end |
#inspect ⇒ Object
1216 1217 1218 |
# File 'lib/fileutils.rb', line 1216 def inspect "\#<#{self.class} #{path()}>" end |
#link(dest) ⇒ Object
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 |
# File 'lib/fileutils.rb', line 1359 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 |
#lstat ⇒ Object
1328 1329 1330 1331 1332 1333 1334 |
# File 'lib/fileutils.rb', line 1328 def lstat if dereference? @lstat ||= File.stat(path()) else @lstat ||= File.lstat(path()) end end |
#lstat! ⇒ Object
1336 1337 1338 1339 1340 |
# File 'lib/fileutils.rb', line 1336 def lstat! lstat() rescue SystemCallError nil end |
#path ⇒ Object
1220 1221 1222 1223 1224 1225 1226 |
# File 'lib/fileutils.rb', line 1220 def path if @path File.path(@path) else join(@prefix, @rel) end end |
#pipe? ⇒ Boolean
1279 1280 1281 1282 |
# File 'lib/fileutils.rb', line 1279 def pipe? s = lstat! s and s.pipe? end |
#platform_support ⇒ Object
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 |
# File 'lib/fileutils.rb', line 1470 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 ⇒ Object
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 |
# File 'lib/fileutils.rb', line 1500 def postorder_traverse if directory? entries().each do |ent| ent.postorder_traverse do |e| yield e end end end ensure yield self end |
#prefix ⇒ Object
1228 1229 1230 |
# File 'lib/fileutils.rb', line 1228 def prefix @prefix || @path end |
#preorder_traverse ⇒ Object Also known as: traverse
1490 1491 1492 1493 1494 1495 1496 |
# File 'lib/fileutils.rb', line 1490 def preorder_traverse stack = [self] while ent = stack.pop yield ent stack.concat ent.entries.reverse if ent.directory? end end |
#rel ⇒ Object
1232 1233 1234 |
# File 'lib/fileutils.rb', line 1232 def rel @rel end |
#remove ⇒ Object
1450 1451 1452 1453 1454 1455 1456 |
# File 'lib/fileutils.rb', line 1450 def remove if directory? remove_dir1 else remove_file end end |
#remove_dir1 ⇒ Object
1458 1459 1460 1461 1462 |
# File 'lib/fileutils.rb', line 1458 def remove_dir1 platform_support { Dir.rmdir path().chomp(?/) } end |
#remove_file ⇒ Object
1464 1465 1466 1467 1468 |
# File 'lib/fileutils.rb', line 1464 def remove_file platform_support { File.unlink path } end |
#socket? ⇒ Boolean
1274 1275 1276 1277 |
# File 'lib/fileutils.rb', line 1274 def socket? s = lstat! s and s.socket? end |
#stat ⇒ Object
1306 1307 1308 1309 1310 1311 1312 1313 1314 |
# File 'lib/fileutils.rb', line 1306 def stat return @stat if @stat if lstat() and lstat().symlink? @stat = File.stat(path()) else @stat = lstat() end @stat end |
#stat! ⇒ Object
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 |
# File 'lib/fileutils.rb', line 1316 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
1259 1260 1261 1262 |
# File 'lib/fileutils.rb', line 1259 def symlink? s = lstat! s and s.symlink? end |
#wrap_traverse(pre, post) ⇒ Object
1512 1513 1514 1515 1516 1517 1518 1519 1520 |
# File 'lib/fileutils.rb', line 1512 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 |