Sha256: 78e512b6a9bb51765af3a44b899a850dc8f50c387347cfb44ac1df6a743f60b9

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'ffi'

module FFI
  # Calculate major/minor device numbers for use with mknod etc..
  # @see makedev(3)
  module Device
    extend FFI::Library
    ffi_lib FFI::Library::LIBC

    prefix = FFI::Platform::IS_GNU ? 'gnu_dev_' : ''

    # @!method makedev(major,minor)
    #  @param [Integer] major
    #  @param [Integer] minor
    #  @return [Integer] combined major/minor to a single value to pass to mknod etc
    attach_function :makedev, :"#{prefix}makedev", %i[int int], :int

    # @!method major(dev)
    #  @param [Integer] dev
    #  @return [Integer] the major component of dev
    attach_function :major, :"#{prefix}major", [:int], :int

    # @!method minor(dev)
    #  @param [Integer] dev
    #  @return [Integer] the minor component of dev
    attach_function :minor, :"#{prefix}minor", [:int], :int
  rescue FFI::NotFoundError
    case Platform::NAME
    when 'x86_64-darwin'
      # From https://github.com/golang/go/issues/8106 these functions are not defined on Darwin.
      class << self
        # define	major(x)	((int32_t)(((u_int32_t)(x) >> 24) & 0xff))
        def major(dev)
          (dev >> 24) & 0xff
        end

        # define	minor(x)	((int32_t)((x) & 0xffffff))
        def minor(dev)
          (dev & 0xffffff)
        end

        # define	makedev(x,y) ((dev_t)(((x) << 24) | (y)))
        def makedev(major, minor)
          (major << 24) | minor
        end
      end
    else
      raise
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ffi-libfuse-0.4.0 lib/ffi/devt.rb