SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Drivers/Archive/Tar.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers\Drivers\Archive;
  17.  
  18. use Sprout\Helpers\Drivers\ArchiveDriver;
  19.  
  20.  
  21. /**
  22.  * Archive library tar driver.
  23.  */
  24. class Tar implements ArchiveDriver
  25. {
  26.  
  27. // Compiled archive data
  28. protected $data = '';
  29.  
  30. public function create($paths, $filename = FALSE)
  31. {
  32. // Sort the paths to make sure that directories come before files
  33. sort($paths);
  34.  
  35. foreach ($paths as $set)
  36. {
  37. // Add each path individually
  38. $this->addData($set[0], $set[1], isset($set[2]) ? $set[2] : NULL);
  39. }
  40.  
  41. $tarfile = implode('', $this->data).pack('a1024', ''); // EOF
  42.  
  43. if ($filename == FALSE)
  44. {
  45. return $tarfile;
  46. }
  47.  
  48. if (substr($filename, -3) != 'tar')
  49. {
  50. // Append tar extension
  51. $filename .= '.tar';
  52. }
  53.  
  54. // Create the file in binary write mode
  55. $file = fopen($filename, 'wb');
  56.  
  57. // Lock the file
  58. flock($file, LOCK_EX);
  59.  
  60. // Write the tar file
  61. $return = fwrite($file, $tarfile);
  62.  
  63. // Unlock the file
  64. flock($file, LOCK_UN);
  65.  
  66. // Close the file
  67. fclose($file);
  68.  
  69. return (bool) $return;
  70. }
  71.  
  72. public function addData($file, $name, $contents = NULL)
  73. {
  74. // Determine the file type
  75. $type = is_dir($file) ? 5 : (is_link($file) ? 2 : 0);
  76.  
  77. // Get file stat
  78. $stat = stat($file);
  79.  
  80. // Get path info
  81. $path = pathinfo($file);
  82.  
  83. // File header
  84. $tmpdata =
  85. pack('a100', $name). // Name of file
  86. pack('a8', sprintf('%07o', $stat[2])). // File mode
  87. pack('a8', sprintf('%07o', $stat[4])). // Owner user ID
  88. pack('a8', sprintf('%07o', $stat[5])). // Owner group ID
  89. pack('a12', sprintf('%011o', $type === 2 ? 0 : $stat[7])). // Length of file in bytes
  90. pack('a12', sprintf('%011o', $stat[9])). // Modify time of file
  91. pack('a8', str_repeat(chr(32), 8)). // Reserved for checksum for header
  92. pack('a1', $type). // Type of file
  93. pack('a100', $type === 2 ? readlink($file) : ''). // Name of linked file
  94. pack('a6', 'ustar'). // USTAR indicator
  95. pack('a2', chr(32)). // USTAR version
  96. pack('a32', 'Unknown'). // Owner user name
  97. pack('a32', 'Unknown'). // Owner group name
  98. pack('a8', chr(0)). // Device major number
  99. pack('a8', chr(0)). // Device minor number
  100. pack('a155', $path['dirname'] === '.' ? '' : $path['dirname']). // Prefix for file name
  101. pack('a12', chr(0)); // End
  102.  
  103. $checksum = pack('a8',
  104. sprintf('%07o',
  105. array_map('ord', str_split(substr($tmpdata, 0, 512))))));
  106.  
  107. $this->data[] = substr_replace($tmpdata, $checksum, 148, 8) .
  108. str_pad(file_get_contents($file), (ceil($stat[7] / 512) * 512), chr(0));
  109. }
  110.  
  111. } // End Archive_Tar_Driver Class
  112.