From 282248522fe5db9af4188b0aec22af1c460a6037 Mon Sep 17 00:00:00 2001 From: WeidiDeng Date: Wed, 19 Oct 2022 08:06:36 +0800 Subject: [PATCH] Ignore empty root dir name; return walk error (#355) * ignore empty root dir name when compressing file fix [350](https://github.com/mholt/archiver/issues/350) * check return err from filepath.Walk --- archiver.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/archiver.go b/archiver.go index 35cdc3d3..73ec00d4 100644 --- a/archiver.go +++ b/archiver.go @@ -66,7 +66,7 @@ func (f File) Stat() (fs.FileInfo, error) { return f.FileInfo, nil } func FilesFromDisk(options *FromDiskOptions, filenames map[string]string) ([]File, error) { var files []File for rootOnDisk, rootInArchive := range filenames { - filepath.WalkDir(rootOnDisk, func(filename string, d fs.DirEntry, err error) error { + walkErr := filepath.WalkDir(rootOnDisk, func(filename string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -77,6 +77,10 @@ func FilesFromDisk(options *FromDiskOptions, filenames map[string]string) ([]Fil } nameInArchive := nameOnDiskToNameInArchive(filename, rootOnDisk, rootInArchive) + // this is the root folder and we are adding its contents to target rootInArchive + if info.IsDir() && nameInArchive == "" { + return nil + } // handle symbolic links var linkTarget string @@ -117,6 +121,9 @@ func FilesFromDisk(options *FromDiskOptions, filenames map[string]string) ([]Fil files = append(files, file) return nil }) + if walkErr != nil { + return nil, walkErr + } } return files, nil }