| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 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 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugofs |
| 15 |
|
| 16 |
import ( |
| 17 |
"io/fs" |
| 18 |
"os" |
| 19 |
"sync" |
| 20 |
|
| 21 |
"github.com/spf13/afero" |
| 22 |
) |
| 23 |
|
| 24 |
var _ FilesystemUnwrapper = (*OpenFilesFs)(nil) |
| 25 |
|
| 26 |
// OpenFilesFs is a wrapper around afero.Fs that keeps track of open files. |
| 27 |
type OpenFilesFs struct { |
| 28 |
afero.Fs |
| 29 |
|
| 30 |
mu sync.Mutex |
| 31 |
openFiles map[string]int |
| 32 |
} |
| 33 |
|
| 34 |
func (fs *OpenFilesFs) UnwrapFilesystem() afero.Fs { |
| 35 |
return fs.Fs |
| 36 |
} |
| 37 |
|
| 38 |
func (fs *OpenFilesFs) Create(name string) (afero.File, error) { |
| 39 |
f, err := fs.Fs.Create(name) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
return fs.trackAndWrapFile(f), nil |
| 44 |
} |
| 45 |
|
| 46 |
func (fs *OpenFilesFs) Open(name string) (afero.File, error) { |
| 47 |
f, err := fs.Fs.Open(name) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
return fs.trackAndWrapFile(f), nil |
| 52 |
} |
| 53 |
|
| 54 |
func (fs *OpenFilesFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { |
| 55 |
f, err := fs.Fs.OpenFile(name, flag, perm) |
| 56 |
if err != nil { |
| 57 |
return nil, err |
| 58 |
} |
| 59 |
return fs.trackAndWrapFile(f), nil |
| 60 |
} |
| 61 |
|
| 62 |
func (fs *OpenFilesFs) trackAndWrapFile(f afero.File) afero.File { |
| 63 |
fs.mu.Lock() |
| 64 |
defer fs.mu.Unlock() |
| 65 |
|
| 66 |
if fs.openFiles == nil { |
| 67 |
fs.openFiles = make(map[string]int) |
| 68 |
} |
| 69 |
|
| 70 |
fs.openFiles[f.Name()]++ |
| 71 |
|
| 72 |
return &openFilesFsFile{fs: fs, File: f} |
| 73 |
} |
| 74 |
|
| 75 |
type openFilesFsFile struct { |
| 76 |
fs *OpenFilesFs |
| 77 |
afero.File |
| 78 |
} |
| 79 |
|
| 80 |
func (f *openFilesFsFile) ReadDir(count int) ([]fs.DirEntry, error) { |
| 81 |
return f.File.(fs.ReadDirFile).ReadDir(count) |
| 82 |
} |
| 83 |
|
| 84 |
func (f *openFilesFsFile) Close() (err error) { |
| 85 |
f.fs.mu.Lock() |
| 86 |
defer f.fs.mu.Unlock() |
| 87 |
|
| 88 |
err = f.File.Close() |
| 89 |
|
| 90 |
if f.fs.openFiles == nil { |
| 91 |
return |
| 92 |
} |
| 93 |
|
| 94 |
name := f.Name() |
| 95 |
|
| 96 |
f.fs.openFiles[name]-- |
| 97 |
|
| 98 |
if f.fs.openFiles[name] <= 0 { |
| 99 |
delete(f.fs.openFiles, name) |
| 100 |
} |
| 101 |
|
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
func (fs *OpenFilesFs) OpenFiles() map[string]int { |
| 106 |
fs.mu.Lock() |
| 107 |
defer fs.mu.Unlock() |
| 108 |
|
| 109 |
return fs.openFiles |
| 110 |
} |