fix: normalize Windows backslash paths to forward slashes in image upload#362
Draft
fix: normalize Windows backslash paths to forward slashes in image upload#362
Conversation
…load Agent-Logs-Url: https://github.com/nutanix-cloud-native/packer-plugin-nutanix/sessions/b921ccdc-2d9f-48a3-8999-6d8a53c05c54 Co-authored-by: tuxtof <180613+tuxtof@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #361
On Windows, when uploading an image via
source_image_pathorcd_files, the full Windows path (in 8.3 notation, e.g.C:\USERS\J81BLO~1\image.iso) was appearing as the image name in Prism Central instead of just the filename.Root cause
There were two related issues in
driver.go:CreateImageURL—path.Split(disk.SourceImageURI)only recognises/as a path separator. Any URI that contains Windows-style backslashes (e.g.file:///C:\path\image.iso) would have the full path returned as the "filename".CreateImageFile—filepath.Split(filePath)is OS-specific and correctly handles\on a Windows binary. However, the rawfilePath(with backslashes) was then forwarded to the SDK'sImages.Upload. The SDK derives the image name internally viafilepath.Base(sourcePath). On Linux builds (cross-compiled binaries, WSL, MSYS2, Git Bash),filepath.Basedoes not recognise\as a separator and returns the entire path as the filename — causing the full Windows path to become the image name in Prism Central.Fix
Normalize all backslash separators to forward slashes before any path extraction or SDK call:
CreateImageURL: replacepath.Split(disk.SourceImageURI)withpath.Base(strings.ReplaceAll(…, "\\", "/")).CreateImageFile: computenormalizedPathwithstrings.ReplaceAll(filePath, "\\", "/"), extractfileusingpath.Base(normalizedPath), and passnormalizedPathtov4Client.Images.Uploadso the SDK's internalfilepath.Basecall also produces the correct basename. Windows accepts/as a path separator in all standard file-open calls, soos.Opencontinues to work correctly.path/filepathimport.Testing
go build ./...andgo test ./...pass cleanly.\, sostrings.ReplaceAllis a no-op for them.