[CABMAN] Add commandline support for creating a cab with folders

CORE-17230
This commit is contained in:
Mark Jansen 2020-09-12 16:21:34 +02:00
parent a9f60321f0
commit 70193adc89
No known key found for this signature in database
GPG key ID: B39240EE84BEAE8B
4 changed files with 78 additions and 46 deletions

View file

@ -155,7 +155,7 @@ void CCabinet::ConvertPath(std::string& Path)
} }
const char* CCabinet::GetFileName(const char* Path) std::string CCabinet::GetFileName(const std::string& Path)
/* /*
* FUNCTION: Returns a pointer to file name * FUNCTION: Returns a pointer to file name
* ARGUMENTS: * ARGUMENTS:
@ -172,7 +172,7 @@ const char* CCabinet::GetFileName(const char* Path)
if (IsSeparator(Path [i - 1])) if (IsSeparator(Path [i - 1]))
j = i; j = i;
return Path + j; return Path.c_str() + j;
} }
@ -225,7 +225,7 @@ void CCabinet::SetDestinationPath(const char* DestinationPath)
NormalizePath(DestPath); NormalizePath(DestPath);
} }
ULONG CCabinet::AddSearchCriteria(const char* SearchCriteria) ULONG CCabinet::AddSearchCriteria(const std::string& SearchCriteria, const std::string& TargetFolder)
/* /*
* FUNCTION: Adds a criteria to the search criteria list * FUNCTION: Adds a criteria to the search criteria list
* ARGUMENTS: * ARGUMENTS:
@ -248,6 +248,7 @@ ULONG CCabinet::AddSearchCriteria(const char* SearchCriteria)
// Set the actual criteria string // Set the actual criteria string
Criteria->Search = SearchCriteria; Criteria->Search = SearchCriteria;
Criteria->TargetFolder = TargetFolder;
return CAB_STATUS_SUCCESS; return CAB_STATUS_SUCCESS;
} }
@ -274,6 +275,16 @@ bool CCabinet::HasSearchCriteria()
return !CriteriaList.empty(); return !CriteriaList.empty();
} }
std::string CCabinet::CreateCabFilename(PCFFILE_NODE Node)
{
std::string fname = GetFileName(Node->FileName);
if (!Node->TargetFolder.empty())
{
fname = Node->TargetFolder + fname;
}
return fname;
}
bool CCabinet::SetCompressionCodec(const char* CodecName) bool CCabinet::SetCompressionCodec(const char* CodecName)
/* /*
* FUNCTION: Selects the codec to use for compression * FUNCTION: Selects the codec to use for compression
@ -591,6 +602,7 @@ ULONG CCabinet::FindNext(PCAB_SEARCH Search)
for (PSEARCH_CRITERIA Criteria : CriteriaList) for (PSEARCH_CRITERIA Criteria : CriteriaList)
{ {
// FIXME: We could handle path\filename here
if (MatchFileNamePattern((*Search->Next)->FileName.c_str(), Criteria->Search.c_str())) if (MatchFileNamePattern((*Search->Next)->FileName.c_str(), Criteria->Search.c_str()))
{ {
bFound = true; bFound = true;
@ -1202,7 +1214,7 @@ ULONG CCabinet::WriteFileToScratchStorage(PCFFILE_NODE FileNode)
SourceFile = fopen(FileNode->FileName.c_str(), "rb"); SourceFile = fopen(FileNode->FileName.c_str(), "rb");
if (SourceFile == NULL) if (SourceFile == NULL)
{ {
DPRINT(MID_TRACE, ("File not found (%s).\n", FileNode->FileName.c_str())); DPRINT(MID_TRACE, ("File not found (%s).\n", FileNode->FileNameOnDisk.c_str()));
return CAB_STATUS_NOFILE; return CAB_STATUS_NOFILE;
} }
@ -1227,7 +1239,7 @@ ULONG CCabinet::WriteFileToScratchStorage(PCFFILE_NODE FileNode)
CurrentFolderNode->Commit = true; CurrentFolderNode->Commit = true;
PrevCabinetNumber = CurrentDiskNumber; PrevCabinetNumber = CurrentDiskNumber;
Size = sizeof(CFFILE) + (ULONG)strlen(GetFileName(FileNode->FileName.c_str())) + 1; Size = sizeof(CFFILE) + (ULONG)CreateCabFilename(FileNode).length() + 1;
CABHeader.FileTableOffset += Size; CABHeader.FileTableOffset += Size;
TotalFileSize += Size; TotalFileSize += Size;
DiskSize += Size; DiskSize += Size;
@ -1500,7 +1512,7 @@ ULONG CCabinet::CloseCabinet()
} }
ULONG CCabinet::AddFile(char* FileName) ULONG CCabinet::AddFile(const std::string& FileName, const std::string& TargetFolder)
/* /*
* FUNCTION: Adds a file to the current disk * FUNCTION: Adds a file to the current disk
* ARGUMENTS: * ARGUMENTS:
@ -1534,6 +1546,9 @@ ULONG CCabinet::AddFile(char* FileName)
FileNode->FolderNode = CurrentFolderNode; FileNode->FolderNode = CurrentFolderNode;
FileNode->FileName = NewFileName; FileNode->FileName = NewFileName;
FileNode->TargetFolder = TargetFolder;
if (FileNode->TargetFolder.length() > 0 && FileNode->TargetFolder[FileNode->TargetFolder.length() - 1] != '\\')
FileNode->TargetFolder += '\\';
/* FIXME: Check for and handle large files (>= 2GB) */ /* FIXME: Check for and handle large files (>= 2GB) */
FileNode->File.FileSize = GetSizeOfFile(SrcFile); FileNode->File.FileSize = GetSizeOfFile(SrcFile);
@ -1569,9 +1584,6 @@ bool CCabinet::CreateSimpleCabinet()
*/ */
{ {
bool bRet = false; bool bRet = false;
const char* pszFile;
char szFilePath[PATH_MAX];
char szFile[PATH_MAX];
ULONG Status; ULONG Status;
#if defined(_WIN32) #if defined(_WIN32)
@ -1595,26 +1607,25 @@ bool CCabinet::CreateSimpleCabinet()
for (PSEARCH_CRITERIA Criteria : CriteriaList) for (PSEARCH_CRITERIA Criteria : CriteriaList)
{ {
// Store the file path with a trailing slash in szFilePath // Store the file path with a trailing slash in szFilePath
ConvertPath(Criteria->Search); std::string szSearchPath = Criteria->Search;
pszFile = strrchr(Criteria->Search.c_str(), DIR_SEPARATOR_CHAR); ConvertPath(szSearchPath);
auto sep = szSearchPath.find_last_of(DIR_SEPARATOR_CHAR);
std::string szFilePath;
std::string pszFile;
if(pszFile) if (sep != std::string::npos)
{ {
// Set the pointer to the start of the file name, not the slash pszFile = szSearchPath.substr(sep + 1); // We want the filename, not the dir separator!
pszFile++;
strncpy(szFilePath, Criteria->Search.c_str(), pszFile - Criteria->Search.c_str()); szFilePath = szSearchPath.substr(0, sep + 1);
szFilePath[pszFile - Criteria->Search.c_str()] = 0;
} }
else else
{ {
pszFile = Criteria->Search.c_str(); pszFile = Criteria->Search;
#if defined(_WIN32) #if !defined(_WIN32)
szFilePath[0] = 0;
#else
// needed for opendir() // needed for opendir()
strcpy(szFilePath, "./"); szFilePath = "./";
#endif #endif
} }
@ -1633,10 +1644,10 @@ bool CCabinet::CreateSimpleCabinet()
{ {
if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{ {
strcpy(szFile, szFilePath); std::string szFile = szFilePath;
strcat(szFile, FindFileData.cFileName); szFile += FindFileData.cFileName;
Status = AddFile(szFile); Status = AddFile(szFile, Criteria->TargetFolder);
if(Status != CAB_STATUS_SUCCESS) if(Status != CAB_STATUS_SUCCESS)
{ {
@ -1651,22 +1662,22 @@ bool CCabinet::CreateSimpleCabinet()
FindClose(hFind); FindClose(hFind);
#else #else
// Unix: Use opendir/readdir to loop through all entries, stat to check if it's a file and MatchFileNamePattern to match the file against the pattern // Unix: Use opendir/readdir to loop through all entries, stat to check if it's a file and MatchFileNamePattern to match the file against the pattern
dirp = opendir(szFilePath); dirp = opendir(szFilePath.c_str());
if(dirp) if(dirp)
{ {
while( (dp = readdir(dirp)) ) while( (dp = readdir(dirp)) )
{ {
strcpy(szFile, szFilePath); std::string szFile = szFilePath;
strcat(szFile, dp->d_name); szFile += dp->d_name;
if(stat(szFile, &stbuf) == 0) if(stat(szFile.c_str(), &stbuf) == 0)
{ {
if(stbuf.st_mode != S_IFDIR) if(stbuf.st_mode != S_IFDIR)
{ {
if(MatchFileNamePattern(dp->d_name, pszFile)) if(MatchFileNamePattern(dp->d_name, pszFile.c_str()))
{ {
Status = AddFile(szFile); Status = AddFile(szFile, Criteria->TargetFolder);
if(Status != CAB_STATUS_SUCCESS) if(Status != CAB_STATUS_SUCCESS)
{ {
@ -1890,6 +1901,7 @@ ULONG CCabinet::LocateFile(const char* FileName,
for (PCFFILE_NODE Node : FileList) for (PCFFILE_NODE Node : FileList)
{ {
// FIXME: We could handle path\filename here
if (strcasecmp(FileName, Node->FileName.c_str()) == 0) if (strcasecmp(FileName, Node->FileName.c_str()) == 0)
{ {
CurrentFolderNode = LocateFolderNode(Node->File.FileControlID); CurrentFolderNode = LocateFolderNode(Node->File.FileControlID);
@ -2011,6 +2023,7 @@ ULONG CCabinet::ReadFileTable()
Status = ReadString(Buf, PATH_MAX); Status = ReadString(Buf, PATH_MAX);
if (Status != CAB_STATUS_SUCCESS) if (Status != CAB_STATUS_SUCCESS)
return Status; return Status;
// FIXME: We could split up folder\file.txt here
File->FileName = Buf; File->FileName = Buf;
DPRINT(MAX_TRACE, ("Found file '%s' at uncompressed offset (0x%X). Size (%u bytes) ControlId (0x%X).\n", DPRINT(MAX_TRACE, ("Found file '%s' at uncompressed offset (0x%X). Size (%u bytes) ControlId (0x%X).\n",
@ -2195,7 +2208,7 @@ void CCabinet::DestroyDeletedFileNodes()
DPRINT(MAX_TRACE, ("Deleting file node: '%s'\n", CurNode->FileName.c_str())); DPRINT(MAX_TRACE, ("Deleting file node: '%s'\n", CurNode->FileName.c_str()));
TotalFileSize -= (sizeof(CFFILE) + (ULONG)strlen(GetFileName(CurNode->FileName.c_str())) + 1); TotalFileSize -= (sizeof(CFFILE) + (ULONG)CreateCabFilename(CurNode).length() + 1);
delete CurNode; delete CurNode;
} }
@ -2669,7 +2682,7 @@ ULONG CCabinet::WriteFileEntries()
return CAB_STATUS_CANNOT_WRITE; return CAB_STATUS_CANNOT_WRITE;
} }
std::string fname = GetFileName(File->FileName.c_str()); std::string fname = CreateCabFilename(File);
if (fwrite(fname.c_str(), fname.length() + 1, 1, FileHandle) < 1) if (fwrite(fname.c_str(), fname.length() + 1, 1, FileHandle) < 1)
{ {
DPRINT(MIN_TRACE, ("Cannot write to file.\n")); DPRINT(MIN_TRACE, ("Cannot write to file.\n"));

View file

@ -227,6 +227,7 @@ typedef struct _CFFILE_NODE
{ {
CFFILE File = { 0 }; CFFILE File = { 0 };
std::string FileName; std::string FileName;
std::string TargetFolder;
PCFDATA_NODE DataBlock = nullptr; // First data block of file. NULL if not known PCFDATA_NODE DataBlock = nullptr; // First data block of file. NULL if not known
bool Commit = false; // true if the file data should be committed bool Commit = false; // true if the file data should be committed
bool Delete = false; // true if marked for deletion bool Delete = false; // true if marked for deletion
@ -235,7 +236,8 @@ typedef struct _CFFILE_NODE
typedef struct _SEARCH_CRITERIA typedef struct _SEARCH_CRITERIA
{ {
std::string Search; // The actual search criteria std::string Search; // The actual search criteria
std::string TargetFolder; // The filename will be TargetFolder\file
} SEARCH_CRITERIA, *PSEARCH_CRITERIA; } SEARCH_CRITERIA, *PSEARCH_CRITERIA;
typedef struct _CAB_SEARCH typedef struct _CAB_SEARCH
@ -311,8 +313,8 @@ public:
bool IsSeparator(char Char); bool IsSeparator(char Char);
/* Replaces \ or / with the one used be the host environment */ /* Replaces \ or / with the one used be the host environment */
void ConvertPath(std::string& Path); void ConvertPath(std::string& Path);
/* Returns a pointer to the filename part of a fully qualified filename */ /* Returns the filename part of a fully qualified filename */
const char* GetFileName(const char* Path); std::string GetFileName(const std::string& Path);
/* Normalizes a path */ /* Normalizes a path */
void NormalizePath(std::string& Path); void NormalizePath(std::string& Path);
/* Returns name of cabinet file */ /* Returns name of cabinet file */
@ -342,12 +344,14 @@ public:
/* Returns whether a codec engine is selected */ /* Returns whether a codec engine is selected */
bool IsCodecSelected(); bool IsCodecSelected();
/* Adds a search criteria for adding files to a simple cabinet, displaying files in a cabinet or extracting them */ /* Adds a search criteria for adding files to a simple cabinet, displaying files in a cabinet or extracting them */
ULONG AddSearchCriteria(const char* SearchCriteria); ULONG AddSearchCriteria(const std::string& SearchCriteria, const std::string& TargetFolder);
/* Destroys the search criteria list */ /* Destroys the search criteria list */
void DestroySearchCriteria(); void DestroySearchCriteria();
/* Returns whether we have search criteria */ /* Returns whether we have search criteria */
bool HasSearchCriteria(); bool HasSearchCriteria();
std::string CreateCabFilename(PCFFILE_NODE Node);
#ifndef CAB_READ_ONLY #ifndef CAB_READ_ONLY
/* Creates a simple cabinet based on the search criteria data */ /* Creates a simple cabinet based on the search criteria data */
bool CreateSimpleCabinet(); bool CreateSimpleCabinet();
@ -370,7 +374,7 @@ public:
/* Closes the current cabinet */ /* Closes the current cabinet */
ULONG CloseCabinet(); ULONG CloseCabinet();
/* Adds a file to the current disk */ /* Adds a file to the current disk */
ULONG AddFile(char* FileName); ULONG AddFile(const std::string& FileName, const std::string& TargetFolder);
/* Sets the maximum size of the current disk */ /* Sets the maximum size of the current disk */
void SetMaxDiskSize(ULONG Size); void SetMaxDiskSize(ULONG Size);
#endif /* CAB_READ_ONLY */ #endif /* CAB_READ_ONLY */

View file

@ -193,7 +193,7 @@ void CCABManager::Usage()
printf("ReactOS Cabinet Manager\n\n"); printf("ReactOS Cabinet Manager\n\n");
printf("CABMAN [-D | -E] [-A] [-L dir] cabinet [filename ...]\n"); printf("CABMAN [-D | -E] [-A] [-L dir] cabinet [filename ...]\n");
printf("CABMAN [-M mode] -C dirfile [-I] [-RC file] [-P dir]\n"); printf("CABMAN [-M mode] -C dirfile [-I] [-RC file] [-P dir]\n");
printf("CABMAN [-M mode] -S cabinet filename [...]\n"); printf("CABMAN [-M mode] -S cabinet filename [-F folder] [filename] [...]\n");
printf(" cabinet Cabinet file.\n"); printf(" cabinet Cabinet file.\n");
printf(" filename Name of the file to add to or extract from the cabinet.\n"); printf(" filename Name of the file to add to or extract from the cabinet.\n");
printf(" Wild cards and multiple filenames\n"); printf(" Wild cards and multiple filenames\n");
@ -206,6 +206,7 @@ void CCABManager::Usage()
printf(" -C Create cabinet.\n"); printf(" -C Create cabinet.\n");
printf(" -D Display cabinet directory.\n"); printf(" -D Display cabinet directory.\n");
printf(" -E Extract files from cabinet.\n"); printf(" -E Extract files from cabinet.\n");
printf(" -F Put the files from the next 'filename' filter in the cab in folder\filename.\n");
printf(" -I Don't create the cabinet, only the .inf file.\n"); printf(" -I Don't create the cabinet, only the .inf file.\n");
printf(" -L dir Location to place extracted or generated files\n"); printf(" -L dir Location to place extracted or generated files\n");
printf(" (default is current directory).\n"); printf(" (default is current directory).\n");
@ -233,7 +234,7 @@ bool CCABManager::ParseCmdline(int argc, char* argv[])
int i; int i;
bool ShowUsage; bool ShowUsage;
bool FoundCabinet = false; bool FoundCabinet = false;
std::string NextFolder;
ShowUsage = (argc < 2); ShowUsage = (argc < 2);
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
@ -262,6 +263,19 @@ bool CCABManager::ParseCmdline(int argc, char* argv[])
Mode = CM_MODE_EXTRACT; Mode = CM_MODE_EXTRACT;
break; break;
case 'f':
case 'F':
if (argv[i][2] == 0)
{
i++;
NextFolder = argv[i];
}
else
{
NextFolder = argv[i] + 2;
}
break;
case 'i': case 'i':
case 'I': case 'I':
InfFileOnly = true; InfFileOnly = true;
@ -374,7 +388,8 @@ bool CCABManager::ParseCmdline(int argc, char* argv[])
else if(FoundCabinet) else if(FoundCabinet)
{ {
// For creating simple cabinets, displaying or extracting them, add the argument as a search criteria // For creating simple cabinets, displaying or extracting them, add the argument as a search criteria
AddSearchCriteria(argv[i]); AddSearchCriteria(argv[i], NextFolder);
NextFolder.clear();
} }
else else
{ {
@ -618,7 +633,7 @@ void CCABManager::OnExtract(PCFFILE File,
{ {
if (Verbose) if (Verbose)
{ {
printf("Extracting %s\n", GetFileName(FileName)); printf("Extracting %s\n", GetFileName(FileName).c_str());
} }
} }
@ -651,7 +666,7 @@ void CCABManager::OnAdd(PCFFILE File,
{ {
if (Verbose) if (Verbose)
{ {
printf("Adding %s\n", GetFileName(FileName)); printf("Adding %s\n", GetFileName(FileName).c_str());
} }
} }

View file

@ -6,7 +6,7 @@
* PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
* Colin Finck <mail@colinfinck.de> * Colin Finck <mail@colinfinck.de>
* NOTES: The directive file format is similar to the * NOTES: The directive file format is similar to the
* directive file format used by Microsoft's MAKECAB * directive file format used by Microsoft's MAKECAB (But not entirely compatible!)
* REVISIONS: * REVISIONS:
* CSH 21/03-2001 Created * CSH 21/03-2001 Created
* CSH 15/08-2003 Made it portable * CSH 15/08-2003 Made it portable
@ -1123,17 +1123,17 @@ ULONG CDFParser::PerformFileCopy()
DPRINT(MID_TRACE, ("Adding file: '%s' destination: '%s'.\n", SrcName, DstName)); DPRINT(MID_TRACE, ("Adding file: '%s' destination: '%s'.\n", SrcName, DstName));
Status = AddFile(SrcName); Status = AddFile(SrcName, std::string());
if (Status == CAB_STATUS_CANNOT_OPEN) if (Status == CAB_STATUS_CANNOT_OPEN)
{ {
strcpy(SrcName, FileRelativePath.c_str()); strcpy(SrcName, FileRelativePath.c_str());
strcat(SrcName, BaseFilename); strcat(SrcName, BaseFilename);
Status = AddFile(SrcName); Status = AddFile(SrcName, std::string());
} }
switch (Status) switch (Status)
{ {
case CAB_STATUS_SUCCESS: case CAB_STATUS_SUCCESS:
sprintf(InfLine, "%s=%s", GetFileName(SrcName), DstName); sprintf(InfLine, "%s=%s", GetFileName(SrcName).c_str(), DstName);
WriteInfLine(InfLine); WriteInfLine(InfLine);
break; break;