Class: Archive::Tarfile

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/tarfile.rb

Overview

Note:

Copyright © 2014 by The Board of Trustees of the Leland Stanford Junior University. All rights reserved. See LICENSE for details.

A tar archive file containing a set of digital object files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Tarfile

Returns Initialize a new Tarfile object.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: nil)

    Key,Value pairs specifying initial values of attributes



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/archive/tarfile.rb', line 28

def initialize(options=nil)
  # set defaults
  @format=:posix
  @dereference = true
  @verify = false
  @multi_volume = false
  # override defaults
  options={} if options.nil?
  options.each do |key,value|
    #instance_variable_set("@#{key}", value)
    send "#{key}=", value
  end
end

Instance Attribute Details

#dereferenceBoolean

Returns Follow symlinks and archive the files they point to.

Returns:

  • (Boolean)

    Follow symlinks and archive the files they point to



18
19
20
# File 'lib/archive/tarfile.rb', line 18

def dereference
  @dereference
end

#formatString

  • gnu = GNU tar 1.13.x format

  • posix = POSIX 1003.1-2001 (pax) format

Returns:

  • (String)

    create archive of the specified format



15
16
17
# File 'lib/archive/tarfile.rb', line 15

def format
  @format
end

#multi_volumeBoolean

Returns Create/list/extract multi-volume archive (not yet implemented).

Returns:

  • (Boolean)

    Create/list/extract multi-volume archive (not yet implemented)



24
25
26
# File 'lib/archive/tarfile.rb', line 24

def multi_volume
  @multi_volume
end

#verifyBoolean

Returns Verify that files were copied faithfully.

Returns:

  • (Boolean)

    Verify that files were copied faithfully



21
22
23
# File 'lib/archive/tarfile.rb', line 21

def verify
  @verify
end

Instance Method Details

#create_cmdString

Returns The shell command string to be used to create the tarfile.

Returns:

  • (String)

    The shell command string to be used to create the tarfile



102
103
104
105
106
107
108
109
# File 'lib/archive/tarfile.rb', line 102

def create_cmd
  command = "tar --create --file=#{tarfile_fullpath} --format=#{@format} "
  command << "--dereference " if @dereference
  command << "--verify " if @verify
  command << "--directory='#{source_basepath}' " if source_basepath
  command << source_relative_path.to_s
  command
end

#create_tarfileTarfile

Returns Shell out to the operating system and create the tar archive file.

Returns:

  • (Tarfile)

    Shell out to the operating system and create the tar archive file



112
113
114
115
116
# File 'lib/archive/tarfile.rb', line 112

def create_tarfile
  command = create_cmd
  OperatingSystem.execute(command)
  self
end

#extract_cmdString

Returns The shell command that will extract the tarfile’s contents # @return [Void].

Returns:

  • (String)

    The shell command that will extract the tarfile’s contents # @return [Void]



145
146
147
148
149
# File 'lib/archive/tarfile.rb', line 145

def extract_cmd
  command = "tar --extract --file=#{tarfile_fullpath} "
  command << "--directory='#{target_pathname}' " if target_pathname
  command
end

#extract_tarfileString

Returns Shell out to the operating system and extract the tar archive file.

Returns:

  • (String)

    Shell out to the operating system and extract the tar archive file



152
153
154
155
156
# File 'lib/archive/tarfile.rb', line 152

def extract_tarfile
  command = extract_cmd
  stdout = OperatingSystem.execute(command)
  stdout
end

#list_cmdString

Returns The shell command that will list the tarfile’s contents.

Returns:

  • (String)

    The shell command that will list the tarfile’s contents



119
120
121
122
# File 'lib/archive/tarfile.rb', line 119

def list_cmd
  command = "tar --list --file=#{tarfile_fullpath} "
  command
end

#list_tarfileString

Returns The list of the tarfile’s contents.

Returns:

  • (String)

    The list of the tarfile’s contents



125
126
127
128
129
# File 'lib/archive/tarfile.rb', line 125

def list_tarfile
  command = list_cmd
  list = OperatingSystem.execute(command)
  list
end

#source_basepathPathname

Returns The directory that is the basis of relative paths.

Returns:

  • (Pathname)

    The directory that is the basis of relative paths



85
86
87
# File 'lib/archive/tarfile.rb', line 85

def source_basepath
  @source_basepath
end

