# class File

### Constants

DUPLEX
SETENC_BY_BOM

  Note: need to verify ::IO::SETENC_BY_BOM value before using:

TEXT

  What is the flag value under Win32?

TTY
WSPLIT
WSPLIT_INITIALIZED

### Public Class Methods

find_file_mimetype(the_file=nil) click to toggle source

    # File lib/gxg/gxg_augmented.rb, line 847
    def self.find_file_mimetype(the_file=nil)
      if the_file.is_a?(::String)
        if (/.*(?:\\|\/)(.+)$/.match(the_file.to_s) || the_file.to_s.split("/").size > 0 || the_file == "/")
          if ::File.directory?(the_file)
            raise ArgumentError, "You tried to look at a directory, not a File. (#{the_file.inspect})."
          else
            the_file = ::File.open(the_file, "r")
          end
        else
          raise ArgumentError, "You MUST provide a valid File path."
        end
      end
      if the_file.is_a?(::File)
        the_file.file_type()
      else
        raise ArgumentError, "You MUST provide a valid File object."
      end
    end

gxg_permissions_to_mode(permissions={:owner => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :group => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :other => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }}, type = :file) click to toggle source

    # File lib/gxg/gxg_augmented.rb, line 1120
    def self.gxg_permissions_to_mode(permissions={:owner => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :group => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :other => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }}, type = :file)
      result = 0
      if permissions.is_a?(::Hash)
        octet = 0
        octet_string = "0"
        unix_permissions = ::File.gxg_permissions_to_unix(permissions,type)
        #
        unix_permissions.each_char_with_index do |character, index|
          #
          if (1..3).include?(index)
            case index
            when 1
              unless character == "-"
                octet += 4
              end
            when 2
              unless character == "-"
                octet += 2
              end
            when 3
              unless character == "-"
                octet += 1
              end
            end
            if index == 3
              octet_string << octet.to_s
              octet = 0
            end
          end
          #
          if (4..6).include?(index)
            case index
            when 4
              unless character == "-"
                octet += 4
              end
            when 5
              unless character == "-"
                octet += 2
              end
            when 6
              unless character == "-"
                octet += 1
              end
            end
            if index == 6
              octet_string << octet.to_s
              octet = 0
            end
          end
          #
          if (7..9).include?(index)
            case index
            when 7
              unless character == "-"
                octet += 4
              end
            when 8
              unless character == "-"
                octet += 2
              end
            when 9
              unless character == "-"
                octet += 1
              end
            end
            if index == 9
              octet_string << octet.to_s
              octet = 0
            end
          end
          #
          result = octet_string.to_i(base=8)
        end
        #
      else
        raise ArgumentError, "You MUST provide a Hash with :owner, :group, and :other permission sub-hashes of true/false flags"
      end
      result
    end

gxg_permissions_to_unix(permissions={:owner => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :group => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :other => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }}, type = :file) click to toggle source

    # File lib/gxg/gxg_augmented.rb, line 1027
    def self.gxg_permissions_to_unix(permissions={:owner => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :group => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }, :other => { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }}, type = :file)
      result = "----------"
      if permissions.is_a?(::Hash)
        accumulator = ""
        raw_data = "----------"
        write_permissions = [:write, :create, :destroy, :rename, :move]
        case type
        when :directory
          accumulator << "d"
        when :symlink
          accumulator << "l"
        when :file
          accumulator << "-"
        else
          raise ArgumentError, "You MUST provide a valid file system entity type: :file, :directory or :symlink."
        end
        raw_data.each_char_with_index do |character, index|
          case index
          when 1
            if permissions[:owner][:read]
              accumulator << "r"
            else
              accumulator << character
            end
          when 2
            write_permissions.each do |the_perm|
              if permissions[:owner][(the_perm)]
                accumulator << "w"
                break
              end
            end
            if accumulator[-1] != "w"
              accumulator << character
            end
          when 3
            if permissions[:owner][:execute]
              accumulator << "x"
            else
              accumulator << character
            end
          when 4
            if permissions[:group][:read]
              accumulator << "r"
            else
              accumulator << character
            end
          when 5
            write_permissions.each do |the_perm|
              if permissions[:group][(the_perm)]
                accumulator << "w"
                break
              end
            end
            if accumulator[-1] != "w"
              accumulator << character
            end
          when 6
            if permissions[:group][:execute]
              accumulator << "x"
            else
              accumulator << character
            end
          when 7
            if permissions[:other][:read]
              accumulator << "r"
            else
              accumulator << character
            end
          when 8
            write_permissions.each do |the_perm|
              if permissions[:other][(the_perm)]
                accumulator << "w"
                break
              end
            end
            if accumulator[-1] != "w"
              accumulator << character
            end
          when 9
            if permissions[:other][:execute]
              accumulator << "x"
            else
              accumulator << character
            end
          end
        end
        result = accumulator
      else
        raise ArgumentError, "You MUST provide a Hash with :owner, :group, and :other permission sub-hashes of true/false flags"
      end
      result
    end

