2014-04-06 17:19:33 +00:00
|
|
|
// Dir.cpp - basic directory interface + classes
|
2012-06-01 15:23:19 +00:00
|
|
|
// For conditions of distribution and use, see copyright notice in VFS.h
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "VFSInternal.h"
|
|
|
|
#include "VFSTools.h"
|
|
|
|
#include "VFSFile.h"
|
|
|
|
#include "VFSDir.h"
|
2014-04-06 17:19:33 +00:00
|
|
|
#include "VFSDirView.h"
|
|
|
|
#include "VFSLoader.h"
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-15 13:04:33 +00:00
|
|
|
//#include <stdio.h>
|
|
|
|
|
2012-06-01 15:23:19 +00:00
|
|
|
VFS_NAMESPACE_START
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase::DirBase(const char *fullpath)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
_setName(fullpath);
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase::~DirBase()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:25:58 +00:00
|
|
|
File *DirBase::getFile(const char *fn)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-07 02:40:50 +00:00
|
|
|
while(fn[0] == '.' && fn[1] == '/')
|
|
|
|
fn += 2;
|
|
|
|
|
2014-04-07 00:25:58 +00:00
|
|
|
const char *slashpos = strchr(fn, '/');
|
|
|
|
if(!slashpos)
|
|
|
|
return getFileByName(fn, true);
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-15 13:04:33 +00:00
|
|
|
size_t copysize = std::max<size_t>(slashpos - fn, 1); // always copy the '/' if it's the first char
|
|
|
|
char * const dirname = (char*)VFS_STACK_ALLOC(copysize + 1);
|
|
|
|
memcpy(dirname, fn, copysize);
|
|
|
|
dirname[copysize] = 0;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-07 00:25:58 +00:00
|
|
|
File *f = getFileFromSubdir(dirname, slashpos + 1);
|
|
|
|
VFS_STACK_FREE(dirname);
|
|
|
|
return f;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 13:04:33 +00:00
|
|
|
DirBase *DirBase::getDir(const char *subdir)
|
|
|
|
{
|
|
|
|
// TODO: get rid of alloc
|
|
|
|
std::string fullpath = joinPath(fullname(), subdir);
|
|
|
|
return _getDirEx(subdir, fullpath.c_str(), false, true, true).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns requested subdir or NULL as first, and last existing subdir in the tree as second.
|
|
|
|
std::pair<DirBase*, DirBase*> DirBase::_getDirEx(const char *subdir, const char * const fullpath,
|
|
|
|
bool forceCreate /* = false */, bool lazyLoad /* = true */, bool useSubtrees /* = true */)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-15 13:04:33 +00:00
|
|
|
//printf("DirBase::_getDirEx [%s] in [%s]\n", subdir, fullname());
|
2014-04-06 17:19:33 +00:00
|
|
|
SkipSelfPath(subdir);
|
|
|
|
if(!subdir[0])
|
2014-04-15 13:04:33 +00:00
|
|
|
return std::make_pair(this, this);
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase *ret = NULL;
|
2012-06-01 15:23:19 +00:00
|
|
|
char *slashpos = (char *)strchr(subdir, '/');
|
|
|
|
|
|
|
|
// if there is a '/' in the string, descend into subdir and continue there
|
|
|
|
if(slashpos)
|
|
|
|
{
|
|
|
|
// from a/b/c, cut out the a, without the trailing '/'.
|
|
|
|
const char *sub = slashpos + 1;
|
2014-04-15 13:04:33 +00:00
|
|
|
size_t copysize = std::max<size_t>(slashpos - subdir, 1); // always copy the '/' if it's the first char
|
2012-06-01 15:23:19 +00:00
|
|
|
char * const t = (char*)VFS_STACK_ALLOC(copysize + 1);
|
|
|
|
memcpy(t, subdir, copysize);
|
|
|
|
t[copysize] = 0;
|
2014-04-15 13:04:33 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
if(DirBase *dir = getDirByName(t, lazyLoad, useSubtrees))
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-15 13:04:33 +00:00
|
|
|
std::pair<DirBase*, DirBase*> subpair = dir->_getDirEx(sub, fullpath, forceCreate, lazyLoad); // descend into subdirs
|
|
|
|
ret = subpair.first;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
if(DirBase *dir = getDirByName(subdir, lazyLoad, useSubtrees))
|
|
|
|
ret = dir;
|
2014-04-15 13:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(forceCreate && !ret)
|
|
|
|
ret = _createAndInsertSubtree(subdir);
|
|
|
|
|
|
|
|
return std::make_pair(ret, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
DirBase *DirBase::_createAndInsertSubtree(const char *subdir)
|
|
|
|
{
|
|
|
|
size_t subdirLen = strlen(subdir);
|
|
|
|
char * const buf = (char*)VFS_STACK_ALLOC(subdirLen + 2);
|
|
|
|
buf[0] = '/';
|
|
|
|
memcpy(buf+1, subdir, subdirLen + 1); //copy terminating \0 too
|
|
|
|
char *s = buf+1;
|
|
|
|
|
|
|
|
DirBase *curdir = this;
|
|
|
|
while(*s)
|
|
|
|
{
|
|
|
|
char *slashpos = strchr(s, '/');
|
|
|
|
if(slashpos)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-15 13:04:33 +00:00
|
|
|
if(slashpos == buf+1) // only check this in first round
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-15 13:04:33 +00:00
|
|
|
s = buf;
|
|
|
|
//printf("abs buf: [%s]\n", buf);
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
2014-04-15 13:04:33 +00:00
|
|
|
*slashpos = 0;
|
|
|
|
//printf("_createAndInsertSubtree: [%s]\n", s);
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
2014-04-15 13:04:33 +00:00
|
|
|
|
|
|
|
DirBase *nextdir = curdir->DirBase::getDirByName(s, false, false); // last 2 params are not relevant
|
|
|
|
if(!nextdir)
|
|
|
|
nextdir = curdir->_createNewSubdir(s);
|
|
|
|
curdir->_subdirs[nextdir->name()] = nextdir;
|
|
|
|
curdir = nextdir;
|
|
|
|
if(!slashpos)
|
|
|
|
break;
|
|
|
|
s = slashpos + 1;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 13:04:33 +00:00
|
|
|
VFS_STACK_FREE(buf);
|
|
|
|
return curdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
DirBase *DirBase::_createNewSubdir(const char *subdir) const
|
|
|
|
{
|
|
|
|
assert(*subdir);
|
|
|
|
//printf("_createNewSubdir: [%s]\n", subdir);
|
|
|
|
// -> newname = fullname() + '/' + subdir
|
|
|
|
size_t fullLen = fullnameLen();
|
|
|
|
size_t subdirLen = strlen(subdir);
|
|
|
|
char * const newname = (char*)VFS_STACK_ALLOC(fullLen + subdirLen + 2);
|
|
|
|
char *ptr = newname;
|
|
|
|
if(fullLen)
|
|
|
|
{
|
|
|
|
memcpy(ptr, fullname(), fullLen);
|
|
|
|
ptr += fullLen;
|
|
|
|
if(ptr[-1] != '/')
|
|
|
|
*ptr++ = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ptr, subdir, subdirLen + 1); // copy terminating \0 too
|
|
|
|
//printf("_createNewSubdir: newname = [%s]\n", newname);
|
|
|
|
DirBase *ret = createNew(newname);
|
|
|
|
//printf("_createNewSubdir: fullname = [%s]\n", ret->fullname());
|
|
|
|
VFS_STACK_FREE(newname);
|
2012-06-01 15:23:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-15 13:04:33 +00:00
|
|
|
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
static void _iterDirs(Dirs &m, DirEnumCallback f, void *user)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
for(Dirs::iterator it = m.begin(); it != m.end(); ++it)
|
|
|
|
f(it->second.content(), user);
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
void DirBase::forEachDir(DirEnumCallback f, void *user /* = NULL */, bool safe /* = false */)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
if(safe)
|
|
|
|
{
|
|
|
|
Dirs cp = _subdirs;
|
|
|
|
_iterDirs(cp, f, user);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_iterDirs(_subdirs, f, user);
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase *DirBase::getDirByName(const char *dn, bool /* unused: lazyLoad = true */, bool useSubtrees /* = true */)
|
|
|
|
{
|
2014-04-15 13:04:33 +00:00
|
|
|
if(!dn[0] || (dn[0] == '.' && !dn[1]))
|
|
|
|
return this;
|
|
|
|
|
2014-04-07 02:16:15 +00:00
|
|
|
Dirs::iterator it = _subdirs.find(dn);
|
2014-04-06 17:19:33 +00:00
|
|
|
return it != _subdirs.end() ? it->second : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirBase::clearGarbage()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
for(Dirs::iterator it = _subdirs.begin(); it != _subdirs.end(); ++it)
|
|
|
|
it->second->clearGarbage();
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
Dir::Dir(const char *fullpath, VFSLoader *ldr)
|
|
|
|
: DirBase(fullpath), _loader(ldr)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Dir::~Dir()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
File *Dir::getFileByName(const char *fn, bool lazyLoad /* = true */)
|
|
|
|
{
|
|
|
|
Files::iterator it = _files.find(fn);
|
|
|
|
if(it != _files.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
if(!lazyLoad || !_loader)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Lazy-load file if it's not in the tree yet
|
|
|
|
std::string fn2 = joinPath(fullname(), GetBaseNameFromPath(fn));
|
|
|
|
File *f = _loader->Load(fn2.c_str(), fn2.c_str());
|
|
|
|
if(f)
|
|
|
|
_files[f->name()] = f;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _iterFiles(Files &m, FileEnumCallback f, void *user)
|
|
|
|
{
|
|
|
|
for(Files::iterator it = m.begin(); it != m.end(); ++it)
|
|
|
|
f(it->second.content(), user);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dir::forEachFile(FileEnumCallback f, void *user /* = NULL */, bool safe /* = false */)
|
|
|
|
{
|
|
|
|
load();
|
2012-06-01 15:23:19 +00:00
|
|
|
if(safe)
|
|
|
|
{
|
|
|
|
Files cp = _files;
|
|
|
|
_iterFiles(cp, f, user);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_iterFiles(_files, f, user);
|
|
|
|
}
|
|
|
|
|
2014-04-15 13:04:33 +00:00
|
|
|
void Dir::forEachDir(DirEnumCallback f, void *user /* = NULL */, bool safe /* = false */)
|
|
|
|
{
|
|
|
|
load();
|
|
|
|
DirBase::forEachDir(f, user, safe);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
bool Dir::add(File *f)
|
|
|
|
{
|
2017-01-12 22:52:59 +00:00
|
|
|
return _addRecursiveSkip(f, 0);
|
|
|
|
}
|
2014-04-06 17:19:33 +00:00
|
|
|
|
2017-01-12 22:52:59 +00:00
|
|
|
bool Dir::_addSingle(File *f)
|
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
Files::iterator it = _files.find(f->name());
|
|
|
|
|
|
|
|
if(it != _files.end())
|
|
|
|
{
|
|
|
|
File *oldf = it->second;
|
|
|
|
if(oldf == f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_files.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
_files[f->name()] = f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-12 22:52:59 +00:00
|
|
|
bool Dir::_addRecursiveSkip(File *f, size_t skip /* = 0 */)
|
2014-04-06 17:19:33 +00:00
|
|
|
{
|
|
|
|
Dir *vdir = this;
|
|
|
|
if(f->fullnameLen() - f->nameLen() > skip)
|
|
|
|
{
|
|
|
|
// figure out directory from full file name
|
|
|
|
size_t prefixLen = f->fullnameLen() - f->nameLen() - skip;
|
|
|
|
|
|
|
|
// prefixLen == 0 is invalid, prefixLen == 1 is just a single '/', which will be stripped away below.
|
|
|
|
// in both cases, just use 'this'.
|
2014-04-15 13:04:33 +00:00
|
|
|
// FIXME: will this be a problem for absolute paths?
|
2014-04-06 17:19:33 +00:00
|
|
|
if(prefixLen > 1)
|
|
|
|
{
|
|
|
|
char *dirname = (char*)VFS_STACK_ALLOC(prefixLen);
|
|
|
|
--prefixLen; // -1 to strip the trailing '/'. That's the position where to put the terminating null byte.
|
|
|
|
memcpy(dirname, f->fullname() + skip, prefixLen); // copy trailing null byte
|
|
|
|
dirname[prefixLen] = 0;
|
2014-04-15 13:04:33 +00:00
|
|
|
vdir = safecastNonNull<Dir*>(_getDirEx(dirname, dirname, true, true).first);
|
2014-04-06 17:19:33 +00:00
|
|
|
VFS_STACK_FREE(dirname);
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2017-01-12 22:52:59 +00:00
|
|
|
return vdir->_addSingle(f);
|
2014-04-06 17:19:33 +00:00
|
|
|
}
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
void Dir::clearGarbage()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase::clearGarbage();
|
|
|
|
for(Files::iterator it = _files.begin(); it != _files.end(); ++it)
|
|
|
|
it->second->clearGarbage();
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
bool Dir::_addToView(char *path, DirView& view)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
if(Dir *dir = safecast<Dir*>(getDir(path)))
|
|
|
|
{
|
|
|
|
view.add(dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase *Dir::getDirByName(const char *dn, bool lazyLoad /* = true */, bool useSubtrees /* = true */)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase *sub;
|
|
|
|
if((sub = DirBase::getDirByName(dn, lazyLoad, useSubtrees)))
|
|
|
|
return sub;
|
|
|
|
|
|
|
|
if(!lazyLoad || !_loader)
|
|
|
|
return NULL;
|
|
|
|
|
2014-04-15 13:04:33 +00:00
|
|
|
// Fix for absolute paths: No dir should have '/' (or any other absolute dirs) as subdir.
|
|
|
|
if(fullnameLen() && dn[0] == '/')
|
|
|
|
return NULL;
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
// Lazy-load file if it's not in the tree yet
|
|
|
|
// TODO: get rid of alloc
|
|
|
|
std::string fn2 = joinPath(fullname(), dn);
|
|
|
|
sub = _loader->LoadDir(fn2.c_str(), fn2.c_str());
|
|
|
|
if(sub)
|
2014-04-15 13:04:33 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
_subdirs[sub->name()] = sub;
|
2014-04-15 13:04:33 +00:00
|
|
|
//printf("Lazy loaded: [%s]\n", sub->fullname());
|
|
|
|
}
|
2014-04-06 17:19:33 +00:00
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
2014-04-07 00:25:58 +00:00
|
|
|
File *Dir::getFileFromSubdir(const char *subdir, const char *file)
|
|
|
|
{
|
|
|
|
Dir *d = safecast<Dir*>(getDirByName(subdir, true, false)); // useSubtrees is irrelevant here
|
|
|
|
return d ? d->getFile(file) : NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
// ----- DiskDir start here -----
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
|
|
|
|
DiskDir::DiskDir(const char *path, VFSLoader *ldr) : Dir(path, ldr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskDir *DiskDir::createNew(const char *dir) const
|
|
|
|
{
|
|
|
|
return new DiskDir(dir, getLoader());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiskDir::load()
|
|
|
|
{
|
|
|
|
_files.clear();
|
|
|
|
_subdirs.clear();
|
|
|
|
// TODO: cache existing files and keep them unless they do no longer exist
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
StringList li;
|
|
|
|
GetFileList(fullname(), li);
|
|
|
|
for(StringList::iterator it = li.begin(); it != li.end(); ++it)
|
|
|
|
{
|
2014-04-07 00:25:58 +00:00
|
|
|
DiskFile *f = new DiskFile(joinPath(fullname(), it->c_str()).c_str());
|
2012-06-01 15:23:19 +00:00
|
|
|
_files[f->name()] = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.clear();
|
2013-06-24 17:54:25 +00:00
|
|
|
GetDirList(fullname(), li, 0);
|
2014-04-06 17:19:33 +00:00
|
|
|
for(StringList::iterator it = li.begin(); it != li.end(); ++it)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-07 00:25:58 +00:00
|
|
|
// GetDirList() returns relative paths, so need to join
|
|
|
|
Dir *d = createNew(joinPath(fullname(), it->c_str()).c_str());
|
2012-06-01 15:23:19 +00:00
|
|
|
_subdirs[d->name()] = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 22:52:59 +00:00
|
|
|
|
|
|
|
// ----- MemDir start here -----
|
|
|
|
|
|
|
|
MemDir *MemDir::createNew(const char *dir) const
|
|
|
|
{
|
|
|
|
return new MemDir(dir);
|
|
|
|
}
|
|
|
|
|
2012-06-01 15:23:19 +00:00
|
|
|
VFS_NAMESPACE_END
|