gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/vfs/filesystem_impl_util.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package vfs
    16  
    17  import (
    18  	"strings"
    19  
    20  	"gvisor.dev/gvisor/pkg/abi/linux"
    21  	"gvisor.dev/gvisor/pkg/hostarch"
    22  )
    23  
    24  // GenericParseMountOptions parses a comma-separated list of options of the
    25  // form "key" or "key=value", where neither key nor value contain commas, and
    26  // returns it as a map. If str contains duplicate keys, then the last value
    27  // wins. For example:
    28  //
    29  // str = "key0=value0,key1,key2=value2,key0=value3" -> map{'key0':'value3','key1':”,'key2':'value2'}
    30  //
    31  // GenericParseMountOptions is not appropriate if values may contain commas,
    32  // e.g. in the case of the mpol mount option for tmpfs(5).
    33  func GenericParseMountOptions(str string) map[string]string {
    34  	m := make(map[string]string)
    35  	for _, opt := range strings.Split(str, ",") {
    36  		if len(opt) > 0 {
    37  			res := strings.SplitN(opt, "=", 2)
    38  			if len(res) == 2 {
    39  				m[res[0]] = res[1]
    40  			} else {
    41  				m[opt] = ""
    42  			}
    43  		}
    44  	}
    45  	return m
    46  }
    47  
    48  // GenericStatFS returns a statfs struct filled with the common fields for a
    49  // general filesystem. This is analogous to Linux's fs/libfs.cs:simple_statfs().
    50  func GenericStatFS(fsMagic uint64) linux.Statfs {
    51  	return linux.Statfs{
    52  		Type:       fsMagic,
    53  		BlockSize:  hostarch.PageSize,
    54  		NameLength: linux.NAME_MAX,
    55  	}
    56  }