Class: HP::Cloud::LocalResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/hpcloud/local_resource.rb

Instance Attribute Summary

Attributes inherited from Resource

#container, #cstatus, #destination, #fname, #ftype, #path, #public, #public_url, #readers, #restart, #writers

Instance Method Summary collapse

Methods inherited from Resource

#container_head, #copy, #copy_all, #get_destination, #get_mime_type, #grant, #head, #initialize, #isDirectory, #isFile, #isLocal, #isMulti, #isObject, #isRemote, #is_container?, #is_object_store?, #is_shared?, #is_valid?, #not_implemented, #parse, #remove, #revoke, #set_error, #set_mime_type, #set_restart, #tempurl, #to_hash

Constructor Details

This class inherits a constructor from HP::Cloud::Resource

Instance Method Details

#closeObject



119
120
121
122
123
124
125
126
127
# File 'lib/hpcloud/local_resource.rb', line 119

def close()
  @pbar.increment(@lastread) unless @pbar.nil?
  @pbar.finish unless @pbar.nil?
  @lastread = 0
  @pbar = nil
  @file.close unless @file.nil?
  @file = nil
  return true
end

#copy_file(from) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hpcloud/local_resource.rb', line 129

def copy_file(from)
  if from.isLocal()
    @disable_pbar = true
  end

  return false unless open(true, from.get_size())

  result = true
  if from.isLocal()
    if (from.open() == true)
      while ((chunk = from.read()) != nil) do
        if chunk.empty?
          break
        end
        if ! write(chunk.to_s) then result = false end
      end
      result = false if ! from.close()
    else
      result = false
    end
  else
    from.read() { |chunk|
      if ! write(chunk) 
        result = false
        break
      end
    }
    result = false unless from.close()
    unless from.is_valid?
      set_error(from)
      result = false
    end
  end
  return false unless close()
  return result
end

#foreach(&block) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/hpcloud/local_resource.rb', line 166

def foreach(&block)
  if (isDirectory() == false)
     yield self
    return
  end
  begin
    Dir.foreach(path) { |x|
      if ((x != '.') && (x != '..')) then
        ResourceFactory.create_any(@storage, path + '/' + x).foreach(&block)
      end
    }
  rescue Errno::EACCES
    @cstatus  = CliStatus.new("You don't have permission to access '#{path}'.", :permission_denied)
  end
end

#get_sizeObject



26
27
28
29
30
31
32
# File 'lib/hpcloud/local_resource.rb', line 26

def get_size()
  begin
    return File.size(@fname)
  rescue
    return 0
  end
end

#open(output = false, siz = 0) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hpcloud/local_resource.rb', line 82

def open(output=false, siz=0)
  close()
  @lastread = 0
  begin
    if (output == true)
      @pbar = Progress.new(@destination, siz)
      @file = File.open(@destination, 'w')
    else
      if (@disable_pbar == false)
        @pbar = Progress.new(@fname, get_size())
      end
      @file = File.open(@fname, 'r')
    end
  rescue Exception => e
    @cstatus = CliStatus.new(e.to_s, :permission_denied)
    return false
  end
  return true
end

#read(siz = Excon::CHUNK_SIZE) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/hpcloud/local_resource.rb', line 102

def read(siz = Excon::CHUNK_SIZE)
  @pbar.increment(@lastread) unless @pbar.nil?
  if siz == 0
    @lastread = 0
    return ''
  end
  val = @file.read(siz).to_s
  @lastread = val.length
  return val
end

#set_destination(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hpcloud/local_resource.rb', line 59

def set_destination(name)
  if (@path.nil?)
    @destination = name
  else
    @destination = File.expand_path(@path)
    if isDirectory()
      @destination = "#{@destination}/#{name}"
      dir_path = File.dirname(File.expand_path(@destination))
    else
      dir_path = File.dirname(@destination)
    end
    if !File.directory?(dir_path)
      begin
        FileUtils.mkpath(dir_path)
      rescue Exception => e
        @cstatus = CliStatus.new("Error creating target directory '#{dir_path}'.", :general_error)
        return false
      end
    end
  end
  return true
end

#valid_destination(source) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hpcloud/local_resource.rb', line 42

def valid_destination(source)
  if isDirectory()
    dir_path = File.expand_path(@path)
  else
    if source.isMulti() == true
      @cstatus = CliStatus.new("Invalid target for directory/multi-file copy '#{@fname}'.", :incorrect_usage)
      return false
    end
    dir_path = File.expand_path(File.dirname(@path))
  end
  if !File.directory?(dir_path)
    @cstatus = CliStatus.new("No directory exists at '#{dir_path}'.", :not_found)
    return false
  end
  return true
end

#valid_sourceObject



34
35
36
37
38
39
40
# File 'lib/hpcloud/local_resource.rb', line 34

def valid_source()
  if !File.exists?(@fname)
    @cstatus = CliStatus.new("File not found at '#{@fname}'.", :not_found)
    return false
  end
  return true
end

#write(data) ⇒ Object



113
114
115
116
117
# File 'lib/hpcloud/local_resource.rb', line 113

def write(data)
  @pbar.increment(data.length) unless @pbar.nil?
  @file.write(data)
  return true
end