def binwrite(path, data)
      data = data.gsub(/\n\r/, "\n").gsub(/\r\n/, "\n")
      
      io = StringIO.new(data)
      begin
        # Attempt to upload to the path
        connection.sftp.upload!(io, path)
      rescue Net::SFTP::StatusException => e
        # In the event we don't have permission, upload to 
        # a temp dir, then copy with sudo
        close_sftp!
        
        temp_dir = nil
        base.as_user(nil) do
          current_dir = run("pwd").strip
          temp_dir = "#{current_dir}/.thorssh"
          # Make the dir as the default user
          run("mkdir -p \"#{temp_dir}\"")
        end
        
        temp_file = "#{temp_dir}/#{File.basename(path)}"
        
        # io = StringIO.new(data)
        io = StringIO.new(data)
        connection.sftp.upload!(io, temp_file)
        close_sftp!


        user = base.run_as_user

        # Move the file as the user
        base.as_root do
          folder_path = File.dirname(path)
          unless base.destination_files.exists?(folder_path)
            # Create the directory this is supposed to transfer into
            mkdir_p(folder_path)
            
            # And set the correct user/group
            if user
              chown(user, user, folder_path)
            end
          end
          
          # Copy the text from the source file
          run("cat #{temp_file.inspect} > #{path.inspect}")
          run("rm -f #{temp_file.inspect}")
        end
        
        unless base.destination_files.exists?(path)
          # Something went wrong
          raise PermissionError, "#{path} failed to create/update failed" 
        end
        
        if user
          # Set the correct user as if this user had uploaded
          # directly.
          base.as_root do
            chown(user, user, path)
          end
        end

        return
      end
      close_sftp!
    end
def binwrite(path, data)
      data = data.gsub(/\n\r/, "\n").gsub(/\r\n/, "\n")
      
      io = StringIO.new(data)
      begin
        # Attempt to upload to the path
        connection.sftp.upload!(io, path)
      rescue Net::SFTP::StatusException => e
        # In the event we don't have permission, upload to 
        # a temp dir, then copy with sudo
        close_sftp!
        
        temp_dir = nil
        base.as_user(nil) do
          current_dir = run("pwd").strip
          temp_dir = "#{current_dir}/.thorssh"
          # Make the dir as the default user
          run("mkdir -p \"#{temp_dir}\"")
        end
        
        temp_file = "#{temp_dir}/#{File.basename(path)}"
        
        # io = StringIO.new(data)
        io = StringIO.new(data)
        connection.sftp.upload!(io, temp_file)
        close_sftp!


        user = base.run_as_user

        # Move the file as the user
        base.as_root do
          folder_path = File.dirname(path)
          unless base.destination_files.exists?(folder_path)
            # Create the directory this is supposed to transfer into
            mkdir_p(folder_path)
            
            # And set the correct user/group
            if user
              chown(user, user, folder_path)
            end
          end
          
          # Copy the text from the source file
          run("cat #{temp_file.inspect} > #{path.inspect}")
          run("rm -f #{temp_file.inspect}")
        end
        
        unless base.destination_files.exists?(path)
          # Something went wrong
          raise PermissionError, "#{path} failed to create/update failed" 
        end
        
        if user
          # Set the correct user as if this user had uploaded
          # directly.
          base.as_root do
            chown(user, user, path)
          end
        end

        return
      end
      close_sftp!
    end
TODO: we should just move this to a more standard thing