#source_basepath=(base) ⇒ Void

Returns Set the base path of the source file or directory being archived.

Parameters:

  • base (Pathname, String)

    The directory that is the basis of relative paths

Returns:

  • (Void)

    Set the base path of the source file or directory being archived



91
92
93
94
# File 'lib/archive/tarfile.rb', line 91

def source_basepath=(base)
  raise "No pathname specified" unless base
  @source_basepath = Pathname(base).expand_path
end

#source_fullpathPathname

Returns The full path of the source file or directory being archived.

Returns:

  • (Pathname)

    The full path of the source file or directory being archived



72
73
74
75
# File 'lib/archive/tarfile.rb', line 72

def source_fullpath
  raise "Source pathname is nil" unless @source_pathname
  @source_pathname
end

#source_fullpath=(source) ⇒ Void

Returns Set the full path of the source file or directory being archived.

Parameters:

  • source (Pathname, String)

    The full path of the source file or directory being archived

Returns:

  • (Void)

    Set the full path of the source file or directory being archived



79
80
81
82
# File 'lib/archive/tarfile.rb', line 79

def source_fullpath=(source)
  raise "No pathname specified" unless source
  @source_pathname = Pathname(source).expand_path
end

#source_relative_pathPathname

Returns The relative path from the source base directory to the source directory.

Returns:

  • (Pathname)

    The relative path from the source base directory to the source directory



97
98
99
# File 'lib/archive/tarfile.rb', line 97

def source_relative_path
    source_fullpath.relative_path_from(source_basepath)
end

#tarfile_basepathPathname

Returns The full path of the ancestor dir in which the tar file resides.

Returns:

  • (Pathname)

    The full path of the ancestor dir in which the tar file resides



43
44
45
46
# File 'lib/archive/tarfile.rb', line 43

def tarfile_basepath
  raise "Tarfile basepath is nil" unless @tarfile_basepath
  @tarfile_basepath
end

#tarfile_basepath=(basepath) ⇒ Void

Returns Set the full path of the ancestor dir in which the tar file resides.

Parameters:

  • basepath (Pathname, String)

    The full path of the ancestor dir in which the tar file resides

Returns:

  • (Void)

    Set the full path of the ancestor dir in which the tar file resides



50
51
52
53
# File 'lib/archive/tarfile.rb', line 50

def tarfile_basepath=(basepath)
  raise "No pathname specified" unless basepath
  @tarfile_basepath = Pathname(basepath).expand_path
end

#tarfile_fullpathPathname

Returns the full path of the tar archive file to be created or extracted from.

Returns:

  • (Pathname)

    the full path of the tar archive file to be created or extracted from



56
57
58
# File 'lib/archive/tarfile.rb', line 56

def tarfile_fullpath
  @tarfile_fullpath
end

#tarfile_fullpath=(fullpath) ⇒ Void

Returns Sets the full path of tar file.

Parameters:

  • fullpath (Pathname, String)

    The full path of tar file

Returns:

  • (Void)

    Sets the full path of tar file



62
63
64
# File 'lib/archive/tarfile.rb', line 62

def tarfile_fullpath=(fullpath)
  @tarfile_fullpath = Pathname(fullpath).expand_path
end

#tarfile_relative_pathString

Returns The id (path relative to basepath) of the tar file.

Returns:

  • (String)

    The id (path relative to basepath) of the tar file



67
68
69
# File 'lib/archive/tarfile.rb', line 67

def tarfile_relative_path
  @tarfile_fullpath.relative_path_from(@tarfile_basepath).to_s
end

#target_pathnamePathname

Returns The location of the directory into which the tarfile should be extracted.

Returns:

  • (Pathname)

    The location of the directory into which the tarfile should be extracted



132
133
134
135
# File 'lib/archive/tarfile.rb', line 132

def target_pathname
  raise "Target pathname is nil" unless @target_pathname
  @target_pathname
end

#target_pathname=(target) ⇒ Void

Returns Set the location of the directory into which the tarfile should be extracted.

Parameters:

  • source (Pathname, String)

    The location of the directory into which the tarfile should be extracted

Returns:

  • (Void)

    Set the location of the directory into which the tarfile should be extracted



139
140
141
142
# File 'lib/archive/tarfile.rb', line 139

def target_pathname=(target)
  raise "No target pathname specified" unless target
  @target_pathname = Pathname(target).expand_path
end