lib/dbi/dbrc.rb in dbi-dbrc-1.1.4 vs lib/dbi/dbrc.rb in dbi-dbrc-1.1.5

- old
+ new

@@ -1,10 +1,11 @@ require 'rbconfig' -if Config::CONFIG['host_os'].match('mswin') +if Config::CONFIG['host_os'] =~ /mswin|win32|dos/i require 'win32/file' require 'win32/dir' + require 'win32/process' end require 'sys/admin' # The DBI module serves as a namespace only. @@ -12,16 +13,17 @@ # The DBRC class encapsulates a database resource config file. class DBRC # This error is raised if anything fails trying to read the config file. - # the error that is raised. class Error < StandardError; end - # The version of this library - VERSION = '1.1.4' + # The version of the dbi-dbrc library + VERSION = '1.1.5' + @@windows = Config::CONFIG['host_os'] =~ /mswin|win32|dos/i + # The database or host to be connected to. attr_accessor :database # The user name used for the database or host connection. attr_accessor :user @@ -84,26 +86,29 @@ # # Find the first match for 'foo_user@some_database' under /usr/local # DBI::DBRC.new('some_database', 'foo_usr', '/usr/local') # def initialize(database, user=nil, dbrc_dir=nil) if dbrc_dir.nil? - if Config::CONFIG['host_os'].match('mswin') - home = ENV['USERPROFILE'] || ENV['HOME'] - file = nil - - if home - file = File.join(home, '.dbrc') + raise Error, 'bad directory' unless File.directory?(dbrc_dir) + + uid = Process.uid + home = ENV['HOME'] || ENV['USERPROFILE'] + + if home.nil? + if @@windows + home ||= Sys::Admin.get_user(uid, :localaccount => true).dir else - file = File.join(File.basename(Dir::APPDATA), '.dbrc') + home ||= Sys::Admin.get_user(uid).dir end - - @dbrc_file = file + end + + # Default to the app data directory on Windows if no home dir found + if @@windows && home.nil? + @dbrc_file = File.join(File.basename(Dir::APPDATA), '.dbrc') else - @dbrc_file = File.join( - Sys::Admin.get_user(Process.uid).dir, - '.dbrc' - ) + uid = Process.uid + @dbrc_file = File.join(Sys::Admin.get_user(uid).dir, '.dbrc') end else @dbrc_file = File.join(dbrc_dir, '.dbrc') end @@ -118,22 +123,22 @@ @maximum_reconnects = nil check_file() # If on Win32 and the file is encrypted, decrypt it. - if Config::CONFIG['host_os'].match("mswin") && File.encrypted?(@dbrc_file) + if @@windows && File.encrypted?(@dbrc_file) encrypted = true File.decrypt(@dbrc_file) end parse_dbrc_config_file() validate_data() convert_numeric_strings() create_dsn_string() # If on Win32 and the file was encrypted, re-encrypt it - if Config::CONFIG['host_os'].match("mswin") && encrypted + if @@windows && encrypted File.encrypt(@dbrc_file) end end # Inspection of the DBI::DBRC object. This is identical to the standard @@ -180,10 +185,10 @@ def check_file(file=@dbrc_file) File.open(file){ |f| # Permissions must be set to 600 or better on Unix systems. # Must be hidden on Win32 systems. - if Config::CONFIG['host_os'].match("mswin") + if @@windows unless File.hidden?(file) raise Error, "The .dbrc file must be hidden" end else unless (f.stat.mode & 077) == 0