ref: 74b31afd33033643f1915110453c93fed846eb2e
parent: 72401e717205cf5864beebdcbacaefcd39ce8465
author: qwx <qwx@sciops.net>
date: Sun Aug 15 18:15:58 EDT 2021
M ./fs.c M ./qw/cl_main.c M ./qw/common.c M ./qw/sys.c M ./qw/sys.h fs: try to create game dir if it's missing also remove some unused functions
--- a/fs.c
+++ b/fs.c
@@ -279,6 +279,43 @@
return n;
}
+static int
+mkdir(char *path)
+{
+ int d;
+
+ if(access(path, AEXIST) == 0)
+ return 0;
+ if((d = create(path, OREAD, DMDIR|0777)) < 0){
+ fprint(2, "Sys_mkdir:create: %r\n");
+ return -1;
+ }
+ close(d);
+ return 0;
+}
+
+static int
+mkpath(char *path)
+{
+ char *d;
+
+ d = path;
+ if(d == nil || *d == 0)
+ return -1;
+ if(*d == '/')
+ d++;
+ while(*d != 0){
+ if(*d == '/'){
+ *d = 0;
+ if(mkdir(path) < 0)
+ return -1;
+ *d = '/';
+ }
+ d++;
+ }
+ return mkdir(path);
+}
+
static void
closelmp(Biobuf *bf)
{
@@ -863,6 +900,7 @@
pakdir(va("%s/lib/quake/id1", home));
if(game != nil)
pakdir(va("%s/lib/quake/%s", home, game));
+ mkpath(fsdir);
free(home);
}
}
--- a/qw/cl_main.c
+++ b/qw/cl_main.c
@@ -1168,10 +1168,13 @@
void Host_WriteConfiguration (void)
{
FILE *f;
+ char *path;
if (host_initialized)
{
- f = fopen (va("%s/config.cfg",com_gamedir), "w");
+ path = va("%s/config.cfg", com_gamedir);
+ COM_CreatePath(path);
+ f = fopen(path, "w");
if (!f)
{
Con_Printf ("Couldn't write config.cfg.\n");
--- a/qw/common.c
+++ b/qw/common.c
@@ -1234,13 +1234,19 @@
void COM_CreatePath (char *path)
{
char *ofs;
-
- for (ofs = path+1 ; *ofs ; ofs++)
+
+ ofs = path;
+ if(ofs == nil || *ofs == 0)
+ return;
+ if(*ofs == '/')
+ ofs++;
+ for (; *ofs ; ofs++)
{
if (*ofs == '/')
{ // create the directory
*ofs = 0;
- Sys_mkdir (path);
+ if(Sys_mkdir(path) < 0)
+ return;
*ofs = '/';
}
}
--- a/qw/sys.c
+++ b/qw/sys.c
@@ -60,15 +60,19 @@
return *((int *)(bs+2+2+4+1+4+8+4+4)); /* mtime[4] */
}
-void
+int
Sys_mkdir(char *path)
{
int d;
- if((d = create(path, OREAD, DMDIR|0777)) < 0)
+ if(access(path, AEXIST) == 0)
+ return 0;
+ if((d = create(path, OREAD, DMDIR|0777)) < 0){
fprint(2, "Sys_mkdir:create: %r\n");
- else
- close(d);
+ return -1;
+ }
+ close(d);
+ return 0;
}
vlong
@@ -81,24 +85,6 @@
return -1;
}
return *((vlong *)(bs+2+2+4+1+4+8+4+4+4)); /* length[8] */
-}
-
-vlong
-Sys_FileOpenRead(char *path, int *fd)
-{
- if((*fd = open(path, OREAD)) < 0)
- return -1;
- return flen(*fd);
-}
-
-int
-Sys_FileOpenWrite(char *path)
-{
- int fd;
-
- if((fd = open(path, OREAD|OTRUNC)) < 0)
- sysfatal("Sys_FileOpenWrite:open: %r");
- return fd;
}
double
--- a/qw/sys.h
+++ b/qw/sys.h
@@ -7,10 +7,8 @@
};
extern int svonly;
-vlong Sys_FileOpenRead(char *, int *);
-int Sys_FileOpenWrite(char *);
int Sys_FileTime(char *);
-void Sys_mkdir(char *);
+int Sys_mkdir(char *);
vlong flen(int);
void Sys_Error(char *, ...);