Skip to content

Commit

Permalink
fixing file perms on file write
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Levinson committed Mar 7, 2018
1 parent d446535 commit 29651c2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
5 changes: 4 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ func (c *Compiler) writeScript() {
miniFinal := miniVersion.Bytes()
c.Logger.Debugf("Original Size: %d bytes", len(data))
c.Logger.Debugf("Minified Size: %d bytes", len(miniFinal))
engine.LocalFileCreate(entryFile, miniFinal)
err = ioutil.WriteFile(entryFile, miniFinal, 0644)
if err != nil {
c.Logger.Fatalf("OS Error: %s", err.Error())
}
vm.AssetFiles = append(vm.AssetFiles, entryFile)
}
}
Expand Down
15 changes: 11 additions & 4 deletions engine/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)

Expand Down Expand Up @@ -70,11 +71,17 @@ func LocalFileDelete(path string) error {
return errors.New("The file dosn't exist to delete")
}

func LocalFileCreate(path string, bytes []byte) error {
func LocalFileCreate(path string, bytes []byte, perms string) error {
if LocalFileExists(path) {
return errors.New("The file to create already exists so we won't overwite it")
}
err := ioutil.WriteFile(path, bytes, 0700)
var p os.FileMode
pInt, err := strconv.Atoi(perms)
if err != nil {
return err
}
p = os.FileMode(uint32(pInt))
err = ioutil.WriteFile(path, bytes, p)
if err != nil {
return err
}
Expand All @@ -97,7 +104,7 @@ func LocalFileAppendBytes(filename string, bytes []byte) error {
file.Close()
return nil
}
err := LocalFileCreate(filename, bytes)
err := LocalFileCreate(filename, bytes, "0644")
if err != nil {
return err
}
Expand Down Expand Up @@ -190,7 +197,7 @@ func XorFiles(file1 string, file2 string, outPut string) error {
return err
}
dat3 := XorBytes(dat1[:], dat2[:])
err = LocalFileCreate(outPut, dat3[:])
err = LocalFileCreate(outPut, dat3[:], "0644")
if err != nil {
return err
}
Expand Down
21 changes: 16 additions & 5 deletions engine/file_vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package engine

import (
"os"
"strconv"
"strings"

"github.com/djherbis/times"
Expand Down Expand Up @@ -140,15 +142,19 @@ func (e *Engine) VMDeleteFile(call otto.FunctionCall) otto.Value {
}

func (e *Engine) VMWriteFile(call otto.FunctionCall) otto.Value {
filePath := call.Argument(0)
filePath, err := call.Argument(0).ToString()
if err != nil {
e.Logger.WithField("trace", "true").Errorf("Parameter parsing error: %s", err.Error())
return otto.FalseValue()
}
fileData := call.Argument(1)
fileBytes := e.ValueToByteSlice(fileData)
filePathAsString, err := filePath.Export()
fileMode, err := call.Argument(2).ToString()
if err != nil {
e.Logger.WithField("trace", "true").Errorf("Parameter parsing error: %s", err.Error())
return otto.FalseValue()
}
err = LocalFileCreate(filePathAsString.(string), fileBytes)
fileBytes := e.ValueToByteSlice(fileData)
err = LocalFileCreate(filePath, fileBytes, fileMode)
if err != nil {
e.Logger.WithField("trace", "true").Errorf("Error writing the file: %s", err.Error())
return otto.FalseValue()
Expand Down Expand Up @@ -190,7 +196,12 @@ func (e *Engine) VMCopyFile(call otto.FunctionCall) otto.Value {
e.Logger.WithField("trace", "true").Errorf("Error reading the file: %s", err.Error())
return otto.FalseValue()
}
err = LocalFileCreate(writePath, bytes)
filePerms, err := os.Stat(readPath)
if err != nil {
e.Logger.WithField("trace", "true").Errorf("OS Error: %s", err.Error())
return otto.FalseValue()
}
err = LocalFileCreate(writePath, bytes, strconv.Itoa(int(filePerms.Mode())))
if err != nil {
e.Logger.WithField("trace", "true").Errorf("Error writing the file: %s", err.Error())
return otto.FalseValue()
Expand Down
2 changes: 1 addition & 1 deletion gscript.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gscript

// Version defines the version of gscript
const Version = "v0.0.18"
const Version = "v0.0.19"
3 changes: 1 addition & 2 deletions spec/large_file.gs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ function BeforeDeploy() {
}

function Deploy() {
WriteFile("/Users/flint/Downloads/e301.tar", Asset("e200.tar"), "0644");
return true;
return WriteFile("Z:/Public/e200.tar", Asset("e200.tar"), "0644");
}

function AfterDeploy() {
Expand Down

0 comments on commit 29651c2

Please sign in to comment.