From f3299b2c8c8b7c189a7682d3ce05c034a2532b05 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 21 Jun 2014 00:45:07 -0400 Subject: [PATCH] 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 --- core/logic/smn_filesystem.cpp | 31 ++++++++++++++++++++++++ core/logic/smn_filesystem.h | 45 +++++++++++++++++++++++++++++++++++ plugins/include/files.inc | 9 +++++++ 3 files changed, 85 insertions(+) create mode 100644 core/logic/smn_filesystem.h diff --git a/core/logic/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp index 26575acf..fb785c09 100644 --- a/core/logic/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -39,6 +39,11 @@ #include #include "common_logic.h" +#if defined PLATFORM_WINDOWS +#include +#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}, }; diff --git a/core/logic/smn_filesystem.h b/core/logic/smn_filesystem.h new file mode 100644 index 00000000..e40c03dc --- /dev/null +++ b/core/logic/smn_filesystem.h @@ -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 . + * + * 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 . + * + * 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_ diff --git a/plugins/include/files.inc b/plugins/include/files.inc index 4da32671..c25ed8ac 100644 --- a/plugins/include/files.inc +++ b/plugins/include/files.inc @@ -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. *