From a580c8c5e5bd0a92d23004fade8a79e240def5d3 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Mon, 9 Mar 2015 00:10:26 +0100 Subject: [PATCH] VFS: Add SetFilePermissions native --- amxmodx/file.cpp | 38 ++++++++++++++++++++++++++++++++++++++ plugins/include/file.inc | 16 +++++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/amxmodx/file.cpp b/amxmodx/file.cpp index 1341851b..f64c7a72 100755 --- a/amxmodx/file.cpp +++ b/amxmodx/file.cpp @@ -1101,6 +1101,42 @@ static cell AMX_NATIVE_CALL GetFileTime(AMX *amx, cell *params) return static_cast(time_val); } +#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. */ + +// native bool:SetFilePermissions(const path[], int mode); +static cell SetFilePermissions(AMX *amx, cell *params) +{ + int length; + const char* realpath = build_pathname(get_amxstring(amx, params[1], 0, length)); + +#if defined PLATFORM_WINDOWS + + int mask = 0; + + if (params[2] & (FPERM_U_WRITE | FPERM_G_WRITE | FPERM_O_WRITE)) + { + mask |= _S_IWRITE; + } + + if (params[2] & (FPERM_U_READ | FPERM_G_READ | FPERM_O_READ | FPERM_U_EXEC | FPERM_G_EXEC | FPERM_O_EXEC)) + { + mask |= _S_IREAD; + } + + return _chmod(realpath, mask) == 0; +#elif + return chmod(realpath, params[2]) == 0; +#endif +} + AMX_NATIVE_INFO file_Natives[] = { {"read_dir", read_dir}, @@ -1147,6 +1183,8 @@ AMX_NATIVE_INFO file_Natives[] = {"LoadFileForMe", LoadFileForMe}, {"GetFileTime", GetFileTime}, + {"SetFilePermissions", SetFilePermissions}, + {NULL, NULL} }; diff --git a/plugins/include/file.inc b/plugins/include/file.inc index c617538a..9b73b874 100755 --- a/plugins/include/file.inc +++ b/plugins/include/file.inc @@ -59,7 +59,7 @@ enum FileTimeType #define BLOCK_BYTE 1 /** - * File permissions flags for use with mkdir(). + * File permissions flags for use with mkdir() and SetFilePermissions(). */ #define FPERM_U_READ 0x0100 /* User can read. */ #define FPERM_U_WRITE 0x0080 /* User can write. */ @@ -227,14 +227,14 @@ native file_size(const file[], flag = FSOPT_BYTES_COUNT, bool:use_valve_fs = fal * * @note Registered paths ID are (in priority order) : * GAME All paths related to current mod, including fallback - * Depending settings, it includes: _lv/_addon/_/_hd + * Depending settings, it includes: _lv/_addon/_/_hd * and itself * GAMECONFIG The default writable directory () * GAMEDOWNLOAD The download directory (_download) * GAME_FALLBACK All paths related to fallback game, same as GAME * DEFAULTGAME All paths related to the default game which is "valve", same as GAME * BASE The base path where server is installed - * + * * Note that some paths are non-writable. It includes all _* (expect _download) * and DEFAULTGAME. Any file inside a non-writable path will be ignored if you try to open * it in writing mode. @@ -556,3 +556,13 @@ native LoadFileForMe(const file[], buffer[], maxlength, &length = 0); * @return Returns a file timestamp as a unix timestamp */ native GetFileTime(const file[], FileTimeType:tmode); + +/** + * Changes a file or directories permissions. + * + * @param path Path to the file + * @param mode Permissions to set, see FPERM_* constants + * + * @return True on success, false otherwise + */ +native bool:SetFilePermissions(const path[], mode);