Debian Codegraph @

internal/fs/tarfs/tarfs.go

raw
1 /*
2 Copyright The ORAS Authors.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 package tarfs
17
18 import (
19 "archive/tar"
20 "errors"
21 "fmt"
22 "io"
23 "io/fs"
24 "os"
25 "path"
26 "path/filepath"
27
28 "oras.land/oras-go/v2/errdef"
29 )
30
31 // blockSize is the size of each block in a tar archive.
32 const blockSize int64 = 512
33
34 // TarFS represents a file system (an fs.FS) based on a tar archive.
35 type TarFS struct {
36 path string
37 entries map[string]*entry
38 }
39
40 // entry represents an entry in a tar archive.
41 type entry struct {
42 header *tar.Header
43 pos int64
44 }
45
46 // New returns a file system (an fs.FS) for a tar archive located at path.
47 func New(path string) (*TarFS, error) {
48 pathAbs, err := filepath.Abs(path)
49 if err != nil {
50 return nil, fmt.Errorf("failed to resolve absolute path for %s: %w", path, err)
51 }
52 tarfs := &TarFS{
53 path: pathAbs,
54 entries: make(map[string]*entry),
55 }
56 if err := tarfs.indexEntries(); err != nil {
57 return nil, err
58 }
59 return tarfs, nil
60 }
61
62 // Open opens the named file.
63 // When Open returns an error, it should be of type *PathError
64 // with the Op field set to "open", the Path field set to name,
65 // and the Err field describing the problem.
66 //
67 // Open should reject attempts to open names that do not satisfy
68 // ValidPath(name), returning a *PathError with Err set to
69 // ErrInvalid or ErrNotExist.
70 func (tfs *TarFS) Open(name string) (file fs.File, openErr error) {
71 entry, err := tfs.getEntry("open", name)
72 if err != nil {
73 return nil, err
74 }
75 tarFile, err := os.Open(tfs.path)
76 if err != nil {
77 return nil, err
78 }
79 defer func() {
80 if openErr != nil {
81 tarFile.Close()
82 }
83 }()
84
85 if _, err := tarFile.Seek(entry.pos, io.SeekStart); err != nil {
86 return nil, err
87 }
88 tr := tar.NewReader(tarFile)
89 if _, err := tr.Next(); err != nil {
90 return nil, err
91 }
92 return &entryFile{
93 Reader: tr,
94 Closer: tarFile,
95 header: entry.header,
96 }, nil
97 }
98
99 // Stat returns a FileInfo describing the file.
100 // If there is an error, it should be of type *PathError.
101 func (tfs *TarFS) Stat(name string) (fs.FileInfo, error) {
102 entry, err := tfs.getEntry("stat", name)
103 if err != nil {
104 return nil, err
105 }
106 return entry.header.FileInfo(), nil
107 }
108
109 // getEntry returns the named entry.
110 func (tfs *TarFS) getEntry(operation string, path string) (*entry, error) {
111 if !fs.ValidPath(path) {
112 return nil, &fs.PathError{Op: operation, Path: path, Err: fs.ErrInvalid}
113 }
114 entry, ok := tfs.entries[path]
115 if !ok {
116 return nil, &fs.PathError{Op: operation, Path: path, Err: fs.ErrNotExist}
117 }
118 if entry.header.Typeflag != tar.TypeReg {
119 // support regular files only
120 return nil, fmt.Errorf("%s: type flag %c is not supported: %w",
121 path, entry.header.Typeflag, errdef.ErrUnsupported)
122 }
123 return entry, nil
124 }
125
126 // indexEntries index entries in the tar archive.
127 func (tfs *TarFS) indexEntries() error {
128 tarFile, err := os.Open(tfs.path)
129 if err != nil {
130 return err
131 }
132 defer tarFile.Close()
133
134 tr := tar.NewReader(tarFile)
135 for {
136 header, err := tr.Next()
137 if err != nil {
138 if errors.Is(err, io.EOF) {
139 break
140 }
141 return err
142 }
143 pos, err := tarFile.Seek(0, io.SeekCurrent)
144 if err != nil {
145 return err
146 }
147
148 name := path.Clean(header.Name)
149 tfs.entries[name] = &entry{
150 header: header,
151 pos: pos - blockSize,
152 }
153 }
154 return nil
155 }
156
157 // entryFile represents an entryFile in a tar archive and implements `fs.File`.
158 type entryFile struct {
159 io.Reader
160 io.Closer
161 header *tar.Header
162 }
163
164 // Stat returns a fs.FileInfo describing e.
165 func (e *entryFile) Stat() (fs.FileInfo, error) {
166 return e.header.FileInfo(), nil
167 }