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-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
|
|
|
|
// a key to identify the watch (obviates separate Dir -> watch mapping).
|
|
|
|
//
|
|
|
|
// define this to strip out that code - removes .watch from struct Dir,
|
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-06-08 17:21:42 +02:00
|
|
|
#undef NO_DIR_WATCH
|
|
|
|
|
|
|
|
|
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-06-13 18:12:54 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2004-05-06 19:14:30 +02:00
|
|
|
//
|
|
|
|
// path
|
|
|
|
//
|
2004-06-13 18:12:54 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
// path types:
|
2004-05-13 15:52:48 +02:00
|
|
|
// fn : filename only, no path at all.
|
|
|
|
// f_* : path intended directly for underlying file layer.
|
2004-05-13 19:23:07 +02:00
|
|
|
// component separator is '/'; no absolute paths, or ':', '\\' allowed.
|
2004-05-13 15:52:48 +02:00
|
|
|
// * : as above, but path within the VFS.
|
2004-05-13 19:23:07 +02:00
|
|
|
// "" is root dir; no absolute path allowed.
|
2004-06-13 18:58:22 +02:00
|
|
|
//
|
|
|
|
// . and .. directory entries aren't supported!
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// path1 and path2 may be empty, filenames, or full paths.
|
|
|
|
static int path_append(char* dst, const char* path1, const char* path2)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-13 15:52:48 +02:00
|
|
|
const size_t path1_len = strlen(path1);
|
2004-05-06 19:14:30 +02:00
|
|
|
const size_t path2_len = strlen(path2);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
bool need_separator = false;
|
|
|
|
|
|
|
|
size_t total_len = path1_len + path2_len + 1; // includes '\0'
|
|
|
|
if(path1_len > 0 && path1[path1_len-1] != '/')
|
|
|
|
{
|
|
|
|
total_len++; // for '/'
|
|
|
|
need_separator = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(total_len+1 > VFS_MAX_PATH)
|
|
|
|
return ERR_VFS_PATH_LENGTH;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
char* p = dst;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
strcpy(p, path1);
|
|
|
|
p += path1_len;
|
|
|
|
if(need_separator)
|
2004-05-06 19:14:30 +02:00
|
|
|
*p++ = '/';
|
|
|
|
strcpy(p, path2);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
static int path_validate(const uint line, const char* const path)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-06 19:14:30 +02:00
|
|
|
size_t path_len = 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
const char* msg = 0; // error occurred <==> != 0
|
|
|
|
int err = -1; // pass error code to caller
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-06-18 16:59:50 +02:00
|
|
|
int c = 0, last_c;
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// disallow absolute path for safety, in case of *nix systems.
|
2004-05-06 19:14:30 +02:00
|
|
|
if(path[0] == '/')
|
|
|
|
{
|
|
|
|
msg = "absolute path";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan each char in path string; count length.
|
|
|
|
for(;;)
|
|
|
|
{
|
2004-06-13 18:58:22 +02:00
|
|
|
last_c = c;
|
|
|
|
c = path[path_len++];
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
// whole path is too long
|
|
|
|
if(path_len >= VFS_MAX_PATH)
|
|
|
|
{
|
|
|
|
msg = "path too long";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2004-06-13 18:58:22 +02:00
|
|
|
// disallow:
|
|
|
|
// - ".." (prevent going above the VFS root dir)
|
|
|
|
// - "/." and "./" (security whole when mounting,
|
|
|
|
// and not supported on Windows).
|
|
|
|
// - "//" (makes no sense)
|
|
|
|
if((c == '.' || c == '/') && (last_c == '.' || last_c == '/'))
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-06-13 18:58:22 +02:00
|
|
|
msg = "contains '..', '/.', './', or '//'";
|
|
|
|
goto fail;
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
// disallow OS-specific dir separators
|
|
|
|
if(c == '\\' || c == ':')
|
|
|
|
{
|
|
|
|
msg = "contains OS-specific dir separator (e.g. '\\', ':')";
|
2004-06-08 17:21:42 +02:00
|
|
|
goto fail;
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
// 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:
|
2004-06-09 15:44:12 +02:00
|
|
|
debug_out("path_validate at line %d failed: %s (error code %d)", line, msg, err);
|
2004-05-08 03:11:51 +02:00
|
|
|
debug_warn("path_validate failed");
|
2004-05-06 19:14:30 +02:00
|
|
|
return err;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
ok:
|
|
|
|
return 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-07 03:27:05 +02:00
|
|
|
#define CHECK_PATH(path) CHECK_ERR(path_validate(__LINE__, path))
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2004-05-08 03:11:51 +02:00
|
|
|
// "file system" (tree structure; stores location of each file)
|
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).
|
|
|
|
//
|
2004-07-05 04:32:43 +02:00
|
|
|
// one Loc is allocated for each archive or directory mounted.
|
|
|
|
// therefore, files only /point/ to a (possibly shared) Loc.
|
2004-05-06 19:14:30 +02:00
|
|
|
// if a file's location changes (e.g. after mounting a higher-priority
|
2004-07-05 04:32:43 +02:00
|
|
|
// directory), the VFS entry will point to the new Loc; 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-05-08 03:11:51 +02:00
|
|
|
// not many instances => don't worry about struct size / alignment.
|
2004-07-05 04:32:43 +02:00
|
|
|
struct Loc
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
Handle archive;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-07-09 21:58:43 +02:00
|
|
|
const std::string mount_point;
|
|
|
|
const std::string dir;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-07-09 21:58:43 +02:00
|
|
|
uint pri;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-07-09 21:58:43 +02:00
|
|
|
Loc(Handle _archive, const char* _mount_point, const char* _dir, uint _pri)
|
|
|
|
: archive(_archive), mount_point(_mount_point), dir(_dir), pri(_pri) {}
|
|
|
|
};
|
2004-07-05 04:32:43 +02:00
|
|
|
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
// rationale for separate file / subdir containers:
|
|
|
|
// problems:
|
|
|
|
// - more code for insertion (oh well);
|
|
|
|
// - makes ordered output of all dirents difficult
|
|
|
|
// (but dirs and files are usually displayed separately)
|
|
|
|
// advantages:
|
|
|
|
// - simplifies lookup code: it can just check if a path is there,
|
|
|
|
// no need to check if the entry is actually a directory
|
|
|
|
// - storing Dir objects directly in the map means less
|
|
|
|
// memory allocations / no need to free them.
|
|
|
|
//
|
2004-06-02 17:32:42 +02:00
|
|
|
// add_* aborts if a subdir or file of the same name already exists.
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
typedef std::map<const std::string, const Loc*> Files;
|
2004-05-06 19:14:30 +02:00
|
|
|
typedef Files::iterator FileIt;
|
2004-06-01 19:34:12 +02:00
|
|
|
// notes:
|
2004-07-05 04:32:43 +02:00
|
|
|
// - Loc is allocated and owned by caller (the mount code)
|
|
|
|
// - priority is accessed by following the Loc pointer.
|
2004-06-01 19:34:12 +02:00
|
|
|
// keeping a copy in the map would lead to better cache coherency,
|
2004-07-05 04:32:43 +02:00
|
|
|
// but it's a bit more clumsy (map filename to struct {pri, Loc*}).
|
2004-06-01 19:34:12 +02:00
|
|
|
// revisit if file lookup open is too slow (unlikely).
|
|
|
|
|
|
|
|
struct Dir;
|
|
|
|
typedef std::pair<const std::string, Dir> SubDir;
|
|
|
|
typedef std::map<const std::string, Dir> SubDirs;
|
|
|
|
typedef SubDirs::iterator SubDirIt;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
struct Dir
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-06-08 17:21:42 +02:00
|
|
|
#ifndef NO_DIR_WATCH
|
2004-07-08 16:38:58 +02:00
|
|
|
intptr_t watch;
|
2004-06-08 17:21:42 +02:00
|
|
|
#endif
|
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
int add_file(const char* name, const Loc* loc);
|
|
|
|
const Loc* find_file(const char* name);
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-02 17:32:42 +02:00
|
|
|
int add_subdir(const char* name);
|
|
|
|
Dir* find_subdir(const char* name);
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
void clearR();
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
SubDirs subdirs;
|
|
|
|
Files files;
|
2004-07-05 04:32:43 +02:00
|
|
|
|
|
|
|
uint mounts;
|
|
|
|
|
|
|
|
// 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 Loc is actually in the mount info and is invalid when removed,
|
|
|
|
// but the VFS will be rebuild in that case.
|
|
|
|
//
|
|
|
|
// valid iff mounts == 1
|
|
|
|
const Loc* loc;
|
|
|
|
|
|
|
|
Dir()
|
|
|
|
{
|
|
|
|
mounts = 0;
|
|
|
|
}
|
2004-06-01 19:34:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-06-02 17:32:42 +02:00
|
|
|
int Dir::add_subdir(const char* const fn)
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2004-07-31 21:36:06 +02:00
|
|
|
if(find_file(fn))
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-07-31 21:36:06 +02:00
|
|
|
debug_warn("add_subdir: file of same name already exists");
|
2004-06-01 19:34:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-06-11 04:14:18 +02:00
|
|
|
const std::string fn_s(fn);
|
|
|
|
subdirs[fn_s];
|
2004-06-01 19:34:12 +02:00
|
|
|
// side effect: maps <fn> to a newly constructed Dir()
|
|
|
|
// non-const => cannot be optimized away.
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
2004-06-02 17:32:42 +02:00
|
|
|
Dir* Dir::find_subdir(const char* const fn)
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2004-06-11 04:14:18 +02:00
|
|
|
const std::string fn_s(fn);
|
|
|
|
SubDirIt it = subdirs.find(fn_s);
|
2004-06-01 19:34:12 +02:00
|
|
|
if(it == subdirs.end())
|
2004-05-06 19:14:30 +02:00
|
|
|
return 0;
|
2004-06-01 19:34:12 +02:00
|
|
|
return &it->second;
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
int Dir::add_file(const char* const fn, const Loc* const loc)
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2004-06-02 17:32:42 +02:00
|
|
|
if(find_subdir(fn))
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-07-31 21:36:06 +02:00
|
|
|
debug_warn("add_file: subdir of same name already exists");
|
2004-06-01 19:34:12 +02:00
|
|
|
return -1;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
2004-06-14 14:59:15 +02:00
|
|
|
const std::string fn_s(fn);
|
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
typedef const Loc* Data;
|
|
|
|
// for absolute clarity; the container holds const Loc* objects.
|
2004-06-02 17:32:42 +02:00
|
|
|
// operator[] returns a reference to that.
|
|
|
|
// need this typedef to work around a GCC bug?
|
2004-06-11 04:14:18 +02:00
|
|
|
Data& old_loc = files[fn_s];
|
2004-06-14 14:59:15 +02:00
|
|
|
// default pointer ctor sets it to 0 =>
|
|
|
|
// if fn wasn't already in the container, old_loc is 0.
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
// old loc exists and is higher priority - keep it.
|
|
|
|
if(old_loc && old_loc->pri > loc->pri)
|
|
|
|
return 1;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-06-14 14:59:15 +02:00
|
|
|
// new loc is greater or equal priority - replace old loc.
|
|
|
|
// note: need to also replace if priority is the same, to allow multiple
|
|
|
|
// patch archives; the one with the "largest" filename trumps the others.
|
2004-06-01 19:34:12 +02:00
|
|
|
old_loc = loc;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
const Loc* Dir::find_file(const char* const fn)
|
2004-06-01 19:34:12 +02:00
|
|
|
{
|
2004-06-11 04:14:18 +02:00
|
|
|
const std::string fn_s(fn);
|
|
|
|
FileIt it = files.find(fn_s);
|
2004-06-01 19:34:12 +02:00
|
|
|
if(it == files.end())
|
|
|
|
return 0;
|
|
|
|
return it->second;
|
|
|
|
}
|
2004-05-08 03:11:51 +02:00
|
|
|
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
void Dir::clearR()
|
|
|
|
{
|
|
|
|
SubDirIt it;
|
|
|
|
for(it = subdirs.begin(); it != subdirs.end(); ++it)
|
|
|
|
{
|
|
|
|
Dir& subdir = it->second;
|
|
|
|
subdir.clearR();
|
|
|
|
}
|
|
|
|
|
|
|
|
subdirs.clear();
|
|
|
|
files.clear();
|
2004-06-08 17:21:42 +02:00
|
|
|
|
|
|
|
#ifndef NO_DIR_WATCH
|
2004-06-23 18:29:03 +02:00
|
|
|
res_cancel_watch(watch);
|
2004-07-08 16:38:58 +02:00
|
|
|
watch = 0;
|
2004-06-08 17:21:42 +02:00
|
|
|
#endif
|
2004-06-01 19:34:12 +02:00
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
static Dir vfs_root;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
// tree_lookup flags
|
|
|
|
enum
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-13 15:52:48 +02:00
|
|
|
LF_DEFAULT = 0,
|
2004-06-02 17:32:42 +02:00
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
LF_CREATE_MISSING_DIRS = 1,
|
|
|
|
|
|
|
|
LF_CREATE_MISSING_FILE = 2,
|
2004-06-02 17:32:42 +02:00
|
|
|
|
|
|
|
LF_LAST = 2
|
2004-05-06 19:14:30 +02:00
|
|
|
};
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2004-05-13 19:23:07 +02:00
|
|
|
// starts in VFS root directory (path = "").
|
2004-06-02 17:32:42 +02:00
|
|
|
// path may specify a file or directory; it need not end in '/'.
|
2004-07-05 04:32:43 +02:00
|
|
|
static int tree_lookup(const char* path, const Loc** const loc = 0, Dir** const dir = 0, uint flags = LF_DEFAULT)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-05-13 19:23:07 +02:00
|
|
|
CHECK_PATH(path);
|
2004-06-02 17:32:42 +02:00
|
|
|
assert(loc != 0 || dir != 0);
|
2004-07-05 04:32:43 +02:00
|
|
|
assert(flags <= LF_LAST);
|
2004-06-02 17:32:42 +02:00
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
const bool create_missing_components = !!(flags & LF_CREATE_MISSING_DIRS);
|
|
|
|
const bool create_missing_files = !!(flags & LF_CREATE_MISSING_FILE);
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
// copy into (writeable) buffer so we can 'tokenize' path components
|
2004-06-02 17:32:42 +02:00
|
|
|
// by replacing '/' with '\0'. length check done by CHECK_PATH.
|
2004-05-06 19:14:30 +02:00
|
|
|
char buf[VFS_MAX_PATH];
|
2004-05-13 19:23:07 +02:00
|
|
|
strcpy(buf, path);
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
Dir* cur_dir = &vfs_root;
|
2004-06-02 17:32:42 +02:00
|
|
|
const char* cur_component = buf;
|
|
|
|
|
|
|
|
// subdirectory traverse logic
|
|
|
|
// valid:
|
|
|
|
// "" (root dir),
|
|
|
|
// "*file",
|
|
|
|
// "*dir[/]"
|
|
|
|
// invalid:
|
|
|
|
// "/*" (caught by CHECK_PATH),
|
|
|
|
// "*file/*" (subdir switch will fail)
|
|
|
|
//
|
|
|
|
// a bit tricky: make sure we go through the directory checks for the
|
|
|
|
// last component - it may either be a file or directory name.
|
|
|
|
// we don't require a trailing '/' for dir names because appending it
|
|
|
|
// to a given dir name would be more (unnecessary) work for the caller.
|
|
|
|
|
|
|
|
// successively navigate to the next subdirectory in <path>.
|
2004-05-06 19:14:30 +02:00
|
|
|
for(;;)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-02 17:32:42 +02:00
|
|
|
// "extract" cur_component string (0-terminate by replacing '/')
|
2004-05-06 19:14:30 +02:00
|
|
|
char* slash = strchr(cur_component, '/');
|
2004-06-02 17:32:42 +02:00
|
|
|
if(slash)
|
|
|
|
*slash = 0;
|
2004-06-02 18:12:13 +02:00
|
|
|
|
|
|
|
// early outs:
|
|
|
|
// .. last component and it's a filename
|
|
|
|
if(slash == 0 && loc != 0)
|
|
|
|
break;
|
|
|
|
// .. root dir ("") or trailing '/' in dir name
|
2004-06-02 17:32:42 +02:00
|
|
|
if(*cur_component == '\0' && loc == 0)
|
2004-06-01 19:34:12 +02:00
|
|
|
break;
|
|
|
|
|
2004-06-02 17:32:42 +02:00
|
|
|
// create <cur_component> subdir (no-op if it already exists)
|
2004-06-01 19:34:12 +02:00
|
|
|
if(create_missing_components)
|
2004-06-02 17:32:42 +02:00
|
|
|
cur_dir->add_subdir(cur_component);
|
2004-06-01 19:34:12 +02:00
|
|
|
|
2004-06-02 17:32:42 +02:00
|
|
|
// switch to <cur_component>
|
2004-06-02 18:12:13 +02:00
|
|
|
cur_dir = cur_dir->find_subdir(cur_component);
|
|
|
|
if(!cur_dir)
|
2004-06-02 17:32:42 +02:00
|
|
|
return -ENOENT;
|
2004-06-01 19:34:12 +02:00
|
|
|
|
|
|
|
// next component
|
2004-06-02 18:12:13 +02:00
|
|
|
if(!slash) // done, no more components left
|
2004-06-02 17:32:42 +02:00
|
|
|
break;
|
2004-06-01 19:34:12 +02:00
|
|
|
cur_component = slash+1;
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-06-02 17:32:42 +02:00
|
|
|
// caller wants a file (possibly in addition to its dir)
|
2004-06-01 19:34:12 +02:00
|
|
|
if(loc)
|
2004-05-08 03:11:51 +02:00
|
|
|
{
|
2004-07-05 04:32:43 +02:00
|
|
|
const char* fn = cur_component;
|
|
|
|
|
|
|
|
if(create_missing_files)
|
|
|
|
{
|
|
|
|
// dir wasn't populated via tree_add_dirR => don't know
|
|
|
|
// the dir's Loc => cannot add this file.
|
|
|
|
if(cur_dir->mounts != 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CHECK_ERR(cur_dir->add_file(fn, cur_dir->loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
*loc = cur_dir->find_file(fn);
|
|
|
|
// the file (still) doesn't exist
|
2004-06-01 19:34:12 +02:00
|
|
|
if(!*loc)
|
2004-06-02 17:32:42 +02:00
|
|
|
return -ENOENT;
|
2004-05-08 03:11:51 +02:00
|
|
|
}
|
2004-06-02 17:32:42 +02:00
|
|
|
|
|
|
|
// if loc != 0, we know cur_dir is correct, because it contains
|
|
|
|
// the desired file (otherwise, the find_file above will have failed).
|
|
|
|
// if loc == 0, path is a dir name, and we have successfully traversed
|
|
|
|
// all path components (subdirectories) in the loop.
|
2004-06-01 19:34:12 +02:00
|
|
|
if(dir)
|
|
|
|
*dir = cur_dir;
|
2004-06-02 17:32:42 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
return 0;
|
2004-05-08 03:11:51 +02:00
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
static inline void tree_clear()
|
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
vfs_root.clearR();
|
2004-05-08 03:11:51 +02:00
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
struct FileCBParams
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
Dir* const dir;
|
2004-07-05 04:32:43 +02:00
|
|
|
const Loc* const loc;
|
|
|
|
FileCBParams(Dir* _dir, const Loc* _loc)
|
2004-06-02 17:32:42 +02:00
|
|
|
: dir(_dir), loc(_loc) {}
|
2004-06-21 18:29:47 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
FileCBParams& operator=(const FileCBParams&);
|
2004-05-08 03:11:51 +02:00
|
|
|
};
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
// called for each OS dir ent.
|
|
|
|
// add each file and directory to the VFS dir.
|
|
|
|
//
|
|
|
|
// note:
|
|
|
|
// we don't mount archives here for performance reasons.
|
|
|
|
// that means archives in subdirectories of mount points aren't added!
|
|
|
|
// rationale: can't determine if file is an archive via extension -
|
|
|
|
// they might be called .pk3 or whatnot. for every file in the tree, we'd have
|
|
|
|
// to try to open it as an archive - not good.
|
|
|
|
// this restriction also simplifies the code a bit, but if it's a problem,
|
|
|
|
// just generate a list of archives here and mount them from the caller.
|
2004-07-31 21:36:06 +02:00
|
|
|
static int add_dirent_cb(const char* const path, const uint flags, const ssize_t size, const uintptr_t user)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-06-11 04:14:18 +02:00
|
|
|
UNUSED(size);
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
const FileCBParams* const params = (FileCBParams*)user;
|
2004-07-08 16:38:58 +02:00
|
|
|
Dir* const cur_dir = params->dir;
|
2004-07-05 04:32:43 +02:00
|
|
|
const Loc* const cur_loc = params->loc;
|
2004-06-01 19:34:12 +02:00
|
|
|
|
2004-07-31 21:36:06 +02:00
|
|
|
int err = 1;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-07-31 21:36:06 +02:00
|
|
|
if(flags & LOC_ZIP)
|
|
|
|
{
|
|
|
|
Dir* dir;
|
|
|
|
if(tree_lookup(path, 0, &dir, LF_CREATE_MISSING_DIRS) >= 0)
|
|
|
|
{
|
|
|
|
const char* fn = strrchr(path, '/');
|
|
|
|
if(fn)
|
|
|
|
err = dir->add_file(fn+1, cur_loc);
|
|
|
|
}
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
// directory
|
2004-07-31 21:36:06 +02:00
|
|
|
else if(flags & LOC_DIR)
|
2004-06-02 18:58:35 +02:00
|
|
|
{
|
|
|
|
// leave out CVS dirs in debug builds. this makes possible
|
|
|
|
// user code that relies on a fixed data directory structure.
|
|
|
|
#ifndef NDEBUG
|
2004-07-31 21:36:06 +02:00
|
|
|
if(!strcmp(path, "CVS"))
|
2004-06-02 18:58:35 +02:00
|
|
|
return 0;
|
|
|
|
#endif
|
2004-07-31 21:36:06 +02:00
|
|
|
err = cur_dir->add_subdir(path);
|
2004-06-02 18:58:35 +02:00
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
// file
|
|
|
|
else
|
2004-07-31 21:36:06 +02:00
|
|
|
err = cur_dir->add_file(path, cur_loc);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
if(err < 0)
|
|
|
|
return -EEXIST;
|
2004-05-06 19:14:30 +02:00
|
|
|
return 0;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
static int tree_add_dirR(Dir* const dir, const char* const f_path, const Loc* const loc)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-13 15:52:48 +02:00
|
|
|
CHECK_PATH(f_path);
|
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
dir->mounts++;
|
|
|
|
dir->loc = loc;
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// add files and subdirs to vdir
|
2004-06-02 17:32:42 +02:00
|
|
|
const FileCBParams params(dir, loc);
|
2004-05-13 15:52:48 +02:00
|
|
|
file_enum(f_path, add_dirent_cb, (uintptr_t)¶ms);
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-08 17:21:42 +02:00
|
|
|
#ifndef NO_DIR_WATCH
|
2004-06-23 18:29:03 +02:00
|
|
|
res_watch_dir(f_path, &dir->watch);
|
2004-06-08 17:21:42 +02:00
|
|
|
#endif
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
for(SubDirIt it = dir->subdirs.begin(); it != dir->subdirs.end(); ++it)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
Dir* const subdir = &it->second;
|
|
|
|
const char* const subdir_name_c = (it->first).c_str();
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
char f_subdir_path[VFS_MAX_PATH];
|
2004-06-01 19:34:12 +02:00
|
|
|
CHECK_ERR(path_append(f_subdir_path, f_path, subdir_name_c));
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
tree_add_dirR(subdir, f_subdir_path, loc);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
static int tree_add_loc(Dir* const dir, const Loc* const loc)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
if(loc->archive > 0)
|
2004-05-13 15:52:48 +02:00
|
|
|
{
|
2004-06-02 17:32:42 +02:00
|
|
|
const FileCBParams params(dir, loc);
|
2004-05-08 03:11:51 +02:00
|
|
|
return zip_enum(loc->archive, add_dirent_cb, (uintptr_t)¶ms);
|
2004-05-13 15:52:48 +02:00
|
|
|
}
|
2004-05-08 03:11:51 +02:00
|
|
|
else
|
|
|
|
{
|
2004-05-13 15:52:48 +02:00
|
|
|
const char* f_path_c = loc->dir.c_str();
|
2004-06-01 19:34:12 +02:00
|
|
|
return tree_add_dirR(dir, f_path_c, loc);
|
2004-05-08 03:11:51 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// mount archives and directories into the VFS
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// container must not invalidate iterators after insertion!
|
|
|
|
// (we keep and pass around pointers to Mount.archive_locs elements)
|
|
|
|
// see below.
|
2004-06-02 17:32:42 +02:00
|
|
|
//
|
2004-07-05 04:32:43 +02:00
|
|
|
// not const, because we h_free a handle in Loc
|
2004-06-02 17:32:42 +02:00
|
|
|
// (it resets the handle to 0)
|
2004-07-05 04:32:43 +02:00
|
|
|
typedef std::list<Loc> Locs;
|
2004-05-08 03:11:51 +02:00
|
|
|
typedef Locs::iterator LocIt;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
struct Mount
|
|
|
|
{
|
2004-07-09 21:58:43 +02:00
|
|
|
// note: we basically duplicate the mount information in dir_loc.
|
|
|
|
// this is because it's needed in Mount when remounting, but also
|
|
|
|
// in Loc when adding files. accessing everything via Loc is ugly.
|
|
|
|
// it's no big deal - there won't be many mountings.
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// mounting into this VFS directory ("" for root)
|
2004-07-09 21:58:43 +02:00
|
|
|
const std::string mount_point;
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2004-06-13 18:12:54 +02:00
|
|
|
// what is being mounted; either directory, or archive filename
|
2004-07-09 21:58:43 +02:00
|
|
|
const std::string f_name;
|
2004-05-08 03:11:51 +02:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
uint pri;
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// storage for all Locs 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
|
|
|
|
// are std::lists; all pointers are reset after unmounting something.
|
|
|
|
// the VFS stores Loc* pointers for each file and for dirs with exactly
|
|
|
|
// 1 associated real dir, so that newly written files may be added.
|
2004-07-05 04:32:43 +02:00
|
|
|
Loc dir_loc;
|
2004-05-08 03:11:51 +02:00
|
|
|
Locs archive_locs;
|
2004-07-08 16:38:58 +02:00
|
|
|
// if f_name is a single archive, this stores its Loc.
|
|
|
|
// otherwise, there's one Loc for every archive in the directory
|
|
|
|
// (but not its children - see remount()).
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2004-06-13 18:12:54 +02:00
|
|
|
Mount(const char* _mount_point, const char* _f_name, uint _pri)
|
|
|
|
: mount_point(_mount_point), f_name(_f_name), pri(_pri),
|
2004-07-09 21:58:43 +02:00
|
|
|
dir_loc(0, _mount_point, _f_name, pri), archive_locs() {}
|
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-05-13 15:52:48 +02:00
|
|
|
// support for mounting multiple archives in a directory
|
|
|
|
// (useful for mix-in mods and patches).
|
|
|
|
// all archives are enumerated, added to a Locs list,
|
|
|
|
// and mounted (in alphabetical order!)
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
struct ArchiveCBParams
|
|
|
|
{
|
|
|
|
// we need a full path to open the archive, and only receive
|
|
|
|
// the filename, so prepend this (the directory being searched).
|
|
|
|
const char* f_dir;
|
|
|
|
|
|
|
|
// priority at which the archive is to be mounted.
|
|
|
|
// specify here, instead of when actually adding the archive,
|
|
|
|
// because Locs are created const.
|
|
|
|
uint pri;
|
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
// will add one Loc to this container for
|
2004-05-13 15:52:48 +02:00
|
|
|
// every archive successfully opened.
|
|
|
|
Locs* archive_locs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// called for each directory entry.
|
|
|
|
// add each successfully opened archive to list.
|
|
|
|
static int archive_cb(const char* const fn, const uint flags, const ssize_t size, const uintptr_t user)
|
|
|
|
{
|
2004-06-11 04:14:18 +02:00
|
|
|
UNUSED(size);
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// not interested in subdirectories
|
|
|
|
if(flags & LOC_DIR)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const ArchiveCBParams* const params = (ArchiveCBParams*)user;
|
|
|
|
const char* const f_dir = params->f_dir;
|
|
|
|
const uint pri = params->pri;
|
|
|
|
Locs* const archive_locs = params->archive_locs;
|
|
|
|
|
|
|
|
// get full path (fn is filename only)
|
|
|
|
char f_path[VFS_MAX_PATH];
|
|
|
|
CHECK_ERR(path_append(f_path, f_dir, fn));
|
2004-05-08 03:11:51 +02:00
|
|
|
|
|
|
|
// don't check filename extension - archives won't necessarily
|
|
|
|
// be called .zip (example: Quake III .pk3).
|
|
|
|
// just try to open the file.
|
2004-05-13 15:52:48 +02:00
|
|
|
const Handle archive = zip_archive_open(f_path);
|
2004-05-08 03:11:51 +02:00
|
|
|
if(archive > 0)
|
2004-07-09 21:58:43 +02:00
|
|
|
archive_locs->push_back(Loc(archive, "", "", pri));
|
2004-05-08 03:11:51 +02:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
// only add archive to list; don't add its files into the VFS yet,
|
|
|
|
// to simplify debugging (we see which files are in which archive)
|
2004-05-08 03:11:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
// actually mount the specified entry (either Zip archive or dir).
|
|
|
|
// split out of vfs_mount because we need to mount without changing the
|
|
|
|
// mount list, when invalidating (reloading) the VFS.
|
|
|
|
static int remount(Mount& m)
|
|
|
|
{
|
2004-07-09 21:58:43 +02:00
|
|
|
const char* mount_point = m.mount_point.c_str();
|
|
|
|
const char* f_name = m.f_name.c_str();
|
|
|
|
const uint pri = m.pri;
|
|
|
|
const Loc* dir_loc = &m.dir_loc;
|
|
|
|
Locs& archive_locs = m.archive_locs;
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
Dir* dir;
|
2004-07-05 04:32:43 +02:00
|
|
|
CHECK_ERR(tree_lookup(mount_point, 0, &dir, LF_CREATE_MISSING_DIRS));
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
// check if target is a single Zip archive
|
2004-06-13 18:12:54 +02:00
|
|
|
// (it can't also be a directory - prevented by OS FS)
|
2004-05-13 15:52:48 +02:00
|
|
|
const Handle archive = zip_archive_open(f_name);
|
2004-05-08 03:11:51 +02:00
|
|
|
if(archive > 0)
|
|
|
|
{
|
2004-07-09 21:58:43 +02:00
|
|
|
archive_locs.push_back(Loc(archive, "", "", pri));
|
2004-07-05 04:32:43 +02:00
|
|
|
const Loc* loc = &archive_locs.front();
|
2004-06-01 19:34:12 +02:00
|
|
|
return tree_add_loc(dir, loc);
|
2004-05-08 03:11:51 +02:00
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-06-13 18:12:54 +02:00
|
|
|
// f_name is a directory (not Zip file - would have been opened above)
|
|
|
|
|
2004-07-31 21:36:06 +02:00
|
|
|
// add all loose files and subdirectories in subtree
|
|
|
|
// (before adding archives, so that it doesn't try to add subdirs
|
|
|
|
// that are only in the archive).
|
|
|
|
CHECK_ERR(tree_add_loc(dir, dir_loc));
|
|
|
|
|
2004-06-13 18:12:54 +02:00
|
|
|
// enumerate all archives in dir (but not its subdirs! see above.)
|
2004-05-13 15:52:48 +02:00
|
|
|
ArchiveCBParams params = { f_name, pri, &archive_locs };
|
|
|
|
file_enum(f_name, archive_cb, (uintptr_t)¶ms);
|
|
|
|
|
2004-06-13 18:12:54 +02:00
|
|
|
// .. and add them
|
2004-05-13 15:52:48 +02:00
|
|
|
for(LocIt it = archive_locs.begin(); it != archive_locs.end(); ++it)
|
|
|
|
{
|
2004-07-05 04:32:43 +02:00
|
|
|
const Loc* const loc = &*it;
|
2004-06-13 18:12:54 +02:00
|
|
|
if(tree_add_loc(dir, loc) < 0)
|
|
|
|
debug_warn("adding archive failed");
|
2004-05-13 15:52:48 +02:00
|
|
|
}
|
|
|
|
|
2004-06-09 15:44:12 +02:00
|
|
|
return 0;
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int unmount(Mount& m)
|
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
for(LocIt it = m.archive_locs.begin(); it != m.archive_locs.end(); ++it)
|
|
|
|
{
|
2004-07-05 04:32:43 +02:00
|
|
|
Loc& loc = *it;
|
2004-05-08 03:11:51 +02:00
|
|
|
zip_archive_close(loc.archive);
|
|
|
|
}
|
|
|
|
|
|
|
|
m.archive_locs.clear();
|
2004-05-06 19:14:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
static inline void unmount_all(void)
|
2004-05-08 03:11:51 +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()
|
2004-05-08 03:11:51 +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-11 04:14:18 +02:00
|
|
|
static void vfs_shutdown(void)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-13 15:52:48 +02:00
|
|
|
tree_clear();
|
|
|
|
unmount_all();
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2004-06-13 21:13:32 +02:00
|
|
|
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
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c1 != c2)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// mount either a single archive or a directory into the VFS at
|
|
|
|
// <vfs_mount_point>, which is created if it does not yet exist.
|
2004-07-05 04:32:43 +02:00
|
|
|
// new files override the previous VFS contents if pri(ority) is not lower.
|
2004-06-03 15:57:00 +02:00
|
|
|
// if <name> is a directory, all archives in that directory (but not
|
|
|
|
// its subdirs - see add_dirent_cb) are also mounted in alphabetical order.
|
|
|
|
// name = "." or "./" isn't allowed - see implementation for rationale.
|
2004-07-09 21:58:43 +02:00
|
|
|
int vfs_mount(const char* const mount_point, const char* const name, const uint pri)
|
2004-05-13 15:52:48 +02:00
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
ONCE(atexit2(vfs_shutdown));
|
2004-05-08 03:11:51 +02:00
|
|
|
|
2004-06-13 21:13:32 +02:00
|
|
|
CHECK_PATH(name);
|
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)
|
2004-06-13 21:13:32 +02:00
|
|
|
if(is_subpath(name, it->f_name.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-06-13 19:15:38 +02:00
|
|
|
if(!strcmp(name, "."))
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
|
|
|
debug_warn("vfs_mount: mounting . not allowed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-07-09 21:58:43 +02:00
|
|
|
mounts.push_back(Mount(mount_point, name, pri));
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
// actually mount the entry
|
2004-05-13 15:52:48 +02:00
|
|
|
Mount& m = mounts.back();
|
2004-05-06 19:14:30 +02:00
|
|
|
return remount(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
// FAM 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.
|
2004-05-06 19:14:30 +02:00
|
|
|
int vfs_rebuild()
|
|
|
|
{
|
2004-05-08 03:11:51 +02:00
|
|
|
tree_clear();
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
unmount_all();
|
|
|
|
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-05-06 19:14:30 +02:00
|
|
|
int vfs_unmount(const char* name)
|
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-05-13 15:52:48 +02:00
|
|
|
if(it->f_name == name)
|
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-07-09 21:58:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-07-31 21:36:06 +02:00
|
|
|
// get rid of trailing / in src (must not be included in remove)
|
|
|
|
const char* start = src+remove_len;
|
|
|
|
if(*start == '/' || *start == DIR_SEP)
|
|
|
|
start++;
|
|
|
|
|
2004-07-09 21:58:43 +02:00
|
|
|
// prepend replace.
|
2004-07-31 21:36:06 +02:00
|
|
|
CHECK_ERR(path_append(dst, replace, start));
|
2004-07-09 21:58:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int vfs_make_vfs_path(const char* const path, char* const vfs_path)
|
2004-06-08 17:21:42 +02:00
|
|
|
{
|
|
|
|
for(MountIt it = mounts.begin(); it != mounts.end(); ++it)
|
|
|
|
{
|
2004-07-09 21:58:43 +02:00
|
|
|
const char* remove = it->f_name.c_str();
|
|
|
|
const char* replace = it->mount_point.c_str();
|
|
|
|
|
|
|
|
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-07-09 21:58:43 +02:00
|
|
|
static int make_file_path(const char* vfs_path, const Loc* loc, char* const path)
|
|
|
|
{
|
|
|
|
assert(loc->archive == 0);
|
|
|
|
|
|
|
|
const char* remove = loc->mount_point.c_str();
|
|
|
|
const char* replace = loc->dir.c_str();
|
|
|
|
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
|
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
// 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.
|
2004-06-01 19:34:12 +02:00
|
|
|
SubDirs* subdirs;
|
|
|
|
SubDirIt subdir_it;
|
|
|
|
Files* files;
|
|
|
|
FileIt file_it;
|
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)
|
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
delete vd->subdirs;
|
|
|
|
delete vd->files;
|
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
|
|
|
{
|
2004-06-18 15:32:33 +02:00
|
|
|
if(vd->subdirs)
|
|
|
|
{
|
|
|
|
debug_warn("VDir_reload called when already loaded - why?");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-29 14:00:53 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
Dir* dir;
|
|
|
|
CHECK_ERR(tree_lookup(path, 0, &dir));
|
2004-05-29 14:00:53 +02:00
|
|
|
|
2004-06-18 15:32:33 +02:00
|
|
|
// rationale for copying the dir's data: see VDir definition
|
|
|
|
// note: bad_alloc exception handled by h_alloc.
|
2004-07-09 21:58:43 +02:00
|
|
|
vd->subdirs = new SubDirs(dir->subdirs);
|
2004-06-01 19:34:12 +02:00
|
|
|
vd->subdir_it = vd->subdirs->begin();
|
2004-07-09 21:58:43 +02:00
|
|
|
vd->files = new Files(dir->files);
|
2004-06-01 19:34:12 +02:00
|
|
|
vd->file_it = vd->files->begin();
|
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.
|
|
|
|
// 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* const dir)
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
2004-06-03 15:57:00 +02:00
|
|
|
return h_alloc(H_VDir, dir, 0);
|
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.
|
|
|
|
// all vfsDirEnt.name strings are now invalid.
|
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-07-05 04:32:43 +02:00
|
|
|
// return the next remaining directory entry (in alphabetical order) matching
|
|
|
|
// filter, or a negative error code on error (e.g. end of directory reached).
|
|
|
|
// filter values:
|
2004-06-03 15:57:00 +02:00
|
|
|
// - 0: any file;
|
|
|
|
// - ".": any file without extension (filename doesn't contain '.');
|
|
|
|
// - ".ext": any file with extension ".ext" (which must not contain '.');
|
|
|
|
// - "/": any subdirectory
|
2004-05-29 14:00:53 +02:00
|
|
|
int vfs_next_dirent(const Handle hd, vfsDirEnt* ent, const char* const filter)
|
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
H_DEREF(hd, VDir, vd);
|
2004-05-29 14:00:53 +02:00
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// interpret filter (paranoid)
|
2004-06-01 19:34:12 +02:00
|
|
|
bool filter_dir = false;
|
|
|
|
bool filter_no_ext = false;
|
|
|
|
if(filter)
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
if(filter[0] == '/')
|
|
|
|
{
|
|
|
|
if(filter[1] != '\0')
|
|
|
|
goto invalid_filter;
|
|
|
|
filter_dir = true;
|
|
|
|
}
|
|
|
|
else if(filter[0] == '.')
|
2004-05-29 14:00:53 +02:00
|
|
|
{
|
2004-06-01 19:34:12 +02:00
|
|
|
if(strchr(filter+1, '.'))
|
|
|
|
goto invalid_filter;
|
|
|
|
filter_no_ext = filter[1] == '\0';
|
2004-05-29 14:00:53 +02:00
|
|
|
}
|
2004-06-01 19:34:12 +02:00
|
|
|
else
|
2004-06-02 18:58:35 +02:00
|
|
|
{
|
|
|
|
invalid_filter:
|
|
|
|
debug_warn("vfs_next_dirent: invalid filter");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-06-01 19:34:12 +02:00
|
|
|
}
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// 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.
|
2004-06-01 19:34:12 +02:00
|
|
|
const char* fn;
|
2004-05-29 14:00:53 +02:00
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
// caller wants a subdirectory; return the next one.
|
|
|
|
if(filter_dir)
|
|
|
|
{
|
|
|
|
if(vd->subdir_it == vd->subdirs->end())
|
|
|
|
return -1;
|
|
|
|
fn = vd->subdir_it->first.c_str();
|
|
|
|
++vd->subdir_it;
|
|
|
|
goto have_match;
|
2004-05-29 14:00:53 +02:00
|
|
|
}
|
2004-06-01 19:34:12 +02:00
|
|
|
|
|
|
|
// caller wants a file; loop until one matches or end of list.
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if(vd->file_it == vd->files->end())
|
|
|
|
return -1;
|
|
|
|
fn = vd->file_it->first.c_str();
|
|
|
|
++vd->file_it;
|
|
|
|
|
|
|
|
char* const ext = strrchr(fn, '.');
|
2004-06-02 18:58:35 +02:00
|
|
|
// file has an extension
|
|
|
|
if(ext)
|
|
|
|
{
|
|
|
|
// not filtering, or filter matches extension: match
|
|
|
|
if(!filter || strcmp(ext, filter) == 0)
|
|
|
|
goto have_match;
|
|
|
|
}
|
|
|
|
// file has no extension
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// filter accepts files without an extension: match
|
|
|
|
if(filter_no_ext)
|
|
|
|
goto have_match;
|
|
|
|
}
|
2004-06-01 19:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
have_match:
|
|
|
|
ent->name = fn;
|
|
|
|
return 0;
|
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-07-09 21:58:43 +02:00
|
|
|
int vfs_realpath(const char* fn, char* realpath)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-07-05 04:32:43 +02:00
|
|
|
const Loc* loc;
|
2004-06-03 15:57:00 +02:00
|
|
|
CHECK_ERR(tree_lookup(fn, &loc));
|
|
|
|
|
|
|
|
// file is in normal directory
|
|
|
|
if(loc->archive <= 0)
|
2004-07-09 21:58:43 +02:00
|
|
|
CHECK_ERR(make_file_path(fn, loc, realpath));
|
2004-06-03 15:57:00 +02:00
|
|
|
// file is in archive
|
2004-06-30 20:47:15 +02:00
|
|
|
else
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-07-09 21:58:43 +02:00
|
|
|
const char* archive_fn = h_filename(loc->archive);
|
|
|
|
if(!archive_fn)
|
2004-06-03 15:57:00 +02:00
|
|
|
return -1;
|
2004-07-09 21:58:43 +02:00
|
|
|
CHECK_ERR(path_append(realpath, archive_fn, fn));
|
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.
|
|
|
|
bool vfs_exists(const char* fn)
|
|
|
|
{
|
|
|
|
const Loc* loc;
|
|
|
|
return (tree_lookup(fn, &loc) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 15:57:00 +02:00
|
|
|
// return information about the specified file as in stat(2),
|
|
|
|
// most notably size. stat buffer is undefined on error.
|
|
|
|
int vfs_stat(const char* fn, struct stat* s)
|
|
|
|
{
|
2004-07-05 04:32:43 +02:00
|
|
|
const Loc* loc;
|
2004-06-03 15:57:00 +02:00
|
|
|
CHECK_ERR(tree_lookup(fn, &loc));
|
|
|
|
|
|
|
|
if(loc->archive > 0)
|
2004-07-09 21:58:43 +02:00
|
|
|
CHECK_ERR(zip_stat(loc->archive, fn, s));
|
2004-06-03 15:57:00 +02:00
|
|
|
else
|
|
|
|
{
|
2004-07-09 21:58:43 +02:00
|
|
|
char path[PATH_MAX];
|
|
|
|
CHECK_ERR(make_file_path(fn, loc, path));
|
|
|
|
CHECK_ERR(file_stat(path, s));
|
2004-06-03 15:57:00 +02: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
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
File f;
|
|
|
|
ZFile zf;
|
|
|
|
};
|
2004-03-05 17:23:31 +01:00
|
|
|
|
2004-05-06 19:14:30 +02:00
|
|
|
// be aware when adding fields that we're already pushing the size limit
|
|
|
|
// (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
|
|
|
|
file_close(&vf->f);
|
|
|
|
|
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-06-26 00:19:19 +02:00
|
|
|
static int VFile_reload(VFile* vf, const char* path, Handle)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-03 02:17:24 +02:00
|
|
|
uint& flags = vf_flags(vf);
|
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-07-05 04:32:43 +02:00
|
|
|
const Loc* loc;
|
|
|
|
uint lf = (flags & FILE_WRITE)? LF_CREATE_MISSING_FILE : LF_DEFAULT;
|
|
|
|
CHECK_ERR(tree_lookup(path, &loc, 0, lf));
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-07-05 04:32:43 +02:00
|
|
|
// normal file
|
2004-05-08 03:11:51 +02:00
|
|
|
if(loc->archive <= 0)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-05-13 15:52:48 +02:00
|
|
|
char f_path[PATH_MAX];
|
2004-07-09 21:58:43 +02:00
|
|
|
CHECK_ERR(make_file_path(path, loc, f_path));
|
2004-07-05 04:32:43 +02:00
|
|
|
CHECK_ERR(file_open(f_path, flags, &vf->f));
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
2004-07-05 04:32:43 +02:00
|
|
|
// archive
|
2004-05-06 19:14:30 +02:00
|
|
|
else
|
|
|
|
{
|
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
|
|
|
|
2004-05-13 15:52:48 +02:00
|
|
|
CHECK_ERR(zip_open(loc->archive, path, &vf->zf));
|
2004-05-06 19:14:30 +02:00
|
|
|
|
|
|
|
flags |= VF_ZIP;
|
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// success
|
2004-03-05 17:23:31 +01:00
|
|
|
flags |= VF_OPEN;
|
2004-03-03 00:56:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2004-08-03 00:16:18 +02:00
|
|
|
Handle vfs_open(const char* fn, uint file_flags /* = 0 */)
|
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
|
|
|
|
|
|
|
Handle h = h_alloc(H_VFile, 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-05-06 19:14:30 +02:00
|
|
|
debug_out("vfs_open fn=%s %I64x\n", 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-03-03 00:56:51 +01:00
|
|
|
inline int vfs_close(Handle& h)
|
|
|
|
{
|
2004-05-16 05:31:29 +02:00
|
|
|
#ifdef PARANOIA
|
2004-05-06 19:14:30 +02:00
|
|
|
debug_out("vfs_close %I64x\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-07-09 04:17:37 +02:00
|
|
|
// try to 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).
|
|
|
|
// return bytes of actual data transferred, or a negative error code.
|
|
|
|
// TODO: buffer types
|
2004-07-09 04:17:37 +02:00
|
|
|
ssize_t vfs_io(const Handle hf, const size_t size, void** p)
|
|
|
|
{
|
|
|
|
#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;
|
|
|
|
|
|
|
|
// (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, p);
|
|
|
|
|
|
|
|
// 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, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-05 14:48:13 +02:00
|
|
|
Handle vfs_load(const char* const 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-05-06 19:14:30 +02:00
|
|
|
debug_out("vfs_load fn=%s\n", fn);
|
2004-05-16 05:31:29 +02:00
|
|
|
#endif
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
p = 0; // vfs_io needs initial 0 value
|
2004-03-05 17:23:31 +01:00
|
|
|
size = 0; // in case open or deref fails
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-08-05 14:48:13 +02:00
|
|
|
Handle hf = vfs_open(fn, flags);
|
2004-03-03 00:56:51 +01:00
|
|
|
if(hf <= 0)
|
|
|
|
return hf; // error code
|
|
|
|
H_DEREF(hf, VFile, vf);
|
|
|
|
|
|
|
|
Handle hm = 0;
|
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;
|
|
|
|
goto skip_read;
|
|
|
|
}
|
|
|
|
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!
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // VC6 goto fix
|
2004-07-09 04:17:37 +02:00
|
|
|
ssize_t nread = vfs_io(hf, size, &p);
|
2004-03-03 00:56:51 +01:00
|
|
|
if(nread > 0)
|
2004-08-05 16:02:10 +02:00
|
|
|
{
|
2004-08-07 15:45:03 +02:00
|
|
|
// one case where we need the handle return value is Tex.hm;
|
|
|
|
// we can't have the pointer freed behind our back there.
|
|
|
|
// if someone calls mem_get_ptr or mem_size, it must return the
|
|
|
|
// real memory range (p+size), disregarding padding added by File.
|
|
|
|
// hence, mem_assign finds the actual allocation to which p belongs
|
|
|
|
// (not necessarily starting at p); we mem_assign_user a subrange.
|
2004-03-03 00:56:51 +01:00
|
|
|
hm = mem_assign(p, size);
|
2004-08-07 15:45:03 +02:00
|
|
|
assert(hm > 0);
|
|
|
|
if(mem_assign_user(hm, p, size) < 0)
|
|
|
|
debug_warn("vfs_load: mem_assign_user failed");
|
|
|
|
|
|
|
|
// sanity check: handle returns correct info
|
|
|
|
#ifndef NDEBUG
|
|
|
|
size_t test_size;
|
|
|
|
void* test_p = mem_get_ptr(hm, &test_size);
|
|
|
|
assert(test_p == p && test_size == size);
|
|
|
|
#endif
|
|
|
|
|
2004-08-05 16:02:10 +02:00
|
|
|
if(flags & FILE_CACHE)
|
|
|
|
{
|
|
|
|
vf->hm = hm;
|
|
|
|
// add ref to hm for VFile
|
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
2004-08-05 16:06:16 +02:00
|
|
|
}
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
skip_read:
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-08-05 14:48:13 +02:00
|
|
|
int vfs_store(const char* const fn, void* p, const size_t size, uint flags /* default 0 */)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-07-09 21:58:43 +02:00
|
|
|
Handle hf = vfs_open(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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2004-06-03 02:17:24 +02:00
|
|
|
//
|
|
|
|
// memory mapping
|
|
|
|
//
|
2004-06-03 15:57:00 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-06-03 02:17:24 +02:00
|
|
|
// map the entire file <hf> into memory. if already currently 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)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-03 02:17:24 +02:00
|
|
|
UNUSED(flags);
|
2004-06-02 22:41:05 +02:00
|
|
|
|
2004-06-03 02:17:24 +02:00
|
|
|
p = 0;
|
|
|
|
size = 0;
|
|
|
|
// need to zero these here in case H_DEREF fails
|
|
|
|
|
|
|
|
H_DEREF(hf, VFile, vf);
|
2004-06-02 22:41:05 +02:00
|
|
|
if(vf_flags(vf) & VF_ZIP)
|
2004-06-03 02:17:24 +02:00
|
|
|
return zip_map(&vf->zf, p, size);
|
2004-06-02 22:41:05 +02:00
|
|
|
else
|
2004-06-03 02:17:24 +02:00
|
|
|
return file_map(&vf->f, p, size);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-03 02:17:24 +02:00
|
|
|
// 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)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2004-06-02 22:41:05 +02:00
|
|
|
H_DEREF(hf, VFile, vf);
|
|
|
|
if(vf_flags(vf) & VF_ZIP)
|
2004-06-03 02:17:24 +02:00
|
|
|
return zip_unmap(&vf->zf);
|
2004-06-02 22:41:05 +02:00
|
|
|
else
|
2004-06-03 02:17:24 +02:00
|
|
|
return file_unmap(&vf->f);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
2004-06-03 15:57:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// asynchronous I/O
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2004-07-31 13:03:17 +02:00
|
|
|
// note: VFS and zip return the file I/O handle.
|
|
|
|
// there's no way for zip to do any extra processing, but that's unnecessary
|
|
|
|
// because aio is currently only supported for uncompressed files.
|
|
|
|
|
|
|
|
|
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-06-03 15:57:00 +02:00
|
|
|
Handle vfs_start_io(Handle hf, off_t ofs, size_t size, void* buf)
|
|
|
|
{
|
|
|
|
H_DEREF(hf, VFile, vf);
|
2004-07-31 13:03:17 +02:00
|
|
|
if(vf_flags(vf) & VF_ZIP)
|
|
|
|
return zip_start_io(&vf->zf, ofs, size, buf);
|
|
|
|
return file_start_io(&vf->f, ofs, size, buf);
|
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-07-31 13:03:17 +02:00
|
|
|
inline int vfs_wait_io(Handle hio, void*& p, size_t& size)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-07-31 13:03:17 +02:00
|
|
|
return file_wait_io(hio, 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)
|
|
|
|
inline int vfs_discard_io(Handle& hio)
|
2004-06-03 15:57:00 +02:00
|
|
|
{
|
2004-07-31 13:03:17 +02:00
|
|
|
return file_discard_io(hio);
|
2004-06-09 15:44:12 +02:00
|
|
|
}
|