mode_permission_to_gxg(the_mode=0) click to toggle source

    # File lib/gxg/gxg_augmented.rb, line 866
    def self.mode_permission_to_gxg(the_mode=0)
      result = {}
      if the_mode.is_a?(::Numeric)
        raw_mode = the_mode.to_s(base=8)
        set_user_id = false
        set_group_id = false
        set_sticky_bit = false
        while raw_mode.size < 4
          raw_mode = ("0" << raw_mode)
        end
        if raw_mode.size > 4
          raw_mode = raw_mode[((raw_mode.size - 4)..-1)].to_s
        end
        #
        if (raw_mode[0].to_i & 4) == 4
          set_user_id = true
        end
        if (raw_mode[0].to_i & 2) == 2
          set_group_id = true
        end
        if (raw_mode[0].to_i & 1) ==  1
          set_sticky_bit = true
        end
        raw_permission = { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }
        result = {:effective => nil, :owner => raw_permission.clone, :group => raw_permission.clone, :other => raw_permission.clone}
        #
        # puts "using: #{raw_mode.inspect}"
        # owner permissions
        if (raw_mode[1].to_i & 4) == 4
          result[:owner][:read] = true
        end
        if (raw_mode[1].to_i & 2) == 2
          result[:owner][:write] = true
          result[:owner][:rename] = true
          result[:owner][:move] = true
          result[:owner][:destroy] = true
          result[:owner][:create] = true
        end
        if (raw_mode[1].to_i & 1) == 1
          result[:owner][:execute] = true
        end
        # group permissions
        if (raw_mode[2].to_i & 4) == 4
          result[:group][:read] = true
        end
        if (raw_mode[2].to_i & 2) == 2
          result[:group][:write] = true
          result[:group][:rename] = true
          result[:group][:move] = true
          result[:group][:destroy] = true
          result[:group][:create] = true
        end
        if (raw_mode[2].to_i & 1) == 1
          result[:group][:execute] = true
        end
        # other permissions
        if (raw_mode[3].to_i & 4) == 4
          result[:other][:read] = true
        end
        if (raw_mode[3].to_i & 2) == 2
          result[:other][:write] = true
          result[:other][:rename] = true
          result[:other][:move] = true
          result[:other][:destroy] = true
          result[:other][:create] = true
        end
        if (raw_mode[3].to_i & 1) == 1
          result[:other][:execute] = true
        end
      else
        raise ArgumentError, "You MUST provide an Integer"
      end
      result
    end

