File: manager.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (430 lines) | stat: -rw-r--r-- 10,796 bytes parent folder | download | duplicates (2)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright (c) 2018-2022, 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 layout

import (
	"fmt"
	iofs "io/fs"
	"os"
	"path/filepath"
	"strings"
	"syscall"

	"github.com/sylabs/singularity/v4/internal/pkg/util/fs"
)

const (
	dirMode  os.FileMode = 0o755
	fileMode os.FileMode = 0o644
)

type file struct {
	created bool
	mode    os.FileMode
	uid     int
	gid     int
	content []byte
}

type dir struct {
	created bool
	mode    os.FileMode
	uid     int
	gid     int
}

type symlink struct {
	created bool
	uid     int
	gid     int
	target  string
}

type VFS interface {
	Chown(string, int, int) error
	EvalRelative(string, string) string
	Lchown(string, int, int) error
	Mkdir(string, os.FileMode) error
	Readlink(string) (string, error)
	ReadDir(string) ([]iofs.DirEntry, error)
	Stat(string) (os.FileInfo, error)
	Symlink(string, string) error
	Umask(int) int
	WriteFile(string, []byte, os.FileMode) error
}

type defaultVFS struct{}

func (v *defaultVFS) Chown(name string, uid, gid int) error {
	return os.Chown(name, uid, gid)
}

func (v *defaultVFS) EvalRelative(path, root string) string {
	return fs.EvalRelative(path, root)
}

func (v *defaultVFS) Lchown(name string, uid, gid int) error {
	return os.Lchown(name, uid, gid)
}

func (v *defaultVFS) Mkdir(name string, perm os.FileMode) error {
	return os.Mkdir(name, perm)
}

func (v *defaultVFS) Readlink(name string) (string, error) {
	return os.Readlink(name)
}

func (v *defaultVFS) ReadDir(dir string) ([]iofs.DirEntry, error) {
	return os.ReadDir(dir)
}

func (v *defaultVFS) Stat(name string) (os.FileInfo, error) {
	return os.Stat(name)
}

func (v *defaultVFS) Symlink(oldname, newname string) error {
	return os.Symlink(oldname, newname)
}

func (v *defaultVFS) Umask(mask int) int {
	return syscall.Umask(mask)
}

func (v *defaultVFS) WriteFile(filename string, data []byte, perm os.FileMode) error {
	f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_EXCL, perm)
	if err != nil {
		if !os.IsExist(err) {
			return fmt.Errorf("failed to create file %s: %s", filename, err)
		}
		return err
	}
	if len(data) > 0 {
		_, err = f.Write(data)
	}
	if err1 := f.Close(); err == nil {
		err = err1
	}
	return err
}

// DefaultVFS is the default VFS.
var DefaultVFS VFS = &defaultVFS{}

// Manager manages a filesystem layout in a given path
type Manager struct {
	VFS      VFS
	DirMode  os.FileMode
	FileMode os.FileMode
	rootPath string
	entries  map[string]interface{}
	dirs     []*dir

	// each entries can contain multiple directories, the first
	// directory of each entry is always substituted to the bound
	// directory, the others if any are the directories to create
	// for nested binds support
	ovDirs map[string][]string
}

func (m *Manager) checkPath(path string, checkExist bool) (string, error) {
	if m.entries == nil {
		return "", fmt.Errorf("root path is not set")
	}
	p := filepath.Clean(path)
	if !filepath.IsAbs(p) {
		return "", fmt.Errorf("path %s is not an absolute path", p)
	}
	if checkExist {
		if _, ok := m.entries[p]; ok {
			return "", fmt.Errorf("%s already exists in layout", p)
		}
	} else {
		if _, ok := m.entries[p]; !ok {
			return "", fmt.Errorf("%s doesn't exist in layout", p)
		}
	}
	return p, nil
}

