示例#1
0
static int setup_unix_environment(const char* tarfile) {
  // Extra tar achive from http filesystem.
  if (tarfile) {
    int ret;
    TAR* tar;
    char filename[PATH_MAX];
    strcpy(filename, "/mnt/http/");
    strcat(filename, tarfile);
    ret = tar_open(&tar, filename, NULL, O_RDONLY, 0, 0);
    if (ret) {
      printf("error opening %s\n", filename);
      return 1;
    }

    ret = tar_extract_all(tar, "/");
    if (ret) {
      // TODO(petewil): Remove next line before shipping
      printf("errno is %d\n", errno);
      printf("error extracting %s\n", filename);
      return 1;
    }

    ret = tar_close(tar);
    assert(ret == 0);
  }

  return 0;
}
示例#2
0
文件: theme_loader.cpp 项目: paa/vlc
bool ThemeLoader::extractTarGz( const string &tarFile, const string &rootDir )
{
    TAR *t;
#if defined( HAVE_LIBTAR_H )
    tartype_t gztype = { (openfunc_t) gzopen_frontend,
                         (closefunc_t) gzclose_frontend,
                         (readfunc_t) gzread_frontend,
                         (writefunc_t) gzwrite_frontend };

    if( tar_open( &t, (char *)tarFile.c_str(), &gztype, O_RDONLY, 0,
                  TAR_GNU ) == -1 )
#else
    if( tar_open( &t, (char *)tarFile.c_str(), O_RDONLY ) == -1 )
#endif
    {
        return false;
    }

    if( tar_extract_all( t, (char *)rootDir.c_str() ) != 0 )
    {
        tar_close( t );
        return false;
    }

    if( tar_close( t ) != 0 )
    {
        return false;
    }

    return true;
}
示例#3
0
static int setup_unix_environment(const char* tarfile) {
  // Extra tar achive from http filesystem.
  if (tarfile) {
    int ret;
    TAR* tar;
    char filename[PATH_MAX];
    strcpy(filename, "/mnt/http/");
    strcat(filename, tarfile);
    ret = tar_open(&tar, filename, NULL, O_RDONLY, 0, 0);
    if (ret) {
      printf("error opening %s\n", filename);
      return 1;
    }

    ret = tar_extract_all(tar, "/");
    if (ret) {
      printf("error extracting %s\n", filename);
      return 1;
    }

    ret = tar_close(tar);
    assert(ret == 0);
  }

  // Setup environment variables
  setenv("HOME", "/home", 1);
  setenv("PATH", "/bin", 1);
  setenv("USER", "user", 1);
  setenv("LOGNAME", "user", 1);

  setlocale(LC_CTYPE, "");
  return 0;
}
示例#4
0
/**
  Extract a tar or tar.gz archive in \arg tarfile, to the path in \a destpath.
  If \a verbose is true, echo a line on stdout for each file extracted.
  The libtar and libz implementations are used.  The file \a tarfile is tested
  for the magic number indicating a gzip file, and if present gz stream i/o is
  used, otherwise the file is assumed to be a normal .tar archive.  Note that
  the file extension is ignored since many files such as package format files,
  use other extensions.  The return result is true if the operation succeeded.
*/
bool targz_extract_all( const QString &tarfile, const QString &destpath, bool verbose )
{
    QByteArray pathnameArr = tarfile.toLocal8Bit();
    char *pathname = pathnameArr.data();
    QByteArray prefixArr = destpath.toLocal8Bit();
    char *prefix = destpath.isEmpty() ? 0 : prefixArr.data();

    TAR *tarHandle;

    int options = TAR_GNU;
    if ( verbose ) options |= TAR_VERBOSE;

    int filemode = 0;  // only care about this if creating files (ie untar)

    tartype_t *arctype = 0;

    {
        // QFile and stream go away at end of scope
        QFile tfs( tarfile );
        if ( !tfs.exists() )
        {
            qWarning( "Targz_extract_all: file %s doesnt exist", pathname );
            return false;
        }
        tfs.open(QIODevice::ReadOnly);
        QDataStream tarbytes( &tfs );
        quint8 b1, b2;

        // if the first two bytes are the magic numbers for a gzip format
        // file, use the above zlib wrapped i/o function pointers, otherwise
        // assume normal uncompressed tar format (default)
        tarbytes >> b1 >> b2;
        if ( b1 == 0x1f && b2 == 0x8b ) arctype = &zlibtype;
    }

    int result = tar_open( &tarHandle, pathname, arctype, O_RDONLY, filemode, options);

    if ( result < 0 )
    {
        qWarning( "error opening tar file %s: %s", pathname, strerror(errno) );
        return false;
    }

    result = tar_extract_all( tarHandle, prefix );

    if ( result < 0 )
        qWarning( "error extracting tar file %s: %s", pathname, strerror(errno) );

    tar_close( tarHandle );
    return ( result >= 0 );
}
示例#5
0
文件: package.cpp 项目: foo/magazyn
/*
 * Funkcja opakowująca bibliotekę libtar.
 * Funkcje biblioteki libtar przyjmują jako argumenty modyfikowalne ciągi znaków,
 * natomiast do funkcji extract_archive przekazywane są argumenty w postaci std::string.
 * Typ std::string udostępnia funkcję konwersji do niemodyfikowalnego C'owego ciągu znaków.
 * Dlatego konstruowane są tymczasowe modyfikowalne ciągi znaków, do których kopiowane są
 * znaki z argumentów std::string.
 */
