func GetPhysicalDriveFromPath()

in internal/utils.go [156:202]


func GetPhysicalDriveFromPath(ctx context.Context, path string, windows bool, exec commandlineexecutor.Execute) string {

	if path == "" {
		return "unknown"
	} else if windows {
		mapping := strings.Split(path, `:`)
		if len(mapping) <= 1 {
			log.Logger.Warn("Couldn't find windows drive associated with the physical path name.")
			return "unknown"
		}
		return mapping[0]
	}

	dir, filename := filepath.Split(path)
	filePath, filePathErr := CommandLineExecutorWrapper(ctx, "/bin/sh", fmt.Sprintf(" -c 'find %s -type f -iname \"%s\" -print'", dir, filename), exec)
	if filePathErr != nil {
		log.Logger.Warn(filePathErr)
		return "unknown"
	}

	physicalPathMount, physicalPathErr := CommandLineExecutorWrapper(ctx, "/bin/sh", fmt.Sprintf(" -c 'df --output=target %s| tail -n 1'", filePath), exec)
	if physicalPathErr != nil {
		log.Logger.Warn(physicalPathErr)
		return "unknown"
	}

	resultMount, mountErr := CommandLineExecutorWrapper(ctx, "/bin/sh", fmt.Sprintf(" -c ' mount |grep sd'"), exec)
	if mountErr != nil {
		log.Logger.Warn(mountErr)
		return "unknown"
	}

	allMounts := strings.TrimSuffix(resultMount, "\n")
	physicalDriveHelper := regexp.MustCompile(` `+physicalPathMount+` `).Split(allMounts, -1)

	physicalDrives := []string{}
	for i := 0; i < len(physicalDriveHelper)-1; i++ {
		splitStr := regexp.MustCompile("\n| |/").Split(physicalDriveHelper[i], -1)
		physicalDrives = append(physicalDrives, splitStr[len(splitStr)-2])
	}
	physicalDrive := strings.Join(physicalDrives, ", ")

	if physicalDrive == "" {
		return "unknown"
	}
	return physicalDrive
}