Skip to content

Commit

Permalink
Merge pull request #143 from rubixchain/allen/fix/smart-contract-rena…
Browse files Browse the repository at this point in the history
…me-issue

Fix: Smart contract rename
  • Loading branch information
gklps authored May 3, 2024
2 parents 756b357 + ff5f296 commit 876b418
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions server/smart_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -99,7 +100,7 @@ func (s *Server) APIGenerateSmartContract(req *ensweb.Request) *ensweb.Result {
binaryCodeFile.Close()
binaryCodeDestFile.Close()

err = os.Rename(binaryCodeFile.Name(), binaryCodeDest)
err = moveFile(binaryCodeFile.Name(), binaryCodeDest)
if err != nil {
binaryCodeFile.Close()
s.log.Error("Generate smart contract failed, failed to move binary code file", "err", err)
Expand All @@ -125,7 +126,7 @@ func (s *Server) APIGenerateSmartContract(req *ensweb.Request) *ensweb.Result {
rawCodeFile.Close()
rawCodeDestFile.Close()

err = os.Rename(rawCodeFile.Name(), rawCodeDest)
err = moveFile(rawCodeFile.Name(), rawCodeDest)
if err != nil {
binaryCodeDestFile.Close()
rawCodeDestFile.Close()
Expand Down Expand Up @@ -154,7 +155,7 @@ func (s *Server) APIGenerateSmartContract(req *ensweb.Request) *ensweb.Result {
schemaFile.Close()
schemaDestFile.Close()

err = os.Rename(schemaFile.Name(), schemaDest)
err = moveFile(schemaFile.Name(), schemaDest)
if err != nil {
binaryCodeDestFile.Close()
rawCodeDestFile.Close()
Expand Down Expand Up @@ -196,6 +197,51 @@ func (s *Server) APIGenerateSmartContract(req *ensweb.Request) *ensweb.Result {
return s.BasicResponse(req, true, "Smart contract generated successfully", nil)
}

// moveFile tries to rename the file first; if it fails, it falls back to copying
func moveFile(src, dst string) error {
err := os.Rename(src, dst)
if err != nil {
if linkErr, ok := err.(*os.LinkError); ok {
fmt.Println("os.Rename failed, attempting to copy:", linkErr)

// Open the source file
sourceFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("error opening source file: %w", err)
}
defer sourceFile.Close()

// Create the destination file
destinationFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("error creating destination file: %w", err)
}
defer destinationFile.Close()

// Copy the contents
if _, err = io.Copy(destinationFile, sourceFile); err != nil {
return fmt.Errorf("error copying file: %w", err)
}

// Close the files explicitly before deleting
if err = sourceFile.Close(); err != nil {
return fmt.Errorf("error closing source file: %w", err)
}
if err = destinationFile.Close(); err != nil {
return fmt.Errorf("error closing destination file: %w", err)
}

// Delete the original file
if err = os.Remove(src); err != nil {
return fmt.Errorf("error removing original file: %w", err)
}
} else {
return fmt.Errorf("os.Rename error: %w", err)
}
}
return nil
}

// SmartContract godoc
// @Summary Fetch Smart Contract
// @Description This API will Fetch smart contract
Expand Down

0 comments on commit 876b418

Please sign in to comment.