github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap-update-ns/sorting.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package main 21 22 import ( 23 "strings" 24 25 "github.com/snapcore/snapd/osutil" 26 ) 27 28 // byOriginAndMagicDir allows sorting an array of entries by the source of mount 29 // entry (overname, layout, content) and lexically by mount point name. 30 // Automagically adds a trailing slash to paths. 31 type byOriginAndMagicDir []osutil.MountEntry 32 33 func (c byOriginAndMagicDir) Len() int { return len(c) } 34 func (c byOriginAndMagicDir) Swap(i, j int) { c[i], c[j] = c[j], c[i] } 35 func (c byOriginAndMagicDir) Less(i, j int) bool { 36 iMe := c[i] 37 jMe := c[j] 38 39 iOrigin := iMe.XSnapdOrigin() 40 jOrigin := jMe.XSnapdOrigin() 41 if iOrigin == "overname" && iOrigin != jOrigin { 42 // should ith element be created by 'overname' mapping, it is 43 // always sorted before jth element, if that one comes from 44 // layouts or content interface 45 return true 46 } 47 48 iDir := c[i].Dir 49 jDir := c[j].Dir 50 if !strings.HasSuffix(iDir, "/") { 51 iDir = iDir + "/" 52 } 53 if !strings.HasSuffix(jDir, "/") { 54 jDir = jDir + "/" 55 } 56 return iDir < jDir 57 }