1
0
mirror of https://github.com/restic/restic.git synced 2025-10-08 23:43:16 +00:00
Files
.github
changelog
cmd
contrib
doc
docker
helpers
internal
archiver
backend
bloblru
cache
checker
crypto
debug
dump
errors
filter
fs
const.go
const_unix.go
const_windows.go
deviceid_unix.go
deviceid_windows.go
doc.go
file.go
file_unix.go
file_windows.go
file_windows_test.go
fs_local.go
fs_local_vss.go
fs_reader.go
fs_reader_test.go
fs_track.go
helpers.go
interface.go
path_prefix.go
path_prefix_test.go
setflags_linux.go
setflags_linux_test.go
setflags_other.go
stat.go
stat_bsd.go
stat_test.go
stat_unix.go
stat_windows.go
vss.go
vss_windows.go
fuse
hashing
migrations
options
pack
repository
restic
restorer
selfupdate
test
textfile
ui
walker
.dockerignore
.gitattributes
.gitignore
.golangci.yml
CHANGELOG.md
CONTRIBUTING.md
GOVERNANCE.md
LICENSE
Makefile
README.md
VERSION
build.go
doc.go
go.mod
go.sum
restic/internal/fs/fs_track.go

55 lines
1.3 KiB
Go
Raw Normal View History

package fs
import (
"fmt"
"os"
"runtime"
"runtime/debug"
)
// Track is a wrapper around another file system which installs finalizers
// for open files which call panic() when they are not closed when the garbage
// collector releases them. This can be used to find resource leaks via open
// files.
type Track struct {
FS
}
// Open wraps the Open method of the underlying file system.
func (fs Track) Open(name string) (File, error) {
f, err := fs.FS.Open(fixpath(name))
if err != nil {
return nil, err
}
return newTrackFile(debug.Stack(), name, f), nil
}
// OpenFile wraps the OpenFile method of the underlying file system.
func (fs Track) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
f, err := fs.FS.OpenFile(fixpath(name), flag, perm)
if err != nil {
return nil, err
}
return newTrackFile(debug.Stack(), name, f), nil
}
type trackFile struct {
File
}
func newTrackFile(stack []byte, filename string, file File) *trackFile {
f := &trackFile{file}
runtime.SetFinalizer(f, func(f *trackFile) {
fmt.Fprintf(os.Stderr, "file %s not closed\n\nStacktrack:\n%s\n", filename, stack)
panic("file " + filename + " not closed")
})
return f
}
func (f *trackFile) Close() error {
runtime.SetFinalizer(f, nil)
return f.File.Close()
}