hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

createcounting_fs.go (2374B)

    1 // Copyright 2019 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 	"fmt"
   18 	"os"
   19 	"sort"
   20 	"strings"
   21 	"sync"
   22 
   23 	"github.com/spf13/afero"
   24 )
   25 
   26 // Reseter is implemented by some of the stateful filesystems.
   27 type Reseter interface {
   28 	Reset()
   29 }
   30 
   31 // DuplicatesReporter reports about duplicate filenames.
   32 type DuplicatesReporter interface {
   33 	ReportDuplicates() string
   34 }
   35 
   36 var (
   37 	_ FilesystemUnwrapper = (*createCountingFs)(nil)
   38 )
   39 
   40 func NewCreateCountingFs(fs afero.Fs) afero.Fs {
   41 	return &createCountingFs{Fs: fs, fileCount: make(map[string]int)}
   42 }
   43 
   44 func (fs *createCountingFs) UnwrapFilesystem() afero.Fs {
   45 	return fs.Fs
   46 }
   47 
   48 // ReportDuplicates reports filenames written more than once.
   49 func (c *createCountingFs) ReportDuplicates() string {
   50 	c.mu.Lock()
   51 	defer c.mu.Unlock()
   52 
   53 	var dupes []string
   54 
   55 	for k, v := range c.fileCount {
   56 		if v > 1 {
   57 			dupes = append(dupes, fmt.Sprintf("%s (%d)", k, v))
   58 		}
   59 	}
   60 
   61 	if len(dupes) == 0 {
   62 		return ""
   63 	}
   64 
   65 	sort.Strings(dupes)
   66 
   67 	return strings.Join(dupes, ", ")
   68 }
   69 
   70 // createCountingFs counts filenames of created files or files opened
   71 // for writing.
   72 type createCountingFs struct {
   73 	afero.Fs
   74 
   75 	mu        sync.Mutex
   76 	fileCount map[string]int
   77 }
   78 
   79 func (c *createCountingFs) Reset() {
   80 	c.mu.Lock()
   81 	defer c.mu.Unlock()
   82 
   83 	c.fileCount = make(map[string]int)
   84 }
   85 
   86 func (fs *createCountingFs) onCreate(filename string) {
   87 	fs.mu.Lock()
   88 	defer fs.mu.Unlock()
   89 
   90 	fs.fileCount[filename] = fs.fileCount[filename] + 1
   91 }
   92 
   93 func (fs *createCountingFs) Create(name string) (afero.File, error) {
   94 	f, err := fs.Fs.Create(name)
   95 	if err == nil {
   96 		fs.onCreate(name)
   97 	}
   98 	return f, err
   99 }
  100 
  101 func (fs *createCountingFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
  102 	f, err := fs.Fs.OpenFile(name, flag, perm)
  103 	if err == nil && isWrite(flag) {
  104 		fs.onCreate(name)
  105 	}
  106 	return f, err
  107 }