gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap-update-ns/sorting.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2022 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  	"gitee.com/mysnapcore/mysnapd/osutil"
    26  )
    27  
    28  // byOvernameAndMountPoint allows sorting an array of entries by the
    29  // source of mount entry (whether it's an overname or not) and
    30  // lexically by mount point name.  Automagically adds a trailing slash
    31  // to paths.
    32  type byOvernameAndMountPoint []osutil.MountEntry
    33  
    34  func (c byOvernameAndMountPoint) Len() int      { return len(c) }
    35  func (c byOvernameAndMountPoint) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
    36  func (c byOvernameAndMountPoint) Less(i, j int) bool {
    37  	iMe := c[i]
    38  	jMe := c[j]
    39  
    40  	iOrigin := iMe.XSnapdOrigin()
    41  	jOrigin := jMe.XSnapdOrigin()
    42  	if iOrigin != jOrigin {
    43  		// overname entries should always be sorted first, before
    44  		// entries from layouts or content interface
    45  		if iOrigin == "overname" {
    46  			// overname ith element should be sorted before
    47  			return true
    48  		}
    49  		if jOrigin == "overname" {
    50  			// non-overname ith element should be sorted after
    51  			return false
    52  		}
    53  	}
    54  
    55  	iDir := c[i].Dir
    56  	jDir := c[j].Dir
    57  	if !strings.HasSuffix(iDir, "/") {
    58  		iDir = iDir + "/"
    59  	}
    60  	if !strings.HasSuffix(jDir, "/") {
    61  		jDir = jDir + "/"
    62  	}
    63  	return iDir < jDir
    64  }
    65  
    66  // byOriginAndMountPoint allows sorting an array of entries by the
    67  // source of mount entry (overname, other, layout) and lexically by
    68  // mount point name.  Automagically adds a trailing slash to paths.
    69  type byOriginAndMountPoint []osutil.MountEntry
    70  
    71  func (c byOriginAndMountPoint) Len() int      { return len(c) }
    72  func (c byOriginAndMountPoint) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
    73  func (c byOriginAndMountPoint) Less(i, j int) bool {
    74  	iMe := c[i]
    75  	jMe := c[j]
    76  
    77  	iOrigin := iMe.XSnapdOrigin()
    78  	jOrigin := jMe.XSnapdOrigin()
    79  	if iOrigin != jOrigin {
    80  		// overname entries should always be sorted first, before
    81  		// entries from layouts or content interface
    82  		if iOrigin == "overname" {
    83  			// overname ith element should be sorted before
    84  			return true
    85  		}
    86  		if jOrigin == "overname" {
    87  			// non-overname ith element should be sorted after
    88  			return false
    89  		}
    90  		// neither is overname, can be layout or nothing (implied
    91  		// content)
    92  		if iOrigin == "layout" {
    93  			return false
    94  		}
    95  		// i is not layout, so it must be nothing (implied content)
    96  		if jOrigin == "layout" {
    97  			return true
    98  		}
    99  	}
   100  
   101  	iDir := c[i].Dir
   102  	jDir := c[j].Dir
   103  	if !strings.HasSuffix(iDir, "/") {
   104  		iDir = iDir + "/"
   105  	}
   106  	if !strings.HasSuffix(jDir, "/") {
   107  		jDir = jDir + "/"
   108  	}
   109  	return iDir < jDir
   110  }