Module: Recls::Ximpl

Defined in:
lib/recls/ximpl/os.rb,
lib/recls/ximpl/unix.rb,
lib/recls/ximpl/util.rb,
lib/recls/ximpl/windows.rb

Defined Under Namespace

Modules: OS, Util Classes: FileStat

Class Method Summary collapse

Class Method Details

.absolute_path(path, refdir = nil) ⇒ Object

determines the absolute path of a given path



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/recls/ximpl/util.rb', line 401

def self.absolute_path(path, refdir = nil)

	case path
	when ::NilClass
		return nil
	when ::String
	when ::Recls::Entry
		path = path.to_s
	else
		raise TypeError, "parameter path ('#{path}') is of type #{path.class} must be an instance of #{::String} or #{::Recls::Entry}"
	end

	return '' if path.empty?

	f1_windows_root, f2_directory, f3_basename, dummy1, dummy2, dummy3, dummy4 = Util.split_path(path)

	if f2_directory =~ /^[\\\/]/
		return path
	end

	cwd = refdir ? refdir : Dir.getwd

	trailing_slash = Util.get_trailing_slash(path)

	if '.' == path
		return Util.trim_trailing_slash cwd
	elsif 2 == path.size and trailing_slash
		return Util.append_trailing_slash(cwd, path[1..1])
	end

	cwd = Util.append_trailing_slash(cwd)

	path = "#{cwd}#{path}"

	path = canonicalise_path path

	if trailing_slash
		path = Util.append_trailing_slash path, trailing_slash
	else
		path = Util.trim_trailing_slash path
	end

	path
end

.basename(path) ⇒ Object

obtains the basename of a path, e.g. the basename of

abc/def/ghi.jkl

or (on Windows)

C:\abc\def\ghi.jkl

is

ghi.jkl


454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/recls/ximpl/util.rb', line 454

def self.basename(path)

	return nil if not path

	# NOTE: we don't implement in terms of split_path
	# because all but the UNC case work with just
	# detecting the last (back)slash

	if Recls::Ximpl::OS::OS_IS_WINDOWS
		wr, rem = Util.get_windows_root(path)

		if not rem
			return ''
		else
			path = rem
		end
	end

	if not path.is_a? String
		path = path.to_s
	end

	if path =~ /^.*[\/\\](.*)/
		$1
	else
		path
	end
end

.canonicalise_path(path) ⇒ Object

Canonicalises a path

Note: contains a trailing slash if, in the context of the given path, the last element of the canonicalised path is a directory unequivocally



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/recls/ximpl/util.rb', line 383

def self.canonicalise_path(path)

	return nil if not path
	return '' if path.empty?

	f1_windows_root, f2_directory, f3_basename, dummy1, dummy2, directory_parts, dummy3 = Util.split_path(path)

	if not f2_directory
		canonicalised_directory = nil
	else
		canonicalised_directory, consume_basename = Util.canonicalise_parts(directory_parts, f3_basename)
		f3_basename = nil if consume_basename
	end

	return "#{f1_windows_root}#{canonicalised_directory}#{f3_basename}"
end

.combine_paths(origin, path, options) ⇒ Object



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/recls/ximpl/util.rb', line 602

def self.combine_paths(origin, path, options)

	f1_windows_root, f2_directory, f3_basename, dummy1, dummy2, dummy3, dummy4 = Util.split_path(path)

#			return path if f1_windows_root
	return path if f2_directory && Util.is_path_name_separator(f2_directory[0])

	r	=	File.join origin, path

	if options[:clean_path]

		r = Pathname.new(r).cleanpath
	end

	r
end

.derive_relative_path(origin, path) ⇒ Object

obtains the relative path of a given path and a reference directory



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/recls/ximpl/util.rb', line 548

def self.derive_relative_path(origin, path)

	return nil if path.nil?
	return nil if path.empty?
	return path if origin.nil?
	return path if origin.empty?

	path			=	self.canonicalise_path path
	origin			=	self.canonicalise_path origin

	path_splits		=	Util.split_path(path)
	origin_splits	=	Util.split_path(origin)

	# if different windows root, then cannot provide relative
	if path_splits[0] and origin_splits[0]
		return path if path_splits[0] != origin_splits[0]
	end

	trailing_slash	=	Util.get_trailing_slash(path)

	path_parts		=	path_splits[6]
	origin_parts	=	origin_splits[6]

	while true

		break if path_parts.empty?
		break if origin_parts.empty?

		path_part	=	path_parts[0]
		origin_part	=	origin_parts[0]

		if 1 == path_parts.size || 1 == origin_parts.size
			path_part	=	Util.append_trailing_slash(path_part)
			origin_part	=	Util.append_trailing_slash(origin_part)
		end

		if path_part == origin_part
			path_parts.shift
			origin_parts.shift
		else
			break
		end
	end

	return ".#{trailing_slash}" if path_parts.empty? and origin_parts.empty?

	# at this point, all reference parts should be converted into '..'

	return origin_parts.map { |rp| '..' }.join('/') if path_parts.empty?

	return '../' * origin_parts.size + path_parts.join('')
end

.dir_entries_maybe(dir, flags) ⇒ Object

Elicits the contents of the given directory, or, if the flag STOP_ON_ACCESS_FAILURE is specified throws an exception if the directory does not exist

Some known conditions:

  • (Mac OSX) /dev/fd/<N> - some of these stat() as directories but

    Dir.new fails with ENOTDIR
    


629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/recls/ximpl/util.rb', line 629

def self.dir_entries_maybe(dir, flags)

	begin

		Dir.new(dir).to_a

	rescue SystemCallError => x

		# TODO this should be filtered up and/or logged

		if(0 != (STOP_ON_ACCESS_FAILURE & flags))
			raise
		end

		return []
	end
end

.directory_from_directory_path(directory_path) ⇒ Object

obtains the directory from the directory path



519
520
521
522
523
524
# File 'lib/recls/ximpl/util.rb', line 519

def self.directory_from_directory_path(directory_path)

	wr, rem =  Util.get_windows_root(directory_path)

	rem
end

.directory_parts_from_directory(directory) ⇒ Object

obtains the directory parts from a directory



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/recls/ximpl/util.rb', line 527

def self.directory_parts_from_directory(directory)

	return nil if not directory

	directory_parts = []

	until directory.empty?
		if directory =~ /^([^\\\/]*[\\\/])/
			directory_parts << $1
			directory = $'
		else
			directory_parts << directory
			directory = ''
		end
	end

	directory_parts
end

.file_ext(path) ⇒ Object

obtains the file extension of a basename, e.g. the file_ext of

ghi.jkl

is

.jkl


488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/recls/ximpl/util.rb', line 488

def self.file_ext(path)

	return nil if not path

	use_split_path = false

	if Recls::Ximpl::OS::OS_IS_WINDOWS
		if path.include? ?\\
			use_split_path = true
		end
	end

	if path.include? ?/
		use_split_path = true
	end

	if use_split_path
		ext = Util.split_path(path)[4]
	else
		if path =~ /^.*(\.[^.]*)$/
			ext = $1
		else
			ext = nil
		end
	end

	return ext ? ext : ''
end