2001-07-22 19:03:26 +00:00
|
|
|
/*
|
|
|
|
sv_crudefile.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 2001 Adam Olsen
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-07-22 19:03:26 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CTYPE_H
|
|
|
|
# include <ctype.h>
|
|
|
|
#endif
|
2001-08-29 02:12:57 +00:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2001-07-22 19:03:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
2007-11-06 10:17:14 +00:00
|
|
|
#include "QF/dstring.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2007-11-06 10:17:14 +00:00
|
|
|
#include "QF/sys.h"
|
2003-02-14 22:36:10 +00:00
|
|
|
#include "QF/va.h"
|
2001-07-22 19:03:26 +00:00
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
#include "compat.h"
|
2020-06-21 14:15:17 +00:00
|
|
|
|
|
|
|
#include "qw/include/crudefile.h"
|
2001-07-22 19:03:26 +00:00
|
|
|
|
|
|
|
int cf_maxsize; // max combined file size (eg quota)
|
|
|
|
int cf_cursize; // current combined file size
|
2001-08-29 02:12:57 +00:00
|
|
|
|
2001-07-22 19:03:26 +00:00
|
|
|
typedef struct cf_file_s {
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2001-07-22 19:03:26 +00:00
|
|
|
char *path;
|
|
|
|
char *buf;
|
|
|
|
int size;
|
|
|
|
int writtento;
|
2004-04-12 12:20:26 +00:00
|
|
|
char mode; // 'r' for read, 'w' for write, 'a' for append
|
2001-07-22 19:03:26 +00:00
|
|
|
} cf_file_t;
|
2001-08-29 02:12:57 +00:00
|
|
|
|
|
|
|
cf_file_t *cf_filep;
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
int crudefile_quota;
|
|
|
|
static cvar_t crudefile_quota_cvar = {
|
|
|
|
.name = "crudefile_quota",
|
|
|
|
.description =
|
|
|
|
"Maximum space available to the Crude File system, -1 to totally "
|
|
|
|
"disable file writing",
|
|
|
|
.default_value = "-1",
|
|
|
|
.flags = CVAR_ROM,
|
|
|
|
.value = { .type = &cexpr_int, .value = &crudefile_quota },
|
|
|
|
};
|
2001-08-29 02:12:57 +00:00
|
|
|
int cf_filepcount; // elements in array
|
|
|
|
int cf_openfiles; // used elements
|
2001-07-22 19:03:26 +00:00
|
|
|
|
|
|
|
#define CF_DIR "cf/"
|
|
|
|
#define CF_MAXFILES 100
|
|
|
|
#define CF_BUFFERSIZE 256
|
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
|
2001-07-22 19:03:26 +00:00
|
|
|
/*
|
|
|
|
CF_ValidDesc
|
|
|
|
|
|
|
|
Returns 1 if the file descriptor is valid.
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2001-07-22 19:03:26 +00:00
|
|
|
CF_ValidDesc (int desc)
|
|
|
|
{
|
|
|
|
if (desc >= 0 && desc < cf_filepcount && cf_filep[desc].file)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_AlreadyOpen
|
|
|
|
|
|
|
|
Returns 1 if mode == 'r' and the file is already open for
|
2004-04-12 12:20:26 +00:00
|
|
|
writing, or if if mode == 'w' or 'a' and the file's already open at all.
|
2001-07-22 19:03:26 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2001-07-22 19:03:26 +00:00
|
|
|
CF_AlreadyOpen (const char * path, char mode)
|
|
|
|
{
|
|
|
|
int i;
|
2001-08-29 02:12:57 +00:00
|
|
|
|
2001-07-22 19:03:26 +00:00
|
|
|
for (i = 0; i < cf_filepcount; i++) {
|
|
|
|
if (!cf_filep[i].file)
|
|
|
|
continue;
|
2004-04-12 12:20:26 +00:00
|
|
|
if (mode == 'r' && (cf_filep[i].mode == 'w' || cf_filep[i].mode == 'a') &&
|
2001-08-29 02:12:57 +00:00
|
|
|
strequal (path, cf_filep[i].path))
|
2001-07-22 19:03:26 +00:00
|
|
|
return 1;
|
2004-04-12 12:20:26 +00:00
|
|
|
if ((mode == 'w' || mode == 'a')&& strequal (path, cf_filep[i].path))
|
2001-07-22 19:03:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_GetFileSize
|
|
|
|
|
|
|
|
Returns the size of the specified file
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2001-07-22 19:03:26 +00:00
|
|
|
CF_GetFileSize (const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (!stat (path, &buf))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return buf.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_BuildQuota
|
|
|
|
|
|
|
|
Calculates the currently used space
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
|
|
|
CF_BuildQuota (void)
|
2001-07-22 19:03:26 +00:00
|
|
|
{
|
2003-02-14 22:36:10 +00:00
|
|
|
static dstring_t *path;
|
2001-07-22 19:03:26 +00:00
|
|
|
struct dirent *i;
|
2001-08-29 02:12:57 +00:00
|
|
|
DIR *dir;
|
2001-07-22 19:03:26 +00:00
|
|
|
|
|
|
|
if (!path)
|
2003-02-14 22:36:10 +00:00
|
|
|
path = dstring_new ();
|
2003-05-23 17:17:01 +00:00
|
|
|
dsprintf (path, "%s/%s/%s", qfs_userpath, qfs_gamedir->dir.def, CF_DIR);
|
2001-07-22 19:03:26 +00:00
|
|
|
|
2003-02-14 22:36:10 +00:00
|
|
|
dir = opendir (path->str);
|
2001-07-22 19:03:26 +00:00
|
|
|
if (!dir)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cf_cursize = 0;
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
while ((i = readdir (dir))) {
|
2021-01-31 07:01:20 +00:00
|
|
|
cf_cursize += CF_GetFileSize (va (0, "%s/%s", path->str, i->d_name));
|
2001-07-22 19:03:26 +00:00
|
|
|
}
|
2001-08-29 09:27:30 +00:00
|
|
|
closedir (dir);
|
2001-07-22 19:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_Init
|
|
|
|
|
|
|
|
Ye ol' Init function :)
|
|
|
|
*/
|
|
|
|
void
|
2003-01-06 18:28:13 +00:00
|
|
|
CF_Init (void)
|
2001-07-22 19:03:26 +00:00
|
|
|
{
|
2003-03-21 21:25:44 +00:00
|
|
|
CF_BuildQuota ();
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Register (&crudefile_quota_cvar, 0, 0);
|
|
|
|
cf_maxsize = crudefile_quota;
|
2001-07-22 19:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_CloseAllFiles
|
|
|
|
|
|
|
|
Closes all open files, printing warnings if developer is on
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CF_CloseAllFiles ()
|
|
|
|
{
|
|
|
|
int i;
|
2001-08-29 02:12:57 +00:00
|
|
|
|
2001-07-22 19:03:26 +00:00
|
|
|
for (i = 0; i < cf_filepcount; i++)
|
|
|
|
if (cf_filep[i].file) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "Warning: closing Crude File %d left "
|
2010-11-23 05:09:30 +00:00
|
|
|
"over from last map\n", i);
|
2003-03-21 21:25:44 +00:00
|
|
|
CF_Close (i);
|
2001-07-22 19:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_Open
|
|
|
|
|
|
|
|
cfopen opens a file, either for reading or writing (not both).
|
|
|
|
returns a file descriptor >= 0 on success, < 0 on failure.
|
|
|
|
mode is either r or w.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
CF_Open (const char *path, const char *mode)
|
|
|
|
{
|
2003-02-14 22:36:10 +00:00
|
|
|
char *j;
|
|
|
|
dstring_t *fullpath = dstring_new ();
|
2001-08-29 02:12:57 +00:00
|
|
|
int desc, oldsize, i;
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2001-07-22 19:03:26 +00:00
|
|
|
|
|
|
|
if (cf_openfiles >= CF_MAXFILES) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for paths with ..
|
2003-03-21 21:25:44 +00:00
|
|
|
if (strequal (path, "..")
|
|
|
|
|| !strncmp (path, "../", 3)
|
|
|
|
|| strstr (path, "/../")
|
|
|
|
|| (strlen (path) >= 3
|
|
|
|
&& strequal (path + strlen (path) - 3, "/.."))) {
|
2001-07-22 19:03:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-12 11:25:53 +00:00
|
|
|
if (!(strequal(mode, "w") || strequal(mode, "r") || strequal(mode, "a"))) {
|
2001-07-22 19:03:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode[0] == 'w' && cf_maxsize < 0) { // can't even delete if quota < 0
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-05-23 17:17:01 +00:00
|
|
|
dsprintf (fullpath, "%s/%s/%s", qfs_gamedir->dir.def, CF_DIR, path);
|
2001-07-22 19:03:26 +00:00
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
j = fullpath->str + strlen (fullpath->str) - strlen (path);
|
2001-07-22 19:03:26 +00:00
|
|
|
for (i = 0; path[i]; i++, j++) // strcpy, but force lowercase
|
2012-08-18 12:42:49 +00:00
|
|
|
*j = tolower ((byte) path[i]);
|
2001-07-22 19:03:26 +00:00
|
|
|
*j = '\0';
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
if (CF_AlreadyOpen (fullpath->str, mode[0])) {
|
2003-02-14 22:36:10 +00:00
|
|
|
dstring_delete (fullpath);
|
2001-07-22 19:03:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode[0] == 'w')
|
2003-02-14 22:36:10 +00:00
|
|
|
oldsize = CF_GetFileSize (fullpath->str);
|
2001-07-22 19:03:26 +00:00
|
|
|
else
|
|
|
|
oldsize = 0;
|
|
|
|
|
2003-05-23 17:17:01 +00:00
|
|
|
file = QFS_Open (fullpath->str, mode);
|
2001-07-22 19:03:26 +00:00
|
|
|
if (file) {
|
|
|
|
if (cf_openfiles >= cf_filepcount) {
|
|
|
|
cf_filepcount++;
|
2003-03-21 21:25:44 +00:00
|
|
|
cf_filep = realloc (cf_filep, sizeof (cf_file_t) * cf_filepcount);
|
2001-07-22 19:03:26 +00:00
|
|
|
if (!cf_filep) {
|
|
|
|
Sys_Error ("CF_Open: memory allocation error!");
|
|
|
|
}
|
|
|
|
cf_filep[cf_filepcount - 1].file = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (desc = 0; cf_filep[desc].file; desc++)
|
|
|
|
;
|
2003-02-14 22:36:10 +00:00
|
|
|
cf_filep[desc].path = fullpath->str;
|
2001-07-22 19:03:26 +00:00
|
|
|
cf_filep[desc].file = file;
|
|
|
|
cf_filep[desc].buf = 0;
|
|
|
|
cf_filep[desc].size = 0;
|
|
|
|
cf_filep[desc].writtento = 0;
|
|
|
|
cf_filep[desc].mode = mode[0];
|
|
|
|
|
|
|
|
cf_cursize -= oldsize;
|
|
|
|
cf_openfiles++;
|
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_Close
|
|
|
|
|
|
|
|
cfclose closes a file descriptor. returns nothing. to prevent
|
|
|
|
leakage, all open files are closed on map load, and warnings are
|
|
|
|
printed if developer is set to 1.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CF_Close (int desc)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
if (!CF_ValidDesc (desc))
|
2001-07-22 19:03:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (cf_filep[desc].mode == 'w' && !cf_filep[desc].writtento)
|
2003-03-21 21:25:44 +00:00
|
|
|
unlink (cf_filep[desc].path);
|
2001-07-22 19:03:26 +00:00
|
|
|
|
|
|
|
path = cf_filep[desc].path;
|
|
|
|
|
|
|
|
Qclose (cf_filep[desc].file);
|
|
|
|
cf_filep[desc].file = 0;
|
2003-03-21 21:25:44 +00:00
|
|
|
free (cf_filep[desc].buf);
|
2001-07-22 19:03:26 +00:00
|
|
|
cf_openfiles--;
|
|
|
|
|
|
|
|
cf_cursize -= CF_GetFileSize (path);
|
2003-03-21 21:25:44 +00:00
|
|
|
free (path);
|
2001-07-22 19:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_Read
|
|
|
|
|
|
|
|
cfread returns a single string read in from the file. an empty
|
|
|
|
string either means an empty string or eof, use cfeof to check.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
CF_Read (int desc)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
if (!CF_ValidDesc (desc) || cf_filep[desc].mode != 'r') {
|
2001-07-22 19:03:26 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
int foo;
|
|
|
|
if (cf_filep[desc].size <= len) {
|
2001-08-29 02:12:57 +00:00
|
|
|
char *t = realloc (cf_filep[desc].buf, cf_filep[desc].size +
|
|
|
|
CF_BUFFERSIZE);
|
2001-07-22 19:03:26 +00:00
|
|
|
if (!t) {
|
|
|
|
Sys_Error ("CF_Read: memory allocation error!");
|
|
|
|
}
|
|
|
|
cf_filep[desc].buf = t;
|
|
|
|
cf_filep[desc].size += CF_BUFFERSIZE;
|
|
|
|
}
|
2003-03-21 21:25:44 +00:00
|
|
|
foo = Qgetc (cf_filep[desc].file);
|
2001-07-22 19:03:26 +00:00
|
|
|
if (foo == EOF)
|
|
|
|
foo = 0;
|
|
|
|
cf_filep[desc].buf[len] = (char) foo;
|
|
|
|
len++;
|
|
|
|
} while (cf_filep[desc].buf[len - 1]);
|
|
|
|
|
|
|
|
return cf_filep[desc].buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_Write
|
|
|
|
|
|
|
|
cfwrite writes a string to the file, including a trailing nul,
|
|
|
|
returning the number of characters written. It returns 0 if
|
|
|
|
there was an error in writing, such as if the quota would have
|
|
|
|
been exceeded.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
CF_Write (int desc, const char *buf) // should be const char *, but Qwrite isn't...
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
2004-04-12 12:20:26 +00:00
|
|
|
if (!CF_ValidDesc (desc) || !(cf_filep[desc].mode == 'w' || cf_filep[desc].mode == 'a') || cf_cursize >=
|
2001-08-29 02:12:57 +00:00
|
|
|
cf_maxsize) {
|
2001-07-22 19:03:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
len = strlen (buf) + 1;
|
2001-07-22 19:03:26 +00:00
|
|
|
if (len > cf_maxsize - cf_cursize) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
len = Qwrite (cf_filep[desc].file, buf, len);
|
2001-07-22 19:03:26 +00:00
|
|
|
if (len < 0)
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cf_cursize += len;
|
|
|
|
cf_filep[desc].writtento = 1;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_EOF
|
|
|
|
|
|
|
|
cfeof returns 1 if you're at the end of the file, 0 if not, and
|
|
|
|
-1 on a bad descriptor.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
CF_EOF (int desc)
|
|
|
|
{
|
2003-03-21 21:25:44 +00:00
|
|
|
if (!CF_ValidDesc (desc)) {
|
2001-07-22 19:03:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qeof (cf_filep[desc].file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CF_Quota
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
returns the number of characters left in the quota, or <= 0 if it's met or
|
|
|
|
(somehow) exceeded.
|
2001-07-22 19:03:26 +00:00
|
|
|
*/
|
2002-11-05 19:12:51 +00:00
|
|
|
int
|
2003-03-21 21:25:44 +00:00
|
|
|
CF_Quota ()
|
2001-07-22 19:03:26 +00:00
|
|
|
{
|
|
|
|
return cf_maxsize - cf_cursize;
|
|
|
|
}
|