Example #1
0
bool Bundle::extractArchive(const KArchiveDirectory *dir, const QString &path)
{
    const QStringList l = dir->entries();

    QStringList::const_iterator it;
    for (it = l.constBegin(); it != l.constEnd(); ++it) {
        const KArchiveEntry* entry = dir->entry((*it));
        QString fullPath = QString("%1/%2").arg(path).arg(*it);
        if (entry->isDirectory()) {
            QString outDir = QString("%1%2").arg(m_tempDir->name()).arg(path);
            QDir qdir(outDir);
            qdir.mkdir(*it);
            extractArchive(static_cast<const KArchiveDirectory*>(entry), fullPath);
        } else if (entry->isFile()) {
            QString outName = QString("%1%2").arg(m_tempDir->name()).arg(fullPath.remove(0, 1));
            //qDebug()<<"-------- "<<outName;
            QFile f(outName);
            if (!f.open(QIODevice::WriteOnly)) {
                qWarning("Couldn't create %s", qPrintable(outName));
                continue;
            }
            const KArchiveFile *archiveFile = static_cast<const KArchiveFile*>(entry);
            f.write(archiveFile->data());
            f.close();
        } else {
            qWarning("Unidentified entry at %s", qPrintable(fullPath));
        }
    }
    return true;
}
Example #2
0
int LibArchiveWrapper::extract(std::string filename, std::string directory) {
	const char* file = filename.c_str();
	const char* dir = directory.c_str();
	//If directory structure doesnt exist create one
	boost::filesystem::path Path(directory);
	if(!boost::filesystem::is_directory(Path)){
		if(boost::filesystem::is_regular_file(Path)){
			return -1;
		}else{
			boost::filesystem::create_directories(Path);
		}
	}
	return extractArchive(file, dir);
}
Example #3
0
bool Bundle::open()
{
    if (!m_tempDir) {
        initTempDir();
    }

    if (m_data.isEmpty()) {
        return false;
    }

    QBuffer buffer(&m_data);
    KZip zip(&buffer);
    if (!zip.open(QIODevice::ReadOnly)) {
        qWarning("Couldn't open the bundle!");
        return false;
    }

    const KArchiveDirectory *dir = zip.directory();

    const KArchiveDirectory *foundDir = recursiveFind(dir);
    if (!foundDir) {
        qWarning("not a bundle");
        m_isValid = false;
        zip.close();
        return 0;
    }

    m_isValid = extractArchive(foundDir, QLatin1String(""));
    qDebug()<<"Dir = "<<foundDir->name() << m_isValid;

    if (m_isValid) {
        setPath(m_tempDir->name());
    }

    zip.close();

    return m_isValid;
}
int main(int argc, char **argv){
	return extractArchive("archive.zip", "test-data/archive_out");
}
Example #5
0
int main(int argc, char **argv) {
  GWEN_DB_NODE *db;
  const char *cmd;
  int rv;
  int err;
  GWEN_GUI *gui;
  const GWEN_ARGS args[]={
  {
    GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
    GWEN_ArgsType_Int,             /* type */
    "help",                       /* name */
    0,                            /* minnum */
    0,                            /* maxnum */
    "h",                          /* short option */
    "help",                       /* long option */
    "Show this help screen",      /* short description */
    "Show this help screen"       /* long description */
  }
  };

  err=GWEN_Init();
  if (err) {
    fprintf(stderr, "Could not initialize Gwenhywfar.\n");
    return 2;
  }

  gui=GWEN_Gui_CGui_new();
  GWEN_Gui_SetGui(gui);

  GWEN_Logger_Open(GSA_LOGDOMAIN, "gsa", 0,
		   GWEN_LoggerType_Console,
		   GWEN_LoggerFacility_User);

  GWEN_Logger_SetLevel(GSA_LOGDOMAIN, GWEN_LoggerLevel_Warning);
  GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning);

#ifdef GSA_IS_EXPERIMENTAL
  fprintf(stderr, "\n");
  fprintf(stderr, "\n");
  fprintf(stderr, "=================== WARNING ===================\n");
  fprintf(stderr, "This tool is still EXPERIMENTAL !!!\n");
  fprintf(stderr, "Please DON'T USE it with your data files !\n");
  fprintf(stderr, "===============================================\n");
  fprintf(stderr, "\n");
  fprintf(stderr, "\n");
