Skip to content

Commit

Permalink
Merge pull request containers#236 from isimluk/extract-idmap-parser
Browse files Browse the repository at this point in the history
Refactor: Extract ParseIDMap func to idtools package
  • Loading branch information
rhatdan authored Nov 12, 2018
2 parents 46a6d79 + 7b209d3 commit 5235ded
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 99 deletions.
52 changes: 2 additions & 50 deletions cmd/containers-storage/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/containers/storage"
"github.com/containers/storage/opts"
Expand Down Expand Up @@ -50,52 +48,6 @@ func paramIDMapping() (*storage.IDMappingOptions, error) {
if paramSubUIDMap == "" && paramSubGIDMap != "" {
paramSubUIDMap = paramSubGIDMap
}
nonDigitsToWhitespace := func(r rune) rune {
if strings.IndexRune("0123456789", r) == -1 {
return ' '
} else {
return r
}
}
parseTriple := func(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 := func(idMapSpec, mapType string) (idmap []idtools.IDMap, err error) {
if len(idMapSpec) > 0 {
idSpec := strings.Fields(strings.Map(nonDigitsToWhitespace, idMapSpec))
if len(idSpec)%3 != 0 {
return nil, fmt.Errorf("%s map is malformed", mapType)
}
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("%s map is malformed", mapType)
}
mapping := idtools.IDMap{
ContainerID: int(cid),
HostID: int(hid),
Size: int(size),
}
idmap = append(idmap, mapping)
}
}
return idmap, nil
}
if paramSubUIDMap != "" && paramSubGIDMap != "" {
mappings, err := idtools.NewIDMappings(paramSubUIDMap, paramSubGIDMap)
if err != nil {
Expand All @@ -104,11 +56,11 @@ func paramIDMapping() (*storage.IDMappingOptions, error) {
options.UIDMap = mappings.UIDs()
options.GIDMap = mappings.GIDs()
}
parsedUIDMap, err := parseIDMap(paramUIDMap, "uid")
parsedUIDMap, err := idtools.ParseIDMap(paramUIDMap, "uid")
if err != nil {
return nil, err
}
parsedGIDMap, err := parseIDMap(paramGIDMap, "gid")
parsedGIDMap, err := idtools.ParseIDMap(paramGIDMap, "gid")
if err != nil {
return nil, err
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/idtools/parser.go
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
}
60 changes: 11 additions & 49 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -3203,56 +3202,19 @@ func ReloadConfigurationFile(configFile string, storeOptions *StoreOptions) {
storeOptions.UIDMap = mappings.UIDs()
storeOptions.GIDMap = mappings.GIDs()
}
nonDigitsToWhitespace := func(r rune) rune {
if strings.IndexRune("0123456789", r) == -1 {
return ' '
} else {
return r
}
}
parseTriple := func(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

uidmap, err := idtools.ParseIDMap(config.Storage.Options.RemapUIDs, "remap-uids")
if err != nil {
fmt.Print(err)
} else {
storeOptions.UIDMap = append(storeOptions.UIDMap, uidmap...)
}
parseIDMap := func(idMapSpec, mapSetting string) (idmap []idtools.IDMap) {
if len(idMapSpec) > 0 {
idSpec := strings.Fields(strings.Map(nonDigitsToWhitespace, idMapSpec))
if len(idSpec)%3 != 0 {
fmt.Printf("Error initializing ID mappings: %s setting is malformed.\n", mapSetting)
return nil
}
for i := range idSpec {
if i%3 != 0 {
continue
}
cid, hid, size, err := parseTriple(idSpec[i : i+3])
if err != nil {
fmt.Printf("Error initializing ID mappings: %s setting is malformed.\n", mapSetting)
return nil
}
mapping := idtools.IDMap{
ContainerID: int(cid),
HostID: int(hid),
Size: int(size),
}
idmap = append(idmap, mapping)
}
}
return idmap
gidmap, err := idtools.ParseIDMap(config.Storage.Options.RemapGIDs, "remap-gids")
if err != nil {
fmt.Print(err)
} else {
storeOptions.GIDMap = append(storeOptions.GIDMap, gidmap...)
}
storeOptions.UIDMap = append(storeOptions.UIDMap, parseIDMap(config.Storage.Options.RemapUIDs, "remap-uids")...)
storeOptions.GIDMap = append(storeOptions.GIDMap, parseIDMap(config.Storage.Options.RemapGIDs, "remap-gids")...)
if os.Getenv("STORAGE_DRIVER") != "" {
storeOptions.GraphDriverName = os.Getenv("STORAGE_DRIVER")
}
Expand Down

0 comments on commit 5235ded

Please sign in to comment.