public function testAutoRollback() { // Setup $this->autoRollbackHelper(); $this->backupFile->buildFromDirectory($this->archivedDir); $this->backupFile->addEmptyDir("testDirectory"); $this->backupFile->compress(\Phar::GZ, '.tgz'); $newFile = $this->backupPath . '/' . uniqid() . '_code.tgz'; copy($this->backupFileName, $newFile); if (file_exists($this->backupFileName)) { unset($this->backupFile); unlink($this->backupFileName); } $gtzFile = str_replace('tar', 'tgz', $this->backupFileName); if (file_exists($gtzFile)) { unlink($gtzFile); } $this->backupFileName = $newFile; // Change the contents of a.txt $this->autoRollbackHelper(1); $this->assertEquals('foo changed', file_get_contents($this->archivedDir . 'a.txt')); $this->backupInfo->expects($this->once())->method('getBlacklist')->willReturn(['excluded']); // Rollback process $this->rollBack->execute($this->backupFileName); // Assert that the contents of a.txt has been restored properly $this->assertEquals('foo', file_get_contents($this->archivedDir . 'a.txt')); }
/** * Archive cleanup */ public function __destruct() { // Compress the archive on exit if ($this->compression != Compression::NONE()) { // Due to the way Phar works, it MUST create a new archive with a different extension. We will allow this, // then rename the new archive back to the original filename $ext = uniqid(); $fn = substr($this->filename, 0, strrpos($this->filename, '.') ?: null); $this->archive->compress((int) $this->compression->value(), $ext); // New archive does not get created on empty databases if (file_exists($fn . '.' . $ext)) { rename($fn . '.' . $ext, $this->filename); } } }
public static function createArchive($arg, $origin_dir, $archive_file, $overwrite = false) { if (!extension_loaded('phar')) { throw new pakeException(__CLASS__ . ' module requires "phar" extension'); } if (false === $overwrite and file_exists($archive_file)) { return true; } if (self::endsWith($archive_file, '.tar.gz')) { $archive_file = substr($archive_file, 0, -3); $compress = Phar::GZ; $extension = '.tar.gz'; if (!extension_loaded('zlib')) { throw new pakeException('GZip compression method is not available on this system (install "zlib" extension)'); } } elseif (self::endsWith($archive_file, '.tgz')) { $archive_file = substr($archive_file, 0, -3) . 'tar'; $compress = Phar::GZ; $extension = '.tgz'; if (!extension_loaded('zlib')) { throw new pakeException('GZip compression method is not available on this system (install "zlib" extension)'); } } elseif (self::endsWith($archive_file, '.tar.bz2')) { $archive_file = substr($archive_file, 0, -4); $compress = Phar::BZ2; $extension = '.tar.bz2'; if (!extension_loaded('bz2')) { throw new pakeException('BZip2 compression method is not available on this system (install "bzip2" extension)'); } } elseif (self::endsWith($archive_file, '.tar') or self::endsWith($archive_file, '.zip')) { $compress = Phar::NONE; } else { throw new pakeException("Only .zip, .tar, .tar.gz and .tar.bz2 archives are supported"); } $files = pakeFinder::get_files_from_argument($arg, $origin_dir, true); pake_echo_action('file+', $archive_file); try { $arc = new PharData($archive_file); foreach ($files as $file) { $full_path = $origin_dir . '/' . $file; pake_echo_action('archive', '-> ' . $file); if (is_dir($full_path)) { $arc->addEmptyDir($file); } else { $arc->addFile($full_path, $file); } } if (Phar::NONE !== $compress) { $new_name = substr($archive_file, 0, -4) . $extension; pake_echo_action('file+', $new_name); $arc->compress($compress, $extension); unset($arc); pake_remove($archive_file, '/'); } } catch (PharException $e) { unset($arc); pake_remove($archive_file); throw $e; } }
/** * @covers common\classes\AutoLoader::load_extension */ public function test_load_extension() { $extensions = glob(ROOT_PATH . DS . 'extensions' . DS . '*'); foreach ($extensions as $extension) { $name = ucwords(explode('.', basename($extension))[0]); self::assertTrue(AutoLoader::load_extension($name)); } self::assertFalse(AutoLoader::load_extension('NotExistedClass')); $name = 'fakeextension'; $extension_file = ROOT_PATH . DS . 'extensions' . DS . $name; $extension_build_dir = ROOT_PATH . DS . 'tests' . DS . 'resource' . DS . $name; $extension_file_tar = "{$extension_file}.tar"; $extension_file_gz = "{$extension_file}.tar.gz"; if (file_exists($extension_file_gz)) { Phar::unlinkArchive($extension_file_gz); } $phar = new PharData($extension_file_tar); $phar->buildFromDirectory($extension_build_dir); $phar->compress(Phar::GZ); if (file_exists($extension_file_tar)) { unlink($extension_file_tar); } self::assertTrue(AutoLoader::load_extension($name)); unlink($extension_file_gz); }
/** * {@inheritdoc} */ public function archive($sources, $target, $format, array $excludes = array()) { $sources = realpath($sources); // Phar would otherwise load the file which we don't want if (file_exists($target)) { unlink($target); } try { $filename = substr($target, 0, strrpos($target, $format) - 1); // Check if compress format if (isset(static::$compressFormats[$format])) { // Current compress format supported base on tar $target = $filename . '.tar'; } $phar = new \PharData($target, null, null, static::$formats[$format]); $files = new ArchivableFilesFinder($sources, $excludes); $phar->buildFromIterator($files, $sources); if (isset(static::$compressFormats[$format])) { // Check can be compressed? if (!$phar->canCompress(static::$compressFormats[$format])) { throw new \RuntimeException(sprintf('Can not compress to %s format', $format)); } // Delete old tar unlink($target); // Compress the new tar $phar->compress(static::$compressFormats[$format]); // Make the correct filename $target = $filename . '.' . $format; } return $target; } catch (\UnexpectedValueException $e) { $message = sprintf("Could not create archive '%s' from '%s': %s", $target, $sources, $e->getMessage()); throw new \RuntimeException($message, $e->getCode(), $e); } }
/** * @param string $project * @return array $return */ public function backupArchive($project) { // Exception if not defined if (!isset($this->config[$project]['archive'])) { throw new \Exception('No "Archive config" for this project', 1); } // Get backup settings $settings = $this->getArchiveSettings($project); // Get backup file prefix $backupFilePrefix = ''; if (!empty($settings['backup_file_prefix'])) { $backupFilePrefix = $settings['backup_file_prefix']; } // Set filename $filename = $backupFilePrefix . date('Ymd_His') . '.tar'; // Data required to backup Database $path = $this->getBackupPath($project, 'archive'); // Create Archive $phar = new \PharData($path . '/' . $filename); $phar->buildFromIterator(new \ArrayIterator($this->getArchiveFilesList($project))); // Compress and unlink none compress archive $phar->compress(\Phar::GZ); unlink($path . '/' . $filename); return $this->getBackupInfo($path, $filename); }
public function imgCreate($tag, $dkFileData) { file_put_contents('Dockerfile', $dkFileData); $dkfiletar = new PharData('Dockerfile.tar'); $dkfiletar->addFile('Dockerfile'); //执行此行代码后生成Dockerfile.tar.gz文件 $dkfiletar->compress(Phar::GZ); $this->cmObj->curlPostFile(DOCKER_URL . '/build?t=' . $tag . '&nocache=1', 'Dockerfile.tar.gz', array('Content-Type:application/tar')); return $this->cmObj->curlHttpCode(); }
/** * Create tgz archive. */ public function compress() { $tarFile = $this->rootDir . '/' . $this->tmpName . '.tar'; $tgzFile = $this->rootDir . '/' . $this->tmpName . '.tgz'; $phar = new \PharData($tarFile); foreach ($this->files as $file) { $phar->addFile($file[0], $file[1]); } // Compress .tar file to .tgz $phar->compress(\Phar::GZ, '.tgz'); rename($tgzFile, $this->rootDir . '/' . $this->name); // Both files (.tar and .tgz) exist. Remove the temporary .tar archive unlink($tarFile); }
private static function build_extension($name) { $name = strtolower($name); $extension_file = ROOT_PATH . DS . 'extensions' . DS . $name; $extension_build_dir = ROOT_PATH . DS . 'build' . DS . $name; $extension_file_tar = "{$extension_file}.tar"; $extension_file_gz = "{$extension_file}.tar.gz"; if (file_exists($extension_file_gz)) { \Phar::unlinkArchive($extension_file_gz); } $phar = new \PharData($extension_file_tar); $phar->buildFromDirectory($extension_build_dir); $phar->compress(\Phar::GZ); if (file_exists($extension_file_tar)) { unlink($extension_file_tar); } }
/** * Create an empty.box in the temp dir that we can use to test * compression/decompression and adding metadata.json to it. * * PHP's PharData does NOT work with vfsStream so we're required * to put this on the actual filesystem. */ public static function setUpBeforeClass() { $fixturesPath = $GLOBALS['PHPUNIT_FIXTURES_DIR'] . DIRECTORY_SEPARATOR . 'empty.box'; $p = new \PharData("tmp.tar"); $emptyBoxFiles = array('box.ovf', 'box-disk1.vmdk', 'metadata.json', 'Vagrantfile'); foreach ($emptyBoxFiles as $file) { $p->addFile("{$fixturesPath}/{$file}", basename($file)); } $p->compress(\Phar::GZ); unset($p); // Windows: make $p release the tmp.tar file if (!unlink("tmp.tar")) { fwrite(STDERR, "unlink('tmp.tar') failed in " . getcwd() . "\n"); system('ls -la'); } rename("tmp.tar.gz", "empty.box"); self::$emptyPackageBox = file_get_contents("empty.box"); }
public function clean() { # Maximum size of log file allowed, in bytes ( 100000000 = 100 MB) $max_size = 100000000; chdir(LOG_PATH); foreach (glob("*.log") as $_file) { if (filesize($_file) >= $max_size) { $tar = new \PharData(basename($_file, ".log") . '-error-log-archive.tar'); $tar->addFile($_file); $tar->compress(\Phar::GZ); # Move tarball to archives folder once complete if (is_readable('archive/' . $_file . '-error-log-archive.tar')) { rename($_file . '-error-log-archive.tar', 'archive/' . $_file . '-error-log-archive.tar'); } else { rename($_file . '-error-log-archive.tar', 'archive/' . $_file . '_' . time() . '-error-log-archive.tar'); } } } }
function __c3_build_html_report(PHP_CodeCoverage $codeCoverage, $path) { $writer = new PHP_CodeCoverage_Report_HTML(); $writer->process($codeCoverage, $path . 'html'); if (file_exists($path . '.tar')) { unlink($path . '.tar'); } $phar = new PharData($path . '.tar'); $phar->setSignatureAlgorithm(Phar::SHA1); $files = $phar->buildFromDirectory($path . 'html'); array_map('unlink', $files); if (in_array('GZ', Phar::getSupportedCompression())) { if (file_exists($path . '.tar.gz')) { unlink($path . '.tar.gz'); } $phar->compress(\Phar::GZ); // close the file so that we can rename it unset($phar); unlink($path . '.tar'); rename($path . '.tar.gz', $path . '.tar'); } return $path . '.tar'; }
public function cmd_artifact(array $params = array()) { \System\Directory::check(BASE_DIR . static::DIR_PACKAGES); $target = BASE_DIR . static::DIR_PACKAGES . '/artifact.tar'; $result = $target . '.gz'; if (isset($params[0])) { $target = $params[0]; } if (file_exists($target)) { unlink($target); } if (file_exists($result)) { unlink($result); } $iter = new \RecursiveDirectoryIterator(BASE_DIR); $iter->setFlags(\FileSystemIterator::SKIP_DOTS); $iter = new ProjectDirectoryRecursiveIterator($iter); $iter = new \RecursiveIteratorIterator($iter); $archive = new \PharData($target); $archive->buildFromIterator($iter, BASE_DIR); $archive->compress(\Phar::GZ); unlink($target); }
/** * @inheritdoc */ public function run(&$cmdParams, &$params) { $res = true; $taskRunner = $this->taskRunner; $toBeAdded = []; $srcList = !empty($cmdParams[0]) ? $cmdParams[0] : []; $destFile = !empty($cmdParams[1]) ? $taskRunner->parsePath($cmdParams[1]) : ''; $srcBaseDir = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : ''; $format = !empty($cmdParams[3]) ? strtolower($cmdParams[3]) : self::CMP_GZ; $options = !empty($cmdParams[4]) ? $cmdParams[4] : []; if (!empty($srcBaseDir) && !is_dir($srcBaseDir)) { Log::throwException('compress: srcBaseDir has to be a directory'); } if (empty($destFile) || file_exists($destFile) && is_dir($destFile)) { Log::throwException('compress: Destination has to be a file'); } switch ($format) { case self::CMP_NONE: $extension = '.tar'; $compression = \Phar::NONE; $doCompress = false; break; case self::CMP_GZ: $extension = '.tar.gz'; $compression = \Phar::GZ; $doCompress = true; break; case self::CMP_BZ2: $extension = '.tar.bz2'; $compression = \Phar::BZ2; $doCompress = true; break; default: $extension = ''; $compression = false; $doCompress = false; Log::throwException('compress: Invalid format specified: ' . $format); break; } // if $srcList is specified but it is a string, we convert it to an array if (!empty($srcList) && is_string($srcList)) { $srcList = explode(',', $srcList); } foreach ($srcList as $srcPath) { $parsedPath = $taskRunner->parseStringAliases(trim($srcPath)); if (!empty($srcBaseDir)) { $toBeAdded[$parsedPath] = $srcBaseDir . DIRECTORY_SEPARATOR . $parsedPath; } else { $toBeAdded[] = $parsedPath; } } $this->controller->stdout("Creating archive: "); $this->controller->stdout($destFile, Console::FG_BLUE); $archive = null; $destDir = dirname($destFile); $destBaseFile = $destDir . DIRECTORY_SEPARATOR . basename($destFile, '.tar'); if (!$this->controller->dryRun) { if (!is_dir($destDir)) { FileHelper::createDirectory($destDir); } @unlink($destFile); @unlink($destBaseFile); try { $archive = new \PharData($destFile); } catch (\Exception $e) { Log::throwException($e->getMessage()); } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } $this->controller->stdout("\n"); $this->controller->stdout("Adding to archive: "); foreach ($toBeAdded as $srcRelPath => $srcFullPath) { if (file_exists($srcFullPath)) { $this->controller->stdout("\n - " . $srcFullPath); if (!$this->controller->dryRun) { if (is_dir($srcFullPath)) { $files = FileHelper::findFiles($srcFullPath, $options); $archive->buildFromIterator(new ArrayIterator($files), !empty($srcBaseDir) ? $srcBaseDir : $srcFullPath); } elseif (FileHelper::filterPath($srcFullPath, $options)) { $archive->addFile($srcFullPath, !empty($srcBaseDir) ? $srcRelPath : basename($srcFullPath)); } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } } else { $this->controller->stderr("\n{$srcFullPath} does not exists!\n", Console::FG_RED); } } $this->controller->stdout("\n"); if ($doCompress) { $this->controller->stdout("Compressing archive: "); $this->controller->stdout($destBaseFile . $extension, Console::FG_CYAN); if (!$this->controller->dryRun) { @unlink($destBaseFile . $extension); try { $archive->compress($compression, $extension); } catch (\Exception $e) { Log::throwException($e->getMessage()); } @unlink($destFile); } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } $this->controller->stdout("\n"); } return $res; }
/** * Pack to a tar.gz archive * * @param string $path * @param string $target * return bool */ public function packTargz($source, $target) { try { $a = new PharData($target . '.tar'); $a->buildFromDirectory($source); $a->compress(Phar::GZ); unset($a); Phar::unlinkArchive($target . '.tar'); return true; } catch (Exception $e) { return false; } }
/** * Check and (if available) compress TAR arctive * * @param \PharData $phar Archive to compress * @param string &$name Archive file name * * @return \PharData */ protected static function compress(\PharData $phar, &$name) { if (static::canCompress()) { \Includes\Utils\FileManager::deleteFile($name); if ($extension = static::getExtension()) { \Includes\Utils\FileManager::deleteFile($name .= '.' . $extension); } $phar->compress(self::COMPRESSION_TYPE); } return $phar; }
} if ($imageFileType == "zip") { unpack_zip($target_file); $target_file = str_replace('.zip', '.tar.gz', $target_file); $zip_file = pathinfo($_FILES["fileToUpload"]['name']); $tar_file = $target_dir . strtolower($zip_file['filename']); if (is_file($tar_file . '.tar.gz')) { unlink($tar_file . '.tar.gz'); } if (is_file($tar_file . '.zip')) { unlink($tar_file . '.zip'); } try { $a = new PharData($tar_file . '.tar'); $a->buildFromDirectory($target_dir . strtolower($zip_file['filename'])); $a->compress(Phar::GZ); unset($a); Phar::unlinkArchive($tar_file . '.tar'); } catch (Exception $e) { $response->status = 500; $response->message = $e->getMessage(); $response->json($response); } } $wrong_folder = current(explode(".", $_FILES["fileToUpload"]["name"])); //store data in DB $response = read_json($target_file, $id, 'gz', $wrong_folder); } else { $response->status = 500; $response->message = 'Sorry, your file was not uploaded.'; $response->json($response);
/** * Compress files with Tar archiver * * @param string $archive_name - archive name (zip, tgz, gz and tar.gz supported) * @param string $file_list - list of files to place into archive * @param string $dirname - directory, where the files should be get from * @return bool true */ function fn_compress_files($archive_name, $file_list, $dirname = '') { if (!class_exists('PharData')) { fn_set_notification('E', __('error'), __('error_class_phar_data_not_found')); return false; } if (empty($dirname)) { $dirname = Registry::get('config.dir.files'); } if (!is_array($file_list)) { $file_list = array($file_list); } $ext = fn_get_file_ext($archive_name); $_exts = explode('.', $archive_name); array_shift($_exts); $first_dot_ext = '.' . implode('.', $_exts); // https://bugs.php.net/bug.php?id=58852. Phar gets ext from the first dot: 'test.1.2.3.tgz' -> ext = 1.2.3.tgz $arch = fn_normalize_path($dirname . '/' . $archive_name); fn_rm($arch); if ($ext != 'zip') { $arch = fn_normalize_path($dirname . '/' . $archive_name . '.tmp'); fn_rm($arch); } if ($ext == 'gz' && strpos($archive_name, '.tar.gz') !== false) { $ext = 'tar.gz'; } try { $phar = new PharData($arch); foreach ($file_list as $file) { $path = fn_normalize_path($dirname . '/' . $file); if (is_file($path)) { $phar->addFile($path, basename($path)); } elseif (is_dir($path)) { $phar->buildFromDirectory($path); } } if ($ext == 'zip') { $phar->compressFiles(Phar::GZ); } else { $phar->compress(Phar::GZ, $first_dot_ext); // We need to unset Phar because the PharData class still has the file "open". // Windows servers cannot delete the files with the "open" handlers. unset($phar); fn_rm($arch); } } catch (Exception $e) { fn_set_notification('E', __('error'), $e->getMessage()); return false; } return true; }
function _mtzmap($type) { $types = array('MrBUMP' => array('root' => 'auto_mrbump/', 'files' => array('PostMRRefine.pdb', 'PostMRRefine.mtz'), 'log' => 'MRBUMP.log'), 'Dimple' => array('root' => 'fast_dp/dimple/', 'files' => array('final.pdb', 'final.mtz'), 'log' => 'dimple.log'), 'FastEP' => array('root' => 'fast_ep/', 'files' => array('sad.mtz', 'sad_fa.pdb'), 'log' => 'fast_ep.log')); if (!array_key_exists($type, $types)) { $this->_error('No such downstream type'); } else { $t = $types[$type]; } $info = $this->db->pq('SELECT dc.imageprefix as imp, dc.datacollectionnumber as run, dc.imagedirectory as dir, p.proposalcode || p.proposalnumber || \'-\' || s.visit_number as vis FROM datacollection dc INNER JOIN blsession s ON s.sessionid=dc.sessionid INNER JOIN proposal p ON (p.proposalid = s.proposalid) WHERE dc.datacollectionid=:1', array($this->arg('id'))); if (!sizeof($info)) { $this->_error('No such data collection', 'The specified data collection does not exist'); } else { $info = $info[0]; } $this->db->close(); $info['DIR'] = $this->ads($info['DIR']); $root = str_replace($info['VIS'], $info['VIS'] . '/processed', $info['DIR']) . $info['IMP'] . '_' . $info['RUN'] . '_/' . $t['root']; $file = $root . $t['files'][0]; if (file_exists($file)) { if ($this->has_arg('log')) { if (file_exists($root . $t['log'])) { $this->app->contentType("text/plain"); readfile($root . $t['log']); } else { $this->_error('Not found', 'That file couldnt be found'); } } else { if (!file_exists('/tmp/' . $this->arg('id') . '_' . $type . '.tar.gz')) { $a = new PharData('/tmp/' . $this->arg('id') . '_' . $type . '.tar'); $a->addFile($file, $t['files'][0]); $a->addFile($root . $t['files'][1], $t['files'][1]); $a->compress(Phar::GZ); unlink('/tmp/' . $this->arg('id') . '_' . $type . '.tar'); } $this->_header($this->arg('id') . '_' . $type . '.tar.gz'); readfile('/tmp/' . $this->arg('id') . '_' . $type . '.tar.gz'); exit; } } else { $this->_error('File not found', $type . ' files were not found'); } }
/** * Create package. */ public function create(array $args = array()) { $archBasename = $this->pkg->getSimpleName() . '-' . $this->pkg->getPrettyVersion(); /* Work around bug #67417 [NEW]: ::compress modifies archive basename creates temp file and rename it */ $tempName = getcwd() . '/pkl-tmp.tar'; if (file_exists($tempName)) { unlink($tempName); } $arch = new \PharData($tempName); $pkgDir = $this->pkg->getRootDir(); foreach ($this->pkg->getFiles() as $file) { if (is_file($file)) { $name = str_replace($pkgDir, '', $file); $arch->addFile($file, $name); } } if (file_exists($tempName)) { @unlink($tempName . '.gz'); } $arch->compress(\Phar::GZ); unset($arch); rename($tempName . '.gz', $archBasename . '.tgz'); unlink($tempName); if ($this->cb) { $cb = $this->cb; $cb($this->pkg); } }
protected function compressBackup($path) { $compressed = new \PharData(base_path() . '/backup/' . $this->timestamp . '.tar'); $compressed->buildFromDirectory($path); $compressed->compress(\Phar::GZ); }
/** * Archiving the file * * @param unknown $filePath * @param string $preFix * @param string $debug * @throws Exception */ private static function _zipFile($files, $preFix = '', $debug = false) { $tarFilePath = self::$_outputFileDir . '/' . self::FILE_NAME . '.tar'; $start = self::_log('== Archiving the file: ' . $tarFilePath, __CLASS__ . '::' . __FUNCTION__, $preFix); $csvFilePath = '/tmp/' . md5('ProductToMagento_CSV_' . trim(UDate::now())) . '.csv'; $tarFile = new PharData($tarFilePath); //add csv file self::_log('Generating the CSV file: ' . $csvFilePath, '', $preFix . self::TAB); $objWriter = PHPExcel_IOFactory::createWriter($files['phpExcel'], 'CSV'); $objWriter->save($csvFilePath); self::_log('Adding the CSV file to: ' . $tarFilePath, '', $preFix . self::TAB); $tarFile->addFile($csvFilePath, self::FILE_NAME . '.csv'); //add image files if (isset($files['imageFiles']) && count($files['imageFiles']) > 0) { $imageDir = self::$_imageDirName; $tarFile->addEmptyDir($imageDir); foreach ($files['imageFiles'] as $index => $imageFile) { self::_log('Processing file: ' . $index, '', $preFix . self::TAB . self::TAB); if (!isset($imageFile['filePath'])) { self::_log('No File Path SET. SKIP ', '', $preFix . self::TAB . self::TAB . self::TAB); continue; } if (!is_file($imageFile['filePath'])) { self::_log('File NOT FOUND: ' . $imageFile['filePath'], '', $preFix . self::TAB . self::TAB . self::TAB); continue; } $tarFile->addFile($imageFile['filePath'], $imageDir . '/' . $imageFile['fileName']); self::_log('Added File:' . $imageFile['fileName'] . ', from path: ' . $imageFile['filePath'], '', $preFix . self::TAB . self::TAB . self::TAB); } } else { self::_log('No image files to add.', '', $preFix . self::TAB); } // COMPRESS archive.tar FILE. COMPRESSED FILE WILL BE archive.tar.gz self::_log('Compressing file: ' . $tarFilePath, '', $preFix . self::TAB . self::TAB . self::TAB); $tarFile->compress(Phar::GZ); // NOTE THAT BOTH FILES WILL EXISTS. SO IF YOU WANT YOU CAN UNLINK archive.tar self::_log('REMOVING the orginal file: ' . $tarFilePath, '', $preFix . self::TAB); unlink($tarFilePath); self::_log('REMOVED', '', $preFix . self::TAB . self::TAB); //remving temp csv file self::_log('REMOVING the tmp csv file: ' . $csvFilePath, '', $preFix . self::TAB); unlink($csvFilePath); self::_log('REMOVED', '', $preFix . self::TAB . self::TAB); self::_log('== Archived', __CLASS__ . '::' . __FUNCTION__, $preFix, $start); }
/** * テンプレート一覧からのダウンロード * * @param Application $app * @param Request $request * @param $id */ public function download(Application $app, Request $request, $id) { /** @var $Template \Eccube\Entity\Template */ $Template = $app['eccube.repository.template']->find($id); if (!$Template) { throw new NotFoundHttpException(); } // 該当テンプレートのディレクトリ $config = $app['config']; $templateCode = $Template->getCode(); $targetRealDir = $config['root_dir'] . '/app/template/' . $templateCode; $targetHtmlRealDir = $config['root_dir'] . '/html/template/' . $templateCode; // 一時ディレクトリ $uniqId = sha1(Str::random(32)); $tmpDir = $config['template_temp_realdir'] . '/' . $uniqId; $appDir = $tmpDir . '/app'; $htmlDir = $tmpDir . '/html'; // ファイル名 $tarFile = $config['template_temp_realdir'] . '/' . $uniqId . '.tar'; $tarGzFile = $tarFile . '.gz'; $downloadFileName = $Template->getCode() . '.tar.gz'; // 該当テンプレートを一時ディレクトリへコピーする. $fs = new Filesystem(); $fs->mkdir(array($appDir, $htmlDir)); $fs->mirror($targetRealDir, $appDir); $fs->mirror($targetHtmlRealDir, $htmlDir); // tar.gzファイルに圧縮する. $phar = new \PharData($tarFile); $phar->buildFromDirectory($tmpDir); // appディレクトリがない場合は, 空ディレクトリを追加 // @see https://github.com/EC-CUBE/ec-cube/issues/742 if (empty($phar['app'])) { $phar->addEmptyDir('app'); } $phar->compress(\Phar::GZ); // ダウンロード完了後にファイルを削除する. // http://stackoverflow.com/questions/15238897/removing-file-after-delivering-response-with-silex-symfony $app->finish(function (Request $request, Response $response, \Silex\Application $app) use($tmpDir, $tarFile, $tarGzFile) { $app['monolog']->addDebug('remove temp file: ' . $tmpDir); $app['monolog']->addDebug('remove temp file: ' . $tarFile); $app['monolog']->addDebug('remove temp file: ' . $tarGzFile); $fs = new Filesystem(); $fs->remove($tmpDir); $fs->remove($tarFile); $fs->remove($tarGzFile); }); return $app->sendFile($tarGzFile)->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $downloadFileName); }
echo "\n{$filename}\t{$base_dir}/{$year}/{$month}/{$day}\n"; exit; } try { $filename = "{$year}{$month}{$day}" . "_old.tar"; $tmppath = "tmp/{$filename}"; echo "\r{$tmppath}\r"; if (file_exists($tmppath)) { unlink($tmppath); } if (file_exists("{$tmppath}.gz")) { unlink("{$tmppath}.gz"); } $day_tar = new PharData($tmppath); $day_tar->buildFromDirectory("{$base_dir}/{$year}/{$month}/{$day}", '/\\.js\\.old/'); $day_tar->compress(Phar::GZ); if (file_exists("{$tmppath}.gz")) { $client->putObject(array('Bucket' => "uhcan", 'Key' => "{$base_upload_dir}/{$year}/{$month}/{$filename}.gz", 'SourceFile' => "{$tmppath}.gz")); $client->waitUntilObjectExists(array('Bucket' => "uhcan", 'Key' => "{$base_upload_dir}/{$year}/{$month}/{$filename}.gz")); unset($day_tar); unlink($tmppath); unlink("{$tmppath}.gz"); Phar::unlinkArchive($tmppath); Phar::unlinkArchive("{$tmppath}.gz"); //echo "\r$tmppath\r"; } else { //echo "\rNo gzip file created: $tmppath\r"; } unset($day_tar); } catch (Exception $e) { echo "Exception : " . $e;
/** * Pack files * * @param type $files * @param type $compress */ function packFiles($files = null, $compress = false) { try { // We remove the old archive if (is_file('netbeans.tar')) { unlink('netbeans.tar'); } $phar = new PharData('netbeans.tar'); if ($files) { // We only pack the specified files } else { // We pack the whole directory $phar->buildFromDirectory('netbeans'); } if ($compress) { $phar->compress(Phar::GZ); } } catch (Exception $e) { echo "Exception : " . $e; } }
/** * Realizar un backup de la aplicación y comprimirlo. * * @param string $backupFile nombre del archivo de backup * @throws SPException * @return bool */ private static function backupApp($backupFile) { if (!class_exists('PharData')) { if (Util::runningOnWindows()) { throw new SPException(SPException::SP_CRITICAL, _('Esta operación sólo es posible en entornos Linux')); } elseif (!self::backupAppLegacyLinux($backupFile)) { throw new SPException(SPException::SP_CRITICAL, _('Error al realizar backup en modo compatibilidad')); } return true; } $compressedFile = $backupFile . '.gz'; try { if (file_exists($compressedFile)) { unlink($compressedFile); } $archive = new \PharData($backupFile); $archive->buildFromDirectory(Init::$SERVERROOT); $archive->compress(\Phar::GZ); unlink($backupFile); } catch (\Exception $e) { throw new SPException(SPException::SP_CRITICAL, $e->getMessage()); } return file_exists($backupFile); }
/** * Build docker image (Docker API v1.20) * * POST /build request returns empty body with different headers so it's not needed to build something * like UnexpectedStatusCodeException. We can get response success or fail only. * * @param array $options * dockerfile - Path within the build context to the Dockerfile. This is ignored if remote is specified and points to an individual filename. * t – A repository name (and optionally a tag) to apply to the resulting image in case of success. * remote – A Git repository URI or HTTP/HTTPS URI build source. If the URI specifies a filename, the file’s contents are placed into a file called Dockerfile. * q – Suppress verbose build output. * nocache – Do not use the cache when building the image. * pull - Attempt to pull the image even if an older image exists locally. * rm - Remove intermediate containers after a successful build (default behavior). * forcerm - Always remove intermediate containers (includes rm). * memory - Set memory limit for build. * memswap - Total memory (memory + swap), -1 to disable swap. * cpushares - CPU shares (relative weight). * cpusetcpus - CPUs in which to allow execution (e.g., 0-3, 0,1). * cpuperiod - The length of a CPU period in microseconds. * cpuquota - Microseconds of CPU time that the container can get in a CPU period * * @param $dockerfileString - Dockerfile contents * @param callable|null $callback * @return bool * @throws \GuzzleHttp\Exception\RequestException */ public function build($dockerfileString, callable $callback = null, array $options = []) { if (null === $callback) { $callback = function () { }; } // Create archive with Dockerfile content $phar = new \PharData(sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('docker_php_dockerfile_tmp_', true) . '.tar'); $phar['Dockerfile'] = $dockerfileString; $dockerfileCompressed = $phar->compress(\Phar::GZ); $dockerfileResource = fopen($dockerfileCompressed->getPath(), 'r'); // remove file from cache unlink($phar->getPath()); unset($phar); $caughtException = null; try { $response = $this->client->post('/build?' . http_build_query($options), ['headers' => ['Content-Type' => 'application/tar'], 'body' => $dockerfileResource, 'callback' => $callback, 'wait' => true]); if ($response->getStatusCode() !== '200') { return false; } } catch (RequestException $e) { /** @var RequestException $caughtException */ $caughtException = $e; } // remove second archive from cache unlink($dockerfileCompressed->getPath()); unset($dockerfileCompressed); // this is try-finally replacement for current version php54 if ($caughtException) { throw $caughtException; } return true; }
/** * Archive a directory * * @param Application $app Silex Application * @param Request $request Request parameters * * @return JsonResponse Array of objects, directory content after the archive process */ function archive(Application $app, Request $request) { if ($app["rights.canArchiveDirectory"] == false) { $app->abort(403, "This user doesn't have the rights to archive a directory"); } $dirpath = Utils\check_path($app['cakebox.root'], $request->get('path')); if (!isset($dirpath)) { $app->abort(400, "Missing parameters"); } $dir = "{$app['cakebox.root']}/{$dirpath}"; if (file_exists($dir) === false) { $app->abort(404, "Directory not found"); } if (is_dir($dir) === false) { $app->abort(403, "This is not a directory"); } $dirpath_info = pathinfo($dir); $dirname = $dirpath_info["dirname"]; $basename = $dirpath_info["basename"]; if (is_writable($dir) === false) { $app->abort(403, "Parent directory is not writable"); } $archive_path = "{$app['cakebox.root']}/{$dirpath}/../{$basename}.tar"; if (file_exists("{$archive_path}.inc") || file_exists($archive_path)) { $app->abort(406, "This directory already have a tar file or is already under a tar process"); } file_put_contents("{$archive_path}.inc", "Creation of {$basename}.tar.inc is in progress... If not, remove this file manualy."); $p = new \PharData($archive_path); $p->compress(\Phar::NONE); $p->buildFromDirectory($dir); unlink("{$archive_path}.inc"); $subRequest = Request::create('/api/directories', 'GET', ['path' => dirname($dirpath)]); return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST); }
/** * @param String $action * @param Array $httpVars * @param Array $fileVars * @throws Exception */ public function receiveAction($action, $httpVars, $fileVars) { //VAR CREATION OUTSIDE OF ALL CONDITIONS, THEY ARE "MUST HAVE" VAR !! $messages = ConfService::getMessages(); $repository = ConfService::getRepository(); $userSelection = new UserSelection($repository, $httpVars); $nodes = $userSelection->buildNodes(); $currentDirPath = AJXP_Utils::safeDirname($userSelection->getUniqueNode()->getPath()); $currentDirPath = rtrim($currentDirPath, "/") . "/"; $currentDirUrl = $userSelection->currentBaseUrl() . $currentDirPath; if (empty($httpVars["compression_id"])) { $compressionId = sha1(rand()); $httpVars["compression_id"] = $compressionId; } else { $compressionId = $httpVars["compression_id"]; } $progressCompressionFileName = $this->getPluginCacheDir(false, true) . DIRECTORY_SEPARATOR . "progressCompressionID-" . $compressionId . ".txt"; if (empty($httpVars["extraction_id"])) { $extractId = sha1(rand()); $httpVars["extraction_id"] = $extractId; } else { $extractId = $httpVars["extraction_id"]; } $progressExtractFileName = $this->getPluginCacheDir(false, true) . DIRECTORY_SEPARATOR . "progressExtractID-" . $extractId . ".txt"; if ($action == "compression") { $archiveName = AJXP_Utils::sanitize(AJXP_Utils::decodeSecureMagic($httpVars["archive_name"]), AJXP_SANITIZE_FILENAME); $archiveFormat = $httpVars["type_archive"]; $tabTypeArchive = array(".tar", ".tar.gz", ".tar.bz2"); $acceptedExtension = false; foreach ($tabTypeArchive as $extensionArchive) { if ($extensionArchive == $archiveFormat) { $acceptedExtension = true; break; } } if ($acceptedExtension == false) { file_put_contents($progressCompressionFileName, "Error : " . $messages["compression.16"]); throw new AJXP_Exception($messages["compression.16"]); } $typeArchive = $httpVars["type_archive"]; //if we can run in background we do it if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) { $archivePath = $currentDirPath . $archiveName; file_put_contents($progressCompressionFileName, $messages["compression.5"]); AJXP_Controller::applyActionInBackground($repository->getId(), "compression", $httpVars); AJXP_XMLWriter::header(); AJXP_XMLWriter::triggerBgAction("check_compression_status", array("repository_id" => $repository->getId(), "compression_id" => $compressionId, "archive_path" => SystemTextEncoding::toUTF8($archivePath)), $messages["compression.5"], true, 2); AJXP_XMLWriter::close(); return null; } else { $maxAuthorizedSize = 4294967296; $currentDirUrlLength = strlen($currentDirUrl); $tabFolders = array(); $tabAllRecursiveFiles = array(); $tabFilesNames = array(); foreach ($nodes as $node) { $nodeUrl = $node->getUrl(); if (is_file($nodeUrl) && filesize($nodeUrl) < $maxAuthorizedSize) { array_push($tabAllRecursiveFiles, $nodeUrl); array_push($tabFilesNames, substr($nodeUrl, $currentDirUrlLength)); } if (is_dir($nodeUrl)) { array_push($tabFolders, $nodeUrl); } } //DO A FOREACH OR IT'S GONNA HAVE SOME SAMES FILES NAMES foreach ($tabFolders as $value) { $dossiers = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($value)); foreach ($dossiers as $file) { if ($file->isDir()) { continue; } array_push($tabAllRecursiveFiles, $file->getPathname()); array_push($tabFilesNames, substr($file->getPathname(), $currentDirUrlLength)); } } //WE STOP IF IT'S JUST AN EMPTY FOLDER OR NO FILES if (empty($tabFilesNames)) { file_put_contents($progressCompressionFileName, "Error : " . $messages["compression.17"]); throw new AJXP_Exception($messages["compression.17"]); } try { $tmpArchiveName = tempnam(AJXP_Utils::getAjxpTmpDir(), "tar-compression") . ".tar"; $archive = new PharData($tmpArchiveName); } catch (Exception $e) { file_put_contents($progressCompressionFileName, "Error : " . $e->getMessage()); throw $e; } $counterCompression = 0; //THE TWO ARRAY ARE MERGED FOR THE FOREACH LOOP $tabAllFiles = array_combine($tabAllRecursiveFiles, $tabFilesNames); foreach ($tabAllFiles as $fullPath => $fileName) { try { $archive->addFile(AJXP_MetaStreamWrapper::getRealFSReference($fullPath), $fileName); $counterCompression++; file_put_contents($progressCompressionFileName, sprintf($messages["compression.6"], round($counterCompression / count($tabAllFiles) * 100, 0, PHP_ROUND_HALF_DOWN) . " %")); } catch (Exception $e) { unlink($tmpArchiveName); file_put_contents($progressCompressionFileName, "Error : " . $e->getMessage()); throw $e; } } $finalArchive = $tmpArchiveName; if ($typeArchive != ".tar") { $archiveTypeCompress = substr(strrchr($typeArchive, "."), 1); file_put_contents($progressCompressionFileName, sprintf($messages["compression.7"], strtoupper($archiveTypeCompress))); if ($archiveTypeCompress == "gz") { $archive->compress(Phar::GZ); } elseif ($archiveTypeCompress == "bz2") { $archive->compress(Phar::BZ2); } $finalArchive = $tmpArchiveName . "." . $archiveTypeCompress; } $destArchive = AJXP_MetaStreamWrapper::getRealFSReference($currentDirUrl . $archiveName); rename($finalArchive, $destArchive); AJXP_Controller::applyHook("node.before_create", array($destArchive, filesize($destArchive))); if (file_exists($tmpArchiveName)) { unlink($tmpArchiveName); unlink(substr($tmpArchiveName, 0, -4)); } $newNode = new AJXP_Node($currentDirUrl . $archiveName); AJXP_Controller::applyHook("node.change", array(null, $newNode, false)); file_put_contents($progressCompressionFileName, "SUCCESS"); } } elseif ($action == "check_compression_status") { $archivePath = AJXP_Utils::decodeSecureMagic($httpVars["archive_path"]); $progressCompression = file_get_contents($progressCompressionFileName); $substrProgressCompression = substr($progressCompression, 0, 5); if ($progressCompression != "SUCCESS" && $substrProgressCompression != "Error") { AJXP_XMLWriter::header(); AJXP_XMLWriter::triggerBgAction("check_compression_status", array("repository_id" => $repository->getId(), "compression_id" => $compressionId, "archive_path" => SystemTextEncoding::toUTF8($archivePath)), $progressCompression, true, 5); AJXP_XMLWriter::close(); } elseif ($progressCompression == "SUCCESS") { $newNode = new AJXP_Node($userSelection->currentBaseUrl() . $archivePath); $nodesDiffs = array("ADD" => array($newNode), "REMOVE" => array(), "UPDATE" => array()); AJXP_Controller::applyHook("node.change", array(null, $newNode, false)); AJXP_XMLWriter::header(); AJXP_XMLWriter::sendMessage($messages["compression.8"], null); AJXP_XMLWriter::writeNodesDiff($nodesDiffs, true); AJXP_XMLWriter::close(); if (file_exists($progressCompressionFileName)) { unlink($progressCompressionFileName); } } elseif ($substrProgressCompression == "Error") { AJXP_XMLWriter::header(); AJXP_XMLWriter::sendMessage(null, $progressCompression); AJXP_XMLWriter::close(); if (file_exists($progressCompressionFileName)) { unlink($progressCompressionFileName); } } } elseif ($action == "extraction") { $fileArchive = AJXP_Utils::sanitize(AJXP_Utils::decodeSecureMagic($httpVars["file"]), AJXP_SANITIZE_DIRNAME); $fileArchive = substr(strrchr($fileArchive, DIRECTORY_SEPARATOR), 1); $authorizedExtension = array("tar" => 4, "gz" => 7, "bz2" => 8); $acceptedArchive = false; $extensionLength = 0; $counterExtract = 0; $currentAllPydioPath = $currentDirUrl . $fileArchive; $pharCurrentAllPydioPath = "phar://" . AJXP_MetaStreamWrapper::getRealFSReference($currentAllPydioPath); $pathInfoCurrentAllPydioPath = pathinfo($currentAllPydioPath, PATHINFO_EXTENSION); //WE TAKE ONLY TAR, TAR.GZ AND TAR.BZ2 ARCHIVES foreach ($authorizedExtension as $extension => $strlenExtension) { if ($pathInfoCurrentAllPydioPath == $extension) { $acceptedArchive = true; $extensionLength = $strlenExtension; break; } } if ($acceptedArchive == false) { file_put_contents($progressExtractFileName, "Error : " . $messages["compression.15"]); throw new AJXP_Exception($messages["compression.15"]); } $onlyFileName = substr($fileArchive, 0, -$extensionLength); $lastPosOnlyFileName = strrpos($onlyFileName, "-"); $tmpOnlyFileName = substr($onlyFileName, 0, $lastPosOnlyFileName); $counterDuplicate = substr($onlyFileName, $lastPosOnlyFileName + 1); if (!is_int($lastPosOnlyFileName) || !is_int($counterDuplicate)) { $tmpOnlyFileName = $onlyFileName; $counterDuplicate = 1; } while (file_exists($currentDirUrl . $onlyFileName)) { $onlyFileName = $tmpOnlyFileName . "-" . $counterDuplicate; $counterDuplicate++; } if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) { file_put_contents($progressExtractFileName, $messages["compression.12"]); AJXP_Controller::applyActionInBackground($repository->getId(), "extraction", $httpVars); AJXP_XMLWriter::header(); AJXP_XMLWriter::triggerBgAction("check_extraction_status", array("repository_id" => $repository->getId(), "extraction_id" => $extractId, "currentDirUrl" => $currentDirUrl, "onlyFileName" => $onlyFileName), $messages["compression.12"], true, 2); AJXP_XMLWriter::close(); return null; } mkdir($currentDirUrl . $onlyFileName, 0777, true); chmod(AJXP_MetaStreamWrapper::getRealFSReference($currentDirUrl . $onlyFileName), 0777); try { $archive = new PharData(AJXP_MetaStreamWrapper::getRealFSReference($currentAllPydioPath)); $fichiersArchive = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pharCurrentAllPydioPath)); foreach ($fichiersArchive as $file) { $fileGetPathName = $file->getPathname(); if ($file->isDir()) { continue; } $fileNameInArchive = substr(strstr($fileGetPathName, $fileArchive), strlen($fileArchive) + 1); try { $archive->extractTo(AJXP_MetaStreamWrapper::getRealFSReference($currentDirUrl . $onlyFileName), $fileNameInArchive, false); } catch (Exception $e) { file_put_contents($progressExtractFileName, "Error : " . $e->getMessage()); throw new AJXP_Exception($e); } $counterExtract++; file_put_contents($progressExtractFileName, sprintf($messages["compression.13"], round($counterExtract / $archive->count() * 100, 0, PHP_ROUND_HALF_DOWN) . " %")); } } catch (Exception $e) { file_put_contents($progressExtractFileName, "Error : " . $e->getMessage()); throw new AJXP_Exception($e); } file_put_contents($progressExtractFileName, "SUCCESS"); $newNode = new AJXP_Node($currentDirUrl . $onlyFileName); AJXP_Controller::findActionAndApply("index", array("file" => $newNode->getPath()), array()); } elseif ($action == "check_extraction_status") { $currentDirUrl = $httpVars["currentDirUrl"]; $onlyFileName = $httpVars["onlyFileName"]; $progressExtract = file_get_contents($progressExtractFileName); $substrProgressExtract = substr($progressExtract, 0, 5); if ($progressExtract != "SUCCESS" && $progressExtract != "INDEX" && $substrProgressExtract != "Error") { AJXP_XMLWriter::header(); AJXP_XMLWriter::triggerBgAction("check_extraction_status", array("repository_id" => $repository->getId(), "extraction_id" => $extractId, "currentDirUrl" => $currentDirUrl, "onlyFileName" => $onlyFileName), $progressExtract, true, 4); AJXP_XMLWriter::close(); } elseif ($progressExtract == "SUCCESS") { $newNode = new AJXP_Node($currentDirUrl . $onlyFileName); $nodesDiffs = array("ADD" => array($newNode), "REMOVE" => array(), "UPDATE" => array()); AJXP_Controller::applyHook("node.change", array(null, $newNode, false)); AJXP_XMLWriter::header(); AJXP_XMLWriter::sendMessage(sprintf($messages["compression.14"], $onlyFileName), null); AJXP_XMLWriter::triggerBgAction("check_index_status", array("repository_id" => $newNode->getRepositoryId()), "starting indexation", true, 5); AJXP_XMLWriter::writeNodesDiff($nodesDiffs, true); AJXP_XMLWriter::close(); if (file_exists($progressExtractFileName)) { unlink($progressExtractFileName); } } elseif ($substrProgressExtract == "Error") { AJXP_XMLWriter::header(); AJXP_XMLWriter::sendMessage(null, $progressExtract); AJXP_XMLWriter::close(); if (file_exists($progressExtractFileName)) { unlink($progressExtractFileName); } } } }
/** * @throws BuildException */ public function main() { $this->checkPreconditions(); try { $this->log('Building archive: ' . $this->destinationFile->__toString(), Project::MSG_INFO); /** * Delete old archive, if exists. */ if ($this->destinationFile->exists()) { $isDeleted = $this->destinationFile->delete(); if (!$isDeleted) { $this->log("Could not delete destination file {$this->destinationFile}", Project::MSG_WARN); } } $pharData = new PharData($this->baseDirectory->getPath() . '/' . $this->destinationFile->getName()); foreach ($this->filesets as $fileset) { $this->log('Adding specified files in ' . $fileset->getDir($this->project) . ' to archive', Project::MSG_VERBOSE); $pharData->buildFromIterator($fileset->getIterator(), $fileset->getDir($this->project)); } if ($this->compression !== PHAR::NONE && $pharData->canCompress($this->compression)) { try { $pharData->compress($this->compression); } catch (UnexpectedValueException $uve) { $pharData->compressFiles($this->compression); } unset($pharData); } } catch (Exception $e) { throw new BuildException('Problem creating archive: ' . $e->getMessage(), $e, $this->getLocation()); } }