-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
88 lines (70 loc) · 2.49 KB
/
file.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
// Copyright (c) 2022 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package filecache
import (
"io/fs"
"os"
"time"
)
// File represents the cached file returned by Get() method.
type File[K Key] struct {
parent *Cache[K]
key K
hash Hash
file *os.File
size int64
lastMod time.Time
}
// Name returns the string representation of the associated key.
func (f *File[_]) Name() string { return f.key.String() }
// Read implements io.Reader interface.
func (f *File[_]) Read(b []byte) (int, error) {
return f.file.Read(b) //nolint:wrapcheck
}
// ReadAt implements io.ReaderAt interface.
func (f *File[_]) ReadAt(b []byte, off int64) (int, error) {
return f.file.ReadAt(b, off) //nolint:wrapcheck
}
// Seek implements io.Seeker interface.
func (f *File[_]) Seek(offset int64, whence int) (int64, error) {
return f.file.Seek(offset, whence) //nolint:wrapcheck
}
// Close implements io.Closer interface.
func (f *File[_]) Close() error {
f.parent.unref(f.hash)
return f.file.Close() //nolint:wrapcheck
}
// OSFile returns the underlying os.File object. Use with caution, as all writes
// will fail and operations such as deleting, renaming or closing the file will
// cause unexpected results. Only use if it is required to pass the file to a
// package that accepts only os.File or the file descriptor. Also use File.Close
// instead of os.File.Close even if this method is called.
func (f *File[_]) OSFile() *os.File { return f.file }
// Stat returns the file information.
func (f *File[K]) Stat() (os.FileInfo, error) {
return &FileInfo[K]{
key: f.key,
size: f.size,
lastMod: f.lastMod,
}, nil
}
// FileInfo implements fs.FileInfo interface.
type FileInfo[K Key] struct {
key K
size int64
lastMod time.Time
}
// Name returns the string representation of the key. Note that it is not the
// underlying file path.
func (i *FileInfo[K]) Name() string { return i.key.String() }
// Size returns the file size in byte.
func (i *FileInfo[_]) Size() int64 { return i.size }
// Mode always returns 0400.
func (i *FileInfo[_]) Mode() fs.FileMode { return 0o0400 }
// ModTime returns the last access time or created time of the cache entry.
func (i *FileInfo[_]) ModTime() time.Time { return i.lastMod }
// IsDir always returns false, since a directory can not be cached.
func (*FileInfo[_]) IsDir() bool { return false }
// Sys returns the associated key.
func (i *FileInfo[_]) Sys() any { return i.key }