2007-11-20 10:51:07 -08:00
|
|
|
/**
|
|
|
|
|
* =========================================================================
|
|
|
|
|
* File : vfs.cpp
|
|
|
|
|
* Project : 0 A.D.
|
|
|
|
|
* Description :
|
|
|
|
|
* =========================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// license: GPL; see lib/license.txt
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
#include "vfs.h"
|
|
|
|
|
|
2008-05-01 08:41:42 -07:00
|
|
|
#include "lib/allocators/shared_ptr.h"
|
2007-11-20 10:51:07 -08:00
|
|
|
#include "lib/path_util.h"
|
2007-12-20 12:14:21 -08:00
|
|
|
#include "lib/file/common/file_stats.h"
|
|
|
|
|
#include "lib/file/common/trace.h"
|
2007-12-01 10:32:43 -08:00
|
|
|
#include "lib/file/archive/archive.h"
|
2007-12-20 12:14:21 -08:00
|
|
|
#include "lib/file/io/io.h"
|
2007-11-20 10:51:07 -08:00
|
|
|
#include "vfs_tree.h"
|
2007-12-01 10:32:43 -08:00
|
|
|
#include "vfs_lookup.h"
|
|
|
|
|
#include "file_cache.h"
|
2007-11-20 10:51:07 -08:00
|
|
|
|
|
|
|
|
|
2007-12-20 12:14:21 -08:00
|
|
|
class VFS : public IVFS
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
|
|
|
|
public:
|
2008-05-13 12:43:02 -07:00
|
|
|
VFS(size_t cacheSize)
|
|
|
|
|
: m_cacheSize(cacheSize), m_fileCache(m_cacheSize)
|
2007-12-20 12:14:21 -08:00
|
|
|
, m_trace(CreateTrace(4*MiB))
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
virtual LibError Mount(const VfsPath& mountPoint, const Path& path, int flags /* = 0 */, size_t priority /* = 0 */)
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
2007-12-22 10:15:52 -08:00
|
|
|
debug_assert(vfs_path_IsDirectory(mountPoint));
|
2008-01-07 12:03:19 -08:00
|
|
|
// note: mounting subdirectories is now allowed.
|
2007-12-20 12:14:21 -08:00
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
VfsDirectory* directory;
|
|
|
|
|
CHECK_ERR(vfs_Lookup(mountPoint, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_CREATE));
|
2008-01-07 12:03:19 -08:00
|
|
|
PRealDirectory realDirectory(new RealDirectory(path, priority, flags));
|
2007-12-22 10:15:52 -08:00
|
|
|
directory->Attach(realDirectory);
|
2007-12-01 10:32:43 -08:00
|
|
|
return INFO::OK;
|
2007-11-20 10:51:07 -08:00
|
|
|
}
|
|
|
|
|
|
2007-12-20 12:14:21 -08:00
|
|
|
virtual LibError GetFileInfo(const VfsPath& pathname, FileInfo* pfileInfo) const
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
2007-12-22 10:15:52 -08:00
|
|
|
VfsDirectory* directory; VfsFile* file;
|
|
|
|
|
LibError ret = vfs_Lookup(pathname, &m_rootDirectory, directory, &file);
|
2007-12-20 12:14:21 -08:00
|
|
|
if(!pfileInfo) // just indicate if the file exists without raising warnings.
|
|
|
|
|
return ret;
|
|
|
|
|
CHECK_ERR(ret);
|
2007-12-22 10:15:52 -08:00
|
|
|
*pfileInfo = FileInfo(file->Name(), file->Size(), file->MTime());
|
2007-11-20 10:51:07 -08:00
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-20 12:14:21 -08:00
|
|
|
virtual LibError GetDirectoryEntries(const VfsPath& path, FileInfos* files, DirectoryNames* subdirectoryNames) const
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
2007-12-22 10:15:52 -08:00
|
|
|
debug_assert(vfs_path_IsDirectory(path));
|
|
|
|
|
VfsDirectory* directory;
|
|
|
|
|
CHECK_ERR(vfs_Lookup(path, &m_rootDirectory, directory, 0));
|
|
|
|
|
directory->GetEntries(files, subdirectoryNames);
|
2007-11-20 10:51:07 -08:00
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// note: only allowing either reads or writes simplifies file cache
|
|
|
|
|
// coherency (need only invalidate when closing a FILE_WRITE file).
|
2008-07-17 07:23:51 -07:00
|
|
|
virtual LibError CreateFile(const VfsPath& pathname, const shared_ptr<u8>& fileContents, size_t size)
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
2007-12-22 10:15:52 -08:00
|
|
|
VfsDirectory* directory;
|
|
|
|
|
CHECK_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_CREATE));
|
2007-12-20 12:14:21 -08:00
|
|
|
|
2008-07-17 07:23:51 -07:00
|
|
|
const PRealDirectory& realDirectory = directory->AssociatedDirectory();
|
2007-12-20 12:14:21 -08:00
|
|
|
const std::string& name = pathname.leaf();
|
|
|
|
|
RETURN_ERR(realDirectory->Store(name, fileContents, size));
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
const VfsFile file(name, (off_t)size, time(0), realDirectory->Priority(), realDirectory);
|
|
|
|
|
directory->AddFile(file);
|
2007-12-20 12:14:21 -08:00
|
|
|
|
2007-11-20 10:51:07 -08:00
|
|
|
// wipe out any cached blocks. this is necessary to cover the (rare) case
|
|
|
|
|
// of file cache contents predating the file write.
|
2007-12-20 12:14:21 -08:00
|
|
|
m_fileCache.Remove(pathname);
|
2007-12-01 10:32:43 -08:00
|
|
|
|
2007-12-20 12:14:21 -08:00
|
|
|
m_trace->NotifyStore(pathname.string().c_str(), size);
|
2007-12-01 10:32:43 -08:00
|
|
|
return INFO::OK;
|
2007-11-20 10:51:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// read the entire file.
|
|
|
|
|
// return number of bytes transferred (see above), or a negative error code.
|
|
|
|
|
//
|
|
|
|
|
// if non-NULL, <cb> is called for each block transferred, passing <cbData>.
|
|
|
|
|
// it returns how much data was actually transferred, or a negative error
|
|
|
|
|
// code (in which case we abort the transfer and return that value).
|
|
|
|
|
// the callback mechanism is useful for user progress notification or
|
|
|
|
|
// processing data while waiting for the next I/O to complete
|
|
|
|
|
// (quasi-parallel, without the complexity of threads).
|
2007-12-20 12:14:21 -08:00
|
|
|
virtual LibError LoadFile(const VfsPath& pathname, shared_ptr<u8>& fileContents, size_t& size)
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
2007-12-20 12:14:21 -08:00
|
|
|
const bool isCacheHit = m_fileCache.Retrieve(pathname, fileContents, size);
|
2007-11-20 10:51:07 -08:00
|
|
|
if(!isCacheHit)
|
|
|
|
|
{
|
2007-12-22 10:15:52 -08:00
|
|
|
VfsDirectory* directory; VfsFile* file;
|
|
|
|
|
CHECK_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file));
|
2007-12-20 12:14:21 -08:00
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
size = file->Size();
|
2008-05-01 08:41:42 -07:00
|
|
|
// safely handle zero-length files
|
|
|
|
|
if(!size)
|
|
|
|
|
fileContents = DummySharedPtr((u8*)0);
|
2008-05-13 12:43:02 -07:00
|
|
|
else if(size > m_cacheSize)
|
2007-12-20 12:14:21 -08:00
|
|
|
{
|
|
|
|
|
fileContents = io_Allocate(size);
|
2007-12-22 10:15:52 -08:00
|
|
|
RETURN_ERR(file->Load(fileContents));
|
2007-12-20 12:14:21 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fileContents = m_fileCache.Reserve(size);
|
2007-12-22 10:15:52 -08:00
|
|
|
RETURN_ERR(file->Load(fileContents));
|
2007-12-20 12:14:21 -08:00
|
|
|
m_fileCache.Add(pathname, fileContents, size);
|
|
|
|
|
}
|
2007-11-20 10:51:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats_io_user_request(size);
|
2007-12-20 12:14:21 -08:00
|
|
|
stats_cache(isCacheHit? CR_HIT : CR_MISS, size);
|
|
|
|
|
m_trace->NotifyLoad(pathname.string().c_str(), size);
|
2007-11-20 10:51:07 -08:00
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-01 10:32:43 -08:00
|
|
|
// rebuild the VFS, i.e. re-mount everything. open files are not affected.
|
|
|
|
|
// necessary after loose files or directories change, so that the VFS
|
|
|
|
|
// "notices" the changes and updates file locations. res calls this after
|
|
|
|
|
// dir_watch reports changes; can also be called from the console after a
|
|
|
|
|
// rebuild command. there is no provision for updating single VFS dirs -
|
|
|
|
|
// it's not worth the trouble.
|
2007-12-20 12:14:21 -08:00
|
|
|
virtual void Clear()
|
2007-12-01 10:32:43 -08:00
|
|
|
{
|
|
|
|
|
m_rootDirectory.ClearR();
|
2007-11-20 10:51:07 -08:00
|
|
|
}
|
|
|
|
|
|
2007-12-20 12:14:21 -08:00
|
|
|
virtual void Display() const
|
2007-11-20 10:51:07 -08:00
|
|
|
{
|
2007-12-01 10:32:43 -08:00
|
|
|
m_rootDirectory.DisplayR(0);
|
2007-11-20 10:51:07 -08:00
|
|
|
}
|
|
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
virtual LibError GetRealPath(const VfsPath& pathname, Path& realPathname)
|
2007-12-20 12:14:21 -08:00
|
|
|
{
|
2007-12-22 10:15:52 -08:00
|
|
|
VfsDirectory* directory;
|
|
|
|
|
CHECK_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, 0));
|
2008-07-17 07:23:51 -07:00
|
|
|
const PRealDirectory& realDirectory = directory->AssociatedDirectory();
|
2008-01-07 12:03:19 -08:00
|
|
|
realPathname = realDirectory->GetPath() / pathname.leaf();
|
2007-12-20 12:14:21 -08:00
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
2007-12-01 10:32:43 -08:00
|
|
|
|
2007-11-20 10:51:07 -08:00
|
|
|
private:
|
2008-05-13 12:43:02 -07:00
|
|
|
size_t m_cacheSize;
|
2007-11-20 10:51:07 -08:00
|
|
|
FileCache m_fileCache;
|
2007-12-01 10:32:43 -08:00
|
|
|
PITrace m_trace;
|
2008-05-13 12:43:02 -07:00
|
|
|
mutable VfsDirectory m_rootDirectory;
|
2007-11-20 10:51:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2008-05-13 12:43:02 -07:00
|
|
|
PIVFS CreateVfs(size_t cacheSize)
|
2007-12-01 10:32:43 -08:00
|
|
|
{
|
2008-05-13 12:43:02 -07:00
|
|
|
return PIVFS(new VFS(cacheSize));
|
2007-12-01 10:32:43 -08:00
|
|
|
}
|