Debian Codegraph @

tpl/os/os.go

raw
1 // Copyright 2017 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 os provides template functions for interacting with the operating
15 // system.
16 package os
17
18 import (
19 "errors"
20 "fmt"
21 _os "os"
22 "path/filepath"
23
24 "github.com/bep/overlayfs"
25 "github.com/gohugoio/hugo/common/herrors"
26 "github.com/gohugoio/hugo/deps"
27 "github.com/gohugoio/hugo/hugofs"
28 "github.com/spf13/afero"
29 "github.com/spf13/cast"
30 )
31
32 // New returns a new instance of the os-namespaced template functions.
33 func New(d *deps.Deps) *Namespace {
34 var readFileFs, workFs *hugofs.DropSymlinksFs
35
36 // The docshelper script does not have or need all the dependencies set up.
37 if d.PathSpec != nil {
38 readFileFs = hugofs.NewDropSymlinksFs(overlayfs.New(overlayfs.Options{
39 Fss: []afero.Fs{
40 d.PathSpec.BaseFs.Work,
41 d.PathSpec.BaseFs.Content.Fs,
42 },
43 }))
44 // See #9599
45 workFs = hugofs.NewDropSymlinksFs(d.PathSpec.BaseFs.WorkDir)
46 }
47
48 return &Namespace{
49 readFileFs: readFileFs,
50 workFs: workFs,
51 deps: d,
52 }
53 }
54
55 // Namespace provides template functions for the "os" namespace.
56 type Namespace struct {
57 readFileFs *hugofs.DropSymlinksFs
58 workFs *hugofs.DropSymlinksFs
59 deps *deps.Deps
60 }
61
62 // Getenv retrieves the value of the environment variable named by the key.
63 // It returns the value, which will be empty if the variable is not present.
64 func (ns *Namespace) Getenv(key any) (string, error) {
65 skey, err := cast.ToStringE(key)
66 if err != nil {
67 return "", nil
68 }
69
70 if err = ns.deps.ExecHelper.Sec().CheckAllowedGetEnv(skey); err != nil {
71 return "", err
72 }
73
74 return _os.Getenv(skey), nil
75 }
76
77 // readFile reads the file named by filename in the given filesystem
78 // and returns the contents as a string.
79 func readFile(fs afero.Fs, filename string) (string, error) {
80 filename = filepath.Clean(filename)
81 if filename == "" || filename == "." || filename == string(_os.PathSeparator) {
82 return "", errors.New("invalid filename")
83 }
84
85 b, err := afero.ReadFile(fs, filename)
86 if err != nil {
87 return "", err
88 }
89
90 return string(b), nil
91 }
92
93 // ReadFile reads the file named by filename relative to the configured WorkingDir.
94 // It returns the contents as a string.
95 // There is an upper size limit set at 1 megabytes.
96 func (ns *Namespace) ReadFile(i any) (string, error) {
97 s, err := cast.ToStringE(i)
98 if err != nil {
99 return "", err
100 }
101
102 if ns.deps.PathSpec != nil {
103 s = ns.deps.PathSpec.RelPathify(s)
104 }
105
106 s, err = readFile(ns.readFileFs, s)
107 if err != nil && herrors.IsNotExist(err) {
108 return "", nil
109 }
110 return s, err
111 }
112
113 // ReadDir lists the directory contents relative to the configured WorkingDir.
114 func (ns *Namespace) ReadDir(i any) ([]_os.FileInfo, error) {
115 path, err := cast.ToStringE(i)
116 if err != nil {
117 return nil, err
118 }
119
120 list, err := afero.ReadDir(ns.workFs, path)
121 if err != nil {
122 if herrors.IsNotExist(err) {
123 return nil, nil
124 }
125 return nil, fmt.Errorf("failed to read directory %q: %s", path, err)
126 }
127
128 return list, nil
129 }
130
131 // FileExists checks whether a file exists under the given path.
132 func (ns *Namespace) FileExists(i any) (bool, error) {
133 path, err := cast.ToStringE(i)
134 if err != nil {
135 return false, err
136 }
137
138 if path == "" {
139 return false, nil
140 }
141
142 status, err := afero.Exists(ns.readFileFs, path)
143 if err != nil {
144 return false, err
145 }
146
147 return status, nil
148 }
149
150 // Stat returns the os.FileInfo structure describing file.
151 func (ns *Namespace) Stat(i any) (_os.FileInfo, error) {
152 path, err := cast.ToStringE(i)
153 if err != nil {
154 return nil, err
155 }
156
157 if path == "" {
158 return nil, nil
159 }
160
161 r, err := ns.readFileFs.Stat(path)
162 if err != nil {
163 if herrors.IsNotExist(err) {
164 return nil, nil
165 }
166 return nil, err
167 }
168
169 return r, nil
170 }