-
Notifications
You must be signed in to change notification settings - Fork 424
/
cache_clean_linux.go
218 lines (195 loc) · 5.4 KB
/
cache_clean_linux.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package singularity
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/sylabs/singularity/internal/pkg/client/cache"
"github.com/sylabs/singularity/internal/pkg/sylog"
)
func cleanLibraryCache() error {
sylog.Debugf("Removing: %v", cache.Library())
err := os.RemoveAll(cache.Library())
if err != nil {
return fmt.Errorf("unable to clean library cache: %v", err)
}
return nil
}
func cleanOciCache() error {
sylog.Debugf("Removing: %v", cache.OciTemp())
err := os.RemoveAll(cache.OciTemp())
if err != nil {
return fmt.Errorf("unable to clean oci-tmp cache: %v", err)
}
return nil
}
func cleanBlobCache() error {
sylog.Debugf("Removing: %v", cache.OciBlob())
err := os.RemoveAll(cache.OciBlob())
if err != nil {
return fmt.Errorf("unable to clean oci-blob cache: %v", err)
}
return nil
}
// CleanCache : clean a type of cache (cacheType string). will return a error if one occurs.
func CleanCache(cacheType string) error {
switch cacheType {
case "library":
err := cleanLibraryCache()
return err
case "oci":
err := cleanOciCache()
return err
case "blob", "blobs":
err := cleanBlobCache()
return err
case "all":
err := cache.Clean()
return err
default:
sylog.Fatalf("Not a valid type: %v", cacheType)
os.Exit(2)
}
return nil
}
func cleanLibraryCacheName(cacheName string) (bool, error) {
foundMatch := false
libraryCacheFiles, err := ioutil.ReadDir(cache.Library())
if err != nil {
return false, fmt.Errorf("unable to opening library cache folder: %v", err)
}
for _, f := range libraryCacheFiles {
cont, err := ioutil.ReadDir(filepath.Join(cache.Library(), f.Name()))
if err != nil {
return false, fmt.Errorf("unable to look in library cache folder: %v", err)
}
for _, c := range cont {
if c.Name() == cacheName {
sylog.Debugf("Removing: %v", filepath.Join(cache.Library(), f.Name(), c.Name()))
err = os.RemoveAll(filepath.Join(cache.Library(), f.Name(), c.Name()))
if err != nil {
return false, fmt.Errorf("unable to remove library cache: %v", err)
}
foundMatch = true
}
}
}
return foundMatch, nil
}
func cleanOciCacheName(cacheName string) (bool, error) {
foundMatch := false
blobs, err := ioutil.ReadDir(cache.OciTemp())
if err != nil {
return false, fmt.Errorf("unable to opening oci-tmp cache folder: %v", err)
}
for _, f := range blobs {
blob, err := ioutil.ReadDir(filepath.Join(cache.OciTemp(), f.Name()))
if err != nil {
return false, fmt.Errorf("unable to look in oci-tmp cache folder: %v", err)
}
for _, b := range blob {
if b.Name() == cacheName {
sylog.Debugf("Removing: %v", filepath.Join(cache.OciTemp(), f.Name(), b.Name()))
err = os.RemoveAll(filepath.Join(cache.OciTemp(), f.Name(), b.Name()))
if err != nil {
return false, fmt.Errorf("unable to remove oci-tmp cache: %v", err)
}
foundMatch = true
}
}
}
return foundMatch, nil
}
// CleanCacheName : will clean a container with the same name as cacheName (in the cache directory).
// if libraryCache == true; only search thrught library cache. if ociCache == true; only search the
// oci-tmp cache. if both are false; search all cache, and if both are true; again, search all cache.
func CleanCacheName(cacheName string, libraryCache, ociCache bool) (bool, error) {
if libraryCache == ociCache {
matchLibrary, err := cleanLibraryCacheName(cacheName)
if err != nil {
return false, err
}
matchOci, err := cleanOciCacheName(cacheName)
if err != nil {
return false, err
}
if matchLibrary == true || matchOci == true {
return true, nil
}
return false, nil
}
match := false
if libraryCache == true {
match, err := cleanLibraryCacheName(cacheName)
if err != nil {
return false, err
}
return match, nil
} else if ociCache == true {
match, err := cleanOciCacheName(cacheName)
if err != nil {
return false, err
}
return match, nil
}
return match, nil
}
// CleanSingularityCache : the main function that drives all these other functions, if allClean == true; clean
// all cache. if typeNameClean contains somthing; only clean that type. if cacheName contains somthing; clean only
// cache with that name.
func CleanSingularityCache(cleanAll bool, cacheCleanTypes []string, cacheName string) error {
libraryClean := false
ociClean := false
blobClean := false
for _, t := range cacheCleanTypes {
switch t {
case "library":
libraryClean = true
case "oci":
ociClean = true
case "blob", "blobs":
blobClean = true
case "all":
cleanAll = true
default:
sylog.Fatalf("Not a valid type: %v", t)
os.Exit(2)
}
}
if len(cacheName) >= 1 && cleanAll != true {
foundMatch, err := CleanCacheName(cacheName, libraryClean, ociClean)
if err != nil {
return err
}
if foundMatch != true {
sylog.Warningf("No cache found with givin name: %v", cacheName)
os.Exit(0)
}
return nil
}
if cleanAll {
if err := CleanCache("all"); err != nil {
return err
}
}
if libraryClean {
if err := CleanCache("library"); err != nil {
return err
}
}
if ociClean {
if err := CleanCache("oci"); err != nil {
return err
}
}
if blobClean {
if err := CleanCache("blob"); err != nil {
return err
}
}
return nil
}