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
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
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>
|
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
#include "QF/console.h"
|
2003-02-14 22:36:10 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-08-29 02:12:57 +00:00
|
|
|
#include "QF/sys.h"
|
2001-07-22 19:03:26 +00:00
|
|
|
#include "QF/cvar.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.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"
|
2001-07-22 19:03:26 +00:00
|
|
|
#include "crudefile.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
char mode; // 'r' for read, 'w' for write
|
|
|
|
} cf_file_t;
|
2001-08-29 02:12:57 +00:00
|
|
|
|
|
|
|
cf_file_t *cf_filep;
|
|
|
|
cvar_t *crudefile_quota;
|
|
|
|
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
|
|
|
|
writing, or if if mode == 'w' and the file's already open at all.
|
|
|
|
*/
|
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;
|
2001-08-29 02:12:57 +00:00
|
|
|
if (mode == 'r' && cf_filep[i].mode == 'w' &&
|
|
|
|
strequal (path, cf_filep[i].path))
|
2001-07-22 19:03:26 +00:00
|
|
|
return 1;
|
2001-08-29 02:12:57 +00:00
|
|
|
if (mode == 'w' && 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))) {
|
2003-02-14 22:36:10 +00:00
|
|
|
cf_cursize += CF_GetFileSize (va ("%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 ();
|
2001-08-29 02:12:57 +00:00
|
|
|
crudefile_quota = Cvar_Get ("crudefile_quota", "-1", CVAR_ROM, NULL,
|
|
|
|
"Maximum space available to the Crude File "
|
|
|
|
"system, -1 to totally disable file writing");
|
2001-07-22 19:03:26 +00:00
|
|
|
cf_maxsize = crudefile_quota->int_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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) {
|
2001-08-29 02:12:57 +00:00
|
|
|
Con_DPrintf ("Warning: closing Crude File %d left 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(strequal(mode, "w") || strequal(mode, "r"))) {
|
|
|
|
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
|
2003-03-21 21:25:44 +00:00
|
|
|
*j = tolower (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;
|
|
|
|
|
2003-03-21 21:25:44 +00:00
|
|
|
if (!CF_ValidDesc (desc) || cf_filep[desc].mode != 'w' || 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;
|
|
|
|
}
|