void extract_archive(const std::string& archive_path, const std::string& destination_path)
{
  TAR* archive;
  char archive_path_c[256];
  strcpy(archive_path_c, archive_path.c_str());
  if(0 != tar_open(&archive, archive_path_c, NULL, O_RDONLY, 0644, TAR_GNU))
    throw std::logic_error(translate("cant_extract_tar") + " " + archive_path + ".");
  
  char dest_path_c[256];
  strcpy(dest_path_c, destination_path.c_str());
  if(0 != tar_extract_all(archive, dest_path_c))
    throw std::logic_error(translate("cant_extract_tar") + " " + archive_path + ".");

}
bool QTarBZip2Decompressor::decompressToFiles()
{
	QString root = mPathSrc + QDir::separator()  + mArchiveFileName;
  const std::string tbz2Filename = root.toStdString();
  const std::string extractTo = mPathDest.toStdString();

  FILE *tbz2File = fopen(tbz2Filename.c_str(), "rb");
  int bzError;

 const int BUF_SIZE = 10000;
// char* buf = new char[BUF_SIZE];
 boost::scoped_array< char > buf ( new char[BUF_SIZE] );
  BZFILE *pBz = BZ2_bzReadOpen(&bzError, tbz2File, 0, 0, 0, 0);

  if ( bzError != BZ_OK ) {
    qDebug() << "bzError != BZ_OK";
    BZ2_bzReadClose( &bzError, pBz );
    //delete[] buf;
    return false;
  }

  const QString tarFilename = root.section( ".bz2", 0, 0 );
  QFile tbz2FileTmp( tarFilename );
  if( !tbz2FileTmp.open( QIODevice::WriteOnly ) ) {
    BZ2_bzReadClose( &bzError, pBz );
    //delete[] buf;
    return false;
  }
  
  bzError = BZ_OK;
  while ( bzError == BZ_OK ) {
    ssize_t bytesRead = BZ2_bzRead ( &bzError, pBz, buf.get(), BUF_SIZE );
    if ( bzError == BZ_OK )
//      fwrite (buf , 1 , bytesRead , tbz2FileTmp );
    tbz2FileTmp.write( buf.get(), bytesRead );
  }
  BZ2_bzReadClose ( &bzError, pBz );

  const std::string tarFilename2 = tarFilename.toStdString();
 TAR *pTar;
 tar_open(&pTar, const_cast< char* > ( tarFilename2.c_str() ), NULL, O_RDONLY, 0644, TAR_GNU);
 tar_extract_all( pTar, const_cast< char* > ( extractTo.c_str()  ) );
 close(tar_fd(pTar));
 QFile::remove( tarFilename );
 //delete[] buf;

return true;
}
示例#7
0
文件: libtar.c 项目: LuaDist/cmake
static int
extract(char *tarfile, char *rootdir)
{
  TAR *t;

#ifdef DEBUG
  puts("opening tarfile...");
#endif
  if (tar_open(&t, tarfile,
#ifdef HAVE_LIBZ
         (use_zlib ? &gztype : NULL),
#else
         NULL,
#endif
         O_RDONLY, 0,
         (verbose ? TAR_VERBOSE : 0)
         | (use_gnu ? TAR_GNU : 0)) == -1)
  {
    fprintf(stderr, "tar_open(): %s\n", strerror(errno));
    return -1;
  }

#ifdef DEBUG
  puts("extracting tarfile...");
#endif
  if (tar_extract_all(t, rootdir) != 0)
  {
    fprintf(stderr, "tar_extract_all(): %s\n", strerror(errno));
    return -1;
  }

#ifdef DEBUG
  puts("closing tarfile...");
#endif
  if (tar_close(t) != 0)
  {
    fprintf(stderr, "tar_close(): %s\n", strerror(errno));
    return -1;
  }

  return 0;
}
示例#8
0
文件: ecl.c 项目: ageneau/ecl-android
int eclnacl_main(int argc, char **argv) {
  umount("/");
  mount("foo", "/", "memfs", 0, NULL);
  mount("./", "/mnt/tars", "httpfs", 0, NULL);

  mkdir("/home", 0777);

  /* Setup home directory to a known location. */
  setenv("HOME", "/", 1);
  /* Blank out USER and LOGNAME. */
  setenv("USER", "", 1);
  setenv("LOGNAME", "", 1);

  printf("Extracting: %s ...\n", "/mnt/tars/" DATA_FILE);
  TAR* tar;
  int ret = tar_open(&tar, "/mnt/tars/" DATA_FILE, NULL, O_RDONLY, 0, 0);
  if(!ret)
  {
    perror("tar_open");
  }

  ret = tar_extract_all(tar, "/");
  if(!ret)
  {
    perror("tar_extract_all");
  }
  printf("Extracting done\n");

  ret = tar_close(tar);
  if(!ret)
  {
    perror("tar_close");
  }

  return ecl_main(argc, argv);
}
示例#9
0
bool Drumkit::install( const QString& path )
{
	_INFOLOG( QString( "Install drumkit %1" ).arg( path ) );
#ifdef H2CORE_HAVE_LIBARCHIVE
	int r;
	struct archive* arch;
	struct archive_entry* entry;

	arch = archive_read_new();

#if ARCHIVE_VERSION_NUMBER < 3000000
	archive_read_support_compression_all( arch );
#else
	archive_read_support_filter_all( arch );
#endif

	archive_read_support_format_all( arch );

#if ARCHIVE_VERSION_NUMBER < 3000000
	if ( ( r = archive_read_open_file( arch, path.toLocal8Bit(), 10240 ) ) ) {
#else
	if ( ( r = archive_read_open_filename( arch, path.toLocal8Bit(), 10240 ) ) ) {
#endif
		_ERRORLOG( QString( "archive_read_open_file() [%1] %2" ).arg( archive_errno( arch ) ).arg( archive_error_string( arch ) ) );
		archive_read_close( arch );

		#if ARCHIVE_VERSION_NUMBER < 3000000
			archive_read_finish( arch );
		#else
			archive_read_free( arch );
		#endif

		return false;
	}
	bool ret = true;
	QString dk_dir = Filesystem::usr_drumkits_dir() + "/";
	while ( ( r = archive_read_next_header( arch, &entry ) ) != ARCHIVE_EOF ) {
		if ( r != ARCHIVE_OK ) {
			_ERRORLOG( QString( "archive_read_next_header() [%1] %2" ).arg( archive_errno( arch ) ).arg( archive_error_string( arch ) ) );
			ret = false;
			break;
		}
		QString np = dk_dir + archive_entry_pathname( entry );

		QByteArray newpath = np.toLocal8Bit();

		archive_entry_set_pathname( entry, newpath.data() );
		r = archive_read_extract( arch, entry, 0 );
		if ( r == ARCHIVE_WARN ) {
			_WARNINGLOG( QString( "archive_read_extract() [%1] %2" ).arg( archive_errno( arch ) ).arg( archive_error_string( arch ) ) );
		} else if ( r != ARCHIVE_OK ) {
			_ERRORLOG( QString( "archive_read_extract() [%1] %2" ).arg( archive_errno( arch ) ).arg( archive_error_string( arch ) ) );
			ret = false;
			break;
		}
	}
	archive_read_close( arch );

	#if ARCHIVE_VERSION_NUMBER < 3000000
		archive_read_finish( arch );
	#else
		archive_read_free( arch );
	#endif

	return ret;
#else // H2CORE_HAVE_LIBARCHIVE
#ifndef WIN32
	// GUNZIP
	QString gzd_name = path.left( path.indexOf( "." ) ) + ".tar";
	FILE* gzd_file = fopen( gzd_name.toLocal8Bit(), "wb" );
	gzFile gzip_file = gzopen( path.toLocal8Bit(), "rb" );
	if ( !gzip_file ) {
		_ERRORLOG( QString( "Error reading drumkit file: %1" ).arg( path ) );
		gzclose( gzip_file );
		fclose( gzd_file );
		return false;
	}
	uchar buf[4096];
	while ( gzread( gzip_file, buf, 4096 ) > 0 ) {
		fwrite( buf, sizeof( uchar ), 4096, gzd_file );
	}
	gzclose( gzip_file );
	fclose( gzd_file );
	// UNTAR
	TAR* tar_file;

	QByteArray tar_path = gzd_name.toLocal8Bit();

	if ( tar_open( &tar_file, tar_path.data(), NULL, O_RDONLY, 0,  TAR_GNU ) == -1 ) {
		_ERRORLOG( QString( "tar_open(): %1" ).arg( QString::fromLocal8Bit( strerror( errno ) ) ) );
		return false;
	}
	bool ret = true;
	char dst_dir[1024];
	QString dk_dir = Filesystem::usr_drumkits_dir() + "/";
	strncpy( dst_dir, dk_dir.toLocal8Bit(), 1024 );
	if ( tar_extract_all( tar_file, dst_dir ) != 0 ) {
		_ERRORLOG( QString( "tar_extract_all(): %1" ).arg( QString::fromLocal8Bit( strerror( errno ) ) ) );
		ret = false;
	}
	if ( tar_close( tar_file ) != 0 ) {
		_ERRORLOG( QString( "tar_close(): %1" ).arg( QString::fromLocal8Bit( strerror( errno ) ) ) );
		ret = false;
	}
	return ret;
#else // WIN32
	_ERRORLOG( "WIN32 NOT IMPLEMENTED" );
	return false;
#endif
#endif
}

};
示例#10
0
int mpk_package_unpackmpk(const char *package_file, char *outdir)
{
    FILE *tbz2_file, *tar_file;
    const char *tar_fpath = "/tmp/mpk-temp.tar";
    BZFILE *bz2;
    int bzerr;
    unsigned char buf[CHUNKSIZE];
    size_t n;
    TAR *tar;

    if (!package_file || !outdir)
        return MPK_FAILURE;

    if (!(tar_file = fopen(tar_fpath, "w")))
        return MPK_FAILURE;

    if (!(tbz2_file = fopen(package_file, "r"))) {
        syslog(LOG_ERR, "could not open file: %s", package_file);
        fclose(tar_file);
        return MPK_FAILURE;
    }

    /* decompress bz2 */

    bz2 = BZ2_bzReadOpen(&bzerr, tbz2_file, 0, 0, NULL, 0);
    if (bzerr != BZ_OK) {
        fclose(tbz2_file);
        fclose(tar_file);
        return MPK_FAILURE;
    }
    while (1) {
        n = BZ2_bzRead(&bzerr, bz2, buf, CHUNKSIZE);
        if (bzerr != BZ_OK && bzerr != BZ_STREAM_END) {
            fclose(tar_file);
            unlink(tar_fpath);
            BZ2_bzReadClose(&bzerr, bz2);
            fclose(tbz2_file);
            return MPK_FAILURE;
        }

        if (fwrite(buf, 1, n, tar_file) != n) {
            fclose(tar_file);
            unlink(tar_fpath);
            BZ2_bzReadClose(&bzerr, bz2);
            fclose(tbz2_file);
            return MPK_FAILURE;
        }

        if (bzerr == BZ_STREAM_END)
            break;
    }

    BZ2_bzReadClose(&bzerr, bz2);
    fclose(tbz2_file);
    fclose(tar_file);

    /* create output directory */

    if (mkdir(outdir, 0700) != 0) {
        syslog(LOG_ERR, "mkdir failed: %s", strerror(errno));
        unlink(tar_fpath);
        return MPK_FAILURE;
    }

    /* unpack tar */

    if (tar_open(&tar, tar_fpath, NULL, O_RDONLY, 0644, 0) != 0) {
        rmdir(outdir);
        unlink(tar_fpath);
        return MPK_FAILURE;
    }

    if (tar_extract_all(tar, outdir) != 0) {
        syslog(LOG_ERR, "tar_extract_all() failed: %s", strerror(errno));
        tar_close(tar);
        rmdir(outdir);
        unlink(tar_fpath);
        return MPK_FAILURE;
    }
    tar_close(tar);
    if (unlink(tar_fpath) != 0)
        return MPK_FAILURE;

    return MPK_SUCCESS;
}
示例#11
0
ResponseCode UpdateRepoTagOfTarball(std::string pathTarball, std::string repo, std::string tag, std::string &idImage) {
  TAR *tar = NULL;
  int ret = 0;
  int th_found = 0;
  int exitcode = 0;
//  char tmp_filepath[] = P_tmpdir "/libtar-tmpfile-XXXXXX";
  //gen path to upload image
  char* tmp_imagepath = tempnam (FileUtils::GetPathDir(pathTarball).c_str(), "imageDirTmp_");
  std::string pathTmpDir = tmp_imagepath;
  free(tmp_imagepath);
  std::cout << "path tmp dir: " << pathTmpDir << std::endl;

  int tmp_fd = -1;

  ret = tar_open(&tar, (char*)pathTarball.c_str(), NULL, O_RDONLY, 0, 0);
  if (ret != 0) {
	fprintf(stdout, "Fail to open file: %s\n", pathTarball.c_str());
//	return FILE_ACTION_ERROR;
	exitcode = 2;
  }
	if (exitcode == 0) {
		if (tar_extract_all(tar, (char*)pathTmpDir.c_str()) != 0) {
            fprintf(stderr, "Fail to extract file: %s\n", pathTarball.c_str());
			exitcode = 2;
		}
	}

	if (exitcode == 0) {
		ret = tar_close(tar);
		if (ret != 0) {
			perror("Failed to close tar file.");
			exitcode = 2;
		}
		tar = NULL;
	}

  //Modify repository file
  if (exitcode == 0) {
	char buf[BUFSIZ];
	ssize_t n_buf = 0;
	FILE *tmpfile = NULL;

	std::ifstream in((pathTmpDir + "/" + REPOSITORIES_FILE).c_str(), std::ios::binary);
	std::string info((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());

	JSONNode n;
	try
	{
		n = libjson::parse(info);
	} catch(const std::invalid_argument& e) {
		std::cerr << "Invalid argument: " << e.what() << '\n';
//		return FILE_ACTION_ERROR;
		exitcode = 1;
	}
	if(exitcode == 0){
		JSONNode::const_iterator i = n.begin();
		//if (i != n.end() && i -> name() == TAG_SERVICE_STR && i -> type() != JSON_ARRAY && i -> type() != JSON_NODE)
		if (i != n.end() && i -> type() == JSON_NODE){
			JSONNode tag_id_node = i->as_node();
			i = tag_id_node.begin();
			if (i != n.end() && i -> type() != JSON_ARRAY && i -> type() != JSON_NODE){
				idImage = i->as_string();
			}else{
				std::cout << "Tarball format error.\n";
		//		return FILE_ACTION_ERROR;
				exitcode = 1;
			}
		}
	}else{
		std::cout << "Tarball format error.\n";
//			return FILE_ACTION_ERROR;
		exitcode = 1;
		}
	}

	if(exitcode == 0){
		JSONNode newRepoNode(JSON_NODE);
		newRepoNode.set_name(repo);
		newRepoNode.push_back(JSONNode(tag, idImage));

		JSONNode newNode;
		newNode.push_back(newRepoNode);
		std::string content = newNode.write();

		FILE * pFile;

		if (exitcode == 0) {
			pFile = fopen ((pathTmpDir + "/" + REPOSITORIES_FILE).c_str() , "w");
			if (pFile == NULL) {
			   printf("Error opening file %s\n", (pathTmpDir + "/" + REPOSITORIES_FILE).c_str());
		//	   return FILE_ACTION_ERROR;
			   exitcode = 1;
			}
		}

		if (exitcode == 0) {
			fwrite (content.c_str() , sizeof(char), content.size(), pFile);
			fclose (pFile);
			printf("content tmp file: %s\n", content.c_str());
		}
	}

	if (exitcode == 0) {
		remove (pathTarball.c_str());
		ret = tar_open(&tar, (char*)pathTarball.c_str(), NULL, O_WRONLY | O_CREAT, 0600, 0);
		if (ret != 0) {
		  fprintf(stderr, "Fail to open file: %s\n", pathTarball.c_str());
		//	  return FILE_ACTION_ERROR;
		  exitcode = 2;
		}

		if (exitcode == 0) {
			if (tar_append_tree(tar, (char*)pathTmpDir.c_str(), "") != 0) {
			  fprintf(stderr, "Fail to compress file: %s\n", pathTarball.c_str());
		//		  return FILE_ACTION_ERROR;
			  exitcode = 2;
			}
		}

		if (exitcode == 0) {
			ret = tar_close(tar);
			if (ret != 0) {
				perror("Failed to close tar file.");
				exitcode = 2;
			} else {
				tar = NULL;
			}
		}
	}
	std::cout << "delete_folder_tree: " << pathTmpDir.c_str() << std::endl;
	if (FileUtils::delete_folder_tree(pathTmpDir.c_str())) {
		fprintf(stderr, "Fail to delete temp dir: %s\n", pathTmpDir.c_str());
	}

	if (exitcode == 0) {
		return FILE_ACTION_SUCCESS;
	} else if (exitcode == 1) {
		return FILE_ACTION_ERROR;
	} else {
		return DATA_ERROR;
	}
}