Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UX][GitHub] azcopy cp fails if the destination path is at the root of the file system (i.e. /) #2588

Merged
merged 10 commits into from
Mar 13, 2024
16 changes: 12 additions & 4 deletions common/writeThoughFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package common

import (
"fmt"
"os"
"regexp"
"strings"
Expand All @@ -44,19 +45,26 @@ var RootShareRegex = regexp.MustCompile(`(^\/\/[^\/]*\/?$)`)

func isRootPath(s string) bool {
shortParentDir := strings.ReplaceAll(ToShortPath(s), OS_PATH_SEPARATOR, AZCOPY_PATH_SEPARATOR_STRING)
return RootDriveRegex.MatchString(shortParentDir) ||
RootShareRegex.MatchString(shortParentDir) ||
return RootDriveRegex.MatchString(shortParentDir) ||
RootShareRegex.MatchString(shortParentDir) ||
strings.EqualFold(shortParentDir, "/")
}


func CreateParentDirectoryIfNotExist(destinationPath string, tracker FolderCreationTracker) error {
// If we're pointing at the root of a drive, don't try because it won't work.
if isRootPath(destinationPath) {
return nil
}

lastIndex := strings.LastIndex(destinationPath, DeterminePathSeparator(destinationPath))
pathSeparator := DeterminePathSeparator(destinationPath)
lastIndex := strings.LastIndex(destinationPath, pathSeparator)

// LastIndex() will return -1 if path separator was not found, we should handle this gracefully
// instead of allowing AzCopy to crash with an out-of-bounds error.
if lastIndex == -1 {
return fmt.Errorf("error: Path separator (%s) not found in destination path. On Linux, this may occur if the destination is the root file, such as '/'. If this is the case, please consider changing your destination.", pathSeparator)
}

directory := destinationPath[:lastIndex]
return CreateDirectoryIfNotExist(directory, tracker)
}
Expand Down
Loading