module EasyIO::Disk

Constants

GetDiskFreeSpaceEx

Public Instance Methods

_cross_domain_entry_copy(source_entry, destination_entry, source_credentials: nil, destination_credentials: nil) click to toggle source
# File lib/easy_io/disk.rb, line 93
def _cross_domain_entry_copy(source_entry, destination_entry, source_credentials: nil, destination_credentials: nil)
  source_credentials ||= :current_user
  destination_credentials ||= :current_user

  # TODO: loop and read to async buffer and write as it goes
  if source_credentials == :current_user
    # TODO: read to memeory
  else
    Process.create(command_line: ?, domain: source_credentials['domain'], with_logon: source_credentials['user_name'], password: source_credentials['password'])
    # TODO: read to memory
  end

  if destination_credentials == :current_user
    # TODO: write from memory
  else
    Process.create(etc)
    # TODO: write from memory
  end
end
cross_domain_copy(source_folder, destination_folder, source_credentials: nil, destination_credentials: nil, file_filter: '*') click to toggle source

When credentials are nil, the current user will be used

# File lib/easy_io/disk.rb, line 85
def cross_domain_copy(source_folder, destination_folder, source_credentials: nil, destination_credentials: nil, file_filter: '*')
  # require 'win32/process'
  Dir.glob("#{source_folder}/#{file_filter}").each do |source_entry|
    destination_entry = source_entry.sub(source_folder, destination_folder)
    _cross_domain_entry_copy(source_entry, destination_entry, source_credentials: source_credentials, destination_credentials: destination_credentials)
  end
end
free_space(path) click to toggle source
# File lib/easy_io/disk.rb, line 9
def free_space(path)
  raise 'Cannot check free space for path provided. Path is empty or nil.' if path.nil? || path.empty? || path == 'null'
  root_folder = root_directory(path)

  raise "Cannot check free space for #{path} - The path was not found." if root_folder.nil? || root_folder.empty?
  root_folder = EasyFormat::Directory.ensure_trailing_slash(root_folder)

  free = [0].pack('Q')
  GetDiskFreeSpaceEx.call(root_folder, 0, 0, free)
  free = free.unpack1('Q')

  (free / 1024.0 / 1024.0).round(2)
end
move_files(search_string, destination_folder) click to toggle source
# File lib/easy_io/disk.rb, line 37
def move_files(search_string, destination_folder)
  files = Dir.glob(search_string.tr('\\', '/'))
  FileUtils.mkdir_p destination_folder unless ::File.directory? destination_folder
  FileUtils.move(files, destination_folder) { true }
end
open_file_and_wait_for_exclusive_lock(path, timeout: 60, status_interval: 15, silent: false) { |file| ... } click to toggle source

Opens a file in the filesystem and locks it exclusively. If it fails, it will keep trying until the timeout. Pass a block to be executed while the file is locked. The ::File object is passed to the block.

# File lib/easy_io/disk.rb, line 62
def open_file_and_wait_for_exclusive_lock(path, timeout: 60, status_interval: 15, silent: false)
  start_time = Time.now
  raise "Cannot create #{::File.basename(path)} - the parent directory does not exist (#{::File.dirname(path)})!" unless ::File.directory?(::File.dirname(path))
  ::File.open(path, ::File::RDWR | ::File::CREAT) do |file|
    loop do
      if Time.now >= start_time + timeout # locking timed out.
        file.close
        raise "Failed to gain exclusive lock on #{path}! Timed out after #{timeout} seconds."
      end
      lock_success = file.flock(File::LOCK_EX | File::LOCK_NB)
      if lock_success
        yield(file) if block_given?
        file.close
        break
      end
      seconds_elapsed = Time.now - start_time
      EasyIO.logger.info "Waiting for another process to unlock #{path}... Time elapsed: #{seconds_elapsed}" if seconds_elapsed % status_interval == 0 && !silent # Output status every (status_interval) seconds
      sleep(1)
    end
  end
end
read_files(search_string) click to toggle source
# File lib/easy_io/disk.rb, line 43
def read_files(search_string)
  files = Dir.glob(search_string.tr('\\', '/'))
  files.map { |file| ::File.read(file) }
end
root_directory(path) click to toggle source

Gets the root directory of a path. Local and UNC paths accepted

# File lib/easy_io/disk.rb, line 49
def root_directory(path)
  computed_path = path
  computed_path = File.dirname(computed_path) while computed_path != File.dirname(computed_path)
  computed_path
end
size(path) click to toggle source
# File lib/easy_io/disk.rb, line 23
def size(path)
  raise 'Cannot check free space for path provided. Path is empty or nil.' if path.nil? || path.empty? || path == 'null'
  root_folder = root_directory(path)

  raise "Cannot check free space for #{path} - The path was not found." if root_folder.nil? || root_folder.empty?
  root_folder = EasyFormat::Directory.ensure_trailing_slash(root_folder)

  total = [0].pack('Q')
  GetDiskFreeSpaceEx.call(root_folder, 0, total, 0)
  total = total.unpack1('Q')

  (total / 1024.0 / 1024.0).round(2)
end
valid_checksum?(file, sha256_checksum) click to toggle source
# File lib/easy_io/disk.rb, line 55
def valid_checksum?(file, sha256_checksum)
  return false if sha256_checksum.nil? || !::File.exist?(file)
  Digest::SHA256.file(file).hexdigest.downcase == sha256_checksum.downcase
end