2004-03-03 00:56:51 +01:00
|
|
|
// virtual file system - transparent access to files in archives;
|
2004-05-13 15:52:48 +02:00
|
|
|
// allows multiple mount points
|
2004-03-03 00:56:51 +01:00
|
|
|
//
|
2004-05-13 15:52:48 +02:00
|
|
|
// Copyright (c) 2004 Jan Wassenberg
|
2004-03-03 00:56:51 +01:00
|
|
|
//
|
|
|
|
// 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/
|
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
#include "lib.h"
|
2004-06-01 19:34:12 +02:00
|
|
|
#include "res.h"
|
2004-06-04 19:47:12 +02:00
|
|
|
#include "zip.h"
|
|
|
|
#include "file.h"
|
2004-05-06 19:14:30 +02:00
|
|
|
#include "adts.h"
|
2004-12-09 21:17:09 +01:00
|
|
|
#include "timer.h"
|
2005-03-27 03:46:54 +02:00
|
|
|
#include "vfs_path.h"
|
|
|
|
#include "vfs_tree.h"
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-06-04 14:41:53 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// currently not thread safe. will have to change that if
|
|
|
|
// a prefetch thread is to be used.
|
2004-03-03 00:56:51 +01:00
|
|
|
// not safe to call before main!
|
|
|
|
|
|
|
|
|
2004-06-08 17:21:42 +02:00
|
|
|
// we add/cancel directory watches from the VFS mount code for convenience -
|
2004-06-23 18:29:03 +02:00
|
|
|
// it iterates through all subdirectories anyway (*) and provides storage for
|
2004-12-07 02:22:25 +01:00
|
|
|
// a key to identify the watch (obviates separate TDir -> watch mapping).
|
2004-06-23 18:29:03 +02:00
|
|
|
//
|
2004-12-07 02:22:25 +01:00
|
|
|
// define this to strip out that code - removes .watch from struct TDir,
|
2004-06-08 17:21:42 +02:00
|
|
|
// and calls to res_watch_dir / res_cancel_watch.
|
2004-06-23 18:29:03 +02:00
|
|
|
//
|
|
|
|
// *: 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.
|
2004-09-19 17:42:32 +02:00
|
|
|
//#define NO_DIR_WATCH
|
2004-06-08 17:21:42 +02:00
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
// 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
|
2004-05-06 19:14:30 +02:00
|
|
|
// version - that's what they're for).
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
// 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).
|
|
|
|
//
|
2005-03-27 03:46:54 +02:00
|
|
|
// one TMountPoint is allocated for each archive or directory mounted.
|
|
|
|
// therefore, files only /point/ to a (possibly shared) TMountPoint.
|
2004-05-06 19:14:30 +02:00
|
|
|
// if a file's location changes (e.g. after mounting a higher-priority
|
2005-03-27 03:46:54 +02:00
|
|
|
// directory), the VFS entry will point to the new TMountPoint; the priority
|
2004-05-06 19:14:30 +02:00
|
|
|
// of both locations is unchanged.
|
|
|
|
//
|
2004-05-08 03:11:51 +02:00
|
|
|
// allocate via mnt_create, passing the location. do not free!
|
2004-05-06 19:14:30 +02:00
|
|
|
// we keep track of all Locs allocated; they are freed at exit,
|
2004-05-08 03:11:51 +02:00
|
|
|
// and by mnt_free_all (useful when rebuilding the VFS).
|
2004-05-06 19:14:30 +02:00
|
|
|
// this is much easier and safer than walking the VFS tree and
|
|
|
|
// freeing every location we find.
|
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// location of a file: either archive or a real directory.
|
2005-03-27 03:46:54 +02:00
|
|
|
// not many instances => don't worry about efficiency.
|
|
|
|
struct TMountPoint
|
2004-12-07 02:22:25 +01:00
|
|
|
{
|
|
|
|
Handle archive;
|
2005-03-27 03:46:54 +02:00
|
|
|
// not freed in dtor, so that users don't have to avoid
|
|
|
|
// TMountPoint temporary objects (that would free the archive)
|
2004-12-07 02:22:25 +01:00
|
|
|
|
|
|
|
const std::string v_mount_point;
|
2004-12-07 23:10:46 +01:00
|
|
|
const std::string p_real_path;
|
2004-12-07 02:22:25 +01:00
|
|
|
|
|
|
|
uint pri;
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
TMountPoint(Handle _archive, const char* _v_mount_point, const char* _p_real_path, uint _pri)
|
2004-12-07 23:10:46 +01:00
|
|
|
: v_mount_point(_v_mount_point), p_real_path(_p_real_path)
|
2004-12-07 02:22:25 +01:00
|
|
|
{
|
|
|
|
archive = _archive;
|
|
|
|
pri = _pri;
|
|
|
|
}
|
2005-01-23 20:49:45 +01:00
|
|
|
|
|
|
|
// no copy ctor, since some members are const
|
|
|
|
private:
|
2005-03-27 03:46:54 +02:00
|
|
|
TMountPoint& operator=(const TMountPoint&);
|
2004-12-07 02:22:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// container must not invalidate iterators after insertion!
|
2005-03-27 03:46:54 +02:00
|
|
|
// (we keep and pass around pointers to Mount.archives elements)
|
2004-12-07 02:22:25 +01:00
|
|
|
// see below.
|
2005-03-27 03:46:54 +02:00
|
|
|
typedef std::list<TMountPoint> TMountPoints;
|
|
|
|
typedef TMountPoints::iterator TMountPointIt;
|
2004-12-07 23:10:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2005-03-27 03:46:54 +02:00
|
|
|
// populate the directory being mounted with files from real subdirectories
|
|
|
|
// and archives.
|
2004-12-07 23:10:46 +01:00
|
|
|
//
|
2005-03-27 03:46:54 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
// attempt to add <fn> to <dir>, storing its status <s> and location <mount_point>.
|
2004-12-09 21:17:09 +01:00
|
|
|
// 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.
|
2004-12-07 23:10:46 +01:00
|
|
|
//
|
|
|
|
// note: if "priority" is the same, replace!
|
|
|
|
// this makes sure mods/patches etc. actually replace files.
|
|
|
|
//
|
2005-03-27 03:46:54 +02:00
|
|
|
// xxx [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 TMountPoint* mount_point)
|
2004-12-07 02:22:25 +01:00
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
TFile* file = tree_add_file(dir, fn);
|
|
|
|
if(!file)
|
|
|
|
return ERR_NO_MEM;
|
2004-12-09 21:17:09 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
const uint pri = mount_point->pri;
|
|
|
|
const bool in_archive = mount_point->archive > 0;
|
|
|
|
const off_t size = s->st_size;
|
|
|
|
const time_t mtime = s->st_mtime;
|
|
|
|
|
|
|
|
// was already added: check if we need to override
|
|
|
|
if(file->mount_point)
|
|
|
|
{
|
|
|
|
// older is higher priority - keep.
|
|
|
|
if(file->mount_point->pri > pri)
|
|
|
|
return 0;
|
2004-12-07 02:22:25 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
// assume they're the same if size and last-modified time match.
|
|
|
|
const bool is_same = (file->size == size) &&
|
|
|
|
fabs(difftime(file->mtime, mtime)) <= 2.0;
|
2004-12-07 23:10:46 +01:00
|
|
|
// (FAT timestamp has 2 second resolution)
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
// strategy: always replace unless: both are the same,
|
|
|
|
// old is archived (fast), and new is loose (slow).
|
|
|
|
if(is_same && file->mount_point->archive > 0 && !in_archive)
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-09 21:17:09 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
file->mount_point = mount_point;
|
|
|
|
file->in_archive = in_archive;
|
|
|
|
file->pri = pri;
|
|
|
|
file->mtime = mtime;
|
|
|
|
file->size = size;
|
2004-12-09 21:17:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// passed through dirent_cb's zip_enum to zip_cb
|
|
|
|
struct ZipCBParams
|
|
|
|
{
|
2005-01-10 14:42:10 +01:00
|
|
|
// tree directory into which we are adding the archive's files
|
|
|
|
TDir* const dir;
|
|
|
|
|
2004-12-09 21:17:09 +01:00
|
|
|
// archive's location; assigned to all files added from here
|
2005-03-27 03:46:54 +02:00
|
|
|
const TMountPoint* const mount_point;
|
2004-12-09 21:17:09 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
ZipCBParams(TDir* dir_, const TMountPoint* loc_)
|
|
|
|
: dir(dir_), mount_point(loc_)
|
2004-12-09 21:17:09 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
CHECK_PATH(path);
|
|
|
|
|
2004-12-10 00:12:02 +01:00
|
|
|
ZipCBParams* params = (ZipCBParams*)user;
|
2005-03-27 03:46:54 +02:00
|
|
|
TDir* dir = params->dir;
|
|
|
|
const TMountPoint* mount_point = params->mount_point;
|
|
|
|
char* last_path = params->last_path;
|
|
|
|
size_t& last_path_len = params->last_path_len;
|
|
|
|
TDir*& last_dir = params->last_dir;
|
2004-12-09 21:17:09 +01:00
|
|
|
|
|
|
|
// extract file name (needed for add_file)
|
|
|
|
const char* fn = path;
|
|
|
|
const char* slash = strrchr(path, '/');
|
|
|
|
if(slash)
|
|
|
|
fn = slash+1;
|
2005-01-10 14:42:10 +01:00
|
|
|
// else: there is no path - it's in the archive's root dir.
|
2004-12-09 21:17:09 +01:00
|
|
|
|
|
|
|
// 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!)
|
|
|
|
const size_t path_len = fn-path;
|
2005-01-10 14:42:10 +01:00
|
|
|
// .. same as last time
|
|
|
|
if(last_dir && path_len == last_path_len &&
|
|
|
|
strnicmp(path, last_path, path_len) == 0)
|
|
|
|
dir = last_dir;
|
2004-12-09 21:17:09 +01:00
|
|
|
// .. last != current: need to do lookup
|
2005-01-10 14:42:10 +01:00
|
|
|
else
|
2004-12-09 21:17:09 +01:00
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
path_copy(last_path, path);
|
|
|
|
last_path_len = path_len;
|
|
|
|
last_path[last_path_len] = '\0';
|
|
|
|
// strip filename (tree_lookup_dir requirement)
|
|
|
|
|
|
|
|
CHECK_ERR(tree_lookup_dir(last_path, &dir, LF_CREATE_MISSING|LF_START_DIR));
|
2004-12-09 21:17:09 +01:00
|
|
|
// we have to create them if missing, since we can't rely on the
|
|
|
|
// archiver placing directories before subdirs or files that
|
2005-01-10 14:42:10 +01:00
|
|
|
// reference them (WinZip doesn't always).
|
|
|
|
// we also need to start at the mount point (dir).
|
2004-12-09 21:17:09 +01:00
|
|
|
|
|
|
|
last_dir = dir;
|
|
|
|
}
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
return add_file(dir, fn, s, mount_point);
|
2004-12-07 02:22:25 +01:00
|
|
|
}
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
struct DirAndPath
|
|
|
|
{
|
|
|
|
TDir* dir;
|
|
|
|
std::string path;
|
|
|
|
DirAndPath(TDir* d, const char* p)
|
|
|
|
: dir(d), path(p) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::deque<const DirAndPath> DirQueue;
|
|
|
|
|
|
|
|
|
2004-12-09 21:17:09 +01:00
|
|
|
// 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
|
2005-03-27 03:46:54 +02:00
|
|
|
const TMountPoint* const mount_point;
|
2004-12-09 21:17:09 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
const char* const p_path;
|
2004-12-09 21:17:09 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
DirQueue* const dir_queue;
|
|
|
|
|
|
|
|
// if the dirent is an archive, its TMountPoint is added here.
|
|
|
|
TMountPoints* const archives;
|
|
|
|
|
|
|
|
DirentCBParams(TDir* d, const TMountPoint* mp, const char* p, DirQueue* dq, TMountPoints* a)
|
|
|
|
: dir(d), mount_point(mp), p_path(p), dir_queue(dq), archives(a) {}
|
2004-12-09 21:17:09 +01:00
|
|
|
|
|
|
|
// 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.
|
2004-05-06 19:14:30 +02:00
|
|
|
//
|
2004-12-07 23:10:46 +01:00
|
|
|
// 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).
|
2004-12-09 21:17:09 +01:00
|
|
|
//
|
|
|
|
// [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)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-12-09 21:17:09 +01:00
|
|
|
const DirentCBParams* params = (const DirentCBParams*)user;
|
2005-03-27 03:46:54 +02:00
|
|
|
TDir* dir = params->dir;
|
|
|
|
const TMountPoint* mount_point = params->mount_point;
|
|
|
|
const char* const p_path = params->p_path;
|
|
|
|
DirQueue* dir_queue = params->dir_queue;
|
|
|
|
TMountPoints* archives = params->archives;
|
2004-12-07 23:10:46 +01:00
|
|
|
// = 0 <==> this is the directory being added by tree_add_dir
|
2004-12-07 02:22:25 +01:00
|
|
|
|
2004-12-09 21:17:09 +01:00
|
|
|
// directory: add it.
|
2005-03-27 03:46:54 +02:00
|
|
|
if(S_ISDIR(s->st_mode) && dir_queue)
|
|
|
|
{
|
|
|
|
// 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(name, "CVS") || !strcmp(name, ".svn"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
char new_path[VFS_MAX_PATH];
|
|
|
|
CHECK_ERR(path_append(new_path, p_path, name));
|
2005-03-27 19:27:49 +02:00
|
|
|
TDir* new_dir = tree_add_dir(dir, name);
|
2005-03-27 03:46:54 +02:00
|
|
|
dir_queue->push_back(DirAndPath(new_dir, new_path));
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-07 02:22:25 +01:00
|
|
|
|
2004-12-09 21:17:09 +01:00
|
|
|
// caller is requesting we look for archives
|
|
|
|
// (only happens in tree_add_dir's dir, not subdirectories. see below)
|
2005-03-27 03:46:54 +02:00
|
|
|
if(archives)
|
2004-12-07 02:22:25 +01:00
|
|
|
{
|
2004-12-09 21:17:09 +01:00
|
|
|
char path[PATH_MAX];
|
2005-03-27 03:46:54 +02:00
|
|
|
path_append(path, mount_point->p_real_path.c_str(), name);
|
2004-12-09 21:17:09 +01:00
|
|
|
// 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)
|
2004-12-07 02:22:25 +01:00
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
archives->push_back(TMountPoint(archive, "", "", mount_point->pri));
|
|
|
|
const TMountPoint* archive_loc = &archives->back();
|
2004-12-07 23:10:46 +01:00
|
|
|
|
2005-01-10 14:42:10 +01:00
|
|
|
ZipCBParams params(dir, archive_loc);
|
2004-12-09 21:17:09 +01:00
|
|
|
return zip_enum(archive, zip_cb, (uintptr_t)¶ms);
|
|
|
|
// bail, so that the archive file isn't added below.
|
|
|
|
}
|
2004-12-07 02:22:25 +01:00
|
|
|
}
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
return add_file(dir, name, s, mount_point);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-10 19:44:41 +01:00
|
|
|
// add the contents of directory <p_path> to this TDir,
|
2005-03-27 03:46:54 +02:00
|
|
|
// marking the files' locations as <mount_point>.
|
2005-01-10 19:44:41 +01:00
|
|
|
// if desired, we recursively add the contents of subdirectories as well.
|
2005-03-27 03:46:54 +02:00
|
|
|
// if <archives> != 0, all archives found in this directory only
|
2005-01-10 19:44:41 +01:00
|
|
|
// (not its subdirs! see below) are opened in alphabetical order,
|
2005-03-27 03:46:54 +02:00
|
|
|
// their files added, and a TMountPoint appended to <*archives>.
|
|
|
|
static int populate_dir(TDir* dir_, const char* p_path_, const TMountPoint* mount_point, bool recursive, TMountPoints* archives)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
DirQueue dir_queue;
|
|
|
|
DirQueue* const pdir_queue = recursive? &dir_queue : 0;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2005-03-27 19:27:49 +02:00
|
|
|
// kickoff (less efficient than goto, but c_str reference requires
|
|
|
|
// pop to come at end of loop => this is easiest)
|
|
|
|
dir_queue.push_back(DirAndPath(dir_, p_path_));
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
do
|
|
|
|
{
|
2005-03-27 19:27:49 +02:00
|
|
|
TDir* const dir = dir_queue.front().dir;
|
2005-03-27 03:46:54 +02:00
|
|
|
const char* p_path = dir_queue.front().path.c_str();
|
2005-03-27 19:27:49 +02:00
|
|
|
|
|
|
|
tree_mount(dir, p_path, mount_point);
|
2004-06-08 17:21:42 +02:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
// add files and subdirs to this dir;
|
|
|
|
// also adds the contents of archives if archives != 0.
|
|
|
|
const DirentCBParams params(dir, mount_point, p_path, pdir_queue, archives);
|
|
|
|
file_enum(p_path, dirent_cb, (uintptr_t)¶ms);
|
2004-12-07 02:22:25 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
// xxx load all archive_loc archives here instead
|
|
|
|
|
|
|
|
archives = 0;
|
|
|
|
// prevent searching for archives in subdirectories (slow!). this
|
|
|
|
// is currently required by the dirent_cb implementation anyway.
|
|
|
|
|
|
|
|
dir_queue.pop_front();
|
|
|
|
// pop at end of loop, because we hold a c_str() reference.
|
|
|
|
}
|
|
|
|
while(!dir_queue.empty());
|
2004-12-07 02:22:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2004-08-19 14:02:15 +02:00
|
|
|
// mount directories into the VFS
|
2004-05-06 19:14:30 +02:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
struct Mount
|
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
// note: we basically duplicate the mount information in mount_point.
|
2004-07-09 21:58:43 +02:00
|
|
|
// it's no big deal - there won't be many mountings.
|
2004-12-07 23:10:46 +01:00
|
|
|
//
|
|
|
|
// reason is, we need this info in Mount when remounting,
|
2005-03-27 03:46:54 +02:00
|
|
|
// but also in TMountPoint when getting real file path.
|
|
|
|
// accessing everything via TDir's TMountPoint is ugly.
|
2004-07-09 21:58:43 +02:00
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// mounting into this VFS directory;
|
|
|
|
// must end in '/' (unless if root dir, i.e. "")
|
2004-08-11 22:24:26 +02:00
|
|
|
const std::string v_mount_point;
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// real directory being mounted
|
|
|
|
const std::string p_real_path;
|
2004-05-08 03:11:51 +02:00
|
|
|
|
2005-01-10 19:44:41 +01:00
|
|
|
// see enum VfsMountFlags
|
|
|
|
int flags;
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
uint pri;
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
// storage for all TMountPoints ensuing from this mounting.
|
2004-07-08 16:38:58 +02:00
|
|
|
// it's safe to store pointers to them: the Mount and Locs containers
|
2004-12-07 23:10:46 +01:00
|
|
|
// are std::lists, and all pointers are reset after unmounting something.
|
2005-03-27 03:46:54 +02:00
|
|
|
TMountPoint mount_point;
|
|
|
|
// referenced by TDir::mount_point (used when creating files for writing)
|
|
|
|
TMountPoints archives;
|
|
|
|
// contains one TMountPoint for every archive in this directory that
|
2004-12-07 23:10:46 +01:00
|
|
|
// was mounted - in alphabetical order!
|
2004-12-07 02:22:25 +01:00
|
|
|
//
|
2004-12-07 23:10:46 +01:00
|
|
|
// multiple archives per dir support is required for patches.
|
2004-12-07 02:22:25 +01:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2005-01-10 19:44:41 +01:00
|
|
|
Mount(const char* v_mount_point_, const char* p_real_path_, int flags_, uint pri_)
|
|
|
|
: v_mount_point(v_mount_point_), p_real_path(p_real_path_),
|
2005-03-27 03:46:54 +02:00
|
|
|
mount_point(0, v_mount_point_, p_real_path_, pri_), archives()
|
2004-12-07 23:10:46 +01:00
|
|
|
{
|
2005-01-10 19:44:41 +01:00
|
|
|
flags = flags_;
|
|
|
|
pri = pri_;
|
2004-12-07 23:10:46 +01:00
|
|
|
}
|
2005-01-10 19:44:41 +01:00
|
|
|
|
|
|
|
// no copy ctor, since some members are const
|
|
|
|
private:
|
|
|
|
Mount& operator=(const Mount&);
|
2004-05-06 19:14:30 +02:00
|
|
|
};
|
|
|
|
|
2004-07-08 16:38:58 +02:00
|
|
|
typedef std::list<Mount> Mounts;
|
2004-05-06 19:14:30 +02:00
|
|
|
typedef Mounts::iterator MountIt;
|
|
|
|
static Mounts mounts;
|
|
|
|
|
|
|
|
|
2004-08-19 14:02:15 +02:00
|
|
|
// 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.
|
2004-05-06 19:14:30 +02:00
|
|
|
static int remount(Mount& m)
|
|
|
|
{
|
2004-08-11 22:24:26 +02:00
|
|
|
const char* v_mount_point = m.v_mount_point.c_str();
|
2004-12-07 23:10:46 +01:00
|
|
|
const char* p_real_path = m.p_real_path.c_str();
|
2005-01-10 19:44:41 +01:00
|
|
|
const int flags = m.flags;
|
2004-08-11 22:24:26 +02:00
|
|
|
const uint pri = m.pri;
|
2005-03-27 19:27:49 +02:00
|
|
|
TMountPoint* mount_point = &m.mount_point;
|
|
|
|
TMountPoints& archives = m.archives;
|
2004-12-07 02:22:25 +01:00
|
|
|
|
|
|
|
// callers have a tendency to forget required trailing '/';
|
2004-12-07 23:10:46 +01:00
|
|
|
// complain if it's not there, unless path = "" (root dir).
|
2004-12-07 02:22:25 +01:00
|
|
|
#ifndef NDEBUG
|
2005-01-10 19:44:41 +01:00
|
|
|
const size_t len = strlen(v_mount_point);
|
2004-12-07 02:22:25 +01:00
|
|
|
if(len && v_mount_point[len-1] != '/')
|
|
|
|
debug_warn("remount: path doesn't end in '/'");
|
|
|
|
#endif
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-12-07 02:22:25 +01:00
|
|
|
TDir* dir;
|
|
|
|
CHECK_ERR(tree_lookup_dir(v_mount_point, &dir, LF_CREATE_MISSING));
|
2004-06-13 18:12:54 +02:00
|
|
|
|
2005-01-10 19:44:41 +01:00
|
|
|
const bool recursive = !!(flags & VFS_MOUNT_RECURSIVE);
|
2005-03-27 03:46:54 +02:00
|
|
|
TMountPoints* parchive_locs = (flags & VFS_MOUNT_ARCHIVES)? &archives : 0;
|
2005-01-10 19:44:41 +01:00
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// add all loose files and subdirectories (recursive).
|
2005-03-27 03:46:54 +02:00
|
|
|
// also mounts all archives in p_real_path and adds to archives.
|
|
|
|
return populate_dir(dir, p_real_path, mount_point, recursive, parchive_locs);
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// don't do this in dtor, to allow use of temporary Mount objects.
|
2004-05-06 19:14:30 +02:00
|
|
|
static int unmount(Mount& m)
|
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
for(TMountPointIt it = m.archives.begin(); it != m.archives.end(); ++it)
|
2004-12-07 02:22:25 +01:00
|
|
|
zip_archive_close(it->archive);
|
2005-03-27 03:46:54 +02:00
|
|
|
m.archives.clear();
|
2004-05-06 19:14:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// trivial, but used by vfs_shutdown and vfs_rebuild
|
2004-05-13 15:52:48 +02:00
|
|
|
static inline void unmount_all(void)
|
2005-03-27 19:27:49 +02:00
|
|
|
{
|
|
|
|
std::for_each(mounts.begin(), mounts.end(), unmount);
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
static inline void remount_all()
|
2005-03-27 19:27:49 +02:00
|
|
|
{
|
|
|
|
std::for_each(mounts.begin(), mounts.end(), remount);
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
|
2004-06-13 21:13:32 +02:00
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// 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.
|
|
|
|
//
|
2005-01-10 19:44:41 +01:00
|
|
|
// flags determines extra actions to perform; see VfsMountFlags.
|
|
|
|
//
|
2004-12-07 23:10:46 +01:00
|
|
|
// p_real_path = "." or "./" isn't allowed - see implementation for rationale.
|
2005-01-10 19:44:41 +01:00
|
|
|
int vfs_mount(const char* v_mount_point, const char* p_real_path, int flags, uint pri)
|
2004-05-13 15:52:48 +02:00
|
|
|
{
|
2004-06-13 18:58:22 +02:00
|
|
|
// 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.
|
2004-06-13 21:13:32 +02:00
|
|
|
// no matter if it's an archive - still shouldn't be a "subpath".
|
2004-05-13 15:52:48 +02:00
|
|
|
for(MountIt it = mounts.begin(); it != mounts.end(); ++it)
|
2005-02-27 15:33:59 +01:00
|
|
|
if(file_is_subpath(p_real_path, it->p_real_path.c_str()))
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
debug_warn("vfs_mount: already mounted");
|
2004-05-06 19:14:30 +02:00
|
|
|
return -1;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
2004-06-13 18:58:22 +02:00
|
|
|
// 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.
|
2004-12-07 23:10:46 +01:00
|
|
|
if(!strcmp(p_real_path, "."))
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
|
|
|
debug_warn("vfs_mount: mounting . not allowed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
// actually mount the entry
|
2005-03-27 19:27:49 +02:00
|
|
|
mounts.push_back(Mount(v_mount_point, p_real_path, flags, pri));
|
|
|
|
return remount(mounts.back());
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02: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
|
2004-08-27 02:33:20 +02:00
|
|
|
// dir_watch reports changes; can also be called from the console after a
|
2004-06-03 15:57:00 +02:00
|
|
|
// rebuild command. there is no provision for updating single VFS dirs -
|
|
|
|
// it's not worth the trouble.
|
2004-05-06 19:14:30 +02:00
|
|
|
int vfs_rebuild()
|
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
unmount_all();
|
2005-03-27 03:46:54 +02:00
|
|
|
tree_clear();
|
|
|
|
tree_init();
|
2004-05-08 03:11:51 +02:00
|
|
|
remount_all();
|
|
|
|
return 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// unmount a previously mounted item, and rebuild the VFS afterwards.
|
2004-12-07 23:10:46 +01:00
|
|
|
int vfs_unmount(const char* p_real_path)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-06 19:14:30 +02:00
|
|
|
for(MountIt it = mounts.begin(); it != mounts.end(); ++it)
|
|
|
|
// found the corresponding entry
|
2004-12-07 23:10:46 +01:00
|
|
|
if(it->p_real_path == p_real_path)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
Mount& m = *it;
|
|
|
|
unmount(m);
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
mounts.erase(it);
|
2004-05-08 03:11:51 +02:00
|
|
|
return vfs_rebuild();
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_PATH_NOT_FOUND;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// if <path> or its ancestors are mounted,
|
|
|
|
// return a VFS path that accesses it.
|
|
|
|
// used when receiving paths from external code.
|
2004-12-10 00:12:02 +01:00
|
|
|
int vfs_make_vfs_path(const char* path, char* vfs_path)
|
2004-06-08 17:21:42 +02:00
|
|
|
{
|
|
|
|
for(MountIt it = mounts.begin(); it != mounts.end(); ++it)
|
|
|
|
{
|
2004-12-07 23:10:46 +01:00
|
|
|
const char* remove = it->p_real_path.c_str();
|
2004-08-11 22:24:26 +02:00
|
|
|
const char* replace = it->v_mount_point.c_str();
|
2004-07-09 21:58:43 +02:00
|
|
|
|
|
|
|
if(path_replace(vfs_path, path, remove, replace) == 0)
|
2004-06-08 17:21:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-06-13 18:12:54 +02:00
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// given <vfs_path> and the file's location,
|
|
|
|
// return the actual filename.
|
|
|
|
// used by vfs_realpath and VFile_reopen.
|
2005-03-27 03:46:54 +02:00
|
|
|
static int make_file_path(char* path, const char* vfs_path, const TMountPoint* mount_point)
|
2004-07-09 21:58:43 +02:00
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
assert(mount_point->archive == 0);
|
2004-07-09 21:58:43 +02:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
const char* remove = mount_point->v_mount_point.c_str();
|
|
|
|
const char* replace = mount_point->p_real_path.c_str();
|
2004-07-09 21:58:43 +02:00
|
|
|
return path_replace(path, vfs_path, remove, replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2004-06-01 19:34:12 +02:00
|
|
|
// directory
|
2004-05-06 19:14:30 +02:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-05-29 14:00:53 +02:00
|
|
|
struct VDir
|
|
|
|
{
|
2005-03-27 19:27:49 +02:00
|
|
|
// xxx we need to cache the complete contents of the directory:
|
2004-06-03 15:57:00 +02:00
|
|
|
// 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.
|
2005-03-27 03:46:54 +02:00
|
|
|
void* latch;
|
2005-03-27 19:27:49 +02:00
|
|
|
|
|
|
|
// safety check
|
|
|
|
#ifndef NDEBUG
|
|
|
|
const char* filter;
|
|
|
|
bool filter_latched;
|
|
|
|
#endif
|
2004-05-29 14:00:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
H_TYPE_DEFINE(VDir);
|
|
|
|
|
|
|
|
static void VDir_init(VDir* vd, va_list args)
|
|
|
|
{
|
2004-06-11 04:14:18 +02:00
|
|
|
UNUSED(vd);
|
|
|
|
UNUSED(args);
|
2004-05-29 14:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void VDir_dtor(VDir* vd)
|
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
tree_close_dir(vd->latch);
|
2004-05-29 14:00:53 +02:00
|
|
|
}
|
|
|
|
|
2004-06-26 00:19:19 +02:00
|
|
|
static int VDir_reload(VDir* vd, const char* path, Handle)
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
if(vd->latch)
|
2004-06-18 15:32:33 +02:00
|
|
|
{
|
|
|
|
debug_warn("VDir_reload called when already loaded - why?");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-29 14:00:53 +02:00
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// add required trailing slash if not already present,
|
|
|
|
// to make caller's life easier.
|
2004-12-07 02:22:25 +01:00
|
|
|
char path_slash[PATH_MAX];
|
|
|
|
CHECK_ERR(path_append(path_slash, path, ""));
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
CHECK_ERR(tree_open_dir(path_slash, &vd->latch));
|
2004-05-29 14:00:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// open a directory for reading its entries via vfs_next_dirent.
|
2004-12-07 23:10:46 +01:00
|
|
|
// <v_dir> need not end in '/'; we add it if not present.
|
2004-06-03 15:57:00 +02:00
|
|
|
// directory contents are cached here; subsequent changes to the dir
|
|
|
|
// are not returned by this handle. rationale: see VDir definition.
|
2004-12-10 00:12:02 +01:00
|
|
|
Handle vfs_open_dir(const char* v_dir)
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
2004-08-18 04:06:23 +02:00
|
|
|
return h_alloc(H_VDir, v_dir, RES_NO_CACHE);
|
|
|
|
// must not cache, since the position in file array
|
|
|
|
// is advanced => not copy-equivalent.
|
2004-05-29 14:00:53 +02:00
|
|
|
}
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// close the handle to a directory.
|
2004-05-29 14:00:53 +02:00
|
|
|
int vfs_close_dir(Handle& hd)
|
|
|
|
{
|
|
|
|
return h_free(hd, H_VDir);
|
|
|
|
}
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
|
2004-11-23 22:12:18 +01:00
|
|
|
// retrieve the next dir entry (in alphabetical order) matching <filter>.
|
2004-12-01 22:37:01 +01:00
|
|
|
// return 0 on success, ERR_DIR_END if no matching entry was found,
|
2004-11-23 22:12:18 +01:00
|
|
|
// or a negative error code on failure.
|
2004-07-05 04:32:43 +02:00
|
|
|
// filter values:
|
2005-03-27 19:27:49 +02:00
|
|
|
// - 0: anything;
|
|
|
|
// - "/": any subdirectory;
|
|
|
|
// - "/|<pattern>": any subdirectory, or as below with <pattern>;
|
|
|
|
// - <pattern>: any file whose name matches; ? and * wildcards are allowed.
|
|
|
|
//
|
|
|
|
// note that the directory entries are only scanned once; after the
|
|
|
|
// end is reached (-> ERR_DIR_END returned), no further entries can
|
|
|
|
// be retrieved, even if filter changes (which shouldn't happen - see impl).
|
2004-11-24 00:51:34 +01:00
|
|
|
//
|
2005-03-27 19:27:49 +02:00
|
|
|
// rationale for returning a pointer to the name string: we're trying to
|
|
|
|
// avoid arbitrary name length limits, so fixed-size buffers are out.
|
|
|
|
// allocating a copy isn't good because it has to be freed by the user
|
|
|
|
// (won't happen). that leaves a (const!) pointer to the internal storage.
|
2004-12-10 00:12:02 +01:00
|
|
|
int vfs_next_dirent(const Handle hd, vfsDirEnt* ent, const char* filter)
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
H_DEREF(hd, VDir, vd);
|
2005-03-27 19:27:49 +02:00
|
|
|
|
|
|
|
// warn if scanning the directory twice with different filters
|
|
|
|
// (this used to work with dir/file because they were stored separately).
|
|
|
|
// it is imaginable that someone will want to change it, but until
|
|
|
|
// there's a good reason, leave this check in. note: only comparing
|
|
|
|
// pointers isn't 100% certain, but it's safe enough and easy.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if(!vd->filter_latched)
|
|
|
|
{
|
|
|
|
vd->filter = filter;
|
|
|
|
vd->filter_latched = true;
|
|
|
|
}
|
|
|
|
if(vd->filter != filter)
|
|
|
|
debug_warn("vfs_next_dirent: filter has changed for this directory. are you scanning it twice?");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return tree_next_dirent(vd->latch, filter, ent);
|
2004-05-29 14:00:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// return actual path to the specified file:
|
|
|
|
// "<real_directory>/fn" or "<archive_name>/fn".
|
2004-08-11 22:24:26 +02:00
|
|
|
int vfs_realpath(const char* v_path, char* realpath)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-12-07 02:22:25 +01:00
|
|
|
TFile* file;
|
2004-12-07 23:10:46 +01:00
|
|
|
char v_exact_path[VFS_MAX_PATH];
|
|
|
|
CHECK_ERR(tree_lookup(v_path, &file, 0, v_exact_path));
|
2004-06-03 15:57:00 +02:00
|
|
|
|
2004-12-07 02:22:25 +01:00
|
|
|
if(file->in_archive)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2005-03-27 03:46:54 +02:00
|
|
|
const char* archive_fn = h_filename(file->mount_point->archive);
|
2004-07-09 21:58:43 +02:00
|
|
|
if(!archive_fn)
|
2004-06-03 15:57:00 +02:00
|
|
|
return -1;
|
2004-12-07 23:10:46 +01:00
|
|
|
CHECK_ERR(path_append(realpath, archive_fn, v_exact_path));
|
2004-06-03 15:57:00 +02:00
|
|
|
}
|
2004-12-07 02:22:25 +01:00
|
|
|
// file is in normal directory
|
|
|
|
else
|
2005-03-27 03:46:54 +02:00
|
|
|
CHECK_ERR(make_file_path(realpath, v_exact_path, file->mount_point));
|
2004-06-03 15:57:00 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-09 04:17:37 +02:00
|
|
|
// does the specified file exist? return false on error.
|
|
|
|
// useful because a "file not found" warning is not raised, unlike vfs_stat.
|
2004-08-11 22:24:26 +02:00
|
|
|
bool vfs_exists(const char* v_fn)
|
2004-07-09 04:17:37 +02:00
|
|
|
{
|
2004-12-07 02:22:25 +01:00
|
|
|
TFile* file;
|
|
|
|
return (tree_lookup(v_fn, &file) == 0);
|
2004-07-09 04:17:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// get file status (mode, size, mtime). output param is zeroed on error.
|
2004-12-07 02:22:25 +01:00
|
|
|
int vfs_stat(const char* v_path, struct stat* s)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-12-07 02:22:25 +01:00
|
|
|
TFile* file;
|
|
|
|
CHECK_ERR(tree_lookup(v_path, &file));
|
2004-06-03 15:57:00 +02:00
|
|
|
|
2004-12-07 02:22:25 +01:00
|
|
|
// all stat members currently supported are stored in TFile,
|
2004-12-07 23:10:46 +01:00
|
|
|
// so we can return that without having to call file_stat().
|
2004-12-07 02:22:25 +01:00
|
|
|
s->st_mode = S_IFREG;
|
2004-12-07 23:10:46 +01:00
|
|
|
s->st_size = file->size;
|
|
|
|
s->st_mtime = file->mtime;
|
2004-12-07 02:22:25 +01:00
|
|
|
|
2004-07-09 21:58:43 +02:00
|
|
|
return 0;
|
2004-06-03 15:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// file
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2005-02-27 20:11:39 +01:00
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
//
|
2004-11-08 00:00:32 +01:00
|
|
|
// logging
|
2004-12-07 23:10:46 +01:00
|
|
|
//
|
2004-11-08 00:00:32 +01:00
|
|
|
|
|
|
|
static int file_listing_enabled;
|
|
|
|
// tristate; -1 = already shut down
|
|
|
|
|
|
|
|
static FILE* file_list;
|
|
|
|
|
|
|
|
|
|
|
|
static void file_listing_shutdown()
|
|
|
|
{
|
2004-12-07 23:10:46 +01:00
|
|
|
if(file_listing_enabled == 1)
|
|
|
|
{
|
|
|
|
fclose(file_list);
|
|
|
|
file_listing_enabled = -1;
|
|
|
|
}
|
2004-11-08 00:00:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
enum
|
|
|
|
{
|
2004-03-05 17:23:31 +01:00
|
|
|
// internal file state flags
|
|
|
|
// make sure these don't conflict with vfs.h flags
|
|
|
|
VF_OPEN = 0x100,
|
2004-06-11 04:14:18 +02:00
|
|
|
VF_ZIP = 0x200
|
2004-03-05 17:23:31 +01:00
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct VFile
|
|
|
|
{
|
|
|
|
// cached contents of file from vfs_load
|
|
|
|
// (can't just use pointer - may be freed behind our back)
|
|
|
|
Handle hm;
|
|
|
|
|
2004-07-09 04:17:37 +02:00
|
|
|
off_t ofs;
|
|
|
|
|
2005-03-29 08:27:35 +02:00
|
|
|
TFile* tf;
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
File f;
|
|
|
|
ZFile zf;
|
|
|
|
};
|
2004-03-05 17:23:31 +01:00
|
|
|
|
2004-12-07 02:22:25 +01:00
|
|
|
// be aware when adding fields that this struct is quite large,
|
|
|
|
// and may require increasing the control block size limit.
|
2004-05-06 19:14:30 +02:00
|
|
|
// (especially in PARANOIA builds, which add a member!)
|
2004-03-03 00:56:51 +01:00
|
|
|
};
|
|
|
|
|
2004-06-11 04:14:18 +02:00
|
|
|
H_TYPE_DEFINE(VFile);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2004-03-05 17:23:31 +01:00
|
|
|
// 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.
|
|
|
|
|
2004-06-02 22:41:05 +02:00
|
|
|
static off_t& vf_size(VFile* vf)
|
2004-03-05 17:23:31 +01:00
|
|
|
{
|
|
|
|
assert(offsetof(struct File, size) == offsetof(struct ZFile, ucsize));
|
|
|
|
return vf->f.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 02:17:24 +02:00
|
|
|
static uint& vf_flags(VFile* vf)
|
2004-03-05 17:23:31 +01:00
|
|
|
{
|
|
|
|
assert(offsetof(struct File, flags) == offsetof(struct ZFile, flags));
|
|
|
|
return vf->f.flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
static void VFile_init(VFile* vf, va_list args)
|
|
|
|
{
|
2004-06-03 02:17:24 +02:00
|
|
|
uint flags = va_arg(args, int);
|
2004-03-05 17:23:31 +01:00
|
|
|
vf_flags(vf) = flags;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void VFile_dtor(VFile* vf)
|
|
|
|
{
|
2004-06-03 02:17:24 +02:00
|
|
|
uint& flags = vf_flags(vf);
|
2004-03-05 17:23:31 +01:00
|
|
|
|
|
|
|
if(flags & VF_OPEN)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-03-05 17:23:31 +01:00
|
|
|
if(flags & VF_ZIP)
|
2004-03-03 00:56:51 +01:00
|
|
|
zip_close(&vf->zf);
|
|
|
|
else
|
2005-03-29 08:27:35 +02:00
|
|
|
{
|
2004-03-03 00:56:51 +01:00
|
|
|
file_close(&vf->f);
|
|
|
|
|
2005-03-29 08:27:35 +02:00
|
|
|
// update file state in VFS tree
|
|
|
|
// (must be done after close, since that calculates the size)
|
|
|
|
if(flags & FILE_WRITE)
|
|
|
|
{
|
|
|
|
vf->tf->mtime = time(0);
|
|
|
|
vf->tf->size = vf->f.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-05 17:23:31 +01:00
|
|
|
flags &= ~(VF_OPEN);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mem_free_h(vf->hm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
static int VFile_reload(VFile* vf, const char* v_path, Handle)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-03 02:17:24 +02:00
|
|
|
uint& flags = vf_flags(vf);
|
2004-12-07 02:22:25 +01:00
|
|
|
// note: no matter if flags are assigned and the function later
|
|
|
|
// fails: the Handle will be closed anyway.
|
2004-03-05 17:23:31 +01:00
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
// we're done if file is already open. need to check this because
|
|
|
|
// reload order (e.g. if resource opens a file) is unspecified.
|
2004-03-05 17:23:31 +01:00
|
|
|
if(flags & VF_OPEN)
|
2004-03-03 00:56:51 +01:00
|
|
|
return 0;
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
file_listing_add(v_path);
|
2004-11-08 00:00:32 +01:00
|
|
|
|
2004-12-07 02:22:25 +01:00
|
|
|
TFile* file;
|
2004-12-07 23:10:46 +01:00
|
|
|
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);
|
2004-08-11 22:24:26 +02:00
|
|
|
if(err < 0)
|
|
|
|
{
|
|
|
|
// don't CHECK_ERR - this happens often and the dialog is annoying
|
2005-03-27 03:46:54 +02:00
|
|
|
debug_out("lookup failed for %s\n", v_path);
|
2004-08-11 22:24:26 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2004-12-07 02:22:25 +01:00
|
|
|
if(file->in_archive)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-07-05 04:32:43 +02:00
|
|
|
if(flags & FILE_WRITE)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
debug_warn("requesting write access to file in archive");
|
2004-05-06 19:14:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
CHECK_ERR(zip_open(file->mount_point->archive, v_exact_path, &vf->zf));
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
flags |= VF_ZIP;
|
|
|
|
}
|
2004-12-07 02:22:25 +01:00
|
|
|
// normal file
|
|
|
|
else
|
|
|
|
{
|
2004-12-07 23:10:46 +01:00
|
|
|
char p_path[PATH_MAX];
|
2005-03-27 03:46:54 +02:00
|
|
|
CHECK_ERR(make_file_path(p_path, v_exact_path, file->mount_point));
|
2004-12-07 23:10:46 +01:00
|
|
|
CHECK_ERR(file_open(p_path, flags, &vf->f));
|
2004-12-07 02:22:25 +01:00
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// success
|
2004-03-05 17:23:31 +01:00
|
|
|
flags |= VF_OPEN;
|
2005-03-29 08:27:35 +02:00
|
|
|
vf->tf = file;
|
2004-03-03 00:56:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-19 17:42:32 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// open the file for synchronous or asynchronous IO. write access is
|
2004-07-05 04:32:43 +02:00
|
|
|
// requested via FILE_WRITE flag, and is not possible for files in archives.
|
2005-01-23 20:49:45 +01:00
|
|
|
// file_flags: default 0
|
|
|
|
Handle vfs_open(const char* v_fn, uint file_flags)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-08-05 14:48:13 +02:00
|
|
|
// 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;
|
2004-08-03 00:16:18 +02:00
|
|
|
|
2004-08-11 22:24:26 +02:00
|
|
|
Handle h = h_alloc(H_VFile, v_fn, res_flags, file_flags);
|
2004-03-03 00:56:51 +01:00
|
|
|
// pass file flags to init
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-16 05:31:29 +02:00
|
|
|
#ifdef PARANOIA
|
2004-09-22 17:20:58 +02:00
|
|
|
debug_out("vfs_open fn=%s %llx\n", v_fn, h);
|
2004-05-16 05:31:29 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return h;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// close the handle to a file.
|
2004-09-22 17:20:58 +02:00
|
|
|
int vfs_close(Handle& h)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-16 05:31:29 +02:00
|
|
|
#ifdef PARANOIA
|
2004-09-22 17:20:58 +02:00
|
|
|
debug_out("vfs_close %llx\n", h);
|
2004-05-16 05:31:29 +02:00
|
|
|
#endif
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
return h_free(h, H_VFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-27 02:33:20 +02:00
|
|
|
// transfer the next <size> bytes to/from the given file.
|
2004-06-03 15:57:00 +02:00
|
|
|
// (read or write access was chosen at file-open time).
|
2004-08-27 02:33:20 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2004-08-12 17:51:31 +02:00
|
|
|
ssize_t vfs_io(const Handle hf, const size_t size, void** p, FileIOCB cb, uintptr_t ctx)
|
2004-07-09 04:17:37 +02:00
|
|
|
{
|
|
|
|
#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;
|
|
|
|
|
2004-08-24 19:34:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-07-09 04:17:37 +02:00
|
|
|
// (vfs_open makes sure it's not opened for writing if zip)
|
|
|
|
if(vf_flags(vf) & VF_ZIP)
|
2004-08-24 19:34:00 +02:00
|
|
|
return zip_read(&vf->zf, ofs, size, buf, cb, ctx);
|
2004-07-09 04:17:37 +02:00
|
|
|
|
|
|
|
// normal file:
|
|
|
|
// let file_io alloc the buffer if the caller didn't (i.e. p = 0),
|
|
|
|
// because it knows about alignment / padding requirements
|
2004-08-24 19:34:00 +02:00
|
|
|
return file_io(&vf->f, ofs, size, buf, cb, ctx);
|
2004-07-09 04:17:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-15 23:49:17 +02:00
|
|
|
#include "timer.h"
|
|
|
|
static double dt;
|
2004-08-16 15:19:10 +02:00
|
|
|
static double totaldata;
|
2004-08-15 23:49:17 +02:00
|
|
|
void dump()
|
|
|
|
{
|
2005-01-23 20:49:45 +01:00
|
|
|
debug_out("TOTAL TIME IN VFS_IO: %f\nthroughput: %f MiB/s\n\n", dt, totaldata/dt/1e6);
|
2004-08-15 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2004-09-19 17:42:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// load the entire file <fn> into memory; return a handle to the memory
|
|
|
|
// and the buffer address/size. output parameters are zeroed on failure.
|
2004-08-27 02:33:20 +02:00
|
|
|
// in addition to the regular file cache, the entire buffer is kept in memory
|
|
|
|
// if flags & FILE_CACHE.
|
2004-09-19 17:42:32 +02:00
|
|
|
//
|
|
|
|
// note: we need the Handle return value for Tex.hm - the data pointer
|
|
|
|
// must be protected against being accidentally free-d in that case.
|
2004-12-10 00:12:02 +01:00
|
|
|
Handle vfs_load(const char* v_fn, void*& p, size_t& size, uint flags /* default 0 */)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-16 05:31:29 +02:00
|
|
|
#ifdef PARANOIA
|
2004-09-22 17:20:58 +02:00
|
|
|
debug_out("vfs_load v_fn=%s\n", v_fn);
|
2004-05-16 05:31:29 +02:00
|
|
|
#endif
|
|
|
|
|
2004-09-19 17:42:32 +02:00
|
|
|
p = 0; size = 0; // zeroed in case vfs_open or H_DEREF fails
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-08-11 22:24:26 +02:00
|
|
|
Handle hf = vfs_open(v_fn, flags);
|
2004-12-15 15:24:12 +01:00
|
|
|
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.
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
H_DEREF(hf, VFile, vf);
|
|
|
|
|
2004-09-19 17:42:32 +02:00
|
|
|
Handle hm = 0; // return value - handle to memory or error code
|
2004-03-05 17:23:31 +01:00
|
|
|
size = vf_size(vf);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2004-06-11 04:14:18 +02:00
|
|
|
assert(vf_size(vf) == (off_t)size && "vfs_load: mismatch between File and Mem size");
|
2004-03-03 00:56:51 +01:00
|
|
|
hm = vf->hm;
|
2004-09-19 17:42:32 +02:00
|
|
|
goto ret;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
else
|
2004-05-08 03:11:51 +02:00
|
|
|
debug_warn("vfs_load: invalid MEM attached to vfile (0 pointer)");
|
2004-03-03 00:56:51 +01:00
|
|
|
// happens if someone frees the pointer. not an error!
|
|
|
|
}
|
|
|
|
|
2004-09-19 17:42:32 +02:00
|
|
|
// 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).
|
|
|
|
{
|
2005-01-23 20:49:45 +01:00
|
|
|
const size_t BLOCK_SIZE = 64*KiB;
|
2004-09-22 17:20:58 +02:00
|
|
|
p = mem_alloc(size, BLOCK_SIZE, 0, &hm);
|
|
|
|
if(!p)
|
|
|
|
goto ret;
|
2004-09-19 17:42:32 +02:00
|
|
|
}
|
2004-09-22 17:20:58 +02:00
|
|
|
|
2004-08-05 16:02:10 +02:00
|
|
|
{
|
2004-09-22 17:20:58 +02:00
|
|
|
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;
|
|
|
|
}
|
2004-08-05 16:06:16 +02:00
|
|
|
}
|
2004-09-19 17:42:32 +02:00
|
|
|
ret:
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
vfs_close(hf);
|
2004-08-05 16:02:10 +02:00
|
|
|
// if FILE_CACHE, it's kept open
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// if we fail, make sure these are set to 0
|
|
|
|
// (they may have been assigned values above)
|
|
|
|
if(hm <= 0)
|
|
|
|
p = 0, size = 0;
|
|
|
|
|
2004-09-22 17:20:58 +02:00
|
|
|
if (hm == 0)
|
|
|
|
debug_out("hm == 0!!\n");
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
return hm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-08 16:38:58 +02:00
|
|
|
// caveat: pads file to next max(4kb, sector_size) boundary
|
|
|
|
// (due to limitation of Win32 FILE_FLAG_NO_BUFFERING I/O).
|
2004-07-09 21:58:43 +02:00
|
|
|
// if that's a problem, specify FILE_NO_AIO when opening.
|
2004-12-10 00:12:02 +01:00
|
|
|
int vfs_store(const char* v_fn, void* p, const size_t size, uint flags /* default 0 */)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-08-11 22:24:26 +02:00
|
|
|
Handle hf = vfs_open(v_fn, flags|FILE_WRITE);
|
2004-03-03 00:56:51 +01:00
|
|
|
if(hf <= 0)
|
|
|
|
return (int)hf; // error code
|
|
|
|
H_DEREF(hf, VFile, vf);
|
2004-07-09 04:17:37 +02:00
|
|
|
const int ret = vfs_io(hf, size, &p);
|
2004-03-03 00:56:51 +01:00
|
|
|
vfs_close(hf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// asynchronous I/O
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-08-15 23:49:17 +02:00
|
|
|
struct IO
|
|
|
|
{
|
2004-09-19 17:42:32 +02:00
|
|
|
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
|
2004-08-15 23:49:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2004-09-19 17:42:32 +02:00
|
|
|
if(io->is_zip)
|
|
|
|
zip_discard_io(&io->zio);
|
|
|
|
else
|
|
|
|
file_discard_io(&io->fio);
|
2004-08-15 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
2005-01-23 20:49:45 +01:00
|
|
|
static int IO_reload(IO* io, const char* fn, Handle h)
|
2004-08-15 23:49:17 +02:00
|
|
|
{
|
2005-01-23 20:49:45 +01:00
|
|
|
UNUSED(io);
|
|
|
|
UNUSED(fn);
|
|
|
|
UNUSED(h);
|
2004-08-15 23:49:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-07-31 13:03:17 +02:00
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// begin transferring <size> bytes, starting at <ofs>. get result
|
2004-07-31 13:03:17 +02:00
|
|
|
// with vfs_wait_io; when no longer needed, free via vfs_discard_io.
|
2004-10-16 22:52:18 +02:00
|
|
|
Handle vfs_start_io(Handle hf, size_t size, void* buf)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-08-15 23:49:17 +02:00
|
|
|
Handle hio = h_alloc(H_IO, 0);
|
|
|
|
H_DEREF(hio, IO, io);
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
H_DEREF(hf, VFile, vf);
|
2004-10-16 22:52:18 +02:00
|
|
|
off_t ofs = vf->ofs;
|
|
|
|
vf->ofs += (off_t)size;
|
|
|
|
|
2004-07-31 13:03:17 +02:00
|
|
|
if(vf_flags(vf) & VF_ZIP)
|
2004-09-19 17:42:32 +02:00
|
|
|
{
|
|
|
|
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);
|
2004-08-18 04:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// indicates if the IO referenced by <io> has completed.
|
|
|
|
// return value: 0 if pending, 1 if complete, < 0 on error.
|
2004-09-22 17:20:58 +02:00
|
|
|
int vfs_io_complete(Handle hio)
|
2004-08-18 04:06:23 +02:00
|
|
|
{
|
|
|
|
H_DEREF(hio, IO, io);
|
2004-09-19 17:42:32 +02:00
|
|
|
if(io->is_zip)
|
|
|
|
return zip_io_complete(&io->zio);
|
|
|
|
else
|
|
|
|
return file_io_complete(&io->fio);
|
2004-06-03 15:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// wait until the transfer <hio> completes, and return its buffer.
|
|
|
|
// output parameters are zeroed on error.
|
2004-09-22 17:20:58 +02:00
|
|
|
int vfs_wait_io(Handle hio, void*& p, size_t& size)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-08-15 23:49:17 +02:00
|
|
|
H_DEREF(hio, IO, io);
|
2004-09-19 17:42:32 +02:00
|
|
|
if(io->is_zip)
|
|
|
|
return zip_wait_io(&io->zio, p, size);
|
|
|
|
else
|
|
|
|
return file_wait_io(&io->fio, p, size);
|
2004-06-03 15:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-31 13:03:17 +02:00
|
|
|
// finished with transfer <hio> - free its buffer (returned by vfs_wait_io)
|
2004-09-22 17:20:58 +02:00
|
|
|
int vfs_discard_io(Handle& hio)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-08-15 23:49:17 +02:00
|
|
|
return h_free(hio, H_IO);
|
2004-06-09 15:44:12 +02:00
|
|
|
}
|
2004-08-27 02:33:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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);
|
|
|
|
}
|
2004-12-07 23:10:46 +01:00
|
|
|
|
|
|
|
|
2005-03-27 03:46:54 +02:00
|
|
|
void vfs_init()
|
|
|
|
{
|
|
|
|
tree_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-07 23:10:46 +01:00
|
|
|
// write a representation of the VFS tree to stdout.
|
2004-12-19 00:30:28 +01:00
|
|
|
void vfs_display()
|
2004-12-07 23:10:46 +01:00
|
|
|
{
|
|
|
|
tree_display();
|
|
|
|
}
|
|
|
|
|
|
|
|
void vfs_shutdown()
|
|
|
|
{
|
|
|
|
file_listing_shutdown();
|
|
|
|
tree_clear();
|
|
|
|
unmount_all();
|
2004-12-09 21:17:09 +01:00
|
|
|
}
|