Class: RubySMB::Server::Share::Provider::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_smb/server/share/provider.rb

Overview

The share provider defines the share and its attributes such as its type and name. It is shared across all client connections and sessions.

Direct Known Subclasses

Disk, Pipe

Defined Under Namespace

Classes: Hook

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hooks: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • name (String)

    The name of this share.



12
13
14
15
# File 'lib/ruby_smb/server/share/provider.rb', line 12

def initialize(name, hooks: nil)
  @name = name
  @hooks = hooks || []
end

Instance Attribute Details

#hooksArray

Returns:

  • (Array)


77
78
79
# File 'lib/ruby_smb/server/share/provider.rb', line 77

def hooks
  @hooks
end

#nameString

Returns:

  • (String)


72
73
74
# File 'lib/ruby_smb/server/share/provider.rb', line 72

def name
  @name
end

Instance Method Details

#add_hook(request_class, callback: nil, location: :before, &block) ⇒ Object

Add a hook to be called when the specified request class is processed. Any hook that was previously installed for the request class and location will be removed. A hook installed with a location of :before will be called with the session and request as the only two arguments. The return value, if provided, will replace the request that is to be processed. A hook installed with a location of :after will be called with the session, request and response as the only three arguments. The return value, if provided, will replace the response that is to be sent to the client.

Parameters:

  • request_class (RubySMB::GenericPacket)

    The request class to register the hook for.

  • callback (Proc) (defaults to: nil)

    The routine to be executed when the request class is being processed.

  • location (Symbol) (defaults to: :before)

    When the callback should be invoked. Must be either :before or :after.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_smb/server/share/provider.rb', line 27

def add_hook(request_class, callback: nil, location: :before, &block)
  unless %i[ before after ].include?(location)
    raise ArgumentError, 'the location argument must be :before or :after'
  end

  unless callback.nil? ^ block.nil?
    raise ArgumentError, 'either a callback or a block must be specified'
  end

  # Remove any hooks that were previously installed, this enforces that only one hook can be present at a time
  # for any particular request class and location combination.
  remove_hook(request_class, location: location)
  @hooks << Hook.new(request_class, location, callback || block)

  nil
end

#new_processor(server_client, session) ⇒ Object

Create a new, session-specific processor instance for this share.

Parameters:



60
61
62
# File 'lib/ruby_smb/server/share/provider.rb', line 60

def new_processor(server_client, session)
  self.class::Processor.new(self, server_client, session)
end

#remove_hook(request_class, location: :before) ⇒ Object

Remove a hook for the specified request class.

Parameters:

  • request_class (RubySMB::GenericPacket)

    The request class to register the hook for.

  • location (Symbol) (defaults to: :before)

    When the callback should be invoked.



48
49
50
51
52
53
54
# File 'lib/ruby_smb/server/share/provider.rb', line 48

def remove_hook(request_class, location: :before)
  @hooks.filter! do |hook|
    hook.request_class == request_class && hook.location == location
  end

  nil
end

#typeObject

The type of this share.



65
66
67
# File 'lib/ruby_smb/server/share/provider.rb', line 65

def type
  self.class::TYPE
end