1*105f6285SAndroid Build Coastguard Worker// Copyright 2020 Google LLC 2*105f6285SAndroid Build Coastguard Worker// 3*105f6285SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*105f6285SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*105f6285SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*105f6285SAndroid Build Coastguard Worker// 7*105f6285SAndroid Build Coastguard Worker// https://www.apache.org/licenses/LICENSE-2.0 8*105f6285SAndroid Build Coastguard Worker// 9*105f6285SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*105f6285SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*105f6285SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*105f6285SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*105f6285SAndroid Build Coastguard Worker// limitations under the License. 14*105f6285SAndroid Build Coastguard Worker 15*105f6285SAndroid Build Coastguard Workerpackage mount 16*105f6285SAndroid Build Coastguard Worker 17*105f6285SAndroid Build Coastguard Workerimport ( 18*105f6285SAndroid Build Coastguard Worker "bufio" 19*105f6285SAndroid Build Coastguard Worker "io" 20*105f6285SAndroid Build Coastguard Worker "os" 21*105f6285SAndroid Build Coastguard Worker "strings" 22*105f6285SAndroid Build Coastguard Worker "syscall" 23*105f6285SAndroid Build Coastguard Worker) 24*105f6285SAndroid Build Coastguard Worker 25*105f6285SAndroid Build Coastguard Workertype systemMounter struct { 26*105f6285SAndroid Build Coastguard Worker} 27*105f6285SAndroid Build Coastguard Worker 28*105f6285SAndroid Build Coastguard Workerfunc NewSystemMounter() *systemMounter { 29*105f6285SAndroid Build Coastguard Worker var f systemMounter 30*105f6285SAndroid Build Coastguard Worker return &f 31*105f6285SAndroid Build Coastguard Worker} 32*105f6285SAndroid Build Coastguard Worker 33*105f6285SAndroid Build Coastguard Workerfunc (f *systemMounter) Mount(source string, target string, fstype string, flags uintptr, data string) error { 34*105f6285SAndroid Build Coastguard Worker return syscall.Mount(source, target, fstype, flags, data) 35*105f6285SAndroid Build Coastguard Worker} 36*105f6285SAndroid Build Coastguard Worker 37*105f6285SAndroid Build Coastguard Workerfunc (f *systemMounter) Unmount(target string, flags int) error { 38*105f6285SAndroid Build Coastguard Worker return syscall.Unmount(target, flags) 39*105f6285SAndroid Build Coastguard Worker} 40*105f6285SAndroid Build Coastguard Worker 41*105f6285SAndroid Build Coastguard Workerfunc (f *systemMounter) List() ([]string, error) { 42*105f6285SAndroid Build Coastguard Worker mountsFile, err := os.Open("/proc/mounts") 43*105f6285SAndroid Build Coastguard Worker if err != nil { 44*105f6285SAndroid Build Coastguard Worker return nil, err 45*105f6285SAndroid Build Coastguard Worker } 46*105f6285SAndroid Build Coastguard Worker defer mountsFile.Close() 47*105f6285SAndroid Build Coastguard Worker mounts, err := f.parseMounts(mountsFile) 48*105f6285SAndroid Build Coastguard Worker if err != nil { 49*105f6285SAndroid Build Coastguard Worker return nil, err 50*105f6285SAndroid Build Coastguard Worker } 51*105f6285SAndroid Build Coastguard Worker var mountList []string 52*105f6285SAndroid Build Coastguard Worker for _, mount := range mounts { 53*105f6285SAndroid Build Coastguard Worker mountList = append(mountList, mount.Path) 54*105f6285SAndroid Build Coastguard Worker } 55*105f6285SAndroid Build Coastguard Worker return mountList, err 56*105f6285SAndroid Build Coastguard Worker} 57*105f6285SAndroid Build Coastguard Worker 58*105f6285SAndroid Build Coastguard Workertype Mount struct { 59*105f6285SAndroid Build Coastguard Worker Device string 60*105f6285SAndroid Build Coastguard Worker Path string 61*105f6285SAndroid Build Coastguard Worker Type string 62*105f6285SAndroid Build Coastguard Worker Opts string 63*105f6285SAndroid Build Coastguard Worker} 64*105f6285SAndroid Build Coastguard Worker 65*105f6285SAndroid Build Coastguard Workerfunc (f *systemMounter) parseMounts(mountSource io.Reader) ([]Mount, error) { 66*105f6285SAndroid Build Coastguard Worker var mounts []Mount 67*105f6285SAndroid Build Coastguard Worker scanner := bufio.NewScanner(mountSource) 68*105f6285SAndroid Build Coastguard Worker for scanner.Scan() { 69*105f6285SAndroid Build Coastguard Worker line := scanner.Text() 70*105f6285SAndroid Build Coastguard Worker fields := strings.Fields(line) 71*105f6285SAndroid Build Coastguard Worker mount := Mount{ 72*105f6285SAndroid Build Coastguard Worker Device: fields[0], 73*105f6285SAndroid Build Coastguard Worker Path: fields[1], 74*105f6285SAndroid Build Coastguard Worker Type: fields[2], 75*105f6285SAndroid Build Coastguard Worker Opts: fields[3], 76*105f6285SAndroid Build Coastguard Worker } 77*105f6285SAndroid Build Coastguard Worker mounts = append(mounts, mount) 78*105f6285SAndroid Build Coastguard Worker } 79*105f6285SAndroid Build Coastguard Worker return mounts, scanner.Err() 80*105f6285SAndroid Build Coastguard Worker} 81