Beispiel #1
0
int main( int argc, char* argv[] )
{
    namespace po = boost::program_options;
    // Declare the supported options.
    po::options_description desc( "Allowed options" );
    std::string to_compress_dir;
    std::string to_uncompress_file;
    desc.add_options()
    ( "help", "produce help message" )
    ( "-c", po::value<std::string>( &to_compress_dir ), "folder to compress" )
    ( "-x", po::value<std::string>( &to_uncompress_file ), "file to extract" )
    ( "-o", po::value<std::string>(), "destination" )
    ;

    po::variables_map vm;
    po::store( po::parse_command_line( argc, argv, desc ), vm );
    po::notify( vm );

    if ( vm.count( "help" ) )
    {
        std::cout << desc << "\n";
        return 1;
    }

    if ( vm.count( "-c" ) )
    {
        std::cout << "Compressing " << vm["-c"].as<std::string>() << ".\n";
        BuildPackage( vm["-c"].as<std::string>(), vm.count( "-o" ) ? vm["-o"].as<std::string>() : std::string() );
    }
    else if ( vm.count( "-x" ) )
    {
        std::cout << "Extracting from " << vm["-x"].as<std::string>() << ".\n";
        ExtractPackage( vm["-x"].as<std::string>(), vm.count( "-o" ) ? vm["-o"].as<std::string>() : std::string() );
    }
    else
    {
        std::cout << desc << "\n";
    }

    return 0;
}
Beispiel #2
0
// package extraction tool main function --------------------------------------
//
void GETPACK_main( int argc, char **argv )
{
	// clear options of main app
	OPT_ClearOptions();

	// register local options
	OPT_RegisterStringOption( "p", "pack",   Tm_SetPackageName );
	OPT_RegisterIntOption(    "o", "offset", Tm_SetPackageOffset );
	OPT_RegisterStringOption( "f", "file",   Tm_SetFileName );
	OPT_RegisterStringOption( "l", "list",   Tm_SetListName );
	OPT_RegisterSetOption(    "v", "view",   Tm_ViewFileList );

	// exec all registered command line options
	if ( !OPT_ExecRegisteredOptions( argc, argv ) ) {
		Err_Printf( options_invalid );
		Exit( EXIT_FAILURE );
	}

	// package name is mandatory
	if ( arg_package_name[ 0 ] == 0 ) {
		Err_Printf( pack_name_missing );
		Exit( EXIT_FAILURE );
	}

	// file name is optional (operate on entire package by default)
	extract_file_name = ( arg_file_name[ 0 ] != 0 ) ? arg_file_name : NULL;

	// list name is optional (do not create a list file by default)
	list_file_name = ( arg_list_name[ 0 ] != 0 ) ? arg_list_name : NULL;

	// extract one file or all files from package
	ExtractPackage( arg_package_name, arg_package_file_offset );

	// end of sub-application
	Exit( EXIT_SUCCESS );
}
Beispiel #3
0
/**
 * This function installs a package by calling ExtractPackage to extract it and InsertPkgDB to insert the package in PACKAGES and FILESPKG tables
 * @param name A package name
 * @return If no error ocurr, 0 is returned. If the package is already installed, 1 is returned (And a message is issued). In case of error, -1 (And an error message is issued)
 */
int InstallPkg(char *package)
{
	char *ptr_name = NULL, *init_path = NULL, *input = NULL;
	char pkgfullpathname[PATH_MAX];
	char PackageOrig[PATH_MAX];
	int fd = 0;
	PkgData Data;

	/* Save locations and initialize the package structure */
	strncpy(PackageOrig, package, PATH_MAX);
	PackageOrig[strlen(package)>PATH_MAX?PATH_MAX:strlen(package)] = '\0';
	init_path = getcwd(malloc(PATH_MAX), PATH_MAX);
	memset(&Data, '\0', sizeof(PkgData));

	/* Does it fisically exist? */
	if ((fd = open(package, O_RDONLY)) < 0) {
		/* Ok, the package doesn't exists, let's go for a mirror */
		if (DownloadPkg(package, pkgfullpathname) != 0) {
			free(init_path);
			return -1;
		}
	}
	else {
		close(fd);
		/* Get the full pathname instead of its relative name */
		if (GetFileFullPath(package, pkgfullpathname)) {
			fprintf(stderr, "Failed to install %s\n", package);
			return -1;
		}
	}
	ptr_name = basename(pkgfullpathname);

	/* Switch to HOME_ROOT */
	if (chdir(HOME_ROOT)) {
		fprintf(stderr, "Can't go to %s (%s)\n", HOME_ROOT, strerror(errno));
		free(init_path);
		return -1;
	}

	/* Fill the package structure according to the package name */
	if (FillPkgDataFromPackage(&Data, ptr_name)) {
		fprintf(stdout, "Wrong package name format. It should be \"" PACKAGE_NAME_FORMAT "\"\n");
		chdir(init_path);
		return -1;
	}

	/* Open the database */
	if (sqlite3_open(dbname, &Database)) {
		fprintf(stderr, "Failed to open database %s (%s)\n", dbname, sqlite3_errmsg(Database));
		chdir(init_path);
		free(init_path);
		return -1;
	}

	/* Check if it is already installed */
	if (ExistsPkg(&Data)) {
		fprintf(stderr, "Package %s already installed\n", PackageOrig);
		chdir(init_path);
		free(init_path);
		return 1;
	}

	/* Get the package hash so we can register it in the database */
	if (GiveMeHash(pkgfullpathname, Data.crc) == -1) {
		chdir(init_path);
		free(init_path);
		return -1;
	}

	/* Extract the package itself */
	if (ExtractPackage(pkgfullpathname, &Data) == -1) {
		chdir(init_path);
		free(init_path);
		return -1;
	}
	PostInstall();

	/* Register the package in the database */
	InsertPkgDB(&Data);

	/* Clean everything up */
	sqlite3_close(Database);
	chdir(init_path);
	if (!noreadme) {
		printf("Press intro to continue");
		getline(&input, (size_t *)&fd, stdin);
		free(input);
	}
	fprintf(stdout, "Package %s installed\n", PackageOrig);
	return 0;
}