fix pathing in cd_files copy to make sure directories make it into the cd root.

This commit is contained in:
Megan Marsh 2020-09-30 15:07:26 -07:00
parent e08e7734f5
commit 69fd5a1527
1 changed files with 21 additions and 7 deletions

View File

@ -236,12 +236,26 @@ func (s *StepCreateCD) AddFile(dst, src string) error {
return err return err
} }
// file is a directory, so we need to parse the filename into a path to
// dicard and a basename
discardPath, _ := filepath.Split(src)
// Add a directory and its subdirectories // Add a directory and its subdirectories
visit := func(pathname string, fi os.FileInfo, err error) error { visit := func(pathname string, fi os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
// Clean up pathing so that we preserve the base directory provided by
// the user but not the local pathing to that directory.
allDirs, base := filepath.Split(pathname)
intermediaryDirs := strings.Replace(allDirs, discardPath, "", 1)
dstPath := filepath.Join(dst, base)
if intermediaryDirs != "" {
dstPath = filepath.Join(dst, intermediaryDirs, base)
}
// add a file // add a file
if !fi.IsDir() { if !fi.IsDir() {
inputF, err := os.Open(pathname) inputF, err := os.Open(pathname)
@ -250,26 +264,26 @@ func (s *StepCreateCD) AddFile(dst, src string) error {
} }
defer inputF.Close() defer inputF.Close()
fileDst, err := os.Create(filepath.Join(dst, pathname)) fileDst, err := os.Create(dstPath)
if err != nil { if err != nil {
return fmt.Errorf("Error opening file %s on CD", src) return fmt.Errorf("Error opening file %s on CD: %s", dstPath, err)
} }
defer fileDst.Close() defer fileDst.Close()
nBytes, err := io.Copy(fileDst, inputF) nBytes, err := io.Copy(fileDst, inputF)
if err != nil { if err != nil {
return fmt.Errorf("Error copying %s to CD", src) return fmt.Errorf("Error copying %s to CD: %s", dstPath, err)
} }
s.filesAdded[pathname] = true s.filesAdded[dstPath] = true
log.Printf("Wrote %d bytes to %s", nBytes, pathname) log.Printf("Wrote %d bytes to %s", nBytes, dstPath)
return err return err
} }
if fi.Mode().IsDir() { if fi.Mode().IsDir() {
// create the directory on the CD, continue walk. // create the directory on the CD, continue walk.
err := os.Mkdir(filepath.Join(dst, pathname), fi.Mode()) err := os.MkdirAll(dstPath, fi.Mode())
if err != nil { if err != nil {
err = fmt.Errorf("error creating new directory %s: %s", err = fmt.Errorf("error creating new directory %s: %s",
filepath.Join(dst, pathname), err) dstPath, err)
} }
return err return err
} }