Class: Archive::Tarfile
- Inherits:
-
Object
- Object
- Archive::Tarfile
- Defined in:
- lib/archive/tarfile.rb
Overview
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
-
#dereference ⇒ Boolean
Follow symlinks and archive the files they point to.
-
#format ⇒ String
-
gnu = GNU tar 1.13.x format * posix = POSIX 1003.1-2001 (pax) format.
-
-
#multi_volume ⇒ Boolean
Create/list/extract multi-volume archive (not yet implemented).
-
#verify ⇒ Boolean
Verify that files were copied faithfully.
Instance Method Summary collapse
-
#create_cmd ⇒ String
The shell command string to be used to create the tarfile.
-
#create_tarfile ⇒ Tarfile
Shell out to the operating system and create the tar archive file.
-
#extract_cmd ⇒ String
The shell command that will extract the tarfile’s contents # @return [Void].
-
#extract_tarfile ⇒ String
Shell out to the operating system and extract the tar archive file.
-
#initialize(options = nil) ⇒ Tarfile
constructor
Initialize a new Tarfile object.
-
#list_cmd ⇒ String
The shell command that will list the tarfile’s contents.
-
#list_tarfile ⇒ String
The list of the tarfile’s contents.
-
#source_basepath ⇒ Pathname
The directory that is the basis of relative paths.
-
#source_basepath=(base) ⇒ Void
Set the base path of the source file or directory being archived.
-
#source_fullpath ⇒ Pathname
The full path of the source file or directory being archived.
-
#source_fullpath=(source) ⇒ Void
Set the full path of the source file or directory being archived.
-
#source_relative_path ⇒ Pathname
The relative path from the source base directory to the source directory.
-
#tarfile_basepath ⇒ Pathname
The full path of the ancestor dir in which the tar file resides.
-
#tarfile_basepath=(basepath) ⇒ Void
Set the full path of the ancestor dir in which the tar file resides.
-
#tarfile_fullpath ⇒ Pathname
The full path of the tar archive file to be created or extracted from.
-
#tarfile_fullpath=(fullpath) ⇒ Void
Sets the full path of tar file.
-
#tarfile_relative_path ⇒ String
The id (path relative to basepath) of the tar file.
-
#target_pathname ⇒ Pathname
The location of the directory into which the tarfile should be extracted.
-
#target_pathname=(target) ⇒ Void
Set the location of the directory into which the tarfile should be extracted.
Constructor Details
#initialize(options = nil) ⇒ Tarfile
Returns Initialize a new Tarfile object.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/archive/tarfile.rb', line 28 def initialize(=nil) # set defaults @format=:posix @dereference = true @verify = false @multi_volume = false # override defaults ={} if .nil? .each do |key,value| #instance_variable_set("@#{key}", value) send "#{key}=", value end end |
Instance Attribute Details
#dereference ⇒ Boolean
Returns Follow symlinks and archive the files they point to.
18 19 20 |
# File 'lib/archive/tarfile.rb', line 18 def dereference @dereference end |
#format ⇒ String
-
gnu = GNU tar 1.13.x format
-
posix = POSIX 1003.1-2001 (pax) format
15 16 17 |
# File 'lib/archive/tarfile.rb', line 15 def format @format end |
#multi_volume ⇒ Boolean
Returns 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 |
#verify ⇒ Boolean
Returns Verify that files were copied faithfully.
21 22 23 |
# File 'lib/archive/tarfile.rb', line 21 def verify @verify end |
Instance Method Details
#create_cmd ⇒ String
Returns 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_tarfile ⇒ Tarfile
Returns 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_cmd ⇒ String
Returns 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_tarfile ⇒ String
Returns 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_cmd ⇒ String
Returns 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_tarfile ⇒ String
Returns 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_basepath ⇒ Pathname
Returns 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.
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). end |
#source_fullpath ⇒ Pathname
Returns 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.
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). end |
#source_relative_path ⇒ Pathname
Returns 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_basepath ⇒ Pathname
Returns 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.
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). end |
#tarfile_fullpath ⇒ Pathname
Returns 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.
62 63 64 |
# File 'lib/archive/tarfile.rb', line 62 def tarfile_fullpath=(fullpath) @tarfile_fullpath = Pathname(fullpath). end |
#tarfile_relative_path ⇒ String
Returns 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_pathname ⇒ Pathname
Returns 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.
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). end |