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:
Steve Checkoway
2004-09-05 10:12:53 +00:00
parent dec895409a
commit e100932068
6 changed files with 97 additions and 122 deletions
@@ -25,32 +25,18 @@ using namespace std;
#include "Util.h" #include "Util.h"
void HandleFile(const CString& file, const CString& dir, void HandleFile(const CString& file, const CString& dir,
const CString& archivePath, bool overwrite) const CString& archivePath)
{ {
static bool archiveMade = false; CString from = dir + '/' + file;
char path[] = "/usr/bin/tar"; CString to = archivePath + '/' + file;
char arg1[] = "-rf"; char copy[] = "/bin/cp";
char arg2[archivePath.length() + 1]; char arg[] = "-R";
char arg3[] = "-C";
char arg4[dir.length() + 1];
char arg5[file.length() + 1];
if (!archiveMade) MakeAllButLast(to);
if (CallTool(copy, arg, from.c_str(), to.c_str(), NULL))
{ {
arg1[1] = 'c'; fprintf(stderr, "Couldn't copy file from \"%s\" to \"%s\".",
archiveMade = true; from.c_str(), to.c_str());
}
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); exit(-10);
} }
} }
@@ -139,22 +125,13 @@ int main(int argc, char *argv[])
return 4; return 4;
} }
CString archivePath = outDir + "/archive.tar"; CString archivePath = outDir;
Processor p(archivePath, HandleFile, GetPath, NULL, false); Processor p(archivePath, HandleFile, GetPath, NULL, false);
while (nextLine < config.GetNumLines()) while (nextLine < config.GetNumLines())
p.ProcessLine(config.GetLine(nextLine), nextLine); 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")) if (!config.WriteFile(outDir + "/config"))
{ {
printf("%s\n", strerror(errno)); printf("%s\n", strerror(errno));
+13 -14
View File
@@ -22,27 +22,25 @@ void Error(const char *fmt, ...);
static id c; static id c;
void HandleFile(const CString& file, const CString& dir, void HandleFile(const CString& file, const CString& dir,
const CString& archivePath, bool overwrite) const CString& archivePath)
{ {
NSString *filePath = [NSString stringWithCString:dir]; NSString *filePath = [NSString stringWithCString:dir];
filePath = [filePath stringByAppendingFormat:@"%s%s", filePath = [filePath stringByAppendingFormat:@"%s%s",
(dir == "/" ? "" : "/"), file.c_str()]; (dir == "/" ? "" : "/"), file.c_str()];
if (!overwrite && DoesFileExist([filePath cString]))
return;
[c postMessage:filePath]; [c postMessage:filePath];
/* This is rediculus */ /* This is rediculus */
char f[file.length() + 1]; char path[] = "/bin/cp";
char d[dir.length() + 1]; char arg[] = "-RP";
char p[archivePath.length() + 1]; CString fromString = archivePath + '/' + file;
char path[] = "/usr/bin/tar"; char from[fromString.length() + 1];
char arg1[] = "-xPf"; CString toString = dir + '/' + file;
char arg3[] = "-C"; char to[toString.length() + 1];
char *const arguments[] = {path, arg1, p, arg3, d, f, NULL}; char *const arguments[] = {path, arg, from, to, NULL};
strcpy(p, archivePath); strcpy(from, fromString);
strcpy(d, dir); strcpy(to, toString);
strcpy(f, file); MakeAllButLast(to);
int fd_dev_null = open("/dev/null", O_RDWR, 0); int fd_dev_null = open("/dev/null", O_RDWR, 0);
@@ -131,7 +129,8 @@ void Echo(const CString& message, bool loud)
c = caller; c = caller;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CString configPath = [mPath cString]; CString configPath = [mPath cString];
CString archivePath = configPath + "/archive.tar.gz"; //CString archivePath = configPath + "/archive.tar.gz";
CString archivePath = configPath;
BOOL success = NO; BOOL success = NO;
configPath += "/config"; configPath += "/config";
+21 -65
View File
@@ -40,33 +40,6 @@ mError(Processor::DefaultError), mInstalling(i)
getwd(cwd); getwd(cwd);
mCWD = cwd; mCWD = cwd;
mConditionals["INSTALLING"] = i; 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;
} }
@@ -124,7 +97,11 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
} }
if (mInstalling) if (mInstalling)
(*mHandleFile)(file, dir, mPath, ResolveConditional(parts[2])); {
if (ResolveConditional(parts[2]))
rm_rf(dir + '/' + file);
(*mHandleFile)(file, dir, mPath);
}
else else
{ {
if (IsADirectory((dir == "/" ? dir : dir + "/" ) + file)) if (IsADirectory((dir == "/" ? dir : dir + "/" ) + file))
@@ -138,12 +115,12 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
fchdir(fd); fchdir(fd);
close(fd); close(fd);
if (list.size() == 0) //empty dir or ignored dir 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) for (unsigned i=0; i<list.size(); ++i)
(*mHandleFile)(list[i], dir, mPath, true); (*mHandleFile)(list[i], dir, mPath);
} }
else else
(*mHandleFile)(file, dir, mPath, true); (*mHandleFile)(file, dir, mPath);
} }
} }
else if (parts.size() == 4) else if (parts.size() == 4)
@@ -171,17 +148,19 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
fDir = mCWD; fDir = mCWD;
} }
if (mInstalling) if (mInstalling)
(*mHandleFile)(fFile, fDir, mPath, {
ResolveConditional(parts[3])); if (ResolveConditional(parts[3]))
rm_rf(fDir + '/' + fFile);
(*mHandleFile)(fFile, fDir, mPath);
}
else else
{ {
// Now the fun part // Now the fun part
char temp[] = "/tmp/tmpXXXXXX"; char temp[] = "/tmp/tmpXXXXXX";
CString dir; CString dir;
size_t pos = fFile.find_last_of('/'); size_t pos = fFile.find_last_of('/');
char arg1[] = "-r"; char arg[] = "-R";
char tool1[] = "/bin/cp"; char tool[] = "/bin/cp";
char tool2[] = "/bin/rm";
mkdtemp(temp); mkdtemp(temp);
dir.Format("%s/%s/%s", temp, fDir == mCWD ? "." : fDir.c_str(), 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()); dir.c_str());
exit(-1); exit(-1);
} }
if (CallTool(tool1, arg1, (iDir + "/" + iFile).c_str(), if (CallTool(tool, arg, (iDir + "/" + iFile).c_str(),
dir.c_str(), NULL)) dir.c_str(), NULL))
{ {
fprintf(stderr, "Couldn't copy file(s).\n"); fprintf(stderr, "Couldn't copy file(s).\n");
@@ -211,15 +190,13 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
fchdir(fd); fchdir(fd);
close(fd); close(fd);
if (list.size() == 0) if (list.size() == 0)
(*mHandleFile)(fFile, temp, mPath, true); (*mHandleFile)(fFile, temp, mPath);
for (unsigned i=0; i<list.size(); ++i) for (unsigned i=0; i<list.size(); ++i)
(*mHandleFile)(list[i], temp, mPath, true); (*mHandleFile)(list[i], temp, mPath);
} }
else else
(*mHandleFile)(fFile, temp, mPath, true); (*mHandleFile)(fFile, temp, mPath);
char arg[] = "-rf"; rm_rf(temp);
CallTool(tool2, arg, temp, NULL);
} }
} }
else else
@@ -358,28 +335,7 @@ void Processor::ProcessLine(const CString& line, unsigned& nextLine)
if (path[0] != '/') if (path[0] != '/')
path = mCWD + "/" + path; path = mCWD + "/" + path;
if (IsADirectory(path)) rm_rf(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));
}
return; return;
} }
+2 -2
View File
@@ -19,8 +19,8 @@ using namespace std;
class Processor class Processor
{ {
private: private:
typedef void (*handleFileFunc)(const CString& file, const CString& dir, const CString& archivePath, typedef void (*handleFileFunc)(const CString& file, const CString& dir,
bool overwrite); const CString& archivePath);
typedef const CString (*getPathFunc)(const CString& ID); typedef const CString (*getPathFunc)(const CString& ID);
typedef void (*errorFunc)(const char *fmt, ...); typedef void (*errorFunc)(const char *fmt, ...);
typedef bool (*askFunc)(const CString& question); typedef bool (*askFunc)(const CString& question);
+41
View File
@@ -120,6 +120,47 @@ err:
return ret; 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) int CallTool3(bool blocking, int fd_in, int fd_out, int fd_err, const char *path, char *const *arguments)
{ {
if (!DoesFileExist(path)) if (!DoesFileExist(path))
+2
View File
@@ -19,6 +19,8 @@ CString LastPathComponent(const CString& path);
void FileListingForDirectoryWithIgnore(const CString& path, CStringArray& list, CStringArray& dirs, void FileListingForDirectoryWithIgnore(const CString& path, CStringArray& list, CStringArray& dirs,
const set<CString>& ignore, bool ignoreDot = true); const set<CString>& ignore, bool ignoreDot = true);
int mkdir_p(const CString& path); 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); 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 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, ...) inline int CallTool2(bool blocking, int fd_in, int fd_out, int fd_err, char *path, ...)