mode_permission_to_unix(the_mode=0, type = :file) click to toggle source

    # File lib/gxg/gxg_augmented.rb, line 941
    def self.mode_permission_to_unix(the_mode=0, type = :file)
      result = "----------"
      if the_mode.is_a?(::Numeric)
        accumulator = ""
        raw_mode = the_mode.to_s(base=8)
        if raw_mode.size < 7
          raw_mode = ("0" << raw_mode)
        end
        set_user_id = false
        set_group_id = false
        set_sticky_bit = false
        if (raw_mode[3].to_i & 4) == 4
          set_user_id = true
        end
        if (raw_mode[3].to_i & 2) == 2
          set_group_id = true
        end
        if (raw_mode[3].to_i & 1) ==  1
          set_sticky_bit = true
        end
        #
        case type.to_s.to_sym
        when :directory
          accumulator << "d"
        when :symlink
          accumulator << "l"
        when :file
          accumulator << "-"
        else
          raise ArgumentError, "You MUST provide a valid file system entity type: :file, :directory or :symlink."
        end
        # owner permissions
        if (raw_mode[1].to_i & 4) == 4
          accumulator << "r"
        else
          accumulator << "-"
        end
        if (raw_mode[1].to_i & 2) == 2
          accumulator << "w"
        else
          accumulator << "-"
        end
        if (raw_mode[1].to_i & 1) == 1
          accumulator << "x"
        else
          accumulator << "-"
        end
        # group permissions
        if (raw_mode[2].to_i & 4) == 4
          accumulator << "r"
        else
          accumulator << "-"
        end
        if (raw_mode[2].to_i & 2) == 2
          accumulator << "w"
        else
          accumulator << "-"
        end
        if (raw_mode[2].to_i & 1) == 1
          accumulator << "x"
        else
          accumulator << "-"
        end
        # other permissions
        if (raw_mode[3].to_i & 4) == 4
          accumulator << "r"
        else
          accumulator << "-"
        end
        if (raw_mode[3].to_i & 2) == 2
          accumulator << "w"
        else
          accumulator << "-"
        end
        if (raw_mode[3].to_i & 1) == 1
          accumulator << "x"
        else
          accumulator << "-"
        end
        result = accumulator
      else
        raise ArgumentError, "You MUST provide an Integer"
      end
      result
    end

unix_permissions_to_gxg(permission_string="") click to toggle source

    # File lib/gxg/gxg_augmented.rb, line 1201
    def self.unix_permissions_to_gxg(permission_string="")
      raw_permission = { :execute => false, :rename => false, :move => false, :destroy => false, :create => false, :write => false, :read => false }
      blank_permissions = {:effective => nil, :owner => raw_permission.clone, :group => raw_permission.clone, :other => raw_permission.clone}
      permission_record = blank_permissions.clone
      #
      if permission_string.is_a?(::String)
        permission_string.each_char_with_index do |character, index|
          #
          case index
          when 1
            if character == "r"
              permission_record[:owner][:read] = true
            end
          when 2
            if character == "w"
              permission_record[:owner][:write] = true
              permission_record[:owner][:rename] = true
              permission_record[:owner][:move] = true
              permission_record[:owner][:destroy] = true
              permission_record[:owner][:create] = true
            end
          when 3
            if character == "x"
              permission_record[:owner][:execute] = true
            end
          when 4
            if character == "r"
              permission_record[:group][:read] = true
            end
          when 5
            if character == "w"
              permission_record[:group][:write] = true
              permission_record[:group][:rename] = true
              permission_record[:group][:move] = true
              permission_record[:group][:destroy] = true
              permission_record[:group][:create] = true
            end
          when 6
            if character == "x"
              permission_record[:group][:execute] = true
            end
          when 7
            if character == "r"
              permission_record[:other][:read] = true
            end
          when 8
            if character == "w"
              permission_record[:other][:write] = true
              permission_record[:other][:rename] = true
              permission_record[:other][:move] = true
              permission_record[:other][:destroy] = true
              permission_record[:other][:create] = true
            end
          when 9
            if character == "x"
              permission_record[:other][:execute] = true
            end
          end
          # No way to calculate Effective Permissions - it will be nil consequently.
          #
        end
        result = permission_record
      else
        result = nil
        raise ArgumentError, "You MUST supply a valid unix permission String"
      end
      result
    end

### Public Instance Methods

file_type() click to toggle source

    # File lib/gxg/gxg_augmented.rb, line 1270
    def file_type()
      # TODO: return GxG-UFS value as well.
      type = ::MimeMagic.by_path(self.path)
      {:mime_type => type.type(), :subtype => type.subtype(), :mediatype => type.mediatype(), :extensions => type.extensions(), :comment => type.comment(), :ufs => nil}
    end