From 3b4bd86a1de3fb1a9065024089fcfec2dae1da85 Mon Sep 17 00:00:00 2001
From: Mnikolenko Productengine <mnikolenko@productengine.com>
Date: Tue, 27 Oct 2020 16:46:31 +0200
Subject: [PATCH] SL-14182 remove old script asset file after saving changes
 and allow renaming files if destination file exists

---
 indra/llfilesystem/llfilesystem.cpp |  4 ++++
 indra/newview/llpreviewscript.cpp   | 14 +++++++++++---
 indra/newview/llpreviewscript.h     |  4 ++++
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/indra/llfilesystem/llfilesystem.cpp b/indra/llfilesystem/llfilesystem.cpp
index c6b20caa698..932ef2a9c63 100644
--- a/indra/llfilesystem/llfilesystem.cpp
+++ b/indra/llfilesystem/llfilesystem.cpp
@@ -99,12 +99,16 @@ bool LLFileSystem::renameFile(const LLUUID& old_file_id, const LLAssetType::ETyp
     new_file_id.toString(new_id_str);
     const std::string new_filename =  LLDiskCache::getInstance()->metaDataToFilepath(new_id_str, new_file_type, extra_info);
 
+    // Rename needs the new file to not exist.
+    LLFileSystem::removeFile(new_file_id, new_file_type);
+
     if (std::rename(old_filename.c_str(), new_filename.c_str()))
     {
         // We would like to return FALSE here indicating the operation
         // failed but the original code does not and doing so seems to
         // break a lot of things so we go with the flow...
         //return FALSE;
+        LL_WARNS() << "Failed to rename " << old_file_id << " to " << new_id_str << " reason: "  << strerror(errno) << LL_ENDL;
     }
 
     return TRUE;
diff --git a/indra/newview/llpreviewscript.cpp b/indra/newview/llpreviewscript.cpp
index eae6c28e35f..e92d85a7e8a 100644
--- a/indra/newview/llpreviewscript.cpp
+++ b/indra/newview/llpreviewscript.cpp
@@ -1693,8 +1693,11 @@ void LLPreviewLSL::saveIfNeeded(bool sync /*= true*/)
         {
             std::string buffer(mScriptEd->mEditor->getText());
 
+            LLUUID old_asset_id = inv_item->getAssetUUID().isNull() ? mScriptEd->getAssetID() : inv_item->getAssetUUID();
+
             LLResourceUploadInfo::ptr_t uploadInfo(std::make_shared<LLScriptAssetUpload>(mItemUUID, buffer, 
-                [](LLUUID itemId, LLUUID, LLUUID, LLSD response) {
+                [old_asset_id](LLUUID itemId, LLUUID, LLUUID, LLSD response) {
+                    LLFileSystem::removeFile(old_asset_id, LLAssetType::AT_LSL_TEXT);
                     LLPreviewLSL::finishedLSLUpload(itemId, response);
                 }));
 
@@ -1742,6 +1745,7 @@ void LLPreviewLSL::onLoadComplete(const LLUUID& asset_uuid, LLAssetType::EType t
 			}
 			preview->mScriptEd->setScriptName(script_name);
 			preview->mScriptEd->setEnableEditing(is_modifiable);
+            preview->mScriptEd->setAssetID(asset_uuid);
 			preview->mAssetStatus = PREVIEW_ASSET_LOADED;
 		}
 		else
@@ -1995,6 +1999,7 @@ void LLLiveLSLEditor::onLoadComplete(const LLUUID& asset_id,
 			instance->loadScriptText(asset_id, type);
 			instance->mScriptEd->setEnableEditing(TRUE);
 			instance->mAssetStatus = PREVIEW_ASSET_LOADED;
+            instance->mScriptEd->setAssetID(asset_id);
 		}
 		else
 		{
@@ -2174,6 +2179,7 @@ void LLLiveLSLEditor::finishLSLUpload(LLUUID itemId, LLUUID taskId, LLUUID newAs
     if (preview)
     {
         preview->mItem->setAssetUUID(newAssetId);
+        preview->mScriptEd->setAssetID(newAssetId);
 
         // Bytecode save completed
         if (response["compiled"])
@@ -2244,12 +2250,14 @@ void LLLiveLSLEditor::saveIfNeeded(bool sync /*= true*/)
     if (!url.empty())
     {
         std::string buffer(mScriptEd->mEditor->getText());
+        LLUUID old_asset_id = mScriptEd->getAssetID();
 
         LLResourceUploadInfo::ptr_t uploadInfo(std::make_shared<LLScriptAssetUpload>(mObjectUUID, mItemUUID, 
                 monoChecked() ? LLScriptAssetUpload::MONO : LLScriptAssetUpload::LSL2, 
                 isRunning, mScriptEd->getAssociatedExperience(), buffer, 
-                [isRunning](LLUUID itemId, LLUUID taskId, LLUUID newAssetId, LLSD response) { 
-                    LLLiveLSLEditor::finishLSLUpload(itemId, taskId, newAssetId, response, isRunning);
+                [isRunning, old_asset_id](LLUUID itemId, LLUUID taskId, LLUUID newAssetId, LLSD response) { 
+                        LLFileSystem::removeFile(old_asset_id, LLAssetType::AT_LSL_TEXT);
+                        LLLiveLSLEditor::finishLSLUpload(itemId, taskId, newAssetId, response, isRunning);
                 }));
 
         LLViewerAssetUpload::EnqueueInventoryUpload(url, uploadInfo);
diff --git a/indra/newview/llpreviewscript.h b/indra/newview/llpreviewscript.h
index 40ab3a3dbb9..4e192ecd04e 100644
--- a/indra/newview/llpreviewscript.h
+++ b/indra/newview/llpreviewscript.h
@@ -142,6 +142,9 @@ class LLScriptEdCore : public LLPanel
 
 	void 			setItemRemoved(bool script_removed){mScriptRemoved = script_removed;};
 
+    void 			setAssetID( const LLUUID& asset_id){ mAssetID = asset_id; };
+    LLUUID 			getAssetID() { return mAssetID; }
+
 private:
 	void		onBtnHelp();
 	void		onBtnDynamicHelp();
@@ -188,6 +191,7 @@ class LLScriptEdCore : public LLPanel
 	LLUUID			mAssociatedExperience;
 	BOOL			mScriptRemoved;
 	BOOL			mSaveDialogShown;
+    LLUUID          mAssetID;
 
 	LLScriptEdContainer* mContainer; // parent view
 
-- 
GitLab