Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #26 from kinvolk/iaguis/assets-fix
Browse files Browse the repository at this point in the history
asset: resolve symlinks when copying assets
  • Loading branch information
alban committed Nov 30, 2015
2 parents f812461 + bc6b186 commit ba7b591
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion proj2aci/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,49 @@ func PrepareAssets(assets []string, rootfs string, placeholderMapping map[string
assetsToProcess := newAssets
newAssets = nil
for _, asset := range assetsToProcess {
splitAsset := filepath.SplitList(asset)
if len(splitAsset) != 2 {
return fmt.Errorf("Malformed asset option: '%v' - expected two absolute paths separated with %v", asset, listSeparator())
}
evalSrc, srcErr := evalPath(splitAsset[0])
if srcErr != nil {
return fmt.Errorf("Could not evaluate symlinks in source asset %q: %v", evalSrc, srcErr)
}
evalDest, destErr := evalPath(splitAsset[1])
asset = getAssetString(evalSrc, evalDest)
Debug("Processing asset:", asset)
if _, ok := processedAssets[asset]; ok {
Debug(" skipped")
continue
}
processedAssets[asset] = struct{}{}
additionalAssets, err := processAsset(asset, rootfs, placeholderMapping)
if destErr != nil {
evalDest, destErr = evalPath(evalDest)
if destErr != nil {
return fmt.Errorf("Could not evaluate symlinks in destination asset %q, even after it was copied to: %v", evalDest, destErr)
}
asset = getAssetString(evalSrc, evalDest)
}
if err != nil {
return err
}
processedAssets[asset] = struct{}{}
newAssets = append(newAssets, additionalAssets...)
}
}
return nil
}

func evalPath(path string) (string, error) {
symlinkDir := filepath.Dir(path)
symlinkBase := filepath.Base(path)
realSymlinkDir, err := filepath.EvalSymlinks(symlinkDir)
if err != nil {
return path, err
}
return filepath.Join(realSymlinkDir, symlinkBase), nil
}

// processAsset validates an asset, replaces placeholders with real
// paths and does the copying. It may return additional assets to be
// processed when asset is an executable or a library.
Expand Down

0 comments on commit ba7b591

Please sign in to comment.