Class: Orca::RemoteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/orca/remote_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, path) ⇒ RemoteFile

Returns a new instance of RemoteFile.



7
8
9
10
11
12
13
# File 'lib/orca/remote_file.rb', line 7

def initialize(context, path)
  @context = context
  @path = path
  @exists = nil
  @permissions = nil
  @owner = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/orca/remote_file.rb', line 5

def path
  @path
end

Instance Method Details

#copy_from(destination) ⇒ Object



40
41
42
# File 'lib/orca/remote_file.rb', line 40

def copy_from(destination)
  destination.copy_to(self)
end

#copy_to(destination) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/orca/remote_file.rb', line 31

def copy_to(destination)
  if destination.is_local?
    download(destination)
  else
    duplicate(destination)
  end
  destination
end

#delete!Object



57
58
59
60
61
# File 'lib/orca/remote_file.rb', line 57

def delete!
  @context.remove(path) if exists?
  invalidate!
  self
end

#download(destination) ⇒ Object



49
50
51
# File 'lib/orca/remote_file.rb', line 49

def download(destination)
  @context.download(path, destination.path)
end

#duplicate(destination) ⇒ Object



44
45
46
47
# File 'lib/orca/remote_file.rb', line 44

def duplicate(destination)
  @context.sudo("cp #{path} #{destination.path}")
  destination
end

#exists?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/orca/remote_file.rb', line 20

def exists?
  return @exists unless @exists.nil?
  result = @context.run(%[if [ -f #{path} ]; then echo "true"; else echo "false"; fi])
  @exists = result.strip == 'true'
end

#groupObject



90
91
92
# File 'lib/orca/remote_file.rb', line 90

def group
  owner[:group]
end

#hashObject



15
16
17
18
# File 'lib/orca/remote_file.rb', line 15

def hash
  return nil unless exists?
  @hash ||= @context.run("sha1sum #{path}")[0...40]
end

#is_local?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/orca/remote_file.rb', line 94

def is_local?
  false
end

#matches?(other) ⇒ Boolean

deosnt check permissions or user. should it?

Returns:

  • (Boolean)


27
28
29
# File 'lib/orca/remote_file.rb', line 27

def matches?(other)
  self.exists? && other.exists? && self.hash == other.hash
end

#ownerObject



79
80
81
82
83
84
# File 'lib/orca/remote_file.rb', line 79

def owner
  @owner ||= begin
    user, group = @context.run("stat --format=%U:%G #{path}").chomp.split(":")
    {:user => user, :group => group}
  end
end

#permissionsObject



69
70
71
# File 'lib/orca/remote_file.rb', line 69

def permissions
  @permissions ||= @context.run("stat --format=%a #{path}").strip.to_i(8)
end

#set_owner(user, group = nil) ⇒ Object



73
74
75
76
77
# File 'lib/orca/remote_file.rb', line 73

def set_owner(user, group=nil)
  @context.sudo("chown -R #{user}:#{group} #{path}")
  invalidate!
  self
end

#set_permissions(mask) ⇒ Object



63
64
65
66
67
# File 'lib/orca/remote_file.rb', line 63

def set_permissions(mask)
  @context.sudo("chmod -R #{sprintf("%o",mask)} #{path}")
  invalidate!
  self
end

#upload(source) ⇒ Object



53
54
55
# File 'lib/orca/remote_file.rb', line 53

def upload(source)
  @context.upload(source.path, path)
end

#userObject



86
87
88
# File 'lib/orca/remote_file.rb', line 86

def user
  owner[:user]
end