func (m *Manager) createParentDir(path string) {
	uid := os.Getuid()
	gid := os.Getgid()

	splitted := strings.Split(path, string(os.PathSeparator))
	l := len(splitted)
	p := ""
	for i := 1; i < l; i++ {
		s := splitted[i : i+1][0]
		p += "/" + s
		if s != "" {
			if _, ok := m.entries[p]; !ok {
				d := &dir{mode: m.DirMode, uid: uid, gid: gid}
				m.entries[p] = d
				m.dirs = append(m.dirs, d)
				// check if the parent directory is part of the overridden
				// directories to force the creation of the destination
				// directory in the right parent directory (nested binds)
				if ovDirs, ok := m.ovDirs[filepath.Dir(p)]; ok {
					m.overrideDir(p, filepath.Join(ovDirs[0], filepath.Base(p)))
				}
			}
		}
	}
}

// SetRootPath sets layout root path
func (m *Manager) SetRootPath(path string) error {
	if !fs.IsDir(path) {
		return fmt.Errorf("%s is not a directory or doesn't exists", path)
	}
	m.rootPath = filepath.Clean(path)
	if m.entries == nil {
		m.entries = make(map[string]interface{})
	} else {
		return fmt.Errorf("root path is already set")
	}
	if m.ovDirs == nil {
		m.ovDirs = make(map[string][]string)
	}
	if m.dirs == nil {
		m.dirs = make([]*dir, 0)
	}
	if m.DirMode == 0o000 {
		m.DirMode = dirMode
	}
	if m.FileMode == 0o000 {
		m.FileMode = fileMode
	}
	d := &dir{mode: m.DirMode, uid: os.Getuid(), gid: os.Getgid()}
	m.entries["/"] = d
	m.dirs = append(m.dirs, d)
	return nil
}

// AddDir adds a directory in layout, will recursively add parent
// directories if they don't exist
func (m *Manager) AddDir(path string) error {
	p, err := m.checkPath(path, true)
	if err != nil {
		return err
	}
	m.createParentDir(p)
	return nil
}

// AddFile adds a file in layout, will recursively add parent
// directories if they don't exist
func (m *Manager) AddFile(path string, content []byte) error {
	p, err := m.checkPath(path, true)
	if err != nil {
		return err
	}
	m.createParentDir(filepath.Dir(p))
	m.entries[p] = &file{mode: m.FileMode, uid: os.Getuid(), gid: os.Getgid(), content: content}
	return nil
}

// AddSymlink adds a symlink in layout, will recursively add parent
// directories if they don't exist
func (m *Manager) AddSymlink(path string, target string) error {
	p, err := m.checkPath(path, true)
	if err != nil {
		return err
	}
	m.createParentDir(filepath.Dir(p))
	m.entries[p] = &symlink{uid: os.Getuid(), gid: os.Getgid(), target: target}
	return nil
}

// overrideDir will substitute another directory to the one associated
// to directory located by path. When called multiple times subsequent
// path are used to store directories to be created for nested binds.
func (m *Manager) overrideDir(path string, realpath string) {
	for _, ovDir := range m.ovDirs[path] {
		if ovDir == realpath {
			return
		}
	}
	m.ovDirs[path] = append(m.ovDirs[path], realpath)
}

// GetOverridePath returns the real path for the session path
func (m *Manager) GetOverridePath(path string) (string, error) {
	if p, ok := m.ovDirs[path]; ok {
		return p[0], nil
	}
	return "", fmt.Errorf("no override directory %s", path)
}

// GetPath returns the full path of layout path
func (m *Manager) GetPath(path string) (string, error) {
	_, err := m.checkPath(path, false)
	if err != nil {
		return "", err
	}
	return filepath.Join(m.rootPath, path), nil
}

// Chmod sets permission mode for path
func (m *Manager) Chmod(path string, mode os.FileMode) error {
	_, err := m.checkPath(path, false)
	if err != nil {
		return err
	}
	//nolint:forcetypeassert
	switch m.entries[path].(type) {
	case *file:
		m.entries[path].(*file).mode = mode
	case *dir:
		m.entries[path].(*dir).mode = mode
	}
	return nil
}

// Chown sets ownership for path
func (m *Manager) Chown(path string, uid, gid int) error {
	_, err := m.checkPath(path, false)
	if err != nil {
		return err
	}
	//nolint:forcetypeassert
	switch m.entries[path].(type) {
	case *file:
		m.entries[path].(*file).uid = uid
		m.entries[path].(*file).gid = gid
	case *dir:
		m.entries[path].(*dir).uid = uid
		m.entries[path].(*dir).gid = gid
	case *symlink:
		m.entries[path].(*symlink).uid = uid
		m.entries[path].(*symlink).gid = gid
	}
	return nil
}

// Create creates the filesystem layout
func (m *Manager) Create() error {
	return m.sync()
}

// Update updates the filesystem layout
func (m *Manager) Update() error {
	return m.sync()
}

func (m *Manager) sync() error {
	uid := os.Getuid()
	gid := os.Getgid()

	if m.entries == nil {
		return fmt.Errorf("root path is not set")
	}

	oldmask := m.VFS.Umask(0)
	defer m.VFS.Umask(oldmask)

	for _, d := range m.dirs[1:] {
		if d.created {
			continue
		}
		path := ""
		for p, e := range m.entries {
			if e == d {
				path = m.rootPath + p
				for _, ovDir := range m.ovDirs[p] {
					if _, err := m.VFS.Stat(ovDir); err != nil {
						if err := m.VFS.Mkdir(ovDir, m.DirMode); err != nil {
							return fmt.Errorf("failed to create %s directory: %s", ovDir, err)
						}
					}
				}
				break
			}
		}
		if path == "" {
			continue
		}
		if d.mode != m.DirMode {
			if err := m.VFS.Mkdir(path, d.mode); err != nil {
				if !os.IsExist(err) {
					return fmt.Errorf("failed to create %s directory: %s", path, err)
				}
				// skip owner change, not created by us
				d.created = true
				continue
			}
		} else {
			if err := m.VFS.Mkdir(path, m.DirMode); err != nil {
				if !os.IsExist(err) {
					return fmt.Errorf("failed to create %s directory: %s", path, err)
				}
				// skip owner change, not created by us
				d.created = true
				continue
			}
		}
		if d.uid != uid || d.gid != gid {
			if err := m.VFS.Chown(path, d.uid, d.gid); err != nil {
				return fmt.Errorf("failed to change owner of %s: %s", path, err)
			}
		}
		d.created = true
	}

	for p, e := range m.entries {
		path := m.rootPath + p
		if ovDir, err := m.GetOverridePath(filepath.Dir(p)); err == nil {
			path = filepath.Join(ovDir, filepath.Base(p))
		}
		switch entry := e.(type) {
		case *file:
			if entry.created {
				continue
			}
			if err := m.VFS.WriteFile(path, entry.content, entry.mode); err != nil {
				if !os.IsExist(err) {
					return fmt.Errorf("failed to create file %s: %s", path, err)
				}
				// skip content write or owner change, not created by us
				entry.created = true
				continue
			}
			if entry.uid != uid || entry.gid != gid {
				if err := m.VFS.Chown(path, entry.uid, entry.gid); err != nil {
					return fmt.Errorf("failed to change %s ownership: %s", path, err)
				}
			}
			entry.created = true
		case *symlink:
			if entry.created {
				continue
			}
			if err := m.VFS.Symlink(entry.target, path); err != nil {
				if !os.IsExist(err) {
					return fmt.Errorf("failed to create symlink %s: %s", path, err)
				}
				// check that current symlink point to the right target if it's a symlink
				// otherwise we consider the entry as already created no matter if it's a
				// file, a directory or something else
				target, err := m.VFS.Readlink(path)
				if err == nil && target != entry.target {
					return fmt.Errorf("symlink %s point to %s instead of %s", path, target, entry.target)
				}
				// skip symlink owner change, not created by us
				entry.created = true
				continue
			}
			if entry.uid != uid || entry.gid != gid {
				if err := m.VFS.Lchown(path, entry.uid, entry.gid); err != nil {
					return fmt.Errorf("failed to change %s ownership: %s", path, err)
				}
			}
			entry.created = true
		}
	}
	return nil
}