Added a new function SetFilePermissions to set permissions of a file. I'm not sure about the Windows implementation. See this feature request: https://bugs.alliedmods.net/show_bug.cgi?id=6152

This commit is contained in:
Andrew 2014-06-21 00:45:07 -04:00
parent 210759907d
commit f3299b2c8c
3 changed files with 85 additions and 0 deletions

View File

@ -39,6 +39,11 @@
#include <ITranslator.h>
#include "common_logic.h"
#if defined PLATFORM_WINDOWS
#include <io.h>
#include "smn_filesystem.h"
#endif
HandleType_t g_FileType;
HandleType_t g_DirType;
IChangeableForward *g_pLogHook = NULL;
@ -465,6 +470,31 @@ static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params)
#endif
}
static cell_t sm_SetFilePermissions(IPluginContext *pContext, const cell_t *params)
{
char *name;
char realpath[PLATFORM_MAX_PATH];
pContext->LocalToString(params[1], &name);
g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
#if defined PLATFORM_WINDOWS
int mask;
if (params[2] & FPERM_U_WRITE || params[2] & FPERM_G_WRITE || params[2] & FPERM_O_WRITE)
{
mask |= _S_IWRITE;
}
if (params[2] & FPERM_U_READ || params[2] & FPERM_G_READ || params[2] & FPERM_O_READ ||
params[2] & FPERM_U_EXEC || params[2] & FPERM_G_EXEC || params[2] & FPERM_O_EXEC)
{
mask |= _S_IREAD;
}
return _chmod(realpath, mask) == 0;
#else
return chmod(realpath, params[2]) == 0;
#endif
}
static cell_t sm_CreateDirectory(IPluginContext *pContext, const cell_t *params)
{
char *name;
@ -971,5 +1001,6 @@ REGISTER_NATIVES(filesystem)
{"AddGameLogHook", sm_AddGameLogHook},
{"RemoveGameLogHook", sm_RemoveGameLogHook},
{"CreateDirectory", sm_CreateDirectory},
{"SetFilePermissions", sm_SetFilePermissions},
{NULL, NULL},
};

View File

@ -0,0 +1,45 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#ifndef _INCLUDE_SOURCEMOD_FILESYSTEM_H_
#define _INCLUDE_SOURCEMOD_FILESYSTEM_H_
#define FPERM_U_READ 0x0100 /* User can read. */
#define FPERM_U_WRITE 0x0080 /* User can write. */
#define FPERM_U_EXEC 0x0040 /* User can exec. */
#define FPERM_G_READ 0x0020 /* Group can read. */
#define FPERM_G_WRITE 0x0010 /* Group can write. */
#define FPERM_G_EXEC 0x0008 /* Group can exec. */
#define FPERM_O_READ 0x0004 /* Anyone can read. */
#define FPERM_O_WRITE 0x0002 /* Anyone can write. */
#define FPERM_O_EXEC 0x0001 /* Anyone can exec. */
#endif //_INCLUDE_SOURCEMOD_FILESYSTEM_H_

View File

@ -376,6 +376,15 @@ native bool:RemoveDir(const String:path[]);
*/
native bool:CreateDirectory(const String:path[], mode);
/**
* Changes a file or directories permissions.
*
* @param path Path to the file.
* @param mode Permissions to set.
* @return True on success, false otherwise.
*/
native bool:SetFilePermissions(const String:path[], mode);
/**
* Returns a file timestamp as a unix timestamp.
*