Debian Codegraph @

hugofs/noop_fs.go

raw
1 // Copyright 2018 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 "errors"
18 "os"
19 "time"
20
21 "github.com/spf13/afero"
22 )
23
24 var (
25 errNoOp = errors.New("this operation is not supported")
26 _ afero.Fs = (*noOpFs)(nil)
27
28 // NoOpFs provides a no-op filesystem that implements the afero.Fs
29 // interface.
30 NoOpFs = &noOpFs{}
31 )
32
33 type noOpFs struct{}
34
35 func (fs noOpFs) Create(name string) (afero.File, error) {
36 panic(errNoOp)
37 }
38
39 func (fs noOpFs) Mkdir(name string, perm os.FileMode) error {
40 return nil
41 }
42
43 func (fs noOpFs) MkdirAll(path string, perm os.FileMode) error {
44 return nil
45 }
46
47 func (fs noOpFs) Open(name string) (afero.File, error) {
48 return nil, os.ErrNotExist
49 }
50
51 func (fs noOpFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
52 return nil, os.ErrNotExist
53 }
54
55 func (fs noOpFs) Remove(name string) error {
56 return nil
57 }
58
59 func (fs noOpFs) RemoveAll(path string) error {
60 return nil
61 }
62
63 func (fs noOpFs) Rename(oldname string, newname string) error {
64 panic(errNoOp)
65 }
66
67 func (fs noOpFs) Stat(name string) (os.FileInfo, error) {
68 return nil, os.ErrNotExist
69 }
70
71 func (fs noOpFs) Name() string {
72 return "noOpFs"
73 }
74
75 func (fs noOpFs) Chmod(name string, mode os.FileMode) error {
76 panic(errNoOp)
77 }
78
79 func (fs noOpFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
80 panic(errNoOp)
81 }
82
83 func (fs *noOpFs) Chown(name string, uid int, gid int) error {
84 panic(errNoOp)
85 }
86
87 // noOpRegularFileOps implements the non-directory operations of a afero.File
88 // panicking for all operations.
89 type noOpRegularFileOps struct{}
90
91 func (f *noOpRegularFileOps) Read(p []byte) (n int, err error) {
92 panic(errNoOp)
93 }
94
95 func (f *noOpRegularFileOps) ReadAt(p []byte, off int64) (n int, err error) {
96 panic(errNoOp)
97 }
98
99 func (f *noOpRegularFileOps) Seek(offset int64, whence int) (int64, error) {
100 panic(errNoOp)
101 }
102
103 func (f *noOpRegularFileOps) Write(p []byte) (n int, err error) {
104 panic(errNoOp)
105 }
106
107 func (f *noOpRegularFileOps) WriteAt(p []byte, off int64) (n int, err error) {
108 panic(errNoOp)
109 }
110
111 func (f *noOpRegularFileOps) Readdir(count int) ([]os.FileInfo, error) {
112 panic(errNoOp)
113 }
114
115 func (f *noOpRegularFileOps) Readdirnames(n int) ([]string, error) {
116 panic(errNoOp)
117 }
118
119 func (f *noOpRegularFileOps) Sync() error {
120 panic(errNoOp)
121 }
122
123 func (f *noOpRegularFileOps) Truncate(size int64) error {
124 panic(errNoOp)
125 }
126
127 func (f *noOpRegularFileOps) WriteString(s string) (ret int, err error) {
128 panic(errNoOp)
129 }