2012-11-20 08:24:47 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
2019-01-12 21:43:48 +00:00
|
|
|
// Copyright (C) 2019 University of California
|
2012-11-20 08:24:47 +00:00
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2012-01-14 05:48:01 +00:00
|
|
|
|
|
|
|
|
2012-11-15 14:06:22 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2012-11-20 08:24:47 +00:00
|
|
|
#pragma warning( disable : 4786 ) // Disable warning messages for vector
|
2012-11-14 20:09:19 +00:00
|
|
|
#include "boinc_win.h"
|
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
|
|
|
#else
|
|
|
|
#ifndef __APPLE_CC__
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <string.h>
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-01-14 05:48:01 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#else
|
|
|
|
extern {
|
|
|
|
#endif
|
2012-11-15 14:06:22 +00:00
|
|
|
int unzip(int argc, char** argv);
|
2012-11-20 08:24:47 +00:00
|
|
|
//int zipmain(int argc, char** argv);
|
2012-01-14 05:48:01 +00:00
|
|
|
#include "./unzip/unzip.h"
|
|
|
|
#include "./zip/zip.h"
|
2012-11-14 20:09:19 +00:00
|
|
|
}
|
2012-01-14 05:48:01 +00:00
|
|
|
|
|
|
|
#include "boinc_zip.h"
|
|
|
|
#include "filesys.h" // from BOINC for DirScan
|
|
|
|
|
|
|
|
// send in an output filename, advanced options (usually NULL), and numFileIn, szfileIn
|
2012-11-15 14:06:22 +00:00
|
|
|
|
2012-01-14 05:48:01 +00:00
|
|
|
#ifndef _MAX_PATH
|
|
|
|
#define _MAX_PATH 255
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned char g_ucSort;
|
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
// a "binary predicate" for use by the std::sort algorithm
|
|
|
|
// return true if "first > second" according to the g_ucSort type
|
|
|
|
|
|
|
|
bool StringVectorSort(const std::string& first, const std::string& second) {
|
|
|
|
bool bRet = false;
|
|
|
|
if (g_ucSort & SORT_NAME
|
|
|
|
&& g_ucSort & SORT_ASCENDING
|
|
|
|
&& strcmp(first.c_str(), second.c_str())<0
|
|
|
|
) {
|
|
|
|
bRet = true;
|
|
|
|
} else if (g_ucSort & SORT_NAME
|
|
|
|
&& g_ucSort & SORT_DESCENDING
|
|
|
|
&& strcmp(first.c_str(), second.c_str())>0
|
|
|
|
) {
|
|
|
|
bRet = true;
|
|
|
|
} else if (g_ucSort & SORT_TIME) {
|
|
|
|
struct stat st[2];
|
|
|
|
stat(first.c_str(), &st[0]);
|
|
|
|
stat(second.c_str(), &st[1]);
|
|
|
|
if (g_ucSort & SORT_ASCENDING) {
|
|
|
|
bRet = st[0].st_mtime < st[1].st_mtime;
|
|
|
|
} else {
|
|
|
|
bRet = st[0].st_mtime > st[1].st_mtime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bRet;
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
int boinc_zip(
|
|
|
|
int bZipType, const std::string szFileZip, const std::string szFileIn
|
|
|
|
) {
|
|
|
|
ZipFileList tempvec;
|
|
|
|
tempvec.push_back(szFileIn);
|
|
|
|
return boinc_zip(bZipType, szFileZip, &tempvec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// same, but with char[] instead of string
|
|
|
|
//
|
|
|
|
int boinc_zip(int bZipType, const char* szFileZip, const char* szFileIn) {
|
|
|
|
string strFileZip, strFileIn;
|
|
|
|
strFileZip.assign(szFileZip);
|
|
|
|
strFileIn.assign(szFileIn);
|
|
|
|
ZipFileList tempvec;
|
|
|
|
tempvec.push_back(strFileIn);
|
|
|
|
return boinc_zip(bZipType, strFileZip, &tempvec);
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
int boinc_zip(
|
|
|
|
int bZipType, const std::string szFileZip, const ZipFileList* pvectszFileIn
|
|
|
|
) {
|
|
|
|
int carg;
|
|
|
|
char** av;
|
|
|
|
int iRet = 0, i = 0, nVecSize = 0;
|
|
|
|
|
|
|
|
if (pvectszFileIn) nVecSize = pvectszFileIn->size();
|
|
|
|
|
|
|
|
// if unzipping but no file out, so it just uses cwd, only 3 args
|
|
|
|
//if (bZipType == UNZIP_IT)
|
|
|
|
// carg = 3 + nVecSize;
|
|
|
|
//else
|
|
|
|
carg = 3 + nVecSize;
|
|
|
|
|
|
|
|
// make a dynamic array
|
|
|
|
av = (char**) calloc(carg+1, sizeof(char*));
|
|
|
|
for (i=0;i<(carg+1);i++) {
|
|
|
|
av[i] = (char*) calloc(_MAX_PATH,sizeof(char));
|
|
|
|
}
|
|
|
|
|
|
|
|
// just form an argc/argv to spoof the "main"
|
|
|
|
// default options are to recurse into directories
|
|
|
|
//if (options && strlen(options))
|
|
|
|
// strcpy(av[1], options);
|
|
|
|
|
|
|
|
if (bZipType == ZIP_IT) {
|
|
|
|
strcpy(av[0], "zip");
|
|
|
|
// default zip options -- no dir names, no subdirs, highest compression, quiet mode
|
|
|
|
if (strlen(av[1])==0) {
|
|
|
|
strcpy(av[1], "-j9q");
|
|
|
|
}
|
|
|
|
strcpy(av[2], szFileZip.c_str());
|
|
|
|
|
|
|
|
//sz 3 onward will be each vector
|
|
|
|
int jj;
|
|
|
|
for (jj=0; jj<nVecSize; jj++) {
|
|
|
|
strcpy(av[3+jj], pvectszFileIn->at(jj).c_str());
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
2012-11-20 08:24:47 +00:00
|
|
|
} else {
|
|
|
|
strcpy(av[0], "unzip");
|
|
|
|
// default unzip options -- preserve subdirs, overwrite
|
|
|
|
if (strlen(av[1])==0) {
|
|
|
|
strcpy(av[1], "-oq");
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
2012-11-20 08:24:47 +00:00
|
|
|
strcpy(av[2], szFileZip.c_str());
|
|
|
|
|
|
|
|
// if they passed in a directory unzip there
|
|
|
|
if (carg == 4) {
|
|
|
|
sprintf(av[3], "-d%s", pvectszFileIn->at(0).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av[carg] = NULL;
|
|
|
|
// printf("args: %s %s %s %s\n", av[0], av[1], av[2], av[3]);
|
|
|
|
|
|
|
|
if (bZipType == ZIP_IT) {
|
|
|
|
if (access(szFileZip.c_str(), 0) == 0) {
|
|
|
|
// old zip file exists so unlink
|
|
|
|
// (otherwise zip will reuse, doesn't seem to be a flag to
|
|
|
|
// bypass zip reusing it
|
|
|
|
unlink(szFileZip.c_str());
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
2012-11-20 08:24:47 +00:00
|
|
|
iRet = zipmain(carg, av);
|
|
|
|
} else {
|
|
|
|
// make sure zip file exists
|
|
|
|
if (access(szFileZip.c_str(), 0) == 0) {
|
|
|
|
iRet = UzpMain(carg, av);
|
|
|
|
} else {
|
|
|
|
iRet = 2;
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
2012-11-20 08:24:47 +00:00
|
|
|
}
|
2012-01-14 05:48:01 +00:00
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
for (i=0;i<carg;i++) {
|
|
|
|
free(av[i]);
|
|
|
|
}
|
|
|
|
free(av);
|
|
|
|
return iRet;
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Description: Supply a directory and a pattern as arguments, along
|
2012-11-20 08:24:47 +00:00
|
|
|
// with a FileList variable to hold the result list; sorted by name.
|
|
|
|
// Returns a vector of files in the directory which match the
|
|
|
|
// pattern. Returns true for success, or false if there was a problem.
|
2012-01-14 05:48:01 +00:00
|
|
|
//
|
|
|
|
// CMC Note: this is a 'BOINC-friendly' implementation of "old" CPDN code
|
2012-11-20 08:24:47 +00:00
|
|
|
// the "wildcard matching"/regexp is a bit crude, it matches substrings in
|
|
|
|
// order in a file; to match all files such as *.pc.*.x4.*.nc" pass
|
|
|
|
// ".pc|.x4.|.nc" for 'pattern'
|
2012-01-14 05:48:01 +00:00
|
|
|
//
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
bool boinc_filelist(
|
|
|
|
const string directory,
|
|
|
|
const string pattern,
|
|
|
|
ZipFileList* pList,
|
|
|
|
const unsigned char ucSort,
|
|
|
|
const bool bClear
|
|
|
|
) {
|
|
|
|
g_ucSort = ucSort; // set the global sort type right off the bat
|
|
|
|
string strFile;
|
|
|
|
// at most three |'s may be passed in pattern match
|
|
|
|
int iPos[3], iFnd, iCtr, i, lastPos;
|
|
|
|
string strFullPath;
|
|
|
|
char strPart[3][32];
|
|
|
|
string spattern = pattern;
|
|
|
|
string strDir = directory;
|
|
|
|
string strUserDir = directory;
|
|
|
|
int iLen = strUserDir.size();
|
2012-01-14 05:48:01 +00:00
|
|
|
|
|
|
|
if (!pList) return false;
|
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
// wildcards are blank!
|
|
|
|
if (pattern == "*" || pattern == "*.*") spattern.assign("");
|
|
|
|
|
|
|
|
if (bClear) {
|
|
|
|
pList->clear(); // removes old entries that may be in pList
|
|
|
|
}
|
|
|
|
|
|
|
|
// first tack on a final slash on user dir if required
|
|
|
|
if (strUserDir[iLen-1] != '\\' && strUserDir[iLen] != '/') {
|
|
|
|
// need a final slash, but what type?
|
|
|
|
// / is safe on all OS's for CPDN at least
|
|
|
|
// but if they already used \ use that
|
|
|
|
// well they didn't use a backslash so just use a slash
|
|
|
|
if (strUserDir.find("\\") == string::npos) {
|
|
|
|
strUserDir += "/";
|
|
|
|
} else {
|
|
|
|
strUserDir += "\\";
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
2012-11-20 08:24:47 +00:00
|
|
|
}
|
2012-01-14 05:48:01 +00:00
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
// transform strDir to either all \\ or all /
|
|
|
|
int j;
|
|
|
|
for (j=0; j<(int)directory.size(); j++) {
|
|
|
|
// take off final / or backslash
|
|
|
|
if (j == ((int)directory.size()-1)
|
|
|
|
&& (strDir[j] == '/' || strDir[j]=='\\')
|
|
|
|
) {
|
|
|
|
strDir.resize(directory.size()-1);
|
|
|
|
} else {
|
2012-01-14 05:48:01 +00:00
|
|
|
#ifdef _WIN32 // transform paths appropriate for OS
|
2012-11-20 08:24:47 +00:00
|
|
|
if (directory[j] == '/') strDir[j] = '\\';
|
2012-01-14 05:48:01 +00:00
|
|
|
#else
|
2012-11-20 08:24:47 +00:00
|
|
|
if (directory[j] == '\\') strDir[j] = '/';
|
2012-01-14 05:48:01 +00:00
|
|
|
#endif
|
2012-11-20 08:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DirScanner dirscan(strDir);
|
|
|
|
memset(strPart, 0x00, 3*32);
|
|
|
|
while (dirscan.scan(strFile)) {
|
|
|
|
iCtr = 0;
|
|
|
|
lastPos = 0;
|
|
|
|
iPos[0] = -1;
|
|
|
|
iPos[1] = -1;
|
|
|
|
iPos[2] = -1;
|
|
|
|
// match the filename against the regexp to see if it's a hit
|
|
|
|
// first get all the |'s to get the pieces to verify
|
|
|
|
//
|
|
|
|
while (iCtr<3 && (iPos[iCtr] = (int) spattern.find('|', lastPos)) > -1) {
|
|
|
|
if (iCtr==0) {
|
|
|
|
strncpy(strPart[0], spattern.c_str(), iPos[iCtr]);
|
|
|
|
} else {
|
|
|
|
strncpy(strPart[iCtr], spattern.c_str()+lastPos, iPos[iCtr]-lastPos);
|
|
|
|
}
|
|
|
|
lastPos = iPos[iCtr]+1;
|
|
|
|
iCtr++;
|
|
|
|
}
|
|
|
|
if (iCtr>0) {
|
|
|
|
// found a | so need to get the part from lastpos onward
|
|
|
|
strncpy(strPart[iCtr], spattern.c_str()+lastPos, spattern.length() - lastPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check no | were found at all
|
|
|
|
if (iCtr == 0) {
|
|
|
|
strcpy(strPart[0], spattern.c_str());
|
|
|
|
iCtr++; // fake iCtr up 1 to get in the loop below
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
bool bFound = true;
|
|
|
|
for (i = 0; i <= iCtr && bFound; i++) {
|
|
|
|
if (i==0) {
|
|
|
|
iFnd = (int) strFile.find(strPart[0]);
|
|
|
|
bFound = (bool) (iFnd > -1);
|
|
|
|
} else {
|
|
|
|
// search forward of the old part found
|
|
|
|
iFnd = (int) strFile.find(strPart[i], iFnd+1);
|
|
|
|
bFound = bFound && (bool) (iFnd > -1);
|
|
|
|
}
|
|
|
|
}
|
2012-01-14 05:48:01 +00:00
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
if (bFound) {
|
|
|
|
// this pattern matched the file, add to vector
|
|
|
|
// NB: first get stat to make sure it really is a file
|
|
|
|
strFullPath = strUserDir + strFile;
|
|
|
|
// only add if the file really exists (i.e. not a directory)
|
|
|
|
if (is_file(strFullPath.c_str())) {
|
|
|
|
pList->push_back(strFullPath);
|
|
|
|
}
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
2012-11-20 08:24:47 +00:00
|
|
|
}
|
2012-01-14 05:48:01 +00:00
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
// sort by file creation time
|
|
|
|
// sort if list is greather than 1
|
|
|
|
if (pList->size()>1) {
|
|
|
|
sort(pList->begin(), pList->end(), StringVectorSort); // may as well sort it?
|
2012-01-14 05:48:01 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
// Read compressed file to memory.
|
|
|
|
//
|
|
|
|
int boinc_UnzipToMemory (char *zip, char *file, string &retstr) {
|
|
|
|
UzpOpts opts; // options for UzpUnzipToMemory()
|
|
|
|
UzpCB funcs; // function pointers for UzpUnzipToMemory()
|
|
|
|
UzpBuffer buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
memset(&funcs, 0, sizeof(funcs));
|
|
|
|
|
|
|
|
funcs.structlen = sizeof(UzpCB);
|
|
|
|
funcs.msgfn = (MsgFn *)printf;
|
|
|
|
funcs.inputfn = (InputFn *)scanf;
|
|
|
|
funcs.pausefn = (PauseFn *)(0x01);
|
|
|
|
funcs.passwdfn = (PasswdFn *)(NULL);
|
|
|
|
|
|
|
|
memset(&buf, 0, sizeof(buf)); // data-blocks needs to be empty
|
|
|
|
ret = UzpUnzipToMemory(zip, file, &opts, &funcs, &buf);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
retstr = (string) buf.strptr;
|
|
|
|
}
|
2012-01-14 05:48:01 +00:00
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
if (buf.strptr) free (buf.strptr);
|
2012-01-14 05:48:01 +00:00
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
return ret;
|
2012-01-14 05:48:01 +00:00
|
|
|
|
2012-11-20 08:24:47 +00:00
|
|
|
}
|