Oihana PHP

tarCopyScript.php

Table of Contents

Functions

tarCopyScript()  : string
Builds the bash script that copies part of a directory into another through two GNU tar.

Functions

tarCopyScript()

Builds the bash script that copies part of a directory into another through two GNU tar.

tarCopyScript(string $source, array<string|int, string> $members, string $destination[, array<string|int, string> $exclude = [] ]) : string

tar -cf - | tar -xf - rather than cp, because tar never reads what it excludes: a cache or an archive left in the tree costs nothing to leave out. What this script adds is the reading of the two exit codes, which set -o pipefail would merge into one:

  • the tar that reads exits 0, 1 when files moved while it read them — modified, removed before being read, or shrunk — and 2 for a fatal error. On a directory that is in use, 1 happens by chance: the same copy passes a minute later. So the script exits with that code, for tarCopyOutcome() to judge from what tar said;
  • the tar that writes must end in 0, always: a full disk shows there. When it does not, the script exits 3, a code the reading tar never uses for this.

It runs under LC_ALL=C: a tar that speaks the language of the machine reports a moved file in that language, and tarCopyOutcome() reads the English messages of GNU tar. The script runs tar as the PATH finds it, and relies on bash for PIPESTATUS: run it with bash -c.

Every path and pattern is escaped for the shell.

Parameters
$source : string

The directory the members are read from.

$members : array<string|int, string>

What is copied, relative to $source.

$destination : string

The directory the copy is written to. It must exist.

$exclude : array<string|int, string> = []

--exclude patterns, as GNU tar reads them (tarExcludesPath()).

Tags
example
use function oihana\files\archive\tar\tarCopyOutcome;
use function oihana\files\archive\tar\tarCopyScript;

$script = tarCopyScript( '/srv/app' , [ 'content' ] , '/tmp/copy' , [ 'content/cache' ] ) ;

$process = proc_open( [ 'bash' , '-c' , $script ] , [ 2 => [ 'pipe' , 'w' ] ] , $pipes ) ;
$error   = stream_get_contents( $pipes[ 2 ] ) ;
fclose( $pipes[ 2 ] ) ;

$outcome = tarCopyOutcome( proc_close( $process ) , $error ) ;
author

Marc Alcaraz (ekameleon)

since
1.7.0
Return values
string —

The script, for bash -c.

On this page

Search results