1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snappy
import (
"path/filepath"
"strings"
"launchpad.net/snappy/dirs"
)
// A SnapDataDir represents a single data directory for a version of a package
type SnapDataDir struct {
Base string
Name string
Origin string
Version string
}
// QualifiedName returns the filesystem directory name for this SnapDataDir
func (dd SnapDataDir) QualifiedName() string {
if dd.Origin != "" {
return dd.Name + "." + dd.Origin
}
return dd.Name
}
func data1(spec, basedir string) []SnapDataDir {
var snaps []SnapDataDir
var filterns bool
verglob := "*"
specns := "*"
// Note that "=" is not legal in a snap name or a snap version
idx := strings.IndexRune(spec, '=')
if idx > -1 {
verglob = spec[idx+1:]
spec = spec[:idx]
}
nameglob := spec + "*"
idx = strings.LastIndexAny(spec, ".")
if idx > -1 {
filterns = true
specns = spec[idx+1:]
spec = spec[:idx]
nameglob = spec + "." + specns
}
dirs, _ := filepath.Glob(filepath.Join(basedir, nameglob, verglob))
// “but, Chipaca”, I hear you say, “why are you doing all this all over
// again, when you could just use .Installed() on an appropriate repo,
// and getOriginFromYaml and all the other lovely tools we already
// have written?”
// To which I can only say: DataDirs finds all the data dirs on the
// system, not just those of packages that are installed. If you've
// removed a package its package.yaml is gone, its data is still there,
// and you want us to be able to clean that up.
for _, dir := range dirs {
version := filepath.Base(dir)
if version == "current" {
continue
}
name := filepath.Base(filepath.Dir(dir))
origin := ""
idx := strings.LastIndexAny(name, ".")
if idx > -1 {
origin = name[idx+1:]
name = name[:idx]
}
if filterns && specns != origin {
continue
}
if spec != "" && spec != name {
continue
}
snaps = append(snaps, SnapDataDir{
Base: basedir,
Name: name,
Origin: origin,
Version: version,
})
}
return snaps
}
// DataDirs returns the list of all SnapDataDirs in the system.
func DataDirs(spec string) []SnapDataDir {
return append(data1(spec, dirs.SnapDataHomeGlob), data1(spec, dirs.SnapDataDir)...)
}
|