diff --git a/filesystem/iso9660/finalize.go b/filesystem/iso9660/finalize.go index 2dc057c2..aaa32629 100644 --- a/filesystem/iso9660/finalize.go +++ b/filesystem/iso9660/finalize.go @@ -908,27 +908,28 @@ func createPathTable(fi []*finalizeFileInfo) *pathTable { } func walkTree(workspace string) ([]*finalizeFileInfo, map[string]*finalizeFileInfo, error) { - cwd, err := os.Getwd() - if err != nil { - return nil, nil, fmt.Errorf("could not get pwd: %v", err) - } - // make everything relative to the workspace - _ = os.Chdir(workspace) var ( dirList = make(map[string]*finalizeFileInfo) fileList = make([]*finalizeFileInfo, 0) entry *finalizeFileInfo serial uint64 ) - err = filepath.Walk(".", func(fp string, fi os.FileInfo, err error) error { + err := filepath.WalkDir(workspace, func(actualPath string, d fs.DirEntry, err error) error { if err != nil { - return fmt.Errorf("error walking path %s: %v", fp, err) + return fmt.Errorf("error walking path %s: %v", actualPath, err) + } + fp := strings.TrimPrefix(actualPath, workspace) + fp = strings.TrimPrefix(fp, string(filepath.Separator)) + if fp == "" { + fp = "." } - name := fi.Name() + name := d.Name() _, extension := calculateShortnameExtension(name) - actualPath := path.Join(workspace, fp) - + fi, err := d.Info() + if err != nil { + return fmt.Errorf("could not get file info for %s: %v", fp, err) + } entry, err = finalizeFileInfoFromFile(fp, actualPath, fi) if err != nil { return err @@ -960,8 +961,6 @@ func walkTree(workspace string) ([]*finalizeFileInfo, map[string]*finalizeFileIn if err != nil { return nil, nil, err } - // reset the workspace - _ = os.Chdir(cwd) return fileList, dirList, nil } diff --git a/filesystem/squashfs/finalize.go b/filesystem/squashfs/finalize.go index 1b5e8e92..7c99e4f2 100644 --- a/filesystem/squashfs/finalize.go +++ b/filesystem/squashfs/finalize.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "fmt" "io" + iofs "io/fs" "os" "path" "path/filepath" @@ -415,21 +416,24 @@ func finalizeFragment(buf []byte, to util.File, toOffset int64, c Compressor) (r // because the inode data is different. // The first entry in the return always will be the root func walkTree(workspace string) ([]*finalizeFileInfo, error) { - cwd, err := os.Getwd() - if err != nil { - return nil, fmt.Errorf("could not get pwd: %v", err) - } - // make everything relative to the workspace - _ = os.Chdir(workspace) dirMap := make(map[string]*finalizeFileInfo) fileList := make([]*finalizeFileInfo, 0) var entry *finalizeFileInfo - _ = filepath.Walk(".", func(fp string, fi os.FileInfo, err error) error { + err := filepath.WalkDir(workspace, func(actualPath string, d iofs.DirEntry, err error) error { if err != nil { return err } + fp := strings.TrimPrefix(actualPath, workspace) + fp = strings.TrimPrefix(fp, string(filepath.Separator)) + if fp == "" { + fp = "." + } isRoot := fp == "." - name := fi.Name() + name := d.Name() + fi, err := d.Info() + if err != nil { + return fmt.Errorf("could not get file info for %s: %v", fp, err) + } m := fi.Mode() var fType fileType switch { @@ -448,7 +452,7 @@ func walkTree(workspace string) ([]*finalizeFileInfo, error) { default: fType = fileRegular } - xattrNames, err := xattr.List(fp) + xattrNames, err := xattr.List(actualPath) if err != nil { return fmt.Errorf("unable to list xattrs for %s: %v", fp, err) } @@ -495,8 +499,9 @@ func walkTree(workspace string) ([]*finalizeFileInfo, error) { fileList = append(fileList, entry) return nil }) - // reset the workspace - _ = os.Chdir(cwd) + if err != nil { + return nil, err + } return fileList, nil }