Skip to content

Commit

Permalink
Add GetFilePermissions (#2177)
Browse files Browse the repository at this point in the history
Co-authored-by: Kenzzer <[email protected]>
  • Loading branch information
Kenzzer and Kenzzer authored Jun 8, 2024
1 parent 0e4b1ca commit 11d3cf9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
34 changes: 34 additions & 0 deletions core/logic/smn_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,39 @@ static cell_t sm_SetFilePermissions(IPluginContext *pContext, const cell_t *para
#endif
}

static cell_t sm_GetFilePermissions(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);

cell_t *mask;
pContext->LocalToPhysAddr(params[2], &mask);

#if defined PLATFORM_WINDOWS
struct _stat buffer;
cell_t valid = _stat(realpath, &buffer) == 0;

if ((buffer.st_mode & _S_IREAD) != 0) {
*mask |= (FPERM_U_READ|FPERM_G_READ|FPERM_O_READ)|(FPERM_U_EXEC|FPERM_G_EXEC|FPERM_O_EXEC);
}

if ((buffer.st_mode & _S_IWRITE) != 0) {
*mask |= (FPERM_U_WRITE|FPERM_G_WRITE|FPERM_O_WRITE);
}

return valid;
#else
struct stat buffer;
cell_t valid = stat(realpath, &buffer) == 0;

*mask = buffer.st_mode;
return valid;
#endif
}

static cell_t sm_CreateDirectory(IPluginContext *pContext, const cell_t *params)
{
char *name;
Expand Down Expand Up @@ -1228,6 +1261,7 @@ REGISTER_NATIVES(filesystem)
{"RemoveGameLogHook", sm_RemoveGameLogHook},
{"CreateDirectory", sm_CreateDirectory},
{"SetFilePermissions", sm_SetFilePermissions},
{"GetFilePermissions", sm_GetFilePermissions},

{"File.ReadLine", sm_ReadFileLine},
{"File.Read", sm_ReadFile},
Expand Down
9 changes: 9 additions & 0 deletions plugins/include/files.inc
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,15 @@ native bool CreateDirectory(const char[] path, int mode=FPERM_O_READ|FPERM_O_EXE
*/
native bool SetFilePermissions(const char[] path, int mode);

/**
* Retrieves a file or directories permissions.
*
* @param path Path to the file.
* @param mode Variable to store the permissions in.
* @return True on success, false otherwise.
*/
native bool GetFilePermissions(const char[] path, int &mode);

/**
* Returns a file timestamp as a unix timestamp.
*
Expand Down

0 comments on commit 11d3cf9

Please sign in to comment.