Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
os-specific create file function
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis committed Jan 10, 2022
1 parent a322a7f commit 004fb74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion filewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func WriteTo(nd Node, fpath string) error {
case *Symlink:
return os.Symlink(nd.Target, fpath)
case File:
f, err := os.OpenFile(fpath, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0666)
f, err := createNewFile(fpath)
defer f.Close()
if err != nil {
return err
Expand Down
10 changes: 9 additions & 1 deletion filewriter_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@

package files

import "strings"
import (
"os"
"strings"
"syscall"
)

var invalidChars = `/` + "\x00"

func isValidFilename(filename string) bool {
return !strings.ContainsAny(filename, invalidChars)
}

func createNewFile(path string) (*os.File, error) {
return os.OpenFile(path, os.O_EXCL|os.O_CREATE|os.O_WRONLY|syscall.O_NOFOLLOW, 0666)
}
9 changes: 8 additions & 1 deletion filewriter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package files

import "strings"
import (
"os"
"strings"
)

var invalidChars = `<>:"/\|?*` + "\x00"

Expand Down Expand Up @@ -37,3 +40,7 @@ func isValidFilename(filename string) bool {
return !strings.ContainsAny(filename, invalidChars) &&
!isReservedName
}

func createNewFile(path string) (*os.File, error) {
return os.OpenFile(path, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0666)
}

0 comments on commit 004fb74

Please sign in to comment.