forked from TRON-US/go-btfs-files
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(filewriter): cleanup writes (ipfs#43)
* chore(filewriter): cleanup writes
- Loading branch information
Showing
8 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//go:build darwin || linux || netbsd || openbsd | ||
// +build darwin linux netbsd openbsd | ||
|
||
package files | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//go:build darwin || linux || netbsd || openbsd | ||
// +build darwin linux netbsd openbsd | ||
|
||
package files | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriteToInvalidPaths(t *testing.T) { | ||
tmppath, err := ioutil.TempDir("", "files-test") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(tmppath) | ||
|
||
path := filepath.Join(tmppath, "output") | ||
|
||
// Check we can actually write to the output path before trying invalid entries. | ||
assert.NoError(t, WriteTo(NewMapDirectory(map[string]Node{ | ||
"valid-entry": NewBytesFile(nil), | ||
}), path)) | ||
os.RemoveAll(path) | ||
|
||
// Now try all invalid entry names | ||
for _, entryName := range []string{"", ".", "..", "/", "", "not/a/base/path"} { | ||
assert.Equal(t, ErrInvalidDirectoryEntry, WriteTo(NewMapDirectory(map[string]Node{ | ||
entryName: NewBytesFile(nil), | ||
}), filepath.Join(path))) | ||
os.RemoveAll(path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package files | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
var invalidChars = `<>:"/\|?*` + "\x00" | ||
|
||
var reservedNames = map[string]struct{}{ | ||
"CON": {}, | ||
"PRN": {}, | ||
"AUX": {}, | ||
"NUL": {}, | ||
"COM1": {}, | ||
"COM2": {}, | ||
"COM3": {}, | ||
"COM4": {}, | ||
"COM5": {}, | ||
"COM6": {}, | ||
"COM7": {}, | ||
"COM8": {}, | ||
"COM9": {}, | ||
"LPT1": {}, | ||
"LPT2": {}, | ||
"LPT3": {}, | ||
"LPT4": {}, | ||
"LPT5": {}, | ||
"LPT6": {}, | ||
"LPT7": {}, | ||
"LPT8": {}, | ||
"LPT9": {}, | ||
} | ||
|
||
func isValidFilename(filename string) bool { | ||
_, isReservedName := reservedNames[filename] | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package files | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriteToInvalidPaths(t *testing.T) { | ||
tmppath, err := ioutil.TempDir("", "files-test") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(tmppath) | ||
|
||
path := filepath.Join(tmppath, "output") | ||
|
||
// Check we can actually write to the output path before trying invalid entries. | ||
assert.NoError(t, WriteTo(NewMapDirectory(map[string]Node{ | ||
"valid-entry": NewBytesFile(nil), | ||
}), path)) | ||
os.RemoveAll(path) | ||
|
||
// Now try all invalid entry names | ||
for _, entryName := range []string{"", ".", "..", "/", "", "not/a/base/path", | ||
"<", ">", ":", "\"", "\\", "|", "?", "*", "\x00", | ||
"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"} { | ||
assert.Equal(t, ErrInvalidDirectoryEntry, WriteTo(NewMapDirectory(map[string]Node{ | ||
entryName: NewBytesFile(nil), | ||
}), filepath.Join(path))) | ||
os.RemoveAll(path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters