always check for an error first when walking a path

This commit is contained in:
Matthew Hooker 2017-01-26 16:32:21 -08:00
parent 67b8b70b8d
commit 22d1322bd9
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
7 changed files with 25 additions and 5 deletions

View File

@ -23,11 +23,14 @@ type artifact struct {
func NewArtifact(dir string) (packer.Artifact, error) {
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
}
return err
return nil
}
if err := filepath.Walk(dir, visit); err != nil {

View File

@ -28,6 +28,9 @@ type artifact struct {
func NewArtifact(dir string) (packer.Artifact, error) {
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
for _, unnecessaryFile := range unnecessaryFiles {
if unnecessary, _ := regexp.MatchString(unnecessaryFile, path); unnecessary {
return os.RemoveAll(path)
@ -38,7 +41,7 @@ func NewArtifact(dir string) (packer.Artifact, error) {
files = append(files, path)
}
return err
return nil
}
if err := filepath.Walk(dir, visit); err != nil {

View File

@ -444,11 +444,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Compile the artifact list
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
}
return err
return nil
}
if err := filepath.Walk(b.config.OutputDir, visit); err != nil {

View File

@ -23,6 +23,9 @@ type artifact struct {
func NewArtifact(dir string) (packer.Artifact, error) {
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
}

View File

@ -23,11 +23,13 @@ type localArtifact struct {
func NewLocalArtifact(dir string) (packer.Artifact, error) {
files := make([]string, 0, 5)
visit := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
}
return err
return nil
}
if err := filepath.Walk(dir, visit); err != nil {

View File

@ -114,6 +114,9 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
var crawlDirectoryFiles []string
crawlDirectory := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
crawlDirectoryFiles = append(crawlDirectoryFiles, path)
ui.Message(fmt.Sprintf("Adding file: %s", path))

View File

@ -21,6 +21,9 @@ const TestFixtures = "test-fixtures"
func getDirectory(path string) []string {
var result []string
walk := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && !strings.HasSuffix(path, "/") {
path = path + "/"
}