lib/u3d/utils.rb in u3d-1.1.5 vs lib/u3d/utils.rb in u3d-1.2.0

- old
+ new

@@ -149,10 +149,32 @@ def ensure_dir(dir) FileUtils.mkpath(dir) unless File.directory?(dir) end + def get_write_access(dir) + if U3dCore::Helper.operating_system == :win + yield + else + stat_command = if U3dCore::Helper.operating_system == :linux + "stat -c \"%U,%a\" #{dir}" + elsif U3dCore::Helper.operating_system == :mac + "stat -f \"%Su,%A\" #{dir}" + end + owner, access = U3dCore::CommandExecutor.execute(command: stat_command, admin: false).strip.split(',') + current_user = U3dCore::CommandExecutor.execute(command: 'whoami', admin: false) + U3dCore::CommandExecutor.execute(command: "chown #{current_user}: #{dir}", admin: true) + U3dCore::CommandExecutor.execute(command: "chmod u+w #{dir}", admin: true) + begin + yield + ensure + U3dCore::CommandExecutor.execute(command: "chown #{owner}: #{dir}", admin: true) + U3dCore::CommandExecutor.execute(command: "chmod #{access} #{dir}", admin: true) + end + end + end + # if total is nil (unknown, falls back to print_progress_nosize) def print_progress(current, total, started_at) if total.nil? print_progress_nosize(current, started_at) return @@ -189,9 +211,10 @@ getdir = Win32API.new('shell32', 'SHGetFolderPath', 'LLLLP', 'L') result = getdir.call(0, CSIDL_LOCAL_APPDATA, 0, 0, windir) raise "Unable to get Local Appdata directory, returned with value #{result}" unless result.zero? windir.rstrip! + windir = windir.encode("UTF-8", Encoding.find('filesystem')) windir = File.expand_path(windir.rstrip) return windir if Dir.exist? windir raise "Local Appdata retrieved (#{windir}) is not correct" end