From 6728db0d0d37ec8ce383788339aafbd69ba6a3eb Mon Sep 17 00:00:00 2001 From: Kapil Sharma Date: Tue, 9 Apr 2024 17:44:56 +0530 Subject: [PATCH] closing file before removing it Signed-off-by: Kapil Sharma --- events/syscall/update_package_repository.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/events/syscall/update_package_repository.go b/events/syscall/update_package_repository.go index f8cd2541..46979517 100644 --- a/events/syscall/update_package_repository.go +++ b/events/syscall/update_package_repository.go @@ -11,7 +11,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ - package syscall import ( @@ -30,12 +29,15 @@ func UpdatePackageRepository(h events.Helper) error { // Check if the file exists if _, err := os.Stat(path); err != nil { - // If the file doesn't exist, create it - _, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0755)) + // If the file doesn't exist, create it and open it in write only mode + file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0755)) if err != nil { return err } - defer os.Remove(path) + defer file.Close() + // Remove file after closing it to free file occupied space + os.Remove(path) + } else { // If the file exists, open it for writing file, err := os.OpenFile(path, os.O_WRONLY, os.FileMode(0755))