#endif

  db=GWEN_DB_Group_new("arguments");
  rv=GWEN_Args_Check(argc, argv, 1,
		     GWEN_ARGS_MODE_ALLOW_FREEPARAM |
		     GWEN_ARGS_MODE_STOP_AT_FREEPARAM,
		     args,
		     db);
  if (rv==GWEN_ARGS_RESULT_ERROR) {
    fprintf(stderr, "ERROR: Could not parse arguments main\n");
    return -1;
  }
  else if (rv==GWEN_ARGS_RESULT_HELP) {
    GWEN_BUFFER *ubuf;

    ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
    GWEN_Buffer_AppendString(ubuf,
                             I18N("GWEN's Simple Archiver"));
    GWEN_Buffer_AppendString(ubuf,
                             " (Gwenhywfar v" GWENHYWFAR_VERSION_FULL_STRING ")\n");
    GWEN_Buffer_AppendString(ubuf,
                             I18N("Usage: "));
    GWEN_Buffer_AppendString(ubuf, argv[0]);
    GWEN_Buffer_AppendString(ubuf,
                             I18N(" [GLOBAL OPTIONS] COMMAND "
                                  "[LOCAL OPTIONS]\n"));
    GWEN_Buffer_AppendString(ubuf,
                             I18N("\nGlobal Options:\n"));
    if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
      fprintf(stderr, "ERROR: Could not create help string\n");
      return 1;
    }
    GWEN_Buffer_AppendString(ubuf,
                             I18N("\nCommands:\n\n"));
    GWEN_Buffer_AppendString(ubuf,
                             I18N("  create:\n"
                                  "    This command creates an archive file"
                                  "\n\n"));
    GWEN_Buffer_AppendString(ubuf,
			     I18N("  add:\n"
                                  "    Add files and folders to an archive file\n\n"));
    GWEN_Buffer_AppendString(ubuf,
			     I18N("  list:\n"
                                  "    List files and folders in an archive file\n\n"));

    GWEN_Buffer_AppendString(ubuf,
			     I18N("  check:\n"
                                  "    Check integrity of files and folders in an archive file\n\n"));

    fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
    GWEN_Buffer_free(ubuf);
    return 0;
  }
  if (rv) {
    argc-=rv-1;
    argv+=rv-1;
  }

  cmd=GWEN_DB_GetCharValue(db, "params", 0, 0);
  if (!cmd) {
    fprintf(stderr, "ERROR: Command needed.\n");
    return 1;
  }

  if (strcasecmp(cmd, "create")==0) {
    rv=createArchive(db, argc, argv);
  }
  else if (strcasecmp(cmd, "add")==0) {
    rv=add2Archive(db, argc, argv);
  }
  else if (strcasecmp(cmd, "list")==0) {
    rv=listArchive(db, argc, argv);
  }
  else if (strcasecmp(cmd, "check")==0) {
    rv=checkArchive(db, argc, argv);
  }
  else if (strcasecmp(cmd, "extract")==0) {
    rv=extractArchive(db, argc, argv);
  }
  else if (strcasecmp(cmd, "sign")==0) {
    rv=signArchive(db, argc, argv);
  }
  else if (strcasecmp(cmd, "verify")==0) {
    rv=verifyArchive(db, argc, argv);
  }
  else if (strcasecmp(cmd, "mkkey")==0) {
    rv=mkArchiveKey(db, argc, argv);
  }
  else if (strcasecmp(cmd, "rfi")==0) {
    rv=releaseFillIn(db, argc, argv);
  }
  else {
    fprintf(stderr, "ERROR: Unknown command \"%s\".\n", cmd);
    rv=1;
  }

  err=GWEN_Fini();
  if (err) {
    fprintf(stderr,
            "WARNING: Could not deinitialize Gwenhywfar.\n");
  }

  return rv;
}
int downloadCb(){
	IupSetFunction("IDLE_ACTION", NULL);
	IupLoopStep();
	char *cachePath = path_get_nwjs_cache();
	char *indexJsonPath = string_concat(2, cachePath, "../index.json");
	recursiveMkdir(cachePath, 0755); //Create directories if they don't already exist
	indexJsonFile_t versionIndex = {};
	if(download(INDEXJSON_URL, indexJsonPath, genericCb) != DOWNLOAD_SUCCESS){
		printf("[nwjsmanager][DEBUG] Failed to download version index at URL '%s'. Cached version will be used if available.\n", INDEXJSON_URL);
	}
	indexJson_file_parse(indexJsonPath, &versionIndex);
	if(versionIndex.nwjsVersionCount != 0){
		if(update_required(&versionIndex)){
			Ihandle *info = IupGetHandle("info");
			IupSetStrf(info, "TITLE", "Downloading nwjsmanager update (you are running v%s, but v%d.%d.%d is available).", NWJSMANAGER_VERSION, versionIndex.nwjsmanagerLatestVersion.major, versionIndex.nwjsmanagerLatestVersion.minor, versionIndex.nwjsmanagerLatestVersion.patch);
			if(!update_install(&versionIndex, progressCb, _argc, _argv)){
				IupMessage(app->name, "Failed to install update!");
				return IUP_CLOSE;
			}
		}
		nwjsVersion_t *launchVersion = NULL;
		for(int i = 0; i < versionIndex.nwjsVersionCount; i++)
			if(packageJson_file_is_nw_version_OK(app, versionIndex.nwjsVersions[i].version) && (!launchVersion || semver_gt(versionIndex.nwjsVersions[i].version, launchVersion->version)))
				launchVersion = &versionIndex.nwjsVersions[i];
		if(!launchVersion){
			IupMessage(app->name, "Error: no nw.js version compatible with the application was found. Please try to reinstall the application, or contact the developer if the problem persists.");
		}else{
			printf("[nwjsmanager][DEBUG] Downloading nw.js %d.%d.%d\n", launchVersion->version.major, launchVersion->version.minor, launchVersion->version.patch);
			char *versionName = malloc(255); //Using 255 as max length (see https://github.com/mojombo/semver/blob/master/semver.md#does-semver-have-a-size-limit-on-the-version-string)
			sprintf(versionName, "v%d.%d.%d", launchVersion->version.major, launchVersion->version.minor, launchVersion->version.patch);
			nwjsDownload_t *downloads = &launchVersion->defaultDownloads;
			char *url;
			#ifdef _WIN32
				//Actually on Windows only a 32-bit binary is distributed since it will work out-of-the box thanks to Wow64. The IsWow64 function (see win-only/IsWow64.c) is used to detect at runtime if we are on a 64-bit system and properly select the nw.js architecture.
				if(IsWow64())
					url = downloads->win64;
				else
					url = downloads->win32;
			#else
				//On linux, architecture is detected at compile time because two different binaries are distributed (i386 and x86_64)
				#ifdef __x86_64__
					url = downloads->linux64;
				#else
					url = downloads->linux32;
				#endif
			#endif
			char *file = string_concat(2, cachePath, "download");
			int result = download(url, file, progressCb);
			if(result == DOWNLOAD_SUCCESS){
				result = extractArchive(file, cachePath);
				remove(file);
				char *oldName = strrchr(url, '/') + sizeof(char);
				char *oldNameEnd = strstr(oldName, ".zip");
				if(!oldNameEnd)
					oldNameEnd = strstr(oldName, ".tar.gz");
				*oldNameEnd = '\0';
				char *oldPath = string_concat(2, cachePath, oldName);
				*oldNameEnd = '.';
				char *newPath = string_concat(2, cachePath, versionName);
				rename(oldPath, newPath);
				free(oldPath);
				#ifndef _WIN32
					recursiveChmod(newPath, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IXOTH);
				#endif
				free(newPath);
				globalResult = DOWNLOADERGUI_SUCCESS;
			}
			free(file);
			free(versionName);
		}
	}else
		IupMessage(app->name, "Error: this application requires the nw.js runtime, but a suitable version of the runtime files isn't installed. An attempt was done to download it, but it wasn't possible to retrive the version index.\nPlease check your Internet connection (this is necessary only for the first run of the application). Also, be sure the current user is allowed to write to the nw.js binary cache directory.");
	indexJson_file_free(&versionIndex);
	free(indexJsonPath);
	free(cachePath);
	return IUP_CLOSE;
}
int extract(){
	return extractArchive("test-data/downloadedFile", "test-data/extractDir") == ARCHIVE_SUCCESS;
}