1
0
forked from 0ad/0ad
0ad/source/lib/res/vfs.cpp

1916 lines
50 KiB
C++
Executable File

// virtual file system - transparent access to files in archives;
// allows multiple mount points
//
// Copyright (c) 2004 Jan Wassenberg
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// Contact info:
// Jan.Wassenberg@stud.uni-karlsruhe.de
// http://www.stud.uni-karlsruhe.de/~urkt/
#include "precompiled.h"
#include "lib.h"
#include "res.h"
#include "zip.h"
#include "file.h"
#include "adts.h"
#include "hotload.h" // see NO_DIR_WATCH
#include "timer.h"
#include <string.h>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <algorithm>
// currently not thread safe. will have to change that if
// a prefetch thread is to be used.
// not safe to call before main!
// we add/cancel directory watches from the VFS mount code for convenience -
// it iterates through all subdirectories anyway (*) and provides storage for
// a key to identify the watch (obviates separate TDir -> watch mapping).
//
// define this to strip out that code - removes .watch from struct TDir,
// and calls to res_watch_dir / res_cancel_watch.
//
// *: the add_watch code would need to iterate through subdirs and watch
// each one, because the monitor API (e.g. FAM) may only be able to
// watch single directories, instead of a whole subdirectory tree.
//#define NO_DIR_WATCH
// pathnames are case-insensitive.
// implementation:
// when mounting, we get the exact filenames as reported by the OS;
// we allow open requests with mixed case to match those,
// but still use the correct case when passing to other libraries
// (e.g. the actual open() syscall, called via file_open).
// rationale:
// necessary, because some exporters output .EXT uppercase extensions
// and it's unreasonable to expect that users will always get it right.
// rationale for no forcibly-close support:
// issue:
// we might want to edit files while the game has them open.
// usual case: edit file, notify engine that it should be reloaded.
// here: need to tell the engine to stop what it's doing and close the file;
// only then can the artist write to the file, and trigger a reload.
//
// work involved:
// since closing a file with pending aios results in undefined
// behavior on Win32, we would have to keep track of all aios from each file,
// and cancel them. we'd also need to notify the higher level resource user
// that its read was cancelled, as opposed to failing due to read errors
// (which might cause the game to terminate).
//
// this is just more work than benefit. cases where the game holds on to files
// are rare:
// - streaming music (artist can use regular commands to stop the current
// track, or all music)
// - if the engine happens to be reading that file at the moment (expected
// to happen only during loading, and these are usually one-shot anway,
// i.e. it'll be done soon)
// - bug (someone didn't close a file - tough luck, and should be fixed
// instead of hacking around it).
// - archives (these remain open. allowing reload would mean we'd have to keep
// track of all files from an archive, and reload them all. another hassle.
// anyway, if files are to be changed in-game, then change the plain-file
// version - that's what they're for).
//////////////////////////////////////////////////////////////////////////////
//
// path
//
//////////////////////////////////////////////////////////////////////////////
// path types:
// p_*: posix (e.g. mount object name or for open())
// v_*: vfs (e.g. mount point)
// fn : filename only (e.g. from readdir)
// dir_name: directory only, no path (e.g. subdir name)
//
// all paths must be relative (no leading '/'); components are separated
// by '/'; no ':', '\\', "." or ".." allowed; root dir is "".
//
// grammar:
// path ::= dir*file?
// dir ::= name/
// file ::= name
// name ::= [^/]
// if path is invalid (see source for criteria), print a diagnostic message
// (indicating line number of the call that failed) and
// return a negative error code. used by CHECK_PATH.
static int path_validate(const uint line, const char* path)
{
size_t path_len = 0; // counted as we go; checked against max.
const char* msg = 0; // error occurred <==> != 0
int err = -1; // what we pass to caller
int c = 0, last_c; // used for ./ detection
// disallow "/", because it would create a second 'root' (with name = "").
// root dir is "".
if(path[0] == '/')
{
msg = "starts with '/'";
goto fail;
}
// scan each char in path string; count length.
for(;;)
{
last_c = c;
c = path[path_len++];
// whole path is too long
if(path_len >= VFS_MAX_PATH)
{
msg = "path too long";
goto fail;
}
// disallow:
// - ".." (prevent going above the VFS root dir)
// - "./" (security hole when mounting and not supported on Windows).
// allow "/.", because CVS backup files include it.
if(last_c == '.' && (c == '.' || c == '/'))
{
msg = "contains '..' or './'";
goto fail;
}
// disallow OS-specific dir separators
if(c == '\\' || c == ':')
{
msg = "contains OS-specific dir separator (e.g. '\\', ':')";
goto fail;
}
// end of string, all is well.
if(c == '\0')
goto ok;
}
// failed somewhere - err is the error code,
// or -1 if not set specifically above.
fail:
debug_out("path_validate at line %d failed: %s (error code %d)\n", line, msg, err);
debug_warn("path_validate failed");
return err;
ok:
return 0;
}
#define CHECK_PATH(path) CHECK_ERR(path_validate(__LINE__, path))
// convenience function
static inline void path_copy(char* dst, const char* src)
{
strncpy(dst, src, VFS_MAX_PATH);
}
// combine <path1> and <path2> into one path, and write to <dst>.
// if necessary, a directory separator is added between the paths.
// each may be empty, filenames, or full paths.
// total path length (including '\0') must not exceed VFS_MAX_PATH.
static int path_append(char* dst, const char* path1, const char* path2)
{
const size_t len1 = strlen(path1);
const size_t len2 = strlen(path2);
size_t total_len = len1 + len2 + 1; // includes '\0'
// check if we need to add '/' between path1 and path2
bool need_separator = false;
const bool first_no_slash = (len1 == 0) || (path1[len1-1] != '/');
// note: the second can't start with '/' (not allowed by path_validate)
if(first_no_slash)
{
total_len++; // for '/'
need_separator = true;
}
if(total_len+1 > VFS_MAX_PATH)
return ERR_PATH_LENGTH;
strcpy(dst, path1);
dst += len1;
if(need_separator)
*dst++ = '/';
strcpy(dst, path2);
return 0;
}
// strip <remove> from the start of <src>, prepend <replace>,
// and write to <dst>.
// used when converting VFS <--> real paths.
static int path_replace(char* dst, const char* src, const char* remove, const char* replace)
{
// remove doesn't match start of <src>
const size_t remove_len = strlen(remove);
if(strncmp(src, remove, remove_len) != 0)
return -1;
// get rid of trailing / in src (must not be included in remove)
const char* start = src+remove_len;
if(*start == '/' || *start == DIR_SEP)
start++;
// prepend replace.
CHECK_ERR(path_append(dst, replace, start));
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
// file system tree storing information about each file
//
///////////////////////////////////////////////////////////////////////////////
// TLoc = location of a file in the tree.
// TFile = all information about a file stored in the tree.
// TDir = container holding TFile-s representing a dir. in the tree.
//
// the VFS stores the location (archive or directory) of each file;
// this allows multiple search paths without having to check each one
// when opening a file (slow).
//
// one TLoc is allocated for each archive or directory mounted.
// therefore, files only /point/ to a (possibly shared) TLoc.
// if a file's location changes (e.g. after mounting a higher-priority
// directory), the VFS entry will point to the new TLoc; the priority
// of both locations is unchanged.
//
// allocate via mnt_create, passing the location. do not free!
// we keep track of all Locs allocated; they are freed at exit,
// and by mnt_free_all (useful when rebuilding the VFS).
// this is much easier and safer than walking the VFS tree and
// freeing every location we find.
// case-independent string for comparing file/directory names.
// modified from GotW #29.
struct ci_char_traits : public std::char_traits<char>
// inherit all the other functions that we don't need to override.
{
static bool eq(char c1, char c2)
{ return tolower(c1) == tolower(c2); }
static bool ne(char c1, char c2)
{ return tolower(c1) != tolower(c2); }
static bool lt( char c1, char c2 )
{ return tolower(c1) < tolower(c2); }
static int compare(const char* s1, const char* s2, size_t n)
{ return stricmp(s1, s2); }
static const char* find(const char* s, int n, char a)
{
while(n-- > 0 && tolower(*s) != tolower(a))
++s;
return s;
}
};
typedef std::basic_string<char, ci_char_traits> ci_string;
// location of a file: either archive or a real directory.
// not many instances => don't worry about struct size / alignment.
struct TLoc
{
Handle archive;
// not freed in dtor, so that users don't have to avoid
// TLoc temporary objects (that would free the archive)
const std::string v_mount_point;
const std::string p_real_path;
uint pri;
TLoc(Handle _archive, const char* _v_mount_point, const char* _p_real_path, uint _pri)
: v_mount_point(_v_mount_point), p_real_path(_p_real_path)
{
archive = _archive;
pri = _pri;
}
};
// container must not invalidate iterators after insertion!
// (we keep and pass around pointers to Mount.archive_locs elements)
// see below.
typedef std::list<const TLoc> TLocs;
typedef TLocs::iterator TLocIt;
// rationale for separate file / subdir containers:
// problems:
// - duplicates find/add routines
// - makes ordered output of all dirents difficult;
// irrelevant, since dirs and files are enumerated separately.
// advantages:
// - simplifies code: no need to always check if a node is a dir;
// no messing around with union TNode
// - negligibly reduces memory use (sizeof TDir > sizeof TFile)
//
// add_file aborts if a subdir of the same name already exists,
// and vice versa.
struct TFile
{
// required:
const TLoc* loc;
// allocated and owned by caller (mount code)
// set by ctor if struct stat is available:
time_t mtime;
off_t size;
// these can also be extracted from TLoc,
// but better cache coherency when accessing them here.
u16 pri;
u32 in_archive : 1;
// c_str() pointer to the TFiles container's key for this node.
// set by TDir::find_file; used by callers needing the exact case,
// e.g. for case-sensitive syscalls; see rationale above.
const char* exact_fn;
TFile(const TLoc* _loc = 0, const struct stat* s = 0)
{
loc = _loc;
if(s)
mtime = s->st_mtime, size = s->st_size;
else
mtime = 0, size = 0;
pri = _loc->pri;
in_archive = _loc->archive > 0;
exact_fn = 0; // safety
}
};
typedef std::pair<const ci_string, TFile> TFilePair;
typedef std:: map<const ci_string, TFile> TFiles;
typedef TFiles::iterator TFileIt;
struct TDir;
typedef std::pair<const ci_string, TDir> TDirPair;
typedef std:: map<const ci_string, TDir> TDirs;
typedef TDirs::iterator TDirIt;
struct TDir
{
#ifndef NO_DIR_WATCH
intptr_t watch;
#endif
TDirs subdirs;
TFiles files;
// if exactly one real directory is mounted into this virtual dir,
// this points to its location. used to add files to VFS when writing.
//
// the TLoc is actually in the mount info and is invalid when
// that's unmounted, but the VFS would then be rebuilt anyway.
//
// = 0 if no real dir mounted here; = -1 if more than one.
const TLoc* loc;
// c_str() pointer to the TDirs container's key for this node.
// set by TDir::find_dir; used by callers needing the exact case,
// e.g. for case-sensitive syscalls; see rationale above.
const char* exact_dir_name;
TDir()
{
#ifndef NO_DIR_WATCH
watch = 0; // not yet initialized
#endif
loc = 0;
exact_dir_name = 0; // safety
}
int add_file(const char* fn, const TLoc* loc, const struct stat* s);
TFile* find_file(const char* fn);
int add_subdir(const char* d_name);
TDir* find_subdir(const char* d_name);
void clearR();
void displayR(int indent_level = 0);
int addR(const char* p_path, const TLoc* dir_loc, TLocs* archive_locs = 0);
};
int TDir::add_subdir(const char* dir_name)
{
if(find_file(dir_name))
{
debug_warn("TDir::add_subdir: file of same name already exists");
return -1;
}
TDirPair pair = std::make_pair(dir_name, TDir());
std::pair<TDirIt, bool> ret = subdirs.insert(pair);
return ret.second? 0 : 1;
}
TDir* TDir::find_subdir(const char* dir_name)
{
TDirIt it = subdirs.find(dir_name);
if(it == subdirs.end())
return 0;
TDir* dir= &it->second;
dir->exact_dir_name = it->first.c_str(); // see decl
return dir;
}
int TDir::add_file(const char* fn, const TLoc* loc, const struct stat* s)
{
if(find_subdir(fn))
{
debug_warn("TDir::add_file: subdir of same name already exists");
return -1;
}
TFilePair pair = std::make_pair(fn, TFile(loc, s));
std::pair<TFileIt, bool> ret = files.insert(pair);
return ret.second? 0 : 1;
}
TFile* TDir::find_file(const char* fn)
{
TFileIt it = files.find(fn);
if(it == files.end())
return 0;
TFile* file = &it->second;
file->exact_fn = it->first.c_str(); // see decl
return file;
}
// empty this directory and all subdirectories; used when rebuilding VFS.
void TDir::clearR()
{
// recurse for all subdirs
// (preorder traversal - need to do this before clearing the list)
TDirIt it;
for(it = subdirs.begin(); it != subdirs.end(); ++it)
{
TDir& subdir = it->second;
subdir.clearR();
}
// wipe out this directory
subdirs.clear();
files.clear();
#ifndef NO_DIR_WATCH
res_cancel_watch(watch);
watch = 0;
#endif
}
void TDir::displayR(int indent_level)
{
const char indent[] = " ";
// list all files in this dir
for(TFileIt file_it = files.begin(); file_it != files.end(); ++file_it)
{
const char* name = file_it->first.c_str();
TFile* file = &file_it->second;
char is_archive = file->in_archive? 'A' : 'L';
char* timestamp = ctime(&file->mtime);
timestamp[24] = '\0'; // remove '\n'
const off_t size = file->size;
for(int i = 0; i < indent_level; i++)
printf(indent);
char fmt[25];
int chars = 80 - indent_level*(sizeof(indent)-1);
sprintf(fmt, "%%-%d.%ds (%%c; %%6d; %%s)\n", chars, chars);
// build format string: tell it how long the filename may be,
// so that it takes up all space before file info column.
printf(fmt, name, is_archive, size, timestamp);
}
// recurse over all subdirs
for(TDirIt dir_it = subdirs.begin(); dir_it != subdirs.end(); ++dir_it)
{
TDir* subdir = &dir_it->second;
const char* subdir_name = dir_it->first.c_str();
// write subdir's name
// note: do it now, instead of in recursive call so that:
// - we don't have to pass dir_name parameter;
// - the VFS root node isn't displayed.
for(int i = 0; i < indent_level; i++)
printf(indent);
printf("[%s/]\n", subdir_name);
subdir->displayR(indent_level+1);
}
}
///////////////////////////////////////////////////////////////////////////////
static TDir vfs_root;
static inline void tree_clear()
{
vfs_root.clearR();
}
// write a representation of the VFS tree to stdout.
static inline void tree_display()
{
vfs_root.displayR();
}
enum TreeLookupFlags
{
LF_CREATE_MISSING = 1
};
// starting at VFS root, traverse <path> and pass back information
// for its last directory component.
//
// if <flags> & LF_CREATE_MISSING, all missing subdirectory components are
// added to the VFS.
// if <exact_path> != 0, it receives a copy of <path> with
// the exact case of each component as returned by the OS
// (useful for calling external case-sensitive code).
//
// <path> can be to a file or dir (in which case it must end in '/',
// to make sure the last component is treated as a directory).
//
// return 0 on success, or a negative error code
// (in which case output params are undefined).
static int tree_lookup_dir(const char* path, TDir** pdir, uint flags = 0, char* exact_path = 0)
{
CHECK_PATH(path);
assert(!(flags & ~LF_CREATE_MISSING)); // no undefined bits set
// can't check if path ends in '/' here - we're called via tree_lookup.
const bool create_missing = !!(flags & LF_CREATE_MISSING);
// copy into (writeable) buffer so we can 'tokenize' path components
// by replacing '/' with '\0'. length check done by CHECK_PATH.
char v_path[VFS_MAX_PATH];
strcpy(v_path, path);
char* cur_component = v_path;
TDir* cur_dir = &vfs_root;
// successively navigate to the next subdirectory in <path>.
for(;;)
{
// "extract" cur_component string (0-terminate by replacing '/')
char* slash = (char*)strchr(cur_component, '/');
// .. it's a filename (since it doesn't end in '/') => done.
if(!slash)
break;
*slash = '\0';
// create <cur_component> subdir (no-op if it already exists)
if(create_missing)
CHECK_ERR(cur_dir->add_subdir(cur_component));
cur_dir = cur_dir->find_subdir(cur_component);
if(!cur_dir)
return ERR_PATH_NOT_FOUND;
if(exact_path)
exact_path += sprintf(exact_path, "%s/", cur_dir->exact_dir_name);
// no length check needed: length is the same as path
cur_component = slash+1;
}
// success.
*pdir = cur_dir;
return 0;
}
// pass back file information for <path> (relative to VFS root).
//
// if <flags> & LF_CREATE_MISSING, the file is added to VFS unless
// a higher-priority file of the same name already exists
// (used by VFile_reload when opening for writing).
// if <exact_path> != 0, it receives a copy of <path> with
// the exact case of each component as returned by the OS
// (useful for calling external case-sensitive code).
//
// return 0 on success, or a negative error code
// (in which case output params are undefined).
static int tree_lookup(const char* path, TFile** pfile, uint flags = 0, char* exact_path = 0)
{
// path and flags checked by tree_lookup_dir
TDir* dir;
CHECK_ERR(tree_lookup_dir(path, &dir, flags, exact_path));
const bool create_missing = !!(flags & LF_CREATE_MISSING);
// strip away path to file
const char* fn = path;
const char* slash = strrchr(path, '/');
if(slash)
fn = slash+1;
// empty filename ("" or "dir/" etc.)
if(fn[0] == '\0')
return -1;
if(create_missing)
{
// not exactly one real dir mounted here =>
// we don't have a location where to add the file.
if(!dir->loc || dir->loc == (TLoc*)-1)
return -1;
CHECK_ERR(dir->add_file(fn, dir->loc, (struct stat*)0));
// we don't know stat (e.g. size, mtime) yet -
// it's updated when file is closed.
}
TFile* file = dir->find_file(fn);
// the file (still) doesn't exist
if(!file)
return ERR_FILE_NOT_FOUND;
if(exact_path)
strcat(exact_path, file->exact_fn);
*pfile = file;
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// attempt to add <fn> to <dir>, storing its status <s> and location <loc>.
// overrides previously existing files of the same name if the new one
// is more important, determined via priority and file location.
// called by zip_cb and dirent_cb.
//
// note: if "priority" is the same, replace!
// this makes sure mods/patches etc. actually replace files.
//
// called by add_dirent_cb.
//
// [total time 27ms, with ~2000 files and up-to-date archive]
static int add_file(TDir* dir, const char* fn, const struct stat* s, const TLoc* loc)
{
int ret = dir->add_file(fn, loc, s);
CHECK_ERR(ret);
// wasn't in dir yet - it's added and we're done.
if(ret == 0)
return 0;
const TFile new_file(loc, s);
// note: cleaner but slower than accessing stat members,
// and directly assigning TFile fields when overriding.
TFile& old_file = *dir->find_file(fn);
// since add_file failed, find_file must succeed
// older is higher priority - keep.
if(old_file.pri > new_file.pri)
return 0;
// assume they're the same if size and last-modified time match.
bool is_same = (old_file.size == new_file.size) &&
fabs(difftime(old_file.mtime, new_file.mtime)) <= 2.0;
// (FAT timestamp has 2 second resolution)
// strategy: always replace, unless both are the same,
// old is archived (fast), and new is loose (slow).
if(is_same && old_file.in_archive && !new_file.in_archive)
return 0;
old_file = new_file;
return 0;
}
// passed through dirent_cb's zip_enum to zip_cb
struct ZipCBParams
{
// archive's location; assigned to all files added from here
const TLoc* const loc;
// storage for directory lookup optimization (see below).
// held across one zip_enum's zip_cb calls.
char last_path[VFS_MAX_PATH];
size_t last_path_len;
TDir* last_dir;
ZipCBParams(const TLoc* _loc)
: loc(_loc)
{
last_path[0] = '\0';
last_path_len = 0;
last_dir = 0;
}
// no copy ctor, since some members are const
private:
ZipCBParams& operator=(const ZipCBParams&);
};
// called by dirent_cb's zip_enum for each file in the archive.
// we get the full path, since that's what is stored in Zip archives.
//
// [total time 21ms, with ~2000 file's (includes add_file cost)]
static int zip_cb(const char* path, const struct stat* s, uintptr_t user)
{
ZipCBParams* params = (ZipCBParams*)user;
const TLoc* loc = params->loc;
char* last_path = params->last_path;
size_t& last_path_len = params->last_path_len;
TDir*& last_dir = params->last_dir;
// extract file name (needed for add_file)
const char* fn = path;
const char* slash = strrchr(path, '/');
if(slash)
fn = slash+1;
// else: file is in archive's root dir, and fn = path
// into which directory should the file be inserted?
// naive approach: tree_lookup_dir the path (slow!)
// optimization: store the last file's path; if it's the same,
// use the directory we looked up last time (much faster!)
TDir* dir = last_dir;
const size_t path_len = fn-path;
// .. last != current: need to do lookup
if(path_len != last_path_len ||
strnicmp(path, last_path, path_len) != 0)
{
CHECK_ERR(tree_lookup_dir(path, &dir, LF_CREATE_MISSING));
// we have to create them if missing, since we can't rely on the
// archiver placing directories before subdirs or files that
// reference them (WinZip doesn't).
path_copy(last_path, path);
last_path_len = path_len;
last_dir = dir;
}
return add_file(dir, fn, s, loc);
}
// passed through TDir::addR's file_enum to dirent_cb
struct DirentCBParams
{
// tree dir into which the dirent is to be added
TDir* const dir;
// real dir's location; assigned to all files added from this mounting
const TLoc* const loc;
// if the dirent is an archive, its TLoc is added here.
TLocs* const archive_locs;
DirentCBParams(TDir* _dir, const TLoc* _loc, TLocs* _archive_locs)
: dir(_dir), loc(_loc), archive_locs(_archive_locs) {}
// no copy ctor, since members are const
private:
DirentCBParams& operator=(const DirentCBParams&);
};
// called by TDir::addR's file_enum for each entry in a real directory.
//
// if called for a real directory, it is added to VFS.
// else if called for a loose file that is a valid archive (*),
// it is mounted (all of its files are added)
// else the file is added to VFS.
//
// * we only perform this check in the directory being mounted,
// i.e. passed in by tree_add_dir. to determine if a file is an archive,
// we have to open it and read the header, which is slow.
// can't just check extension, because it might not be .zip (e.g. Quake3 .pk3).
//
// [total time 61ms, with ~2000 files (includes zip_cb and add_file cost)]
static int dirent_cb(const char* name, const struct stat* s, uintptr_t user)
{
const DirentCBParams* params = (const DirentCBParams*)user;
TDir* dir = params->dir;
const TLoc* loc = params->loc;
TLocs* archive_locs = params->archive_locs;
// = 0 <==> this is the directory being added by tree_add_dir
// directory: add it.
if(S_ISDIR(s->st_mode))
return dir->add_subdir(name);
// caller is requesting we look for archives
// (only happens in tree_add_dir's dir, not subdirectories. see below)
if(archive_locs)
{
char path[PATH_MAX];
path_append(path, loc->p_real_path.c_str(), name);
// HACK: only works for tree_add_dir's dir
// note: don't bother checking extension -
// archives won't necessarily be called .zip (e.g. Quake III .pk3).
// we just try and open the file.
Handle archive = zip_archive_open(path);
if(archive > 0)
{
archive_locs->push_back(TLoc(archive, "", "", loc->pri));
const TLoc* archive_loc = &archive_locs->back();
ZipCBParams params(archive_loc);
return zip_enum(archive, zip_cb, (uintptr_t)&params);
// bail, so that the archive file isn't added below.
}
}
return add_file(dir, name, s, loc);
}
// add the directory <p_path>, its files, and all subdirectories
// (recursively) to the tree, marking location as <dir_loc>.
// if archive_locs != 0, all archives found in this directory
// (but not subdirs! see below) are opened and their TLoc stored there.
int TDir::addR(const char* p_path, const TLoc* dir_loc, TLocs* archive_locs)
{
// more than one real dir mounted into VFS dir
// (=> can't create files for writing here)
if(loc)
loc = (TLoc*)-1;
else
loc = dir_loc;
#ifndef NO_DIR_WATCH
res_watch_dir(p_path, &watch);
#endif
// add files and subdirs to vdir
const DirentCBParams params(this, dir_loc, archive_locs);
file_enum(p_path, dirent_cb, (uintptr_t)&params);
// recurse over all subdirs
for(TDirIt it = subdirs.begin(); it != subdirs.end(); ++it)
{
TDir* subdir = &it->second;
const char* d_subdir_name = (it->first).c_str();
// don't clutter the tree with versioning system dirs.
// only applicable for normal dirs; the archive builder
// takes care of removing these there.
if(!strcmp(d_subdir_name, "CVS") || !strcmp(d_subdir_name, ".svn"))
continue;
char p_subdir_path[PATH_MAX];
CHECK_ERR(path_append(p_subdir_path, p_path, d_subdir_name));
subdir->addR(p_subdir_path, dir_loc, (TLocs*)0);
// note: archive_locs = 0 means we won't search for archives
// in subdirectories (slow!). this is currently required by
// the dirent_cb implementation anyway; see above.
}
return 0;
}
static int tree_add_dir(TDir* dir, const char* p_path, const TLoc* dir_loc, TLocs* archive_locs)
{
return dir->addR(p_path, dir_loc, archive_locs);
}
///////////////////////////////////////////////////////////////////////////////
//
// mount directories into the VFS
//
///////////////////////////////////////////////////////////////////////////////
struct Mount
{
// note: we basically duplicate the mount information in dir_loc.
// it's no big deal - there won't be many mountings.
//
// reason is, we need this info in Mount when remounting,
// but also in TLoc when getting real file path.
// accessing everything via TDir's TLoc is ugly.
// mounting into this VFS directory;
// must end in '/' (unless if root dir, i.e. "")
const std::string v_mount_point;
// real directory being mounted
const std::string p_real_path;
uint pri;
// storage for all Locs ensuing from this mounting.
// it's safe to store pointers to them: the Mount and Locs containers
// are std::lists, and all pointers are reset after unmounting something.
TLoc dir_loc;
// referenced by TDir::dir_loc (used when creating files for writing)
TLocs archive_locs;
// contains one TLoc for every archive in this directory that
// was mounted - in alphabetical order!
//
// multiple archives per dir support is required for patches.
Mount(const char* _v_mount_point, const char* _p_real_path, uint _pri)
: v_mount_point(_v_mount_point), p_real_path(_p_real_path),
dir_loc(0, _v_mount_point, _p_real_path, _pri), archive_locs()
{
pri = _pri;
}
};
typedef std::list<Mount> Mounts;
typedef Mounts::iterator MountIt;
static Mounts mounts;
// actually mount the specified entry. split out of vfs_mount,
// because when invalidating (reloading) the VFS, we need to
// be able to mount without changing the mount list.
static int remount(Mount& m)
{
TIMER(remount);
const char* v_mount_point = m.v_mount_point.c_str();
const char* p_real_path = m.p_real_path.c_str();
const uint pri = m.pri;
TLoc* dir_loc = &m.dir_loc;
TLocs& archive_locs = m.archive_locs;
// callers have a tendency to forget required trailing '/';
// complain if it's not there, unless path = "" (root dir).
#ifndef NDEBUG
size_t len = strlen(v_mount_point);
if(len && v_mount_point[len-1] != '/')
debug_warn("remount: path doesn't end in '/'");
#endif
TDir* dir;
CHECK_ERR(tree_lookup_dir(v_mount_point, &dir, LF_CREATE_MISSING));
// add all loose files and subdirectories (recursive).
// also mounts all archives in p_real_path and adds to archive_locs.
return tree_add_dir(dir, p_real_path, dir_loc, &archive_locs);
}
// don't do this in dtor, to allow use of temporary Mount objects.
static int unmount(Mount& m)
{
for(TLocIt it = m.archive_locs.begin(); it != m.archive_locs.end(); ++it)
zip_archive_close(it->archive);
m.archive_locs.clear();
return 0;
}
// trivial, but used by vfs_shutdown and vfs_rebuild
static inline void unmount_all(void)
{ std::for_each(mounts.begin(), mounts.end(), unmount); }
static inline void remount_all()
{ std::for_each(mounts.begin(), mounts.end(), remount); }
// is s2 a subpath of s1, or vice versa?
// used by vfs_mount.
static bool is_subpath(const char* s1, const char* s2)
{
// make sure s1 is the shorter string
if(strlen(s1) > strlen(s2))
std::swap(s1, s2);
int c1 = 0, last_c1, c2;
for(;;)
{
last_c1 = c1;
c1 = *s1++, c2 = *s2++;
// end of s1 reached:
if(c1 == '\0')
{
// s1 matched s2 up until:
if(c2 == '\0' || // its end (i.e. they're equal length)
c2 == '/' || // start of next component
last_c1 == '/') // ", but both have a trailing slash
// => is subpath
return true;
}
// mismatch => is not subpath
if(c1 != c2)
return false;
}
}
// mount <p_real_path> into the VFS at <vfs_mount_point>,
// which is created if it does not yet exist.
// files in that directory override the previous VFS contents if
// <pri>(ority) is not lower.
// all archives in <p_real_path> are also mounted, in alphabetical order.
//
// p_real_path = "." or "./" isn't allowed - see implementation for rationale.
int vfs_mount(const char* v_mount_point, const char* p_real_path, const uint pri)
{
// make sure it's not already mounted, i.e. in mounts.
// also prevents mounting a parent directory of a previously mounted
// directory, or vice versa. example: mount $install/data and then
// $install/data/mods/official - mods/official would also be accessible
// from the first mount point - bad.
// no matter if it's an archive - still shouldn't be a "subpath".
for(MountIt it = mounts.begin(); it != mounts.end(); ++it)
if(is_subpath(p_real_path, it->p_real_path.c_str()))
{
debug_warn("vfs_mount: already mounted");
return -1;
}
// disallow "." because "./" isn't supported on Windows.
// it would also create a loophole for the parent dir check above.
// "./" and "/." are caught by CHECK_PATH.
if(!strcmp(p_real_path, "."))
{
debug_warn("vfs_mount: mounting . not allowed");
return -1;
}
mounts.push_back(Mount(v_mount_point, p_real_path, pri));
// actually mount the entry
Mount& m = mounts.back();
return remount(m);
}
// 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.
int vfs_rebuild()
{
tree_clear();
unmount_all();
remount_all();
return 0;
}
// unmount a previously mounted item, and rebuild the VFS afterwards.
int vfs_unmount(const char* p_real_path)
{
for(MountIt it = mounts.begin(); it != mounts.end(); ++it)
// found the corresponding entry
if(it->p_real_path == p_real_path)
{
Mount& m = *it;
unmount(m);
mounts.erase(it);
return vfs_rebuild();
}
return ERR_PATH_NOT_FOUND;
}
// if <path> or its ancestors are mounted,
// return a VFS path that accesses it.
// used when receiving paths from external code.
int vfs_make_vfs_path(const char* path, char* vfs_path)
{
for(MountIt it = mounts.begin(); it != mounts.end(); ++it)
{
const char* remove = it->p_real_path.c_str();
const char* replace = it->v_mount_point.c_str();
if(path_replace(vfs_path, path, remove, replace) == 0)
return 0;
}
return -1;
}
// given <vfs_path> and the file's location,
// return the actual filename.
// used by vfs_realpath and VFile_reopen.
static int make_file_path(char* path, const char* vfs_path, const TLoc* loc)
{
assert(loc->archive == 0);
const char* remove = loc->v_mount_point.c_str();
const char* replace = loc->p_real_path.c_str();
return path_replace(path, vfs_path, remove, replace);
}
///////////////////////////////////////////////////////////////////////////////
//
// directory
//
///////////////////////////////////////////////////////////////////////////////
struct VDir
{
// we need to cache the complete contents of the directory:
// if we reference the real directory and it changes,
// the c_str pointers may become invalid, and some files
// may be returned out of order / not at all.
// we copy the directory's subdirectory and file containers.
//
// note: allocate dynamically to make sure they fit into control block.
TDirs* subdirs;
TDirIt subdir_it;
TFiles* files;
TFileIt file_it;
};
H_TYPE_DEFINE(VDir);
static void VDir_init(VDir* vd, va_list args)
{
UNUSED(vd);
UNUSED(args);
}
static void VDir_dtor(VDir* vd)
{
delete vd->subdirs;
delete vd->files;
}
static int VDir_reload(VDir* vd, const char* path, Handle)
{
if(vd->subdirs)
{
debug_warn("VDir_reload called when already loaded - why?");
return 0;
}
// add required trailing slash if not already present,
// to make caller's life easier.
char path_slash[PATH_MAX];
CHECK_ERR(path_append(path_slash, path, ""));
TDir* dir;
CHECK_ERR(tree_lookup_dir(path_slash, &dir));
// rationale for copying the dir's data: see VDir definition
// note: bad_alloc exception handled by h_alloc.
vd->subdirs = new TDirs(dir->subdirs);
vd->subdir_it = vd->subdirs->begin();
vd->files = new TFiles(dir->files);
vd->file_it = vd->files->begin();
return 0;
}
// open a directory for reading its entries via vfs_next_dirent.
// <v_dir> need not end in '/'; we add it if not present.
// directory contents are cached here; subsequent changes to the dir
// are not returned by this handle. rationale: see VDir definition.
Handle vfs_open_dir(const char* v_dir)
{
return h_alloc(H_VDir, v_dir, RES_NO_CACHE);
// must not cache, since the position in file array
// is advanced => not copy-equivalent.
}
// close the handle to a directory.
// all vfsDirEnt.name strings are now invalid.
int vfs_close_dir(Handle& hd)
{
return h_free(hd, H_VDir);
}
// retrieve the next dir entry (in alphabetical order) matching <filter>.
// return 0 on success, ERR_DIR_END if no matching entry was found,
// or a negative error code on failure.
// filter values:
// - 0: any file;
// - "/": any subdirectory
// - anything else: pattern for name (may include '?' and '*' wildcards)
//
// rationale: the filename is currently stored internally as
// std::string (=> less manual memory allocation). we don't want to
// return a reference, because that would break C compatibility.
// we're trying to avoid fixed-size buffers, so that is out as well.
// finally, allocating a copy is not so good because it has to be
// freed by the user (won't happen). returning a volatile pointer
// to the string itself via c_str is the only remaining option.
int vfs_next_dirent(const Handle hd, vfsDirEnt* ent, const char* filter)
{
H_DEREF(hd, VDir, vd);
// caller wants a subdirectory; return the next one.
if(filter && filter[0] == '/' && filter[1] == '\0')
{
if(vd->subdir_it == vd->subdirs->end())
return ERR_DIR_END;
ent->name = vd->subdir_it->first.c_str();
++vd->subdir_it;
return 0;
}
// caller wants a file; loop until one matches or end of list.
for(;;)
{
if(vd->file_it == vd->files->end())
return ERR_DIR_END;
ent->name = vd->file_it->first.c_str();
++vd->file_it;
if(!filter || match_wildcard(ent->name, filter))
return 0;
}
}
// return actual path to the specified file:
// "<real_directory>/fn" or "<archive_name>/fn".
int vfs_realpath(const char* v_path, char* realpath)
{
TFile* file;
char v_exact_path[VFS_MAX_PATH];
CHECK_ERR(tree_lookup(v_path, &file, 0, v_exact_path));
if(file->in_archive)
{
const char* archive_fn = h_filename(file->loc->archive);
if(!archive_fn)
return -1;
CHECK_ERR(path_append(realpath, archive_fn, v_exact_path));
}
// file is in normal directory
else
CHECK_ERR(make_file_path(realpath, v_exact_path, file->loc));
return 0;
}
// does the specified file exist? return false on error.
// useful because a "file not found" warning is not raised, unlike vfs_stat.
bool vfs_exists(const char* v_fn)
{
TFile* file;
return (tree_lookup(v_fn, &file) == 0);
}
// get file status (mode, size, mtime). output param is zeroed on error.
int vfs_stat(const char* v_path, struct stat* s)
{
TFile* file;
CHECK_ERR(tree_lookup(v_path, &file));
// all stat members currently supported are stored in TFile,
// so we can return that without having to call file_stat().
s->st_mode = S_IFREG;
s->st_size = file->size;
s->st_mtime = file->mtime;
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
// file
//
///////////////////////////////////////////////////////////////////////////////
//
// logging
//
static int file_listing_enabled;
// tristate; -1 = already shut down
static FILE* file_list;
static void file_listing_shutdown()
{
if(file_listing_enabled == 1)
{
fclose(file_list);
file_listing_enabled = -1;
}
}
static void file_listing_add(const char* v_fn)
{
// we've already shut down - complain.
if(file_listing_enabled == -1)
{
debug_warn("file_listing_add: called after file_listing_shutdown atexit");
return;
}
// listing disabled.
if(file_listing_enabled == 0)
return;
if(!file_list)
{
file_list = fopen("../logs/filelist.txt", "w");
if(!file_list)
return;
}
fputs(v_fn, file_list);
fputc('\n', file_list);
}
void vfs_enable_file_listing(bool want_enabled)
{
// already shut down - don't allow enabling
if(file_listing_enabled == -1 && want_enabled)
{
debug_warn("vfs_enable_file_listing: enabling after shutdown");
return;
}
file_listing_enabled = (int)want_enabled;
}
///////////////////////////////////////////////////////////////////////////////
enum
{
// internal file state flags
// make sure these don't conflict with vfs.h flags
VF_OPEN = 0x100,
VF_ZIP = 0x200
};
struct VFile
{
// cached contents of file from vfs_load
// (can't just use pointer - may be freed behind our back)
Handle hm;
off_t ofs;
union
{
File f;
ZFile zf;
};
// be aware when adding fields that this struct is quite large,
// and may require increasing the control block size limit.
// (especially in PARANOIA builds, which add a member!)
};
H_TYPE_DEFINE(VFile);
// with #define PARANOIA, File and ZFile get an additional member,
// and VFile was exceeding HDATA_USER_SIZE. flags and size (required
// in File as well as VFile) are now moved into the union.
// use the functions below to insulate against change a bit.
static off_t& vf_size(VFile* vf)
{
assert(offsetof(struct File, size) == offsetof(struct ZFile, ucsize));
return vf->f.size;
}
static uint& vf_flags(VFile* vf)
{
assert(offsetof(struct File, flags) == offsetof(struct ZFile, flags));
return vf->f.flags;
}
static void VFile_init(VFile* vf, va_list args)
{
uint flags = va_arg(args, int);
vf_flags(vf) = flags;
}
static void VFile_dtor(VFile* vf)
{
uint& flags = vf_flags(vf);
if(flags & VF_OPEN)
{
if(flags & VF_ZIP)
zip_close(&vf->zf);
else
file_close(&vf->f);
flags &= ~(VF_OPEN);
}
mem_free_h(vf->hm);
}
static int VFile_reload(VFile* vf, const char* v_path, Handle)
{
uint& flags = vf_flags(vf);
// note: no matter if flags are assigned and the function later
// fails: the Handle will be closed anyway.
// we're done if file is already open. need to check this because
// reload order (e.g. if resource opens a file) is unspecified.
if(flags & VF_OPEN)
return 0;
file_listing_add(v_path);
TFile* file;
char v_exact_path[VFS_MAX_PATH];
uint lf = (flags & FILE_WRITE)? LF_CREATE_MISSING : 0;
int err = tree_lookup(v_path, &file, lf, v_exact_path);
if(err < 0)
{
// don't CHECK_ERR - this happens often and the dialog is annoying
debug_out("tree_lookup failed for %s\n", v_path);
return err;
}
if(file->in_archive)
{
if(flags & FILE_WRITE)
{
debug_warn("requesting write access to file in archive");
return -1;
}
CHECK_ERR(zip_open(file->loc->archive, v_exact_path, &vf->zf));
flags |= VF_ZIP;
}
// normal file
else
{
char p_path[PATH_MAX];
CHECK_ERR(make_file_path(p_path, v_exact_path, file->loc));
CHECK_ERR(file_open(p_path, flags, &vf->f));
}
// success
flags |= VF_OPEN;
return 0;
}
// return the size of an already opened file, or a negative error code.
ssize_t vfs_size(Handle hf)
{
H_DEREF(hf, VFile, vf);
return vf_size(vf);
}
// open the file for synchronous or asynchronous IO. write access is
// requested via FILE_WRITE flag, and is not possible for files in archives.
Handle vfs_open(const char* v_fn, uint file_flags /* = 0 */)
{
// keeping files open doesn't make sense in most cases (because the
// file is used to load resources, which are cached at a higher level).
uint res_flags = RES_NO_CACHE;
if(file_flags & FILE_CACHE)
res_flags = 0;
Handle h = h_alloc(H_VFile, v_fn, res_flags, file_flags);
// pass file flags to init
#ifdef PARANOIA
debug_out("vfs_open fn=%s %llx\n", v_fn, h);
#endif
return h;
}
// close the handle to a file.
int vfs_close(Handle& h)
{
#ifdef PARANOIA
debug_out("vfs_close %llx\n", h);
#endif
return h_free(h, H_VFile);
}
// transfer the next <size> bytes to/from the given file.
// (read or write access was chosen at file-open time).
//
// if non-NULL, <cb> is called for each block transferred, passing <ctx>.
// 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).
//
// p (value-return) indicates the buffer mode:
// - *p == 0: read into buffer we allocate; set *p.
// caller should mem_free it when no longer needed.
// - *p != 0: read into or write into the buffer *p.
// - p == 0: only read into temp buffers. useful if the callback
// is responsible for processing/copying the transferred blocks.
// since only temp buffers can be added to the cache,
// this is the preferred read method.
//
// return number of bytes transferred (see above), or a negative error code.
ssize_t vfs_io(const Handle hf, const size_t size, void** p, FileIOCB cb, uintptr_t ctx)
{
#ifdef PARANOIA
debug_out("vfs_io size=%d\n", size);
#endif
H_DEREF(hf, VFile, vf);
off_t ofs = vf->ofs;
vf->ofs += (off_t)size;
void* buf = 0; // assume temp buffer (p == 0)
if(p)
// user-specified buffer
if(*p)
buf = *p;
// we allocate
else
{
buf = mem_alloc(round_up(size, 4096), 4096);
if(!buf)
return ERR_NO_MEM;
*p = buf;
}
// (vfs_open makes sure it's not opened for writing if zip)
if(vf_flags(vf) & VF_ZIP)
return zip_read(&vf->zf, ofs, size, buf, cb, ctx);
// normal file:
// let file_io alloc the buffer if the caller didn't (i.e. p = 0),
// because it knows about alignment / padding requirements
return file_io(&vf->f, ofs, size, buf, cb, ctx);
}
#include "timer.h"
static double dt;
static double totaldata;
void dump()
{
debug_out("TOTAL TIME IN VFS_IO: %f\nthroughput: %f MB/s\n\n", dt, totaldata/dt/1e6);
}
static ssize_t vfs_timed_io(const Handle hf, const size_t size, void** p, FileIOCB cb = 0, uintptr_t ctx = 0)
{
ONCE(atexit(dump));
double t1=get_time();
totaldata += size;
ssize_t nread = vfs_io(hf, size, p, cb, ctx);
double t2=get_time();
if(t2-t1 < 1.0)
dt += t2-t1;
return nread;
}
// load the entire file <fn> into memory; return a handle to the memory
// and the buffer address/size. output parameters are zeroed on failure.
// in addition to the regular file cache, the entire buffer is kept in memory
// if flags & FILE_CACHE.
//
// note: we need the Handle return value for Tex.hm - the data pointer
// must be protected against being accidentally free-d in that case.
Handle vfs_load(const char* v_fn, void*& p, size_t& size, uint flags /* default 0 */)
{
#ifdef PARANOIA
debug_out("vfs_load v_fn=%s\n", v_fn);
#endif
p = 0; size = 0; // zeroed in case vfs_open or H_DEREF fails
Handle hf = vfs_open(v_fn, flags);
CHECK_ERR(hf);
// note: if we skip this and have H_DEREF report the error,
// we get "invalid handle" instead of vfs_open's error code.
H_DEREF(hf, VFile, vf);
Handle hm = 0; // return value - handle to memory or error code
size = vf_size(vf);
// already read into mem - return existing mem handle
// TODO: what if mapped?
if(vf->hm > 0)
{
p = mem_get_ptr(vf->hm, &size);
if(p)
{
assert(vf_size(vf) == (off_t)size && "vfs_load: mismatch between File and Mem size");
hm = vf->hm;
goto ret;
}
else
debug_warn("vfs_load: invalid MEM attached to vfile (0 pointer)");
// happens if someone frees the pointer. not an error!
}
// allocate memory. does expose implementation details of File
// (padding), but it greatly simplifies returning the Handle
// (if we allow File to alloc, have to make sure the Handle references
// the actual data address, not that of the padding).
{
const size_t BLOCK_SIZE = 64*KB;
p = mem_alloc(size, BLOCK_SIZE, 0, &hm);
if(!p)
goto ret;
}
{
ssize_t nread = vfs_timed_io(hf, size, &p);
// failed
if(nread < 0)
{
mem_free_h(hm);
hm = nread; // error code
}
else
{
if(flags & FILE_CACHE)
vf->hm = hm;
}
}
ret:
vfs_close(hf);
// if FILE_CACHE, it's kept open
// if we fail, make sure these are set to 0
// (they may have been assigned values above)
if(hm <= 0)
p = 0, size = 0;
if (hm == 0)
debug_out("hm == 0!!\n");
return hm;
}
// caveat: pads file to next max(4kb, sector_size) boundary
// (due to limitation of Win32 FILE_FLAG_NO_BUFFERING I/O).
// if that's a problem, specify FILE_NO_AIO when opening.
int vfs_store(const char* v_fn, void* p, const size_t size, uint flags /* default 0 */)
{
Handle hf = vfs_open(v_fn, flags|FILE_WRITE);
if(hf <= 0)
return (int)hf; // error code
H_DEREF(hf, VFile, vf);
const int ret = vfs_io(hf, size, &p);
vfs_close(hf);
return ret;
}
///////////////////////////////////////////////////////////////////////////////
//
// asynchronous I/O
//
///////////////////////////////////////////////////////////////////////////////
struct IO
{
union
{
FileIO fio;
ZipIO zio;
};
bool is_zip; // necessary if we have separate File and Zip IO structures
// default is false, since control block is 0-initialized
};
H_TYPE_DEFINE(IO);
// don't support forcibly closing files => don't need to keep track of
// all IOs pending for each file. too much work, little benefit.
static void IO_init(IO*, va_list)
{
}
static void IO_dtor(IO* io)
{
if(io->is_zip)
zip_discard_io(&io->zio);
else
file_discard_io(&io->fio);
}
// we don't support transparent read resume after file invalidation.
// if the file has changed, we'd risk returning inconsistent data.
// doesn't look possible without controlling the AIO implementation:
// when we cancel, we can't prevent the app from calling
// aio_result, which would terminate the read.
static int IO_reload(IO* io, const char*, Handle)
{
return 0;
}
// begin transferring <size> bytes, starting at <ofs>. get result
// with vfs_wait_io; when no longer needed, free via vfs_discard_io.
Handle vfs_start_io(Handle hf, size_t size, void* buf)
{
Handle hio = h_alloc(H_IO, 0);
H_DEREF(hio, IO, io);
H_DEREF(hf, VFile, vf);
off_t ofs = vf->ofs;
vf->ofs += (off_t)size;
if(vf_flags(vf) & VF_ZIP)
{
io->is_zip = true;
return zip_start_io(&vf->zf, ofs, size, buf, &io->zio);
}
return file_start_io(&vf->f, ofs, size, buf, &io->fio);
}
// indicates if the IO referenced by <io> has completed.
// return value: 0 if pending, 1 if complete, < 0 on error.
int vfs_io_complete(Handle hio)
{
H_DEREF(hio, IO, io);
if(io->is_zip)
return zip_io_complete(&io->zio);
else
return file_io_complete(&io->fio);
}
// wait until the transfer <hio> completes, and return its buffer.
// output parameters are zeroed on error.
int vfs_wait_io(Handle hio, void*& p, size_t& size)
{
H_DEREF(hio, IO, io);
if(io->is_zip)
return zip_wait_io(&io->zio, p, size);
else
return file_wait_io(&io->fio, p, size);
}
// finished with transfer <hio> - free its buffer (returned by vfs_wait_io)
int vfs_discard_io(Handle& hio)
{
return h_free(hio, H_IO);
}
///////////////////////////////////////////////////////////////////////////////
//
// memory mapping
//
///////////////////////////////////////////////////////////////////////////////
// map the entire (uncompressed!) file <hf> into memory. if currently
// already mapped, return the previous mapping (reference-counted).
// output parameters are zeroed on failure.
//
// the mapping will be removed (if still open) when its file is closed.
// however, map/unmap calls should still be paired so that the mapping
// may be removed when no longer needed.
int vfs_map(const Handle hf, const uint flags, void*& p, size_t& size)
{
UNUSED(flags);
p = 0;
size = 0;
// need to zero these here in case H_DEREF fails
H_DEREF(hf, VFile, vf);
if(vf_flags(vf) & VF_ZIP)
return zip_map(&vf->zf, p, size);
else
return file_map(&vf->f, p, size);
}
// decrement the reference count for the mapping belonging to file <f>.
// fail if there are no references; remove the mapping if the count reaches 0.
//
// the mapping will be removed (if still open) when its file is closed.
// however, map/unmap calls should still be paired so that the mapping
// may be removed when no longer needed.
int vfs_unmap(const Handle hf)
{
H_DEREF(hf, VFile, vf);
if(vf_flags(vf) & VF_ZIP)
return zip_unmap(&vf->zf);
else
return file_unmap(&vf->f);
}
// write a representation of the VFS tree to stdout.
inline void vfs_display()
{
tree_display();
}
void vfs_shutdown()
{
file_listing_shutdown();
tree_clear();
unmount_all();
}