forked from containers/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request containers#236 from isimluk/extract-idmap-parser
Refactor: Extract ParseIDMap func to idtools package
- Loading branch information
Showing
3 changed files
with
69 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package idtools | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func nonDigitsToWhitespace(r rune) rune { | ||
if !strings.ContainsRune("0123456789", r) { | ||
return ' ' | ||
} | ||
return r | ||
} | ||
|
||
func parseTriple(spec []string) (container, host, size uint32, err error) { | ||
cid, err := strconv.ParseUint(spec[0], 10, 32) | ||
if err != nil { | ||
return 0, 0, 0, fmt.Errorf("error parsing id map value %q: %v", spec[0], err) | ||
} | ||
hid, err := strconv.ParseUint(spec[1], 10, 32) | ||
if err != nil { | ||
return 0, 0, 0, fmt.Errorf("error parsing id map value %q: %v", spec[1], err) | ||
} | ||
sz, err := strconv.ParseUint(spec[2], 10, 32) | ||
if err != nil { | ||
return 0, 0, 0, fmt.Errorf("error parsing id map value %q: %v", spec[2], err) | ||
} | ||
return uint32(cid), uint32(hid), uint32(sz), nil | ||
} | ||
|
||
// ParseIDMap parses idmap triples from string. | ||
func ParseIDMap(idMapSpec, mapSetting string) (idmap []IDMap, err error) { | ||
if len(idMapSpec) > 0 { | ||
idSpec := strings.Fields(strings.Map(nonDigitsToWhitespace, idMapSpec)) | ||
if len(idSpec)%3 != 0 { | ||
return nil, fmt.Errorf("error initializing ID mappings: %s setting is malformed", mapSetting) | ||
} | ||
for i := range idSpec { | ||
if i%3 != 0 { | ||
continue | ||
} | ||
cid, hid, size, err := parseTriple(idSpec[i : i+3]) | ||
if err != nil { | ||
return nil, fmt.Errorf("error initializing ID mappings: %s setting is malformed", mapSetting) | ||
} | ||
mapping := IDMap{ | ||
ContainerID: int(cid), | ||
HostID: int(hid), | ||
Size: int(size), | ||
} | ||
idmap = append(idmap, mapping) | ||
} | ||
} | ||
return idmap, nil | ||
} |
This file contains 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