Yes, I know it is ugly. Removed gunzip and tar. It does still rely on /bin/cp being there, but that is mostly because I was too lazy to write a replacement for it.
This commit is contained in:
@@ -25,34 +25,20 @@ using namespace std;
|
||||
#include "Util.h"
|
||||
|
||||
void HandleFile(const CString& file, const CString& dir,
|
||||
const CString& archivePath, bool overwrite)
|
||||
const CString& archivePath)
|
||||
{
|
||||
static bool archiveMade = false;
|
||||
char path[] = "/usr/bin/tar";
|
||||
char arg1[] = "-rf";
|
||||
char arg2[archivePath.length() + 1];
|
||||
char arg3[] = "-C";
|
||||
char arg4[dir.length() + 1];
|
||||
char arg5[file.length() + 1];
|
||||
|
||||
if (!archiveMade)
|
||||
{
|
||||
arg1[1] = 'c';
|
||||
archiveMade = true;
|
||||
}
|
||||
|
||||
strcpy(arg2, archivePath);
|
||||
strcpy(arg4, dir);
|
||||
strcpy(arg5, file);
|
||||
|
||||
int r;
|
||||
if ((r = CallTool2(true, -1, -1, -1, path, arg1, arg2, arg3, arg4,
|
||||
arg5, NULL)) != 0)
|
||||
{
|
||||
fprintf(stderr, "%s %s %s %s %s %s returned %d\n",
|
||||
path, arg1, arg2, arg3, arg4, arg5, r);
|
||||
exit(-10);
|
||||
}
|
||||
CString from = dir + '/' + file;
|
||||
CString to = archivePath + '/' + file;
|
||||
char copy[] = "/bin/cp";
|
||||
char arg[] = "-R";
|
||||
|
||||
MakeAllButLast(to);
|
||||
if (CallTool(copy, arg, from.c_str(), to.c_str(), NULL))
|
||||
{
|
||||
fprintf(stderr, "Couldn't copy file from \"%s\" to \"%s\".",
|
||||
from.c_str(), to.c_str());
|
||||
exit(-10);
|
||||
}
|
||||
}
|
||||
|
||||
const CString GetPath(const CString& ID)
|
||||
@@ -139,22 +125,13 @@ int main(int argc, char *argv[])
|
||||
return 4;
|
||||
}
|
||||
|
||||
CString archivePath = outDir + "/archive.tar";
|
||||
CString archivePath = outDir;
|
||||
|
||||
Processor p(archivePath, HandleFile, GetPath, NULL, false);
|
||||
|
||||
while (nextLine < config.GetNumLines())
|
||||
p.ProcessLine(config.GetLine(nextLine), nextLine);
|
||||
|
||||
printf("Compressing the archive.\n");
|
||||
|
||||
char toolPath[] = "/usr/bin/gzip";
|
||||
if (CallTool(toolPath, archivePath.c_str(), NULL))
|
||||
{
|
||||
printf("Gzip failed.\n");
|
||||
return 6;
|
||||
}
|
||||
|
||||
if (!config.WriteFile(outDir + "/config"))
|
||||
{
|
||||
printf("%s\n", strerror(errno));
|
||||
|
||||
@@ -22,28 +22,26 @@ void Error(const char *fmt, ...);
|
||||
static id c;
|
||||
|
||||
void HandleFile(const CString& file, const CString& dir,
|
||||
const CString& archivePath, bool overwrite)
|
||||
const CString& archivePath)
|
||||
{
|
||||
NSString *filePath = [NSString stringWithCString:dir];
|
||||
|
||||
filePath = [filePath stringByAppendingFormat:@"%s%s",
|
||||
(dir == "/" ? "" : "/"), file.c_str()];
|
||||
if (!overwrite && DoesFileExist([filePath cString]))
|
||||
return;
|
||||
[c postMessage:filePath];
|
||||
/* This is rediculus */
|
||||
char f[file.length() + 1];
|
||||
char d[dir.length() + 1];
|
||||
char p[archivePath.length() + 1];
|
||||
char path[] = "/usr/bin/tar";
|
||||
char arg1[] = "-xPf";
|
||||
char arg3[] = "-C";
|
||||
char *const arguments[] = {path, arg1, p, arg3, d, f, NULL};
|
||||
|
||||
strcpy(p, archivePath);
|
||||
strcpy(d, dir);
|
||||
strcpy(f, file);
|
||||
|
||||
char path[] = "/bin/cp";
|
||||
char arg[] = "-RP";
|
||||
CString fromString = archivePath + '/' + file;
|
||||
char from[fromString.length() + 1];
|
||||
CString toString = dir + '/' + file;
|
||||
char to[toString.length() + 1];
|
||||
char *const arguments[] = {path, arg, from, to, NULL};
|
||||
|
||||
strcpy(from, fromString);
|
||||
strcpy(to, toString);
|
||||
MakeAllButLast(to);
|
||||
|
||||
int fd_dev_null = open("/dev/null", O_RDWR, 0);
|
||||
|
||||
if (CallTool3(true, -1, fd_dev_null, fileno(stderr), path, arguments))
|
||||
@@ -131,7 +129,8 @@ void Echo(const CString& message, bool loud)
|
||||
c = caller;
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
CString configPath = [mPath cString];
|
||||
CString archivePath = configPath + "/archive.tar.gz";
|
||||
//CString archivePath = configPath + "/archive.tar.gz";
|
||||
CString archivePath = configPath;
|
||||
BOOL success = NO;
|
||||
|
||||
configPath += "/config";
|
||||
|
||||
@@ -40,34 +40,7 @@ mError(Processor::DefaultError), mInstalling(i)
|
||||
getwd(cwd);
|
||||
mCWD = cwd;
|
||||
mConditionals["INSTALLING"] = i;
|
||||
|
||||
if (mInstalling)
|
||||
{
|
||||
if (!DoesFileExist(path))
|
||||
throw CString("Archive file not present.");
|
||||
|
||||
char archivePath[] = "/tmp/installerXXXXXX.tar";
|
||||
int fd = mkstemps(archivePath, 4);
|
||||
|
||||
if (fd == -1)
|
||||
throw CString("Couldn't create temporary file");
|
||||
|
||||
mPath = archivePath;
|
||||
|
||||
char toolPath[] = "/usr/bin/gunzip";
|
||||
|
||||
try {
|
||||
if(CallTool2(true, -1, fd, -1, toolPath, "-c", path.c_str(), NULL))
|
||||
throw CString("Archive file not handled correctly.");
|
||||
} catch (CString& e) {
|
||||
close(fd);
|
||||
unlink(mPath);
|
||||
throw;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
else
|
||||
mPath = path;
|
||||
mPath = path;
|
||||
}
|
||||
|
||||
Processor::~Processor()
|
||||
@@ -124,7 +97,11 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
|
||||
}
|
||||
|
||||
if (mInstalling)
|
||||
(*mHandleFile)(file, dir, mPath, ResolveConditional(parts[2]));
|
||||
{
|
||||
if (ResolveConditional(parts[2]))
|
||||
rm_rf(dir + '/' + file);
|
||||
(*mHandleFile)(file, dir, mPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (IsADirectory((dir == "/" ? dir : dir + "/" ) + file))
|
||||
@@ -138,12 +115,12 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
|
||||
fchdir(fd);
|
||||
close(fd);
|
||||
if (list.size() == 0) //empty dir or ignored dir
|
||||
(*mHandleFile)(file, dir, mPath, true);
|
||||
(*mHandleFile)(file, dir, mPath);
|
||||
for (unsigned i=0; i<list.size(); ++i)
|
||||
(*mHandleFile)(list[i], dir, mPath, true);
|
||||
(*mHandleFile)(list[i], dir, mPath);
|
||||
}
|
||||
else
|
||||
(*mHandleFile)(file, dir, mPath, true);
|
||||
(*mHandleFile)(file, dir, mPath);
|
||||
}
|
||||
}
|
||||
else if (parts.size() == 4)
|
||||
@@ -171,17 +148,19 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
|
||||
fDir = mCWD;
|
||||
}
|
||||
if (mInstalling)
|
||||
(*mHandleFile)(fFile, fDir, mPath,
|
||||
ResolveConditional(parts[3]));
|
||||
{
|
||||
if (ResolveConditional(parts[3]))
|
||||
rm_rf(fDir + '/' + fFile);
|
||||
(*mHandleFile)(fFile, fDir, mPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Now the fun part
|
||||
char temp[] = "/tmp/tmpXXXXXX";
|
||||
CString dir;
|
||||
size_t pos = fFile.find_last_of('/');
|
||||
char arg1[] = "-r";
|
||||
char tool1[] = "/bin/cp";
|
||||
char tool2[] = "/bin/rm";
|
||||
char arg[] = "-R";
|
||||
char tool[] = "/bin/cp";
|
||||
|
||||
mkdtemp(temp);
|
||||
dir.Format("%s/%s/%s", temp, fDir == mCWD ? "." : fDir.c_str(),
|
||||
@@ -193,7 +172,7 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
|
||||
dir.c_str());
|
||||
exit(-1);
|
||||
}
|
||||
if (CallTool(tool1, arg1, (iDir + "/" + iFile).c_str(),
|
||||
if (CallTool(tool, arg, (iDir + "/" + iFile).c_str(),
|
||||
dir.c_str(), NULL))
|
||||
{
|
||||
fprintf(stderr, "Couldn't copy file(s).\n");
|
||||
@@ -211,15 +190,13 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
|
||||
fchdir(fd);
|
||||
close(fd);
|
||||
if (list.size() == 0)
|
||||
(*mHandleFile)(fFile, temp, mPath, true);
|
||||
(*mHandleFile)(fFile, temp, mPath);
|
||||
for (unsigned i=0; i<list.size(); ++i)
|
||||
(*mHandleFile)(list[i], temp, mPath, true);
|
||||
(*mHandleFile)(list[i], temp, mPath);
|
||||
}
|
||||
else
|
||||
(*mHandleFile)(fFile, temp, mPath, true);
|
||||
char arg[] = "-rf";
|
||||
|
||||
CallTool(tool2, arg, temp, NULL);
|
||||
(*mHandleFile)(fFile, temp, mPath);
|
||||
rm_rf(temp);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -357,29 +334,8 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
|
||||
|
||||
if (path[0] != '/')
|
||||
path = mCWD + "/" + path;
|
||||
|
||||
if (IsADirectory(path))
|
||||
{
|
||||
CStringArray files, dirs;
|
||||
FileListingForDirectoryWithIgnore(path, files, dirs, set<CString>(), false);
|
||||
for (unsigned i=0; i<files.size(); ++i)
|
||||
{
|
||||
if (unlink(files[i]))
|
||||
(*mError)("%s", strerror(errno));
|
||||
}
|
||||
|
||||
while (!dirs.empty())
|
||||
{
|
||||
if (rmdir(dirs.back()))
|
||||
(*mError)("%s", strerror(errno));
|
||||
dirs.pop_back();
|
||||
}
|
||||
}
|
||||
else if (DoesFileExist(path))
|
||||
{
|
||||
if (unlink(path))
|
||||
(*mError)("%s", strerror(errno));
|
||||
}
|
||||
|
||||
rm_rf(path);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ using namespace std;
|
||||
class Processor
|
||||
{
|
||||
private:
|
||||
typedef void (*handleFileFunc)(const CString& file, const CString& dir, const CString& archivePath,
|
||||
bool overwrite);
|
||||
typedef void (*handleFileFunc)(const CString& file, const CString& dir,
|
||||
const CString& archivePath);
|
||||
typedef const CString (*getPathFunc)(const CString& ID);
|
||||
typedef void (*errorFunc)(const char *fmt, ...);
|
||||
typedef bool (*askFunc)(const CString& question);
|
||||
|
||||
@@ -120,6 +120,47 @@ err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int MakeAllButLast(const CString& path)
|
||||
{
|
||||
size_t i = path.find_last_of("/");
|
||||
|
||||
if (i == path.npos)
|
||||
return 0;
|
||||
CString dir = path.substr(0, i);
|
||||
printf("%s -> %s\n", path.c_str(), dir.c_str());
|
||||
return mkdir_p(dir);
|
||||
}
|
||||
|
||||
void rm_rf(const CString& path)
|
||||
{
|
||||
printf("deleting %s\n", path.c_str());
|
||||
if (IsADirectory(path))
|
||||
{
|
||||
CStringArray files, dirs;
|
||||
FileListingForDirectoryWithIgnore(path, files, dirs, set<CString>(), false);
|
||||
for (unsigned i=0; i<files.size(); ++i)
|
||||
{
|
||||
if (unlink(files[i]))
|
||||
fputs(strerror(errno), stderr);
|
||||
}
|
||||
|
||||
while (!dirs.empty())
|
||||
{
|
||||
if (rmdir(dirs.back()))
|
||||
fputs(strerror(errno), stderr);
|
||||
dirs.pop_back();
|
||||
}
|
||||
if (rmdir(path))
|
||||
fputs(strerror(errno), stderr);
|
||||
}
|
||||
else if (DoesFileExist(path))
|
||||
{
|
||||
if (unlink(path))
|
||||
fputs(strerror(errno), stderr);
|
||||
}
|
||||
assert(!DoesFileExist(path));
|
||||
}
|
||||
|
||||
int CallTool3(bool blocking, int fd_in, int fd_out, int fd_err, const char *path, char *const *arguments)
|
||||
{
|
||||
if (!DoesFileExist(path))
|
||||
|
||||
@@ -19,6 +19,8 @@ CString LastPathComponent(const CString& path);
|
||||
void FileListingForDirectoryWithIgnore(const CString& path, CStringArray& list, CStringArray& dirs,
|
||||
const set<CString>& ignore, bool ignoreDot = true);
|
||||
int mkdir_p(const CString& path);
|
||||
int MakeAllButLast(const CString& path);
|
||||
void rm_rf(const CString& path);
|
||||
int CallTool3(bool blocking, int fd_in, int fd_out, int fd_err, const char *path, char *const *arguments);
|
||||
inline int CallTool(char *path, ...) { return CallTool3(true, -1, -1, -1, path, &path); }
|
||||
inline int CallTool2(bool blocking, int fd_in, int fd_out, int fd_err, char *path, ...)
|
||||
|
||||
Reference in New Issue
Block a user