summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-08-27 14:02:54 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-08-28 17:19:34 -0700
commit306423bac260655a9d4b4b152782401f03325519 (patch)
tree0b60a527c8e6a155539b7abd0912ae6b1498514b
parenta9524b0e799c13e90aef846e96c8d32909b5ee39 (diff)
downloadchef-306423bac260655a9d4b4b152782401f03325519.tar.gz
Use FFI for NetUseDel
-rw-r--r--lib/chef/util/windows/net_use.rb15
-rw-r--r--lib/chef/win32/api/net.rb11
-rw-r--r--lib/chef/win32/net.rb20
3 files changed, 42 insertions, 4 deletions
diff --git a/lib/chef/util/windows/net_use.rb b/lib/chef/util/windows/net_use.rb
index 62d7e169dc..34835b6d3d 100644
--- a/lib/chef/util/windows/net_use.rb
+++ b/lib/chef/util/windows/net_use.rb
@@ -21,6 +21,7 @@
#see also cmd.exe: net use /?
require 'chef/util/windows'
+require 'chef/win32/net'
class Chef::Util::Windows::NetUse < Chef::Util::Windows
@@ -76,6 +77,7 @@ class Chef::Util::Windows::NetUse < Chef::Util::Windows
def initialize(localname)
@localname = localname
@name = multi_to_wide(localname)
+ @use_name = localname
end
def add(args)
@@ -111,11 +113,16 @@ class Chef::Util::Windows::NetUse < Chef::Util::Windows
def device
get_info()[:remote]
end
- #XXX should we use some FORCE here?
+
def delete
- rc = NetUseDel.call(nil, @name, USE_NOFORCE)
- if rc != NERR_Success
- raise ArgumentError, get_last_error(rc)
+ begin
+ Chef::ReservedNames::Win32::Net.net_use_del(nil, use_name, :use_noforce)
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
end
end
+
+ def use_name
+ @use_name
+ end
end
diff --git a/lib/chef/win32/api/net.rb b/lib/chef/win32/api/net.rb
index 0519e93a96..a7e20d0aeb 100644
--- a/lib/chef/win32/api/net.rb
+++ b/lib/chef/win32/api/net.rb
@@ -40,6 +40,10 @@ class Chef
UF_NORMAL_ACCOUNT = 0x000200
UF_DONT_EXPIRE_PASSWD = 0x010000
+ USE_NOFORCE = 0
+ USE_FORCE = 1
+ USE_LOTS_OF_FORCE = 2 #every windows API should support this flag
+
NERR_Success = 0
NERR_InvalidComputer = 2351
NERR_NotPrimary = 2226
@@ -272,6 +276,13 @@ class Chef
#);
safe_attach_function :NetUserDel, [:LPCWSTR, :LPCWSTR], :DWORD
+#NET_API_STATUS NetUseDel(
+ #_In_ LMSTR UncServerName,
+ #_In_ LMSTR UseName,
+ #_In_ DWORD ForceCond
+#);
+ safe_attach_function :NetUseDel, [:LMSTR, :LMSTR, :DWORD], :DWORD
+
end
end
end
diff --git a/lib/chef/win32/net.rb b/lib/chef/win32/net.rb
index babe5f829e..e481be7ddb 100644
--- a/lib/chef/win32/net.rb
+++ b/lib/chef/win32/net.rb
@@ -286,6 +286,26 @@ END
net_api_error!(rc)
end
end
+
+ def self.net_use_del(server_name, use_name, force=:use_noforce)
+ server_name = wstring(server_name)
+ use_name = wstring(use_name)
+ force_const = case force
+ when :use_noforce
+ USE_NOFORCE
+ when :use_force
+ USE_FORCE
+ when :use_lots_of_force
+ USE_LOTS_OF_FORCE
+ else
+ raise ArgumentError, "force must be one of [:use_noforce, :use_force, or :use_lots_of_force]"
+ end
+
+ rc = NetUseDel(server_name, use_name, force_const)
+ if rc != NERR_Success
+ net_api_error!(rc)
+ end
+ end
end
NetUser = Net # For backwards compatibility
end