/
filesystem.go
84 lines (70 loc) · 2.39 KB
/
filesystem.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
/*
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package filesystem
import (
"embed"
"fmt"
"os"
"github.com/spf13/afero"
"github.com/ARM-software/golang-utils/utils/commonerrors"
)
type FilesystemType int
const (
StandardFS FilesystemType = iota
InMemoryFS
Embed
Custom
ZipFS
)
var (
FileSystemTypes = []FilesystemType{StandardFS, InMemoryFS}
)
func NewInMemoryFileSystem() FS {
return NewVirtualFileSystem(afero.NewMemMapFs(), InMemoryFS, IdentityPathConverterFunc)
}
func NewStandardFileSystem() FS {
return NewVirtualFileSystem(NewExtendedOsFs(), StandardFS, IdentityPathConverterFunc)
}
func NewEmbedFileSystem(fs *embed.FS) (FS, error) {
wrapped, err := newEmbedFSAdapter(fs)
if err != nil {
return nil, err
}
return NewVirtualFileSystem(wrapped, Embed, IdentityPathConverterFunc), nil
}
// NewZipFileSystem returns a filesystem over the contents of a .zip file.
// Warning: After use of the filesystem, it is crucial to close the zip file (zipFile) which has been opened from source for the entirety of the filesystem use session.
// fs corresponds to the filesystem to use to find the zip file.
func NewZipFileSystem(fs FS, source string, limits ILimits) (zipFs ICloseableFS, zipFile File, err error) {
wrapped, zipFile, err := newZipFSAdapterFromFilePath(fs, source, limits)
if err != nil {
return
}
zipFs = NewCloseableVirtualFileSystem(wrapped, ZipFS, zipFile, fmt.Sprintf(".zip file `%v`", source), IdentityPathConverterFunc)
return
}
// NewZipFileSystemFromStandardFileSystem returns a zip filesystem similar to NewZipFileSystem but assumes the zip file described by source can be found on the standard file system.
func NewZipFileSystemFromStandardFileSystem(source string, limits ILimits) (ICloseableFS, File, error) {
return NewZipFileSystem(NewStandardFileSystem(), source, limits)
}
func NewFs(fsType FilesystemType) FS {
switch fsType {
case StandardFS:
return NewStandardFileSystem()
case InMemoryFS:
return NewInMemoryFileSystem()
}
return NewStandardFileSystem()
}
// ConvertFileSystemError converts file system error into common errors
func ConvertFileSystemError(err error) error {
if err == nil {
return nil
}
if commonerrors.Any(err, os.ErrExist) || commonerrors.CorrespondTo(err, "file exists", "file already exists") {
return commonerrors.ErrExists
}
return err
}