Class: Kward::NativeShell::Directory
- Inherits:
-
Object
- Object
- Kward::NativeShell::Directory
- Defined in:
- lib/kward/shell/native_directory.rb
Overview
Owns directory identity independently of the shell's logical PWD. Never changes the host cwd; only an exec-bound child may enter this descriptor.
Constant Summary collapse
- OPENAT =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["openat"], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_VARIADIC], Fiddle::TYPE_INT)
- FACCESSAT =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["faccessat"], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
- FCHDIR =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["fchdir"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
Instance Method Summary collapse
- #access_path ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #enter ⇒ Object
- #fileno ⇒ Object
-
#initialize(path, base: nil) ⇒ Directory
constructor
A new instance of Directory.
- #initialize_copy(other) ⇒ Object
- #open_file(path, mode = "r") ⇒ Object
- #physical_path ⇒ Object
- #protect_descriptors(descriptors) ⇒ Object
- #resolve(path) ⇒ Object
- #same?(path) ⇒ Boolean
Constructor Details
#initialize(path, base: nil) ⇒ Directory
Returns a new instance of Directory.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kward/shell/native_directory.rb', line 24 def initialize(path, base: nil) raise ArgumentError, "NUL bytes are not allowed in directory paths" if path.include?("\0") fd = OPENAT.call(base ? base.fileno : AT_FDCWD, path, FLAGS, Fiddle::TYPE_INT, 0) raise SystemCallError.new(path, Fiddle.last_error) if fd < 0 @handle = IO.for_fd(fd) if FACCESSAT.call(fd, ".", 1, AT_EACCESS) < 0 error = Fiddle.last_error close raise SystemCallError.new(path, error) end end |
Instance Method Details
#access_path ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kward/shell/native_directory.rb', line 66 def access_path path = if RUBY_PLATFORM.include?("darwin") # F_GETPATH uses the kernel's MAXPATHLEN-sized output buffer. buffer = "\0" * 1024 @handle.fcntl(50, buffer) buffer.split("\0", 2).first else File.readlink("/proc/self/fd/#{fileno}") end raise Errno::ENOENT, "current directory has no pathname" unless same?(path) path end |
#close ⇒ Object
46 47 48 |
# File 'lib/kward/shell/native_directory.rb', line 46 def close @handle.close unless @handle.closed? end |
#closed? ⇒ Boolean
50 51 52 |
# File 'lib/kward/shell/native_directory.rb', line 50 def closed? @handle.closed? end |
#enter ⇒ Object
107 108 109 |
# File 'lib/kward/shell/native_directory.rb', line 107 def enter raise SystemCallError.new("fchdir", Fiddle.last_error) if FCHDIR.call(fileno) < 0 end |
#fileno ⇒ Object
42 43 44 |
# File 'lib/kward/shell/native_directory.rb', line 42 def fileno @handle.fileno end |
#initialize_copy(other) ⇒ Object
36 37 38 39 40 |
# File 'lib/kward/shell/native_directory.rb', line 36 def initialize_copy(other) super @handle = other.handle.dup @handle.close_on_exec = true end |
#open_file(path, mode = "r") ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/kward/shell/native_directory.rb', line 85 def open_file(path, mode = "r") raise ArgumentError, "NUL bytes are not allowed in file paths" if path.include?("\0") flags = mode.is_a?(Integer) ? mode : { "r" => File::RDONLY, "w" => File::WRONLY | File::CREAT | File::TRUNC, "a" => File::WRONLY | File::CREAT | File::APPEND }.fetch(mode) fd = OPENAT.call(fileno, path, flags | CLOEXEC, Fiddle::TYPE_INT, 0o666) raise SystemCallError.new(path, Fiddle.last_error) if fd < 0 file = IO.for_fd(fd) return file unless block_given? begin yield file ensure file.close end end |
#physical_path ⇒ Object
62 63 64 |
# File 'lib/kward/shell/native_directory.rb', line 62 def physical_path File.realpath(access_path) end |
#protect_descriptors(descriptors) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/kward/shell/native_directory.rb', line 99 def protect_descriptors(descriptors) return unless descriptors.include?(fileno) replacement = IO.for_fd(@handle.fcntl(Fcntl::F_DUPFD, [32, descriptors.max + 1].max)) replacement.close_on_exec = true @handle.close @handle = replacement end |
#resolve(path) ⇒ Object
79 80 81 82 83 |
# File 'lib/kward/shell/native_directory.rb', line 79 def resolve(path) return path if path.start_with?("/") # Do not expand_path here: symlink/.. must be resolved by the kernel. File.join(access_path, path.delete_prefix("./")) end |
#same?(path) ⇒ Boolean
54 55 56 57 58 59 60 |
# File 'lib/kward/shell/native_directory.rb', line 54 def same?(path) stat = File.stat(path) current = @handle.stat stat.dev == current.dev && stat.ino == current.ino rescue SystemCallError false end |