int CLuaFileDefs::fileExists ( lua_State* luaVM ) { // bool fileExists ( string filePath ) SString strFile; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strFile ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { std::string strAbsPath; std::string strSubPath; // We have a resource argument? CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strAbsPath, &strSubPath ) ) { std::string strFilePath; // Does file exist? if ( pResource->GetFilePath ( strSubPath.c_str(), strFilePath ) ) { lua_pushboolean ( luaVM, true ); return 1; } else { lua_pushboolean ( luaVM, false ); return 1; } } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileCopy ( lua_State* luaVM ) { // bool fileCopy ( string filePath, string newFilePath, bool overwrite = false ) SString filePath; SString newFilePath; bool bOverwrite; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( filePath ); argStream.ReadString ( newFilePath ); argStream.ReadBool ( bOverwrite, false ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { std::string strCurAbsPath; std::string strNewAbsPath; std::string strCurMetaPath; std::string strNewMetaPath; // We have a resource arguments? CResource* pThisResource = pLuaMain->GetResource (); CResource* pCurResource = pThisResource; CResource* pNewResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( filePath, pCurResource, &strCurAbsPath, &strCurMetaPath ) && CResourceManager::ParseResourcePathInput ( newFilePath, pNewResource, &strNewAbsPath, &strNewMetaPath ) ) { // Do we have permissions? if ( ( pCurResource == pThisResource && pNewResource == pThisResource ) || m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, "ModifyOtherObjects", CAccessControlListRight::RIGHT_TYPE_GENERAL, false ) ) { std::string strCurFilePath; // Same as strCurAbsPath std::string strNewFilePath; // Same as strNewAbsPath // Does source file exist? if ( pCurResource->GetFilePath ( strCurMetaPath.c_str(), strCurFilePath ) ) { // Does destination file exist? if ( !bOverwrite && pNewResource->GetFilePath ( strNewMetaPath.c_str(), strNewFilePath ) ) { argStream.SetCustomError ( SString ( "Destination file already exists (%s)", *newFilePath ), "File error" ); } else { // Make sure the destination folder exists so we can copy the file MakeSureDirExists ( strNewAbsPath ); if ( FileCopy ( strCurAbsPath, strNewAbsPath ) ) { // If file copied return success lua_pushboolean ( luaVM, true ); return 1; } else { argStream.SetCustomError ( SString ( "Unable to copy %s to %s", *filePath, *newFilePath ), "File error" ); } } } else { argStream.SetCustomError ( SString ( "Source file doesn't exist (%s)", *filePath ), "File error" ); } } else { // Make permissions error message SString strWho; if ( pThisResource != pCurResource ) strWho += pCurResource->GetName (); if ( pThisResource != pNewResource ) { if ( !strWho.empty () ) strWho += " and "; strWho += pNewResource->GetName (); } argStream.SetCustomError ( SString ( "ModifyOtherObjects in ACL denied resource %s to access %s", pThisResource->GetName ().c_str (), *strWho ), "ACL issue" ); } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileRename ( lua_State* luaVM ) { // bool fileRename ( string filePath, string newFilePath ) SString strCurFile; SString strNewFile; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strCurFile ); argStream.ReadString ( strNewFile ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { std::string strCurPath; std::string strNewPath; std::string strCurSubPath; std::string strNewSubPath; // We have a resource arguments? CResource* pThisResource = pLuaMain->GetResource (); CResource* pCurResource = pThisResource; CResource* pNewResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strCurFile, pCurResource, &strCurPath, &strCurSubPath ) && CResourceManager::ParseResourcePathInput ( strNewFile, pNewResource, &strNewPath, &strNewSubPath ) ) { // Do we have permissions? if ( ( pCurResource == pThisResource && pNewResource == pThisResource ) || m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, "ModifyOtherObjects", CAccessControlListRight::RIGHT_TYPE_GENERAL, false ) ) { string strCurFilePath; string strNewFilePath; // Does `current` file path exist and `new` file path doesn't exist? if ( pCurResource->GetFilePath ( strCurSubPath.c_str(), strCurFilePath ) && !pNewResource->GetFilePath ( strNewSubPath.c_str(), strNewFilePath ) ) { // Make sure the destination folder exist so we can move the file MakeSureDirExists ( strNewPath.c_str () ); if ( FileRename ( strCurPath.c_str (), strNewPath.c_str () ) ) { // If file renamed/moved return success lua_pushboolean ( luaVM, true ); return 1; } else { // Output error m_pScriptDebugging->LogWarning ( luaVM, "%s; unable to rename/move file", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) ); } } else { // Output error m_pScriptDebugging->LogWarning ( luaVM, "%s failed; source file doesn't exist or destination file already exists", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) ); } } // Do we have not permissions to both - `current` and `new` resources? else if ( pThisResource != pCurResource && pThisResource != pNewResource ) m_pScriptDebugging->LogError ( luaVM, "%s failed; ModifyOtherObjects in ACL denied resource %s to access %s and %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), pThisResource->GetName ().c_str (), pCurResource->GetName ().c_str (), pNewResource->GetName ().c_str () ); // Do we have not permissions to `current` resource? else if ( pThisResource != pCurResource && pThisResource == pNewResource ) m_pScriptDebugging->LogError ( luaVM, "%s failed; ModifyOtherObjects in ACL denied resource %s to access %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), pThisResource->GetName ().c_str (), pCurResource->GetName ().c_str () ); // Do we have not permissions to `new` resource? else m_pScriptDebugging->LogError ( luaVM, "%s failed; ModifyOtherObjects in ACL denied resource %s to access %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), pThisResource->GetName ().c_str (), pNewResource->GetName ().c_str () ); } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }