123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
-
-
-
-
-
-
-
-
-
-
-
- #include <windows.h>
- #include <tchar.h>
-
- #include <string>
- #include <iostream>
- #include <stdexcept>
- #include <algorithm>
- #include <sstream>
- #include <iterator>
-
- #include "Utility.h"
-
- static DWORD LastError = 0;
-
-
-
- void
- OutputUsage(std::string DefaultTempPath) {
- std::cout <<
- "\n"
- "Usage:\n"
- "\n"
- " BuildDistribution -d DistPath -t TempPath\n"
- "\n"
- " where DistPath is the path of the distribution\n"
- " (e.g. SNFMultiSDK_Windows_3.1, and TempPath is the name of the\n"
- " directory to use for temporary storage. TempPath must exist.\n"
- " The default value of TempPath is '" << DefaultTempPath << "'.\n"
- ;
- }
-
- void
- OutputHelp() {
- std::cout <<
- "\n"
- "The program does the following:\n"
- "\n"
- " 1) For each directory DIR that appears in DistPath and also in\n"
- " DistPath\\..:\n"
- "\n"
- " a) Delete the contents of DistPath/DIR, except for .svn.\n"
- "\n"
- " b) Copy the contents of DistPath\\..\\DIR to DistPath\\DIR, except\n"
- " for .svn.\n"
- "\n"
- " 2) Create a temporary directory TempPath/DistName, where DistName is\n"
- " the directory name of DistPath (e.g. if DistName is\n"
- " c:\\dir1\\dir2\\SNFMultiSDK_Windows_3.1, then DistName is\n"
- " SNFMultiSDK_Windows_3.1).\n"
- "\n"
- " 3) For each directory DIR that appears in DistPath and also in\n"
- " DistPath\\..:\n"
- "\n"
- " a) Copy all files from DistPath/DIR to TempPath/DistName, except\n"
- " for .svn.\n\n"
- ;
-
- }
-
- void
- OutputErrorMessage(std::string Message) {
- LPVOID lpMsgBuf;
-
- if (LastError != 0) {
-
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- LastError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL);
- std::cerr << Message << ": " << (LPCSTR) lpMsgBuf;
-
- } else {
-
- std::cerr << Message << "\n";
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
- bool IsInContainer(std::string Needle, StringContainer Haystack);
-
-
-
-
-
-
-
-
-
-
-
-
-
- StringContainer GetDirectoryList(std::string DistPath,
- StringContainer ExcludeName);
-
-
-
-
-
-
-
-
-
-
-
-
-
- StringContainer GetFileList(std::string DistPath,
- StringContainer ExcludeName);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void UpdateOneDirectory(std::string SourceDir,
- std::string DestDir,
- StringContainer ExcludeName);
-
-
-
-
-
-
-
-
- void DeletePath(std::string Path);
-
- void
- GetDirectoryNames(std::string &InputDistPath,
- std::string &DistSourcePath,
- std::string &DistParentPath,
- std::string &DistName) {
-
- DWORD RetVal = 0;
- TCHAR PathPart[MAX_PATH];
- LPTSTR FilePart = NULL;
-
- RetVal = GetFullPathName(InputDistPath.c_str(),
- MAX_PATH,
- PathPart,
- &FilePart);
- LastError = GetLastError();
- if (0 == RetVal) {
-
- throw(std::invalid_argument("Error determining directory names"));
-
- }
- DistSourcePath = PathPart;
-
- if ( (NULL != FilePart) && (0 != *FilePart) ) {
-
-
-
- DistName = FilePart;
-
- } else {
-
- throw(std::invalid_argument("Unable to determine distribution name"));
-
- }
-
- std::string::size_type DistNameIndex;
-
-
-
- DistNameIndex = DistSourcePath.rfind("\\");
- if (std::string::npos == DistNameIndex) {
-
- throw(std::invalid_argument("Unable to determine the name of the "
- "distribution parent directory"));
-
- }
-
- DistParentPath = DistSourcePath.substr(0, DistNameIndex);
-
- }
-
- bool
- IsInContainer(std::string Needle, StringContainer Haystack) {
-
- std::transform(Needle.begin(), Needle.end(), Needle.begin(), ::toupper);
-
- StringContainer::iterator iFile;
-
- for (iFile = Haystack.begin(); iFile != Haystack.end(); iFile++) {
-
- std::transform(iFile->begin(), iFile->end(), iFile->begin(), ::toupper);
- if (*iFile == Needle) {
-
- return true;
-
- }
- }
-
- return false;
-
- }
-
-
- StringContainer
- GetDirectoryList(std::string DistPath, StringContainer ExcludeName) {
-
- DistPath += "\\*.*";
-
- WIN32_FIND_DATA FileData;
- HANDLE ListHandle;
- std::string FileName;
-
- ListHandle = FindFirstFile(DistPath.c_str(),
- &FileData);
-
- StringContainer DirName;
-
-
- if ( (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
-
- (! IsInContainer(FileData.cFileName, ExcludeName)) ) {
-
- DirName.push_back(FileData.cFileName);
-
- }
-
- while (true) {
-
- if (FindNextFile(ListHandle, &FileData)) {
-
- if ( (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
-
- (! IsInContainer(FileData.cFileName, ExcludeName)) ) {
-
- DirName.push_back(FileData.cFileName);
-
- }
-
- } else {
-
- LastError = GetLastError();
- if (ERROR_NO_MORE_FILES == LastError) {
-
- break;
-
- } else {
- std::ostringstream temp;
-
- temp << "Error determining directory list of '"
- << DistPath << "'";
- throw(std::runtime_error(temp.str()));
-
- }
- }
- }
-
- if (FindClose(ListHandle) == 0) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "***Error closing handle for " << DistPath;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- return DirName;
- }
-
- StringContainer
- GetFileList(std::string DirPath, StringContainer ExcludeName) {
-
- std::string DirPathFilter;
-
- DirPathFilter = DirPath + "\\*.*";
-
- WIN32_FIND_DATA FileData;
- HANDLE ListHandle;
-
- ListHandle = FindFirstFile(DirPathFilter.c_str(),
- &FileData);
-
- StringContainer FileNameList;
- std::string FileName;
-
- if ( !(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
-
- (! IsInContainer(FileData.cFileName, ExcludeName)) ) {
-
- FileNameList.push_back(FileData.cFileName);
-
- }
-
- while (true) {
-
- if (FindNextFile(ListHandle, &FileData)) {
-
- if ( !(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
-
- (! IsInContainer(FileData.cFileName, ExcludeName)) ) {
-
- FileNameList.push_back(FileData.cFileName);
-
- }
-
- } else {
-
- LastError = GetLastError();
- if (ERROR_NO_MORE_FILES == LastError) {
-
- break;
-
- } else {
- std::ostringstream temp;
-
- temp << "Error determining directory list of '"
- << DirPath << "'";
- throw(std::runtime_error(temp.str()));
-
- }
- }
- }
-
- if (FindClose(ListHandle) == 0) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "***Error closing handle for " << DirPath;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- return FileNameList;
- }
-
- void
- UpdateOneDirectory(std::string SourceDir,
- std::string DestDir,
- StringContainer ExcludeName) {
-
- StringContainer DestDirFileList;
-
-
- DestDirFileList = GetFileList(DestDir, ExcludeName);
-
- StringContainer::iterator iFile;
- std::string FileToDelete;
-
- for (iFile = DestDirFileList.begin();
- iFile != DestDirFileList.end();
- iFile++) {
-
- if (! IsInContainer(*iFile, ExcludeName)) {
-
- FileToDelete = DestDir + "\\";
- FileToDelete += *iFile;
- if (DeleteFile(FileToDelete.c_str()) == 0) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "***Error deleting " << FileToDelete;
- throw(std::runtime_error(Temp.str()));
-
- }
- }
- }
-
- StringContainer SourceDirFileList;
- std::string SourceFile, DestFile;
-
- SourceDirFileList = GetFileList(SourceDir, ExcludeName);
- for (iFile = SourceDirFileList.begin();
- iFile != SourceDirFileList.end();
- iFile++) {
-
- if (! IsInContainer(*iFile, ExcludeName)) {
-
- SourceFile = SourceDir + "\\";
- SourceFile += *iFile;
- DestFile = DestDir + "\\";
- DestFile += *iFile;
- if (CopyFile(SourceFile.c_str(), DestFile.c_str(), true) == 0) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "Error copying " << SourceFile << " to " << DestFile;
- throw(std::runtime_error(Temp.str()));
-
- }
- }
- }
-
- StringContainer SourceDirDirList, DestDirDirList;
- std::string SourceUpdateDir, DestUpdateDir;
- StringContainer SourceDirExtraDirList, DestDirExtraDirList;
-
- SourceDirDirList = GetDirectoryList(SourceDir, ExcludeName);
- DestDirDirList = GetDirectoryList(DestDir, ExcludeName);
-
- for (iFile = DestDirDirList.begin();
- iFile != DestDirDirList.end();
- iFile++) {
-
- if (! IsInContainer(*iFile, SourceDirDirList)) {
-
- DestDirExtraDirList.push_back(*iFile);
-
- }
-
- }
-
- for (iFile = SourceDirDirList.begin();
- iFile != SourceDirDirList.end();
- iFile++) {
-
- if (! IsInContainer(*iFile, DestDirDirList)) {
-
- SourceDirExtraDirList.push_back(*iFile);
-
- }
-
- }
-
-
- if (!SourceDirExtraDirList.empty() || !DestDirExtraDirList.empty()) {
- std::ostringstream Temp;
-
- if (!SourceDirExtraDirList.empty()) {
-
- Temp << "***Error--There were directories in " << SourceDir
- << " that are not in " << DestDir << ":\n\t";
-
- std::copy(SourceDirExtraDirList.begin(),
- SourceDirExtraDirList.end(),
- std::ostream_iterator<std::string>(Temp, "\n\t"));
-
- }
-
- if (!DestDirExtraDirList.empty()) {
-
- Temp << "***Error--There were directories in " << DestDir
- << " that are not in " << SourceDir << ":\n\t";
- std::copy(DestDirExtraDirList.begin(),
- DestDirExtraDirList.end(),
- std::ostream_iterator<std::string>(Temp, "\n\t"));
-
- }
-
- throw(std::runtime_error(Temp.str()));
- }
-
- for (iFile = DestDirDirList.begin();
- iFile != DestDirDirList.end();
- iFile++) {
-
- SourceUpdateDir = SourceDir + "\\";
- SourceUpdateDir += *iFile;
- DestUpdateDir = DestDir + "\\";
- DestUpdateDir += *iFile;
- UpdateOneDirectory(SourceUpdateDir, DestUpdateDir, ExcludeName);
-
- }
-
- }
-
-
- void
- UpdateCommonDirectories(std::string DistPath, std::string DistParentPath,
- StringContainer ExcludeName) {
-
- StringContainer DistDirList, DistParentDirList;
-
- DistDirList = GetDirectoryList(DistPath, ExcludeName);
- DistParentDirList = GetDirectoryList(DistParentPath, ExcludeName);
-
- #if 0
-
- std::cout << "Directory list of distribution--\n";
- std::copy(DistDirList.begin(),
- DistDirList.end(),
- std::ostream_iterator<std::string>(std::cout, "\n"));
-
- std::cout << "\nDirectory list of distribution parent--\n";
- std::copy(DistParentDirList.begin(),
- DistParentDirList.end(),
- std::ostream_iterator<std::string>(std::cout, "\n"));
-
- #endif
-
- StringContainer::iterator iDistDir;
-
- for (iDistDir = DistDirList.begin();
- iDistDir != DistDirList.end();
- iDistDir++) {
- std::string SourceDir, DestDir;
-
- if (IsInContainer(*iDistDir, DistParentDirList)) {
-
- SourceDir = DistParentPath + "\\";
- SourceDir += *iDistDir;
- DestDir = DistPath + "\\";
- DestDir += *iDistDir;
- UpdateOneDirectory(SourceDir, DestDir, ExcludeName);
- }
-
- }
-
- return;
-
- }
-
- void
- PrepareTempDir(std::string TempPath) {
-
- WIN32_FIND_DATA FileData;
- DWORD LastError;
- HANDLE DirHandle;
- std::ostringstream Temp;
-
- DirHandle = FindFirstFile(TempPath.c_str(), &FileData);
- if (INVALID_HANDLE_VALUE == DirHandle) {
-
- if (!CreateDirectory(TempPath.c_str(), NULL)) {
-
- LastError = GetLastError();
- Temp << "***Error creating the temporary directory " << TempPath;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- return;
-
- }
-
-
- if ( !(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
-
- if (CloseHandle(DirHandle) != 0) {
-
- LastError = GetLastError();
- Temp << "***Error closing handle for " << TempPath;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- if (DeleteFile(TempPath.c_str()) != 0) {
-
- LastError = GetLastError();
- Temp << "***Error deleting " << TempPath;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- PrepareTempDir(TempPath);
- return;
-
- }
-
- if (CloseHandle(DirHandle) != 0) {
-
- LastError = GetLastError();
- Temp << "***Error closing handle for " << TempPath;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- DeletePath(TempPath);
- PrepareTempDir(TempPath);
-
- }
-
- void
- DeletePath(std::string Path) {
-
- StringContainer DirList, FileList, ExcludeName;
- StringContainer::iterator iName;
- std::string NameToDelete;
-
- ExcludeName.push_back(".");
- ExcludeName.push_back("..");
-
- DirList = GetDirectoryList(Path, ExcludeName);
- FileList = GetFileList(Path, ExcludeName);
-
- for (iName = FileList.begin();
- iName != FileList.end();
- iName++) {
-
- NameToDelete = Path + "\\";
- NameToDelete += *iName;
- if (DeleteFile(NameToDelete.c_str()) == 0) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "***Error deleting " << NameToDelete << " in " << Path;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- }
-
- for (iName = DirList.begin();
- iName != DirList.end();
- iName++) {
-
- NameToDelete = Path + "\\";
- NameToDelete += *iName;
- DeletePath(NameToDelete);
-
- }
-
- if (RemoveDirectory(Path.c_str()) == 0) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "***Error removing directory " << Path;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- }
-
- void
- CopyDirectory(std::string SourcePath, std::string DestPath, StringContainer ExcludeName) {
-
- StringContainer DirList, FileList;
- StringContainer::iterator iName;
- std::string SourceName, DestName;
- BOOL FailIfExists = true;
-
- DirList = GetDirectoryList(SourcePath, ExcludeName);
- FileList = GetFileList(SourcePath, ExcludeName);
-
- for (iName = FileList.begin();
- iName != FileList.end();
- iName++) {
-
- SourceName = SourcePath + "\\";
- SourceName += *iName;
- DestName = DestPath + "\\";
- DestName += *iName;
- if (!CopyFile(SourceName.c_str(), DestName.c_str(), FailIfExists)) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "Error copying " << *iName << " to " << DestPath;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- }
-
- for (iName = DirList.begin();
- iName != DirList.end();
- iName++) {
-
- SourceName = SourcePath + "\\";
- SourceName += *iName;
- DestName = DestPath + "\\";
- DestName += *iName;
-
- if (!CreateDirectory(DestName.c_str(), NULL)) {
- std::ostringstream Temp;
-
- LastError = GetLastError();
- Temp << "***Error creating the temporary directory " << DestName;
- throw(std::runtime_error(Temp.str()));
-
- }
-
- CopyDirectory(SourceName, DestName, ExcludeName);
-
- }
-
- }
|