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.
Merge pull request #1 from imstevez/master
merge tag 'v0.2.0' of 'ipfs/go-ipfs-files'
- Loading branch information
Showing
22 changed files
with
368 additions
and
103 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,19 @@ | ||
//go:build darwin || linux || netbsd || openbsd || freebsd || dragonfly | ||
|
||
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,33 @@ | ||
//go:build darwin || linux || netbsd || openbsd | ||
|
||
package files | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriteToInvalidPaths(t *testing.T) { | ||
tmppath, err := os.MkdirTemp("", "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,45 @@ | ||
//go: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,35 @@ | ||
//go:build windows | ||
|
||
package files | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriteToInvalidPaths(t *testing.T) { | ||
tmppath, err := os.MkdirTemp("", "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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//+build !windows | ||
//go:build !windows | ||
|
||
package files | ||
|
||
|
Oops, something went wrong.