github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/mount/libmount/parse.go (about) 1 /* 2 Copyright 2020 The OpenEBS Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package libmount 18 19 import ( 20 "errors" 21 "strings" 22 ) 23 24 var ( 25 ErrInvalidToken error = errors.New("invalid token error") 26 ) 27 28 type Parser interface { 29 Parse(string) (*Filesystem, error) 30 } 31 32 type mountInfoParser struct{} 33 type mountsParser struct{} 34 35 func NewMountsParser() Parser { 36 return mountsParser{} 37 } 38 39 func NewMountInfoParser() Parser { 40 return mountInfoParser{} 41 } 42 43 func NewParser(format MountTabFormat) Parser { 44 switch format { 45 case MntFmtMountInfo: 46 return NewMountInfoParser() 47 default: 48 return NewMountsParser() 49 } 50 } 51 52 // parse one line of mountinfo file 53 func (p mountInfoParser) Parse(line string) (*Filesystem, error) { 54 return nil, errors.New("not implemented") 55 } 56 57 // parse one line of mounts/{fs, m}tab file 58 func (p mountsParser) Parse(line string) (*Filesystem, error) { 59 fs := NewFilesystem() 60 tokens := strings.Fields(line) 61 // [1] source 62 p.parseSourceToken(fs, tokens[0]) 63 64 // [2] target 65 fs.SetTarget(tokens[1]) 66 67 // [3] FS type 68 fs.SetFsType(tokens[2]) 69 70 // TODO: parse further columns 71 72 return fs, nil 73 } 74 75 func (p mountsParser) parseSourceToken(fs *Filesystem, token string) { 76 tag, val, err := parseTagString(token) 77 if err != nil && isValidTagName(tag) { 78 fs.SetTag(tag, val) 79 } 80 fs.SetSource(token) 81 } 82 83 func parseTagString(token string) (string, string, error) { 84 tv := strings.SplitN(token, "=", 2) 85 if len(tv) < 2 { 86 return "", "", ErrInvalidToken 87 } 88 return tv[0], tv[1], nil 89 } 90 91 func isValidTagName(tag string) bool { 92 return (tag == "ID" || 93 tag == "UUID" || 94 tag == "LABEL" || 95 tag == "PARTUUID" || 96 tag == "PARTLABEL") 97 }