2019-10-28 22:46:15 +00:00
|
|
|
/*
|
|
|
|
** file_directory.cpp
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2008-2009 Randy Heit
|
|
|
|
** Copyright 2009 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "resourcefile.h"
|
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "printf.h"
|
2020-04-11 21:50:43 +00:00
|
|
|
#include "findfile.h"
|
2019-10-28 22:46:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Zip Lump
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
struct FDirectoryLump : public FResourceLump
|
|
|
|
{
|
2020-04-11 21:54:33 +00:00
|
|
|
FileReader NewReader() override;
|
|
|
|
int FillCache() override;
|
2019-10-28 22:46:15 +00:00
|
|
|
|
|
|
|
FString mFullPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Zip file
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
class FDirectory : public FResourceFile
|
|
|
|
{
|
|
|
|
TArray<FDirectoryLump> Lumps;
|
2019-11-02 18:12:01 +00:00
|
|
|
const bool nosubdir;
|
2019-10-28 22:46:15 +00:00
|
|
|
|
|
|
|
int AddDirectory(const char *dirpath);
|
|
|
|
void AddEntry(const char *fullpath, int size);
|
|
|
|
|
|
|
|
public:
|
2020-01-12 07:53:11 +00:00
|
|
|
FDirectory(const char * dirname, bool nosubdirflag = false);
|
2020-04-11 21:54:33 +00:00
|
|
|
bool Open(bool quiet, LumpFilterInfo* filter);
|
2019-10-28 22:46:15 +00:00
|
|
|
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-11-02 18:12:01 +00:00
|
|
|
FDirectory::FDirectory(const char * directory, bool nosubdirflag)
|
|
|
|
: FResourceFile(NULL), nosubdir(nosubdirflag)
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
|
|
|
FString dirname;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
directory = _fullpath(NULL, directory, _MAX_PATH);
|
|
|
|
#else
|
|
|
|
// Todo for Linux: Resolve the path before using it
|
|
|
|
#endif
|
|
|
|
dirname = directory;
|
|
|
|
#ifdef _WIN32
|
|
|
|
free((void *)directory);
|
|
|
|
#endif
|
|
|
|
dirname.Substitute("\\", "/");
|
|
|
|
if (dirname[dirname.Len()-1] != '/') dirname += '/';
|
|
|
|
FileName = dirname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Windows version
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int FDirectory::AddDirectory(const char *dirpath)
|
|
|
|
{
|
2020-01-07 00:11:19 +00:00
|
|
|
void * handle;
|
2019-10-28 22:46:15 +00:00
|
|
|
int count = 0;
|
|
|
|
|
2020-01-07 00:11:19 +00:00
|
|
|
FString dirmatch = dirpath;
|
|
|
|
findstate_t find;
|
2019-10-28 22:46:15 +00:00
|
|
|
dirmatch += '*';
|
|
|
|
|
2020-01-07 00:11:19 +00:00
|
|
|
handle = I_FindFirst(dirmatch.GetChars(), &find);
|
|
|
|
if (handle == ((void *)(-1)))
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
|
|
|
Printf("Could not scan '%s': %s\n", dirpath, strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2020-01-07 00:11:19 +00:00
|
|
|
// I_FindName only returns the file's name and not its full path
|
|
|
|
auto attr = I_FindAttr(&find);
|
|
|
|
if (attr & FA_HIDDEN)
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
|
|
|
// Skip hidden files and directories. (Prevents SVN bookkeeping
|
|
|
|
// info from being included.)
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-07 00:11:19 +00:00
|
|
|
FString fi = I_FindName(&find);
|
|
|
|
if (attr & FA_DIREC)
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
2019-11-02 18:12:01 +00:00
|
|
|
if (nosubdir || (fi[0] == '.' &&
|
2020-01-07 00:11:19 +00:00
|
|
|
(fi[1] == '\0' ||
|
|
|
|
(fi[1] == '.' && fi[2] == '\0'))))
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
2020-04-11 21:54:33 +00:00
|
|
|
// Do not record . and .. directories.
|
|
|
|
continue;
|
2019-10-28 22:46:15 +00:00
|
|
|
}
|
|
|
|
FString newdir = dirpath;
|
2019-10-29 00:00:44 +00:00
|
|
|
newdir << fi << '/';
|
2019-10-28 22:46:15 +00:00
|
|
|
count += AddDirectory(newdir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-29 18:53:46 +00:00
|
|
|
if (strstr(fi, ".orig") || strstr(fi, ".bak"))
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
2020-01-07 00:11:19 +00:00
|
|
|
// We shouldn't add backup files to the file system
|
2019-10-28 22:46:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-01-07 00:11:19 +00:00
|
|
|
size_t size = 0;
|
|
|
|
FString fn = FString(dirpath) + fi;
|
|
|
|
if (GetFileInfo(fn, &size, nullptr))
|
|
|
|
{
|
2020-04-11 21:54:33 +00:00
|
|
|
AddEntry(fn, (int)size);
|
2020-01-07 00:11:19 +00:00
|
|
|
count++;
|
|
|
|
}
|
2019-10-28 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 00:11:19 +00:00
|
|
|
} while (I_FindNext (handle, &find) == 0);
|
|
|
|
I_FindClose (handle);
|
2019-10-28 22:46:15 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2020-04-11 21:54:33 +00:00
|
|
|
bool FDirectory::Open(bool quiet, LumpFilterInfo* filter)
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
|
|
|
NumLumps = AddDirectory(FileName);
|
2020-04-11 21:54:33 +00:00
|
|
|
PostProcessArchive(&Lumps[0], sizeof(FDirectoryLump), filter);
|
2019-10-28 22:46:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FDirectory::AddEntry(const char *fullpath, int size)
|
|
|
|
{
|
|
|
|
FDirectoryLump *lump_p = &Lumps[Lumps.Reserve(1)];
|
|
|
|
|
|
|
|
// Store the full path here so that we can access the file later, even if it is from a filter directory.
|
|
|
|
lump_p->mFullPath = fullpath;
|
|
|
|
|
|
|
|
// [mxd] Convert name to lowercase
|
|
|
|
FString name = fullpath + strlen(FileName);
|
|
|
|
name.ToLower();
|
|
|
|
|
|
|
|
// The lump's name is only the part relative to the main directory
|
|
|
|
lump_p->LumpNameSetup(name);
|
|
|
|
lump_p->LumpSize = size;
|
|
|
|
lump_p->Owner = this;
|
|
|
|
lump_p->Flags = 0;
|
2020-04-11 21:54:33 +00:00
|
|
|
lump_p->CheckEmbedded();
|
2019-10-28 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FileReader FDirectoryLump::NewReader()
|
|
|
|
{
|
|
|
|
FileReader fr;
|
|
|
|
fr.OpenFile(mFullPath);
|
|
|
|
return fr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2020-04-11 21:54:33 +00:00
|
|
|
int FDirectoryLump::FillCache()
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
|
|
|
FileReader fr;
|
2020-04-11 21:54:33 +00:00
|
|
|
Cache = new char[LumpSize];
|
2019-10-28 22:46:15 +00:00
|
|
|
if (!fr.OpenFile(mFullPath))
|
|
|
|
{
|
2020-04-11 21:54:33 +00:00
|
|
|
memset(Cache, 0, LumpSize);
|
2019-10-28 22:46:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-04-11 21:54:33 +00:00
|
|
|
fr.Read(Cache, LumpSize);
|
|
|
|
RefCount = 1;
|
2019-10-28 22:46:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// File open
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2020-04-11 21:54:33 +00:00
|
|
|
FResourceFile *CheckDir(const char *filename, bool quiet, bool nosubdirflag, LumpFilterInfo* filter)
|
2019-10-28 22:46:15 +00:00
|
|
|
{
|
2020-09-19 08:07:53 +00:00
|
|
|
auto rf = new FDirectory(filename, nosubdirflag);
|
2020-04-11 21:54:33 +00:00
|
|
|
if (rf->Open(quiet, filter)) return rf;
|
2019-10-28 22:46:15 +00:00
|
|
|
delete rf;
|
2020-09-19 08:07:53 +00:00
|
|
|
return nullptr;
|
2019-10-28 22:46:15 +00:00
|
|
|
}
|
|
|
|
|