X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=fs.c;h=e21a58395b99b6c775903f96bdda2ce20e5f8234;hb=add1a1b0abc10b8e720d74aeac52ad8276ee9fe0;hp=775b97f4d975ac4848fbc5408d278272493866fb;hpb=cfee52a1ec9db338098789cae89ae5cf1f7a6fbf;p=xonotic%2Fdarkplaces.git diff --git a/fs.c b/fs.c index 775b97f4..e21a5839 100644 --- a/fs.c +++ b/fs.c @@ -37,6 +37,8 @@ # include # include # include +# include +# include #else # include # include @@ -65,10 +67,8 @@ # define lseek _lseeki64 #endif -#if _MSC_VER >= 1400 // suppress deprecated warnings -# include -# include +#if _MSC_VER >= 1400 # define read _read # define write _write # define close _close @@ -775,11 +775,7 @@ static pack_t *FS_LoadPackPK3FromFD (const char *packfile, int packhandle, qbool static pack_t *FS_LoadPackPK3 (const char *packfile) { int packhandle; -#if _MSC_VER >= 1400 - _sopen_s(&packhandle, packfile, O_RDONLY | O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE); -#else - packhandle = open (packfile, O_RDONLY | O_BINARY); -#endif + packhandle = FS_SysOpenFD (packfile, "rb", false); if (packhandle < 0) return NULL; return FS_LoadPackPK3FromFD(packfile, packhandle, false); @@ -949,11 +945,7 @@ static pack_t *FS_LoadPackPAK (const char *packfile) pack_t *pack; dpackfile_t *info; -#if _MSC_VER >= 1400 - _sopen_s(&packhandle, packfile, O_RDONLY | O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE); -#else - packhandle = open (packfile, O_RDONLY | O_BINARY); -#endif + packhandle = FS_SysOpenFD(packfile, "rb", false); if (packhandle < 0) return NULL; if(read (packhandle, (void *)&header, sizeof(header)) != sizeof(header)) @@ -1730,10 +1722,11 @@ void FS_Init_SelfPack (void) p = buf; while(COM_ParseToken_Console(&p)) { + size_t sz = strlen(com_token) + 1; // shut up clang if(i >= args_left) break; - q = (char *)Mem_Alloc(fs_mempool, strlen(com_token) + 1); - strlcpy(q, com_token, strlen(com_token) + 1); + q = (char *)Mem_Alloc(fs_mempool, sz); + strlcpy(q, com_token, sz); new_argv[com_argc + i] = q; ++i; } @@ -1890,11 +1883,8 @@ static int FS_ChooseUserDir(userdirmode_t userdirmode, char *userdir, size_t use // see if we can write to this path (note: won't create path) #ifdef WIN32 -# if _MSC_VER >= 1400 - _sopen_s(&fd, va(vabuf, sizeof(vabuf), "%s%s/config.cfg", userdir, gamedirname1), O_WRONLY | O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); // note: no O_TRUNC here! -# else - fd = open (va(vabuf, sizeof(vabuf), "%s%s/config.cfg", userdir, gamedirname1), O_WRONLY | O_CREAT, 0666); // note: no O_TRUNC here! -# endif + // no access() here, we must try to open the file for appending + fd = FS_SysOpenFD(va(vabuf, sizeof(vabuf), "%s%s/config.cfg", userdir, gamedirname1), "a", false); if(fd >= 0) close(fd); #else @@ -1957,6 +1947,7 @@ void FS_Init (void) if (split) { struct stat statresult; + char vabuf[1024]; // truncate to just after the .app/ split[5] = 0; // see if gamedir exists in Resources @@ -2118,9 +2109,10 @@ void FS_Shutdown (void) int FS_SysOpenFD(const char *filepath, const char *mode, qboolean nonblocking) { - int handle; + int handle = -1; int mod, opt; unsigned int ind; + qboolean dolock = false; // Parse the mode string switch (mode[0]) @@ -2151,6 +2143,9 @@ int FS_SysOpenFD(const char *filepath, const char *mode, qboolean nonblocking) case 'b': opt |= O_BINARY; break; + case 'l': + dolock = true; + break; default: Con_Printf ("FS_SysOpen(%s, %s): unknown character in mode (%c)\n", filepath, mode, mode[ind]); @@ -2160,11 +2155,29 @@ int FS_SysOpenFD(const char *filepath, const char *mode, qboolean nonblocking) if (nonblocking) opt |= O_NONBLOCK; -#if _MSC_VER >= 1400 - _sopen_s(&handle, filepath, mod | opt, _SH_DENYNO, _S_IREAD | _S_IWRITE); +#ifdef WIN32 +# if _MSC_VER >= 1400 + _sopen_s(&handle, filepath, mod | opt, (dolock ? ((mod == O_RDONLY) ? _SH_DENYRD : _SH_DENYRW) : _SH_DENYNO), _S_IREAD | _S_IWRITE); +# else + handle = _sopen (filepath, mod | opt, (dolock ? ((mod == O_RDONLY) ? _SH_DENYRD : _SH_DENYRW) : _SH_DENYNO), _S_IREAD | _S_IWRITE); +# endif #else handle = open (filepath, mod | opt, 0666); + if(handle >= 0 && dolock) + { + struct flock l; + l.l_type = ((mod == O_RDONLY) ? F_RDLCK : F_WRLCK); + l.l_whence = SEEK_SET; + l.l_start = 0; + l.l_len = 0; + if(fcntl(handle, F_SETLK, &l) == -1) + { + close(handle); + handle = -1; + } + } #endif